html - 子div没有得到父div的原始高度,为什么?

标签 html css

我在使用 CSS 时遇到了问题。

我已经有很长时间没有使用 css 了,但现在我正在努力做一些非常简单的事情,你可能会说。

嗯,问题是我创建了一个高度为 1100 像素的 div,在该 div 中我还有两个 div,其中一个在顶部,另一个在上面的 div 正下方。 上面的div没有指定高度,只是使用padding来获取高度。下部 div 的高度设置为 100%,所以它(基本上)应该获得父 div 中剩余的高度,不是吗?

此时,下方的div与父div重叠。

这是我的代码:

  html, body {
    	background: #121212;
    	color: #ffffff;
    	font-family: 'Open Sans', sans-serif;
    }
    
    * {
    	margin: 0;
    	padding: 0;
    }
    
    #container {
    	margin: 5% auto;
    	width: 1100px;
    	height: 600px;
    	background: #ff9900;
    }
    #container .inner-box-head {
    	font-style: italic;
    	padding: 5px 0px 5px 0px;
    }
    #container .inner-box {
    	height: 100%;
    }
    #container .inner-box .inner-box-video {
    	width: 70%;
    	height: 100%;
    	background: #fff;
    }
  <!DOCTYPE html>
    <html lang="cs">
    	<head>
    		<title>jadvo.cz</title>
    		<meta charset="utf-8">
    		<link href="css/style.css" rel="stylesheet">
    		<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
    	</head>
    	
    	<body>
    		<div id="container">
    			<div class="inner-box-head">
    			Diváků: 0
    			</div>
    			
    			<div class="inner-box">
    				<div class="inner-box-video">
    					<!--<iframe width="560" height="315" src="https://www.youtube.com/embed/7JJfJgyHYwU?rel=0&amp;controls=0&amp;showinfo=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>-->
    				</div>
    				
    				<div class="inner-box-chat">
    				
    				</div>
    			</div>
    		</div>
    	</body>
    </html>

  

Here you can see the result I'm getting...

最佳答案

And the lower div has height set to 100%, so it (basically) should get the height thats left in the parent div, shouldnt it?

没有。它应该与父级具有相同的高度。 100% 表示父级高度的 100%,而不是未被其他东西使用的父级高度的 100%。

At this time, the lower div is overlapping the parent div.

事实并非如此。 100% 高度的 div 太大而不适合其父级,因此它伸出父级,从而影响到下面的空间。


如果你想要用完剩余空间的元素:使用Flexbox做你的布局。

section {
  display: flex;
  flex-direction: column;
  height: 200px;
  margin: 1em auto;
  max-width: 100px;
}

section header {
  flex: 0 0 auto;
  background: #faa;
  padding: 1ex;
}

section div {
  flex: 1 1 auto;
  background: #aaf;
  padding: 1ex;
}

section footer {
  flex: 0 0 auto;
  background: #afa;
  padding: 1ex;
}
<section>
  <header>Header</header>
  <div>Content</div>
  <footer>Footer</footer>
</section>

<section>
  <header>Header</header>
  <div>Content</div>
  <footer>Footer</footer>
</section>

<section>
  <header>Header</header>
  <div>Content</div>
  <footer>Footer</footer>
</section>

<section>
  <header>Header</header>
  <div>Content</div>
  <footer>Footer</footer>
</section>

关于html - 子div没有得到父div的原始高度,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52152442/

相关文章:

jquery - 动态更新 options_for_select

html - Bootstrap 向左浮动和向右浮动

javascript - 内容滚动 javascript 错误,当到达页面点时内容继续向上滚动

javascript - jquery append() 与 css 显示 :inline-block 的奇怪交互

html - 为什么这个 html 照片库没有居中?

javascript - 创建使用图像和文本的内联符号

jquery - 如何附加输入类型文件

javascript - 将 CSS 属性的值作为变量存储在外部配置文件中

javascript - 文本淡出/阅读更多链接

javascript - 将本地文件(CSS 或 JS)加载到所选网站