带有 % 宽度和 px 边框的 div
我正在尝试创建一个包含 3 个 div 的 div 。
.outter
{
right: 100px;
border: 10px solid white;
}
.main
{
overflow: hidden;
width: 100%;
height: 150px;
}
.left
{
float: left;
width: 40%;
height: 100%;
background-color: green;
border-right: 5px solid white;
}
.center
{
float: left;
width: 40%;
height: 100%;
background-color: red;
border-left: 5px solid white;
border-right: 5px solid white;
}
.right
{
float: right;
width: 20%;
height: 100%;
background-color: orange;
border-left: 5px solid white;
}
<div class="outter">
<div class="main">
<div class="left">
</div>
<div class="center">
</div>
<div class="right">
</div>
</div>
</div>
这是我到目前为止所得到的。 -父div的右距固定为100px,边框为10px白色,宽度为100% - 100px; - 内部 div 有 40% + 40% + 20%,它们之间的距离为 10 px(这就是为什么我将 border-left 5 和 border-right 5 放在一起)。
我在设置此选项时遇到问题。我需要的是固定大小的边框和右侧边距。其他 div 应该是动态的以填充100% 宽度
能帮我吗?
有人
I'm trying to create a div with 3 divs inside.
.outter
{
right: 100px;
border: 10px solid white;
}
.main
{
overflow: hidden;
width: 100%;
height: 150px;
}
.left
{
float: left;
width: 40%;
height: 100%;
background-color: green;
border-right: 5px solid white;
}
.center
{
float: left;
width: 40%;
height: 100%;
background-color: red;
border-left: 5px solid white;
border-right: 5px solid white;
}
.right
{
float: right;
width: 20%;
height: 100%;
background-color: orange;
border-left: 5px solid white;
}
<div class="outter">
<div class="main">
<div class="left">
</div>
<div class="center">
</div>
<div class="right">
</div>
</div>
</div>
This is what I got so far.
-The parent div should have a right distance fixed of 100px, a border of 10px white and the widht is the 100% - 100px;
-The inside divs have 40% + 40% + 20% with a distance between them of 10 px (thats why I putted the border-left 5 and border-right 5.
I'm having problems setting this. What I need is to have fixed sized borders and margin to the right. the other divs should be dynamic to fullfill the 100% width.
Can anyone help me?
Regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
box-sizing
来实现此目的。像这样写:检查这个:
http://jsfiddle.net/ubtdT/
You can use
box-sizing
for this. write like this:Check this:
http://jsfiddle.net/ubtdT/
您的 box-model 存在问题。元素不能有 100% 宽度,然后有 10px 边框,因为边框是添加到 100% 宽度之外的,这会导致您的问题。
根据您打算支持的浏览器,您可以使用 CSS3 的 box-sizing财产。通过设置 box-sizing: border-box; ,您可以强制浏览器以指定的宽度和高度渲染框,并在框内添加边框和内边距。这应该可以解决你的问题。请注意旧版浏览器的支持有限。
如果您想进行更多实验,您可以使用新的 CSS3
calc()
来实际计算动态宽度:You have a problem with the box-model. An element cannot have 100% width and then a 10px border, because the border is added outside the 100% width, which is causing your problem.
Depending on what browsers you intend to support, you can make use of CSS3's box-sizing property. By setting
box-sizing: border-box;
, you can force the browser to instead render the box with the specified width and height, and add the border and padding inside the box. Which should solve your problem. Note the limited support in older browsers.If you want to go even more experimental you can use the new CSS3
calc()
to actually calculate a dynamic width: