使用 css 将图标旁边的多行文本垂直居中
我有一个内联有序列表,其中包含图像和一些文本。我希望文本在图标旁边垂直居中。
这是一个包含 1 行和 3 行文本的简单示例。 2 条线也应该很好地垂直对齐。
|----| |----| Somewhat
| | Link | | longer
|----| |----| Link
Joomla 设置为将菜单生成为列表,因此这是我必须使用的代码。
<ul class="menu" id="toolbox">
<li class="item301">
<a href="/business-services/publications.html">
<img src="/images/stories/icon_publications.png" alt="publications" />
<span>Publications</span>
</a>
</li>
<li class="item302">
<a href="/business-services/hamilton-business-directory.html">
<img src="/images/stories/icon_business-directory.png" alt="business-directory" />
<span>Business Directory</span>
</a>
</li>
</ul>
下面的 CSS 在 Firefox 中有效,但在 IE 中无效。
#toolbox a {
padding: 0;
display: inline;
border: 0 none;
}
#toolbox img {
width: 42px;
float: left;
}
#toolbox li {
float: left;
width: 105px;
height: 42px;
}
#toolbox span {
height: 42px;
vertical-align: middle;
display: table-cell;
width: 60px;
}
是否可以只使用 css,或者我正在考虑编辑菜单模块或使用一些 jquery?
I have an inline ordered list that contains an image and some text. I'd like the text to be centered vertically next to the icon.
Here's a quick example with 1 and three lines of text. 2 lines should also be nicely aligned vertically.
|----| |----| Somewhat
| | Link | | longer
|----| |----| Link
Joomla is set to generate the menu as a list, so here's the code that I have to work with.
<ul class="menu" id="toolbox">
<li class="item301">
<a href="/business-services/publications.html">
<img src="/images/stories/icon_publications.png" alt="publications" />
<span>Publications</span>
</a>
</li>
<li class="item302">
<a href="/business-services/hamilton-business-directory.html">
<img src="/images/stories/icon_business-directory.png" alt="business-directory" />
<span>Business Directory</span>
</a>
</li>
</ul>
The css below works in Firefox, but not IE.
#toolbox a {
padding: 0;
display: inline;
border: 0 none;
}
#toolbox img {
width: 42px;
float: left;
}
#toolbox li {
float: left;
width: 105px;
height: 42px;
}
#toolbox span {
height: 42px;
vertical-align: middle;
display: table-cell;
width: 60px;
}
Is it possible to this using only css, or am I looking at editing the menu module or using some jquery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在此页面上,您可以看到 Internet Explorer 不支持 css 值“display: table-cell” - IE8 除外(我认为是 IE9):
http://www.css4you.de/display.html#footer
我不对joomla了解很多...
编辑器是否选择显示的图像?如果没有,您可以尝试将图像设置为背景图像并使用 css 属性“背景位置:左中心”
On this page you can see that the css-value "display: table-cell" is not supported by the internet explorer - excepted by IE8 (and IE9 I think):
http://www.css4you.de/display.html#footer
I don't know much about joomla ...
Does the editor select the displayed image? If not you can try to set the image as background-image and make use of the css-attribute "background-position: left center"
我认为这不可能在所有浏览器中获得相同的结果,就像你在 FF 中所说的那样,它似乎可以正确地完成任务,而在其他浏览器中它会搞砸。我通常这样做的方法是将图像作为链接的背景。它也更好,因为这样您也可以单击图像进行导航。
和CSS:
I don't think that is possible in all browser with the same result, like you said in FF it seems to do the trick right, whereas in other browser it messes up. The way I usually do this is with the image as a background of the link. It's also nicer because this way you can click on the image in order to navigate as well.
and css:
不幸的是,这些都不起作用。所以我最终使用了一些jquery来弥补IE7的不足。这是我使用的:
代码是从 http://vidasp.net/tinydemos/numberOfLines.html。这会检查跨度中的行数并向跨度添加适当的类。然后,我将为生成的每个类添加一个 margin-top 。
不是我正在寻找的解决方案,但它有效。
Unfortunately none of those worked. So I ended up using some jquery to compensate for IE7. Here's what I used:
Code was grabbed from http://vidasp.net/tinydemos/numberOfLines.html. This checks the number of lines in the span and adds an appropriate class to the span. I'll then add a margin-top for each of the classes that are generated.
Not the solution I was looking for, but it works.
你有没有尝试过使用:
自从我遇见他以来,它一直是我最好的伙伴。他的行为就像一个 div,但可以以 text-align:center 为中心,也可以以
vertical-align:middle;
如果您支持 ie7,则需要使用此版本:
希望这有帮助
Have you tried using:
It's been my best mate ever since I met him. He will behave just like a div but can be centered on text-align:center and also
vertical-align:middle;
If you are supporting ie7 you will need to use this version:
Hope this helps