CSS Div 宽度百分比和填充而不破坏布局
可能有一个简单的解决办法,但它已经困扰我很长时间了......
让我解释一下情况。我有一个 ID 为“容器”的 div,它包含页面中的所有内容(还包括页眉和页脚),它将保持所有内容内联,我只需执行 1 个简单的“margin:0 auto;”即可而不是倍数。假设我将 #container 的宽度设置为 80%。现在,如果我将另一个具有相同宽度(80%)的 div 放入其中,并为其指定“标题”的 ID,周围有 10 像素的填充,则布局将“中断”(可以这么说),因为页面将填充量添加到宽度。那么,如何在不使用 #header div 较低百分比等方法的情况下使其保持在范围内?基本上,我想让它变得流畅。
这里有一些示例代码,可以让您了解我在说什么...
CSS
#container {
position:relative;
width:80%;
height:auto;
}
#header {
position:relative;
width:80%;
height:50px;
padding:10px;
}
HTML
<div id="container">
<div id="header">Header contents</div>
</div>
谁能帮我解决这个一直困扰我的问题吗?
There may be a simple fix for this, but it has troubled me for ages now...
Let me explain the situation. I have a div with the ID 'container' that holds all the contents in the page (including header and footer also) that will keep everything inline and I can do just 1 simple 'margin:0 auto;' instead of multiples. So lets say that I have the width of #container set to 80%. Now if I put another div inside with the same width (80%) and give it the ID of 'header' with 10px of padding all around, the layout would "break" (so to speak) because the page adds the padding amount onto the width. So, how can I make it stay in-bounds without using methods such as a lower percentage for the #header div? Basically, I want to make it fluid.
Here is some example code to give you an idea of what I am talking about...
CSS
#container {
position:relative;
width:80%;
height:auto;
}
#header {
position:relative;
width:80%;
height:50px;
padding:10px;
}
HTML
<div id="container">
<div id="header">Header contents</div>
</div>
Can anyone help me out with this issue that has been bugging me forever?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您希望
#header
与容器的宽度相同,并具有 10 像素的填充,则可以省略其宽度声明。这将导致它隐式占据其整个父级的宽度(因为 div 默认是块级元素)。然后,由于您尚未定义其宽度,因此 10px 的填充将正确应用于元素内部,而不是添加到其宽度:
您可以看到它 在此处执行。
使用百分比宽度和像素填充/边距时关键是不要在同一元素上定义它们(如果您想精确控制大小)。将百分比宽度应用于父级,然后将像素填充/边距应用于未设置宽度的
display: block
子级。更新
处理此问题的另一个选择是使用 box-sizing CSS 规则:
这里有一篇文章讨论 box-调整尺寸工作。
If you want the
#header
to be the same width as your container, with 10px of padding, you can leave out its width declaration. That will cause it to implicitly take up its entire parent's width (since a div is by default a block level element).Then, since you haven't defined a width on it, the 10px of padding will be properly applied inside the element, rather than adding to its width:
You can see it in action here.
The key when using percentage widths and pixel padding/margins is not to define them on the same element (if you want to accurately control the size). Apply the percentage width to the parent and then the pixel padding/margin to a
display: block
child with no width set.Update
Another option for dealing with this is to use the box-sizing CSS rule:
Here's a post talking about how box-sizing works.
您还可以使用 CSS calc() 函数来减去填充宽度占容器宽度的百分比。
举个例子:
一定要确保减去的宽度等于总填充量,而不是二分之一。如果你用 16px 填充内部 div 的两侧,那么你应该从最终宽度中减去 32px,假设下面的示例是你想要实现的。
You can also use the CSS calc() function to subtract the width of your padding from the percentage of your container's width.
An example:
Just be sure to make the subtracted width equal to the total padding, not just one half. If you pad both sides of the inner div with 16px, then you should subtract 32px from the final width, assuming that the example below is what you want to achieve.
尝试从
header
中删除position
并将overflow
添加到container
:Try removing the
position
fromheader
and addoverflow
tocontainer
: