调整 div 的宽度以适应其包含多个 div 的内容

发布于 2024-12-13 14:54:05 字数 746 浏览 1 评论 0原文

我有一个小问题。

实际上我正在尝试调整“内容”的宽度以适应其中的多个div。事实上,我正在尝试使这段代码适应多个屏幕,我无法指定特殊的宽度。内联块根本不起作用。我不知道为什么。我会将 div 内容放在页面的中间,并将类放在 div 内容的中间。事实上,这是一个完美的中心。

有想法吗?

HTML

<div id="content">
<div class="badges"></div>    
<div class="badges"></div>    
<div class="badges"></div>    
<div class="badges"></div>    
<div class="badges"></div>    
<div class="badges"></div>    
<div style="clear:both;"></div>       
</div>

CSS

#content{
    display:inline-block;
}

.badges{
    width:220px;
    height:380px;
    float:left;
    background:red;
    margin:10px;
}

谢谢, 巴斯蒂安

I have a small problem.

Actually I'm trying to adapt the width of "content" to multiple div in it. In fact, I'm trying to adapt this code to several screens, I can't specified a special width. The inline-block doesn't work at all. I don't know why. I would center the div content in the middle of my page and center classes in the middle of the div content. A perfect center in fact.

Have an idea?

HTML

<div id="content">
<div class="badges"></div>    
<div class="badges"></div>    
<div class="badges"></div>    
<div class="badges"></div>    
<div class="badges"></div>    
<div class="badges"></div>    
<div style="clear:both;"></div>       
</div>

CSS

#content{
    display:inline-block;
}

.badges{
    width:220px;
    height:380px;
    float:left;
    background:red;
    margin:10px;
}

Thanks,
Bastien

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌伤浅笑 2024-12-20 14:54:05

不要使用浮动。

试试这个:

#content {text-align: center;}
.badges {
    width: 220px;
    height: 380px;
    background: red;
    margin: 10px;
    display: inline-block;
}

这就是 inline-block 应该如何使用。

另外,按照 Kai Qing 的说法更正您的标记。

Don't use float.

Try this:

#content {text-align: center;}
.badges {
    width: 220px;
    height: 380px;
    background: red;
    margin: 10px;
    display: inline-block;
}

That's how inline-block should be used.

Also, correct your markup as Kai Qing says.

天荒地未老 2024-12-20 14:54:05

对于其中之一,您应该使用实际的 div 关闭标签而不是 span 来关闭您的 div。然后也关闭该清除 div

for one you should close your divs with actual div closing tags and not span. Then close that clearing div as well

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文