CSS Div 宽度百分比和填充而不破坏布局

发布于 2024-09-15 01:43:15 字数 739 浏览 7 评论 0原文

可能有一个简单的解决办法,但它已经困扰我很长时间了......

让我解释一下情况。我有一个 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 技术交流群。

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

发布评论

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

评论(3

淑女气质 2024-09-22 01:43:16

如果您希望 #header 与容器的宽度相同,并具有 10 像素的填充,则可以省略其宽度声明。这将导致它隐式占据其整个父级的宽度(因为 div 默认是块级元素)。

然后,由于您尚未定义其宽度,因此 10px 的填充将正确应用于元素内部,而不是添加到其宽度:

#container {
    position: relative;
    width: 80%;
}

#header {
    position: relative;
    height: 50px;
    padding: 10px;
}

您可以看到它 在此处执行

使用百分比宽度和像素填充/边距时关键是不要在同一元素上定义它们(如果您想精确控制大小)。将百分比宽度应用于父级,然后将像素填充/边距应用于未设置宽度的 display: block 子级。


更新

处理此问题的另一个选择是使用 box-sizing CSS 规则:

#container { 
    -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
    -moz-box-sizing: border-box;    /* Firefox, other Gecko */
    box-sizing: border-box;         /* Opera/IE 8+ */

    /* Since this element now uses border-box sizing, the 10px of horizontal
       padding will be drawn inside the 80% width */
    width: 80%;
    padding: 0 10px;
}

这里有一篇文章讨论 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:

#container {
    position: relative;
    width: 80%;
}

#header {
    position: relative;
    height: 50px;
    padding: 10px;
}

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:

#container { 
    -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
    -moz-box-sizing: border-box;    /* Firefox, other Gecko */
    box-sizing: border-box;         /* Opera/IE 8+ */

    /* Since this element now uses border-box sizing, the 10px of horizontal
       padding will be drawn inside the 80% width */
    width: 80%;
    padding: 0 10px;
}

Here's a post talking about how box-sizing works.

梦幻的心爱 2024-09-22 01:43:16

您还可以使用 CSS calc() 函数来减去填充宽度占容器宽度的百分比。

举个例子:

width: calc((100%) - (32px))

一定要确保减去的宽度等于总填充量,而不是二分之一。如果你用 16px 填充内部 div 的两侧,那么你应该从最终宽度中减去 32px,假设下面的示例是你想要实现的。

.outer {
  width: 200px;
  height: 120px;
  background-color: black;
}

.inner {
  height: 40px;
  top: 30px;
  position: relative;
  padding: 16px;
  background-color: teal;
}

#inner-1 {
  width: 100%;
}

#inner-2 {
  width: calc((100%) - (32px));
}
<div class="outer" id="outer-1">
  <div class="inner" id="inner-1"> width of 100% </div>
</div>

<br>
<br>

<div class="outer" id="outer-2">
  <div class="inner" id="inner-2"> width of 100% - 16px </div>
</div>

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:

width: calc((100%) - (32px))

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.

.outer {
  width: 200px;
  height: 120px;
  background-color: black;
}

.inner {
  height: 40px;
  top: 30px;
  position: relative;
  padding: 16px;
  background-color: teal;
}

#inner-1 {
  width: 100%;
}

#inner-2 {
  width: calc((100%) - (32px));
}
<div class="outer" id="outer-1">
  <div class="inner" id="inner-1"> width of 100% </div>
</div>

<br>
<br>

<div class="outer" id="outer-2">
  <div class="inner" id="inner-2"> width of 100% - 16px </div>
</div>

独闯女儿国 2024-09-22 01:43:16

尝试从 header 中删除 position 并将 overflow 添加到 container

#container {
    position:relative;
    width:80%;
    height:auto;
    overflow:auto;
}
#header {
    width:80%;
    height:50px;
    padding:10px;
}

Try removing the position from header and add overflow to container:

#container {
    position:relative;
    width:80%;
    height:auto;
    overflow:auto;
}
#header {
    width:80%;
    height:50px;
    padding:10px;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文