为什么text-align:center和margin:0对定位元素没有影响?看实况例子

时间:2010-11-12 19:24:37

标签: css xhtml

CSS

  * {margin:0;padding:0}

/* General styles */

#gallery {
  padding: 2em 0;
  margin: 0 auto;
  width: 840px;
  font: small Verdana, sans-serif;
}

#gallery #photo-grid {
  width: 100%;
  margin: 0;
  padding: 1em 0;
  list-style: none;
}

#gallery #photo-grid li {
        float: left;

  margin: 0 10px 10px 0;
        background: #def;
  color: #333;
        padding: 0.5em 1em;
}


#gallery #photo-grid li span.photo-title { bottom:-28px;
font-style:italic;
position:absolute;
width:200px
}



/* Positioning and initial formatting */

#gallery #photo-grid {
  padding-left: 20px;

}



#gallery #photo-grid li a {text-decoration:none; width: 200px;
  height:200px;display:block;background:red}
#gallery #photo-grid li a img {border:0; display: block;}

HTML

<ul id="photo-grid">

    <li class="photo">
    <a href="#" class="photo-link">
        <img src="http://placehold.it/200x150" class="photo-img" width="200" height="150" >
    <span class="photo-title">${photo-title}</span>
    </a>

    </li>

        <li class="photo">
    <a href="#" class="photo-link">
        <img src="http://placehold.it/150x200" class="photo-img" width="150" height="200">
    <span class="photo-title">${photo-title}</span>
    </a>
    </li>
</ul>

实例http://jsbin.com/onavo

如果我使用定位来保持图像和底部而不是边距:0自动或文本对齐中心不起作用

我的问题是,我希望保持垂直对齐底部和水平居中,红色框。

1 个答案:

答案 0 :(得分:0)

这是你想要实现的目标吗?

CSS

#gallery{overflow:hidden; padding:10px;}
#gallery #photo-grid{list-style: none;}
#gallery #photo-grid .photo{position:relative; display:inline;}
#gallery #photo-grid .photo-img{border:none;}
#gallery #photo-grid .photo-title{position:absolute; bottom:4px; left:0; right:4px; background-color:#FF0000; padding:10px 2px 10px 2px; text-align:center;}

HTML

<div id="gallery">
    <ul id="photo-grid">
        <li class="photo">
            <a href="#" class="photo-link">
                <img src="http://placehold.it/200x150" class="photo-img" width="200" height="150" >
                <span class="photo-title">${photo-title}</span>
            </a>
        </li>    
        <li class="photo">           
            <a href="#" class="photo-link">
                <img src="http://placehold.it/150x200" class="photo-img" width="150" height="200">
                <span class="photo-title">${photo-title}</span>
            </a>           
        </li>
    </ul>
</div>