gpt4 book ai didi

html - text-align:center 不适用于 div 元素

转载 作者:太空狗 更新时间:2023-10-29 14:46:06 25 4
gpt4 key购买 nike

我已经搜索了大约 45 分钟,但在这里找不到解决我的问题的方法。我希望我的画廊类 div(这些将动态创建)仅使用 css 规则将它们自身对齐在 gallery_container div 的中心。我正在学习,所以任何解释都会有所帮助!

提前致谢!

<head>
<style>
#gallery_container{
text-align: center;
width:100%;
overflow: auto;
background:orange;
}
.gallery{
text-align: left;
border-style: solid;
border-width:3px;
border-top-left-radius: 40px;
border-bottom-right-radius: 40px;
background:yellow;
width:335px;
padding:20px;
float:left;
margin:15px;
}
.gallery h2{
margin-top:0;
}
.gallery img{
height:120px;
width:160px;
float:right;
}
</style>

<body>
<div id ='content_gallery'>
<h2>Gallery</h2>

<div id='gallery_container'>
<div class = gallery>
<img src = 'bowling_01.png'>
<h2>Company bowling</h2>
<h4>Date: June 14, 2013</h4>
<p>The company heads to Boca Bowl for our monthly bowling event!</p>
</div>

<div class = gallery>
<img src = 'bowling_01.png'>
<h2>Company bowling</h2>
<h4>Date: June 14, 2013</h4>
<p>The company heads to Boca Bowl for our monthly bowling event!</p>
</div>

<div class = gallery>
<img src = 'bowling_01.png'>
<h2>Company bowling</h2>
<h4>Date: June 14, 2013</h4>
<p>The company heads to Boca Bowl for our monthly bowling event!</p>
</div>
</div>
</div>
</body>

这里是 fiddle http://jsfiddle.net/9gwKc/1/

最佳答案

这可以通过使用 inline-block 显示来完成,float:left 将始终将元素发送到尽可能最左边。

.gallery {
text-align: left;
border-style: solid;
border-width:3px;
border-top-left-radius: 40px;
border-bottom-right-radius: 40px;
background:yellow;
width:335px;
padding:20px;
/*float:left; remove this*/
margin:15px;

/*add this*/
display:inline-block;
position:relative;
}

关于html - text-align:center 不适用于 div 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17177950/

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