为什么高度是:100%;溢出的父母?

发布于 2025-01-15 23:03:46 字数 849 浏览 1 评论 0原文

我在使用 CSS 时遇到了一些问题。这是我的代码:

#root {
  width: 500px;
  height: 300px;
  border: 1px solid black;
}
#header {
  border-bottom: 1px solid black;
  width: 100%;
  height: 50px;  /* This is an example, we don't know the exact size */
}
#content {
  width: 100%;
  height: 100%;
  border-bottom: 1px solid red;
}
<div id="root">
 <div id="header">Some content</div>
 <div id="content">Why am I overflowing ?</div>
</div>

⚠️我不知道确切的标题大小!

如您所见,内容 div溢出了 div,但我想填充空旷的空间。

我读了很多其他问题,但它们都是关于 max-width 的,而且我没有使用这个属性。

如何用我的 content div 填充空白区域?

I'm having some problem with CSS. Here is my code:

#root {
  width: 500px;
  height: 300px;
  border: 1px solid black;
}
#header {
  border-bottom: 1px solid black;
  width: 100%;
  height: 50px;  /* This is an example, we don't know the exact size */
}
#content {
  width: 100%;
  height: 100%;
  border-bottom: 1px solid red;
}
<div id="root">
 <div id="header">Some content</div>
 <div id="content">Why am I overflowing ?</div>
</div>

⚠️ I don't know exact header size!

As you can see, the content div is overflowing the root div, but i want to fill the empty space.

I read many other questions, but they are all about max-width, and I'm not using this property.

How to fill the empty space with my content div?

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

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

发布评论

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

评论(3

裸钻 2025-01-22 23:03:46

我会转换为简单的 flexbox 布局。

#root {
  width: 500px;
  height: 100px; /* 300px; */
  border: 1px solid black;
  display: flex;
  flex-direction: column;
}

#header {
  border-bottom: 1px solid black;
}

#content {
  flex: auto;
  border-bottom: 1px solid red;
}
<div id="root">
  <div id="header">Some content</div>
  <div id="content">Why am I overflowing ?</div>
</div>

I would convert to a simple flexbox layout.

#root {
  width: 500px;
  height: 100px; /* 300px; */
  border: 1px solid black;
  display: flex;
  flex-direction: column;
}

#header {
  border-bottom: 1px solid black;
}

#content {
  flex: auto;
  border-bottom: 1px solid red;
}
<div id="root">
  <div id="header">Some content</div>
  <div id="content">Why am I overflowing ?</div>
</div>

德意的啸 2025-01-22 23:03:46

由于您不知道 header 的具体长度,有两种解决方案:

root 使用 height:max-content (这将使 content 具有没有更多空间)并根据需要自定义 content 的空间

这将使 header 的任何 height 都不会溢出父元素。

#root {
  width: 500px;
  height: min-content;
  border: 1px solid black;
}
#header {
  border-bottom: 1px solid black;
  width: 100%;

}
#content {
  width: 100%;
  min-height: 100px;
  border-bottom: 1px solid red;
}
<div id="root">
 <div id="header">Some content</div>
 <div id="content">Stack Overflow is a question and answer website for professional and enthusiast programmers. It is the flagship site of the Stack Exchange Network,[4][5][6] created in 2008 by Jeff Atwood and Joel Spolsky.[7][8] It features questions and answers on a wide range of topics in computer programming.[9][10][11] It was created to be a more open alternative to earlier question and answer websites such as Experts-Exchange. Stack Overflow was sold to Prosus, a Netherlands-based consumer internet conglomerate, on 2 June 2021 for $1.8 billion.[12]

The website serves as a platform for users to ask and answer questions, and, through membership and active participation, to vote questions and answers up or down similar to Reddit and edit questions and answers in a fashion similar to a wiki.[13] Users of Stack Overflow can earn reputation points and "badges"; for example, a person is awarded 10 reputation points for receiving an "up" vote on a question or an answer to a question,[14] and can receive badges for their valued contributions,[15] which represents a gamification of the traditional Q&A website. Users unlock new privileges with an increase in reputation like the ability to vote, comment, and even edit other people's posts.[16]</div>
</div>

Since you don't know the specific length for header, two solutions:

Use height:max-content for root (this will make content have no more space) and custom the space for content by what you want

This will make whatever height for header not going to overflow the parent element.

#root {
  width: 500px;
  height: min-content;
  border: 1px solid black;
}
#header {
  border-bottom: 1px solid black;
  width: 100%;

}
#content {
  width: 100%;
  min-height: 100px;
  border-bottom: 1px solid red;
}
<div id="root">
 <div id="header">Some content</div>
 <div id="content">Stack Overflow is a question and answer website for professional and enthusiast programmers. It is the flagship site of the Stack Exchange Network,[4][5][6] created in 2008 by Jeff Atwood and Joel Spolsky.[7][8] It features questions and answers on a wide range of topics in computer programming.[9][10][11] It was created to be a more open alternative to earlier question and answer websites such as Experts-Exchange. Stack Overflow was sold to Prosus, a Netherlands-based consumer internet conglomerate, on 2 June 2021 for $1.8 billion.[12]

The website serves as a platform for users to ask and answer questions, and, through membership and active participation, to vote questions and answers up or down similar to Reddit and edit questions and answers in a fashion similar to a wiki.[13] Users of Stack Overflow can earn reputation points and "badges"; for example, a person is awarded 10 reputation points for receiving an "up" vote on a question or an answer to a question,[14] and can receive badges for their valued contributions,[15] which represents a gamification of the traditional Q&A website. Users unlock new privileges with an increase in reputation like the ability to vote, comment, and even edit other people's posts.[16]</div>
</div>

拍不死你 2025-01-22 23:03:46

您必须减去标题的高度

更新

以获得标题的动态高度
你需要一些 javscript,我确信有更好的解决方案,但这也有效;)

let contentEl = document.querySelector('#content'),
    headerProperties = getComputedStyle(document.querySelector('#header')),
    headerHeight = headerProperties['height'];

    contentEl.style.height = `calc(100% - ${headerHeight})`;
#root {
  width: 500px;
  height: 300px;
  border: 1px solid black;
}

#header {
  border-bottom: 1px solid black;
  width: 100%;
}

#content {
  width: 100%;
  border-bottom: 1px solid red;
}
<div id="root">
    <div id="header">Some content</div>
    <div id="content">Why am I overflowing ?</div>
</div>

所以无论 header 的高度是多少,内容都不会溢出

You have to subtract the height of your header

UPDATE

For the dynamic height of the header
you need some javscript, am sure there is better solutions for that but this works also ;)

let contentEl = document.querySelector('#content'),
    headerProperties = getComputedStyle(document.querySelector('#header')),
    headerHeight = headerProperties['height'];

    contentEl.style.height = `calc(100% - ${headerHeight})`;
#root {
  width: 500px;
  height: 300px;
  border: 1px solid black;
}

#header {
  border-bottom: 1px solid black;
  width: 100%;
}

#content {
  width: 100%;
  border-bottom: 1px solid red;
}
<div id="root">
    <div id="header">Some content</div>
    <div id="content">Why am I overflowing ?</div>
</div>

So no matter what is the height of the header, the content will not overflowing

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