gpt4 book ai didi

css - 为什么 text-align :center and margin:0 auto on positioned element? 没有效果看实例

转载 作者:行者123 更新时间:2023-11-28 14:53:27 26 4
gpt4 key购买 nike

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

如果我使用定位来保持图像和底部比 margin:0 auto 或 text-align center 不起作用

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

最佳答案

这是您要实现的目标吗?

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>

关于css - 为什么 text-align :center and margin:0 auto on positioned element? 没有效果看实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4168297/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com