English 中文(简体)
如何水平对齐列表项中的中心
原标题:How to horizontally align an img to the center inside the list items
  • 时间:2012-05-28 14:15:45
  •  标签:
  • css

这是我的页面结构;

""https://i.sstatic.net/2RQVH.png" alt="此处输入图像描述"/ >

我在下面所有的“ li” 中都有图像。 我正在试图将这些图像在 li 元素中进行对齐。 我正尝试在设置右侧和左侧间距以自动方式进行对齐, 但是没有效果 。

这是CS;

#navcontainer ul li
{
    display: inline-block;
    width:80px;
}

#navcontainer ul li img
{
    margin-right:auto;
    margin-left:auto;
}

我敢肯定,这不是从 cs、 bcs 获取元素的问题, 当我设定宽度或高度时, 或者在它工作的 cs 中的其他任何事物。 但我没有设法将其与中心对齐 。

最佳回答

如果它们重设 < code> inline block , 只需使用 text- align: central 在父元素 < code> >li 上使用 < code > text- ali: center 。

如果他们重写 display: block , 那么您就可以使用 margin: 0 自动 0 自动 < /code> ( top , >right , bottom > ) 。

还值得指出的是, ul 有效子元素的 ul ol 的有效子元素是 li 元素。 任何 other 内容都必须被包裹在 li 中,否则标记无效,然后用户代理在准备 DOM 时试图纠正标记。 不同用户代理之间也不一致。

使用以下标记作为示范:

<ul>
    <li class="inlineBlock"><img src="http://lorempixel.com/100/100/sports" /></li>
    <li class="block"><img src="http://lorempixel.com/100/100/nature" /></li>
</ul>​

使用以下CSS:

li.inlineBlock {
    text-align: center;
}
li.inlineBlock img {
    display: inline-block;
}

li.block img {
    display: block;
    margin: 0 auto 0 auto;
}​

JS Didle demo

问题回答

暂无回答




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

Drop down background url in Safari Issue

selectBox.selectCSS { background: url(/Images/replacementSelectBackground.png) top left no-repeat height:auto; } I have an issue in Safari only where the image is not rendering on top ...

CSS specific for Safari

How do you target specifically safari in css with styles?

Aligning textarea in a form

Ive used the following css code to align my form elements: form { position:relative; } form input { position:absolute; left:11em; } However, the textarea element is not aligned correctly with the ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

CSS problem with page footer

I have defined my page footer in the css file as: #footer { position: absolute; height: 50px; text-align: center; background: #66CCCC; bottom: 0px; left: 0px; width: 100%; height: ...