简体   繁体   English

将带有CSS的图标旁边的多行文本垂直居中

[英]Vertically center multiple lines of text next to an icon with css

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. 这是一个包含1行和3行文本的简单示例。 2 lines should also be nicely aligned vertically. 2条线也应该垂直对齐。

|----|         |----| 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. 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>

The css below works in Firefox, but not IE. 下面的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;
}

Is it possible to this using only css, or am I looking at editing the menu module or using some jquery? 是否可以仅使用CSS做到这一点,还是我正在编辑菜单模块或使用某些jquery?

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; 他的行为就像div一样,但可以以text-align:center为中心,还可以以vertical-align:middle;

If you are supporting ie7 you will need to use this version: 如果支持ie7,则需要使用以下版本:

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

Hope this helps 希望这可以帮助

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 在此页面上,您可以看到Internet Explorer不支持css值“ display:table-cell”-IE8(我认为是IE9)除外: http : //www.css4you.de/display.html#页脚

I don't know much about joomla ... Does the editor select the displayed image? 我对joomla不太了解...编辑器会选择显示的图像吗? If not you can try to set the image as background-image and make use of the css-attribute "background-position: left center" 如果没有,您可以尝试将图像设置为背景图像,并使用css属性“ background-position:left center”

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. 我不认为在所有浏览器中都可能获得相同的结果,就像您在FF中所说的那样,这样做似乎是正确的,而在其他浏览器中,它搞砸了。 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: 和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);
}

Unfortunately none of those worked. 不幸的是,这些都不起作用。 So I ended up using some jquery to compensate for IE7. 因此,我最终使用了一些jquery来补偿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 . 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. 然后,我将为每个生成的类添加一个margin-top。

Not the solution I was looking for, but it works. 不是我一直在寻找的解决方案,但它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM