gpt4 book ai didi

html - CSS 布局和宽度为 % 的内联 block

转载 作者:行者123 更新时间:2023-11-27 23:40:58 25 4
gpt4 key购买 nike

我正在尝试创建一个包含 3 个或更多 DIV 框的流体布局,这些框可以位于固定或流体容器中,如下所示:

example:

到目前为止,我使用了 display: inline-block 来让两个盒子彼此相邻,这确实有效,但是当宽度设置为 48% 且边距设置为 1% 时(当所有内容都是内联时, 如果我正确的话,总和应该是 100%?)第二个 div 换行。

关于第三个盒子,当我将宽度设置为 98%(然后应用 1% 的边距)时,盒子悬在右边的容器上...

我最终得出的结论是: problem:

我可以更改和减少 % 以使其“有点”符合我的要求,但是顶部的两个框和底部的大框并没有很好地对齐。

例如: http://imgur.com/igI73Y1

本质上,我试图制作的是一个可以使用的快速片段,我可以将其添加到网站上的任何容器中,以提供漂亮、干净的布局,为添加内容做好准备。

(我做了几个不同的布局)

我想尝试限制创建的类的数量,以便我可以根据需要轻松地编辑布局。

例如:

.contentbox(使这些框工作的主要“设置”)

.smallbox(如果3个div依次设置为.smallbox,则3个显示为inline)

.normalbox(如果2个div依次设置为.normalbox,则2个显示为inline)

.largebox(如果 1 个 div 设置为 .largebox 它将到达容器的边缘)

我想使用 % 所以无论容器的大小如何,它总是适合而无需更改宽度像素等。

目前这是我得到的:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
#container {
width: 600px;
border: thin solid #000;
}
.contentbox {
position:relative;
border-top-style: solid;
border-right-style: solid;
border-bottom-style: solid;
border-left-style: solid;
border-top-color: #00F;
border-right-color: #00F;
border-bottom-color: #00F;
border-left-color: #00F;
display: inline-block;
width: 48%;
margin: 1%;
vertical-align: top;

}
.largebox {
width:100%;
}
</style>
</head>

<body>
<div id="container">
<div class="contentbox">class="contentbox"</div>
<div class="contentbox">class="contentbox"</div>
<div class="contentbox largebox">class="contentbox largebox"</div>
</div>
</body>
</html>

在此先感谢您的帮助!

最佳答案

您必须考虑到边框会占用宽度,并且大容器两侧的 1% 边距意味着它只能小于 100% 宽度。我将它设置为 94,你的小的设置为 45,并将填充设置为 0!重要;现在可以了。

#container {
width: 100%;
padding: 0!important;
border: thin solid #000;
}
.contentbox {
position: relative;
border-top-style: solid;
border-right-style: solid;
border-bottom-style: solid;
border-left-style: solid;
border-top-color: #00F;
border-right-color: #00F;
border-bottom-color: #00F;
border-left-color: #00F;
display: inline-block;
width: 45%;
margin: 1%!important;
vertical-align: top;
}
.largebox {
width: 94%;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Containers</title>

</head>

<body>
<div id="container">
<div class="contentbox">class="contentbox"</div>
<div class="contentbox">class="contentbox"</div>
<div class="contentbox largebox">class="contentbox largebox"</div>
</div>
</body>

</html>

关于html - CSS 布局和宽度为 % 的内联 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31763720/

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