使用 css 将图标旁边的多行文本垂直居中

发布于 2024-10-03 16:42:47 字数 1275 浏览 4 评论 0原文

我有一个内联有序列表,其中包含图像和一些文本。我希望文本在图标旁边垂直居中。

这是一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

落花浅忆 2024-10-10 16:42:48

在此页面上,您可以看到 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"

没有你我更好 2024-10-10 16:42:48

我认为这不可能在所有浏览器中获得相同的结果,就像你在 FF 中所说的那样,它似乎可以正确地完成任务,而在其他浏览器中它会搞砸。我通常这样做的方法是将图像作为链接的背景。它也更好,因为这样您也可以单击图像进行导航。

<ul class="menu" id="toolbox">
    <li class="item301">
        <a class="link1" href="/business-services/publications.html">Publications</a>
    </li>
    <li class="item302">
        <a class="link2" href="/business-services/hamilton-business-directory.html">Business Directory</a>
    </li>
</ul>

和CSS:

.menu a {
 background-position: left 50%;
 background-repeat: no-repeat;
 display: block;
 padding-left: $px /* $ = the width of your image plus a little more to space nicely */
}

.link1 {
 background-image: url(link1.png);
}

.link2 {
 background-image: url(link2.png);
}

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.

<ul class="menu" id="toolbox">
    <li class="item301">
        <a class="link1" href="/business-services/publications.html">Publications</a>
    </li>
    <li class="item302">
        <a class="link2" href="/business-services/hamilton-business-directory.html">Business Directory</a>
    </li>
</ul>

and css:

.menu a {
 background-position: left 50%;
 background-repeat: no-repeat;
 display: block;
 padding-left: $px /* $ = the width of your image plus a little more to space nicely */
}

.link1 {
 background-image: url(link1.png);
}

.link2 {
 background-image: url(link2.png);
}
看轻我的陪伴 2024-10-10 16:42:48

不幸的是,这些都不起作用。所以我最终使用了一些jquery来弥补IE7的不足。这是我使用的:

$(document).ready(function() {
  if ($.browser.msie && parseInt($.browser.version) == 7) {
  var lineHeight = parseInt( $("#toolbox span").css("lineHeight"), 10 );

$("#toolbox span").each(function() {
    var linesNbr = $(this).height() / lineHeight;
    $(this).addClass("lines" + linesNbr);
  });
  }
});

代码是从 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:

$(document).ready(function() {
  if ($.browser.msie && parseInt($.browser.version) == 7) {
  var lineHeight = parseInt( $("#toolbox span").css("lineHeight"), 10 );

$("#toolbox span").each(function() {
    var linesNbr = $(this).height() / lineHeight;
    $(this).addClass("lines" + linesNbr);
  });
  }
});

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.

泛滥成性 2024-10-10 16:42:48

你有没有尝试过使用:

display:inline-block;

自从我遇见他以来,它一直是我最好的伙伴。他的行为就像一个 div,但可以以 text-align:center 为中心,也可以以 vertical-align:middle;

如果您支持 ie7,则需要使用此版本:

display:inline-block;zoom:1; *display: inline;/*IE7 Fix*/

希望这有帮助

Have you tried using:

display:inline-block;

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:

display:inline-block;zoom:1; *display: inline;/*IE7 Fix*/

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文