开发者

CSS: how to make margin: 0px auto work

I am trying to center images inside another block:

The html code looks like this开发者_开发百科:

<ul>
    <li>       
    <a href="/article/japanese-culture-one/">
    <img src="/site_media/upload/fushimi-inari-fox_jpg_200x200_q85.jpg"
    alt="Лисица Инари - один из мифических
    персонажей культа синто"/> </a> <br />
    <a href="/article/japanese-culture-one/"
    class="article_title">История и
    культура Японии. Часть Первая</a>
    <p>Изолированная от остального мира
    Япония породила действительно
    уникальную культуру, удивляющую
    западного человека своей непохожестью
    и вдохновляющую своей красотой.
    История и культура Японии, живущей по
    своим ...</p> </li> .....
</ul>

What I want to do is to place the image in the center of li. (see http://img.skitch.com/20091128-xf36n8ekhpxyi5rdgnuqrtw36a.png)

The css looks this way:

ul#related{
    list-style:none;
    margin-top: 280px;
}

ul#related li{
    float:left;
    width:30%;
    height: 500px;
    margin:5px;
}

ul#related li a img{
    border:1px solid #E3E3E3;
    padding:2px;
    height: 190px;
    width: 190px;
    text-align: center;

}

Complete code may be accessed on the site: http://j-in.org.ua/article/art/

Thanks in advance!


In order for margin: 0 auto to work, the images need to be block level elements. Add:

ul#related li a img {
    display: block;
    margin: 0 auto;
}

and the images pop into the center like you want.


In my similar case, I was trying to center a span (with 3 inputs) inside a div:

<div id="featurePaginator">
    <span>
        <input type="radio" name="featurePage">
        <input type="radio" name="featurePage">
        <input type="radio" name="featurePage">
    </span>
</div>

Using Magnar's solution in that case, the span's auto width fills the entire div. To make it work (with display: block;), it needs to be set a width to make it work. But I don't know how many inputs will have there, so I came with a better solution, at least to my case, using display: table;:

#featurePaginator span {
    margin: 0px auto;
    display: table;
}

The tables are displayed to fit as thin as its content, so, by setting display: table, I could achieve the centering that I was looking for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜