简体   繁体   English

调整父div的大小以容纳孩子?

[英]Resizing parent div to contain child?

My code is here: https://jsfiddle.net/yaphurt0/8/ 我的代码在这里: https : //jsfiddle.net/yaphurt0/8/
I tried to get rid of the rest of the webpage to show just the necessary section, but it ended up not displaying correctly, and I couldn't figure out why for the life of me. 我试图摆脱网页的其余部分,只显示必要的部分,但最终显示不正确,并且我无法弄清楚为什么会导致我的一生。 Regardless, I've trimmed away what I could and marked in comments in the css file the relevant code. 无论如何,我已经精简了一切,并在css文件的注释中标记了相关代码。

My problem is that I am trying to display 3 boxes at the bottom of the page next to each other. 我的问题是我试图在页面底部彼此相邻显示3个框。 As the window shrinks I use a media query to increase the width of the boxes so there are 2 per line, and then 1 if the the window shrinks further. 随着窗口的缩小,我使用媒体查询来增加框的宽度,因此每行有2个,如果窗口进一步缩小,则为1个。 Of course this means the boxes take up more room vertically, meaning they spill out as the parent div doesn't scale with it. 当然,这意味着这些盒子在垂直方向上会占据更多的空间,这意味着它们会随着父div的缩放而溢出。

I have tried overflow: auto; 我试过overflow: auto; to #me , however this just added a scrollbar to the content, when instead I want the #me to scale accordingly to contain its children. #me ,但是这只是向内容添加了一个滚动条,而我希望#me地缩放以包含其子级。 This is a pretty big problem which is stumping me, as you can see from the main text ("Hi I'm Danny..."), that also suffers from the same issues if the webpage is made very wide and shallow. 正如您从正文(“嗨,我是丹尼...”)中看到的那样,这是一个困扰我的大问题,如果将网页制作得很宽很浅,也会遇到同样的问题。

As much as I'm looking for a solution, I'm really hoping for an explanation so I can understand why the webpage is behaving as it is/what makes the parent scale, so in the future I don't just copy and paste and hope. 在寻找解决方案的过程中,我真的很希望能得到一个解释,以便我能够理解为什么网页按其原样运行/是什么使父级缩放,所以将来我不只是复制并粘贴和希望。

 #me { width: 100%; height: 45%; background-color: white; } #me .container { width: 60%; height: 100%; margin: 0 auto; } #me .container .introduction { height: 30%; margin-bottom: 5%; } #me .container .introduction .title, .subInfo { width: 80%; color: #262626; text-align: center; margin: 0 auto; border-bottom: 2px solid orange; } #me .container .introduction .title { font-family: 'Unica One', cursive; text-transform: uppercase; font-size: 4vw; } #me .container .introduction .subInfo { text-align: center; font-family: 'Unica One', cursive; text-transform: uppercase; font-size: 2vw; } #me .container .infoBody { width: 100%; height: 50%; min-height: 75px; margin: 0 auto; } #me .container .infoBody .columnInfo { float: left; text-align: center; display: flex; justify-content: center; align-items: center; height: 100%; width: 31.5%; margin: 2px; background-color: orange; border: 2px solid #e8eaed; border-radius: 5px; } @media only screen and (min-width: 500px) and (max-width: 1000px) { .minimalHeading { font-size: 5.5vw; } #me .container .infoBody .columnInfo { width: 48.5%; margin: 0 auto; } .minimalHeading .contactMe a { font-size: 4vw; } } @media only screen and (max-width: 500px) { .minimalHeading { font-size: 7.5vw; } #me .container .infoBody .columnInfo { width: 90%; margin: 0 auto; } .minimalHeading .contactMe a { font-size: 5vw; } } 
 <div id="me"> <div class="container"> <div class="introduction"> <p class="title">My Skillset</p> <p class="subInfo">The standard Web-development stuff</p> </div> <div class="infoBody"> <div class="columnInfo">Hi</div> <div class="columnInfo">There</div> <div class="columnInfo">You!</div> </div> </div> </div> 

The problem is in float:left property of the .columnInfo element and the height: 45% of #me element. 问题出在.columnInfo元素的float:left属性和height: #me元素的45%中。 If you remove those, you will see that #me will contain all three .columnInfo elements, but they will be stacked on top of each other. 如果删除这些元素,则会看到#me将包含所有三个.columnInfo元素,但它们将堆叠在一起。 You can use display:flex on the .infobody to make them wrap next to each other. 您可以在.infobody上使用display:flex使它们彼此环绕。 You will have to give your .columninfo elements an absolute height though. 但是,您将必须为.columninfo元素赋予绝对高度。

You can use flex, like mentioned. 您可以使用flex,如前所述。 Or simply add a clear to infoBody, like so: 或简单地向infoBody添加一个明文,如下所示:

// html
<div class="infoBody clear">
//css
.clear::after {
  content: '';
  display: table;
  clear: both;
}

The problem is your .columnInfo . 问题是您的.columnInfo Elements with the float property are no longer part of the normal flow of the page, so the containing div doesn't know how high they are. 具有float属性的元素不再属于页面的常规流程,因此包含div的元素不知道它们的高度。 The clear solves this problem by adding a hidden pseudo element below those columns and forcing the containing div down. clear通过在这些列下面添加一个隐藏的伪元素并强制向下包含div来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM