简体   繁体   English

为什么我的div堆栈在浏览器中不调整大小?

[英]Why don't my divs stack on browser resize?

I'm a newbie and trying to figure out how to stack these two boxes on top of each other when the browser gets resized. 我是一名新手,试图弄清楚如何在调整浏览器大小时将这两个框堆叠在一起。 I don't want to use float, I'd rather stick to inline-block unless anyone else has strong suggestion against it. 我不想使用float,我宁愿坚持使用inline-block,除非其他人对此有强烈的建议。 I was thinking I guess in using inline-block that the boxes would stack when the div got resized with the browser but it isn't happening. 我当时在想使用inline-block,当div在浏览器中调整大小时,这些框会堆叠在一起,但是没有发生。 The boxes are just getting skinner and skinner and the text just wraps and exceeds the box. 盒子越来越贴皮了,文本只是包裹起来并超出了盒子。 ` `

 .cp2_maincontainer { width: 100%; height: 300px; font-size: 0; display: flex; justify-content: space-between; padding: 10px 50px 20px 50px; } .cp2_container { width: 47%; height: 100%; background: no-repeat center; display: inline-block; position: relative; } .cp2_subcontainer { background-color: rgba(211, 211, 211, .8); height: 100%; width: 100%; padding: 10px 15px; font-size: 16px; font-family: playfair display; position: absolute; outline: solid 2px darkgrey; outline-offset: -10px; } .cp2_subcontainer ul { margin-left: 20px; } .cp2_subcontainer p { margin: 10px; } .cp2_subcontainer h3 { padding: 10px 0; } .cp2_container2 { background-color: darkgrey; background: no-repeat center; width: 47%; height: 100%; display: inline-block; position: relative; } .cp2_subcontainer2 { background-color: rgba(211, 211, 211, 0.8); height: 100%; width: 100%; padding: 10px 15px; font-size: 16px; font-family: playfair display; position: absolute; outline: solid 2px darkgrey; outline-offset: -10px; } .cp2_subcontainer2 ul { margin-left: 20px; } .cp2_subcontainer2 p { margin: 10px; } .cp2_subcontainer2 h3 { padding: 10px 0; } .addtextarea { color: black; padding: 10px; width: 100%; font-size: 16px; text-align: justify; } 
 <div class="cp2_maincontainer"> <div class="cp2_container" style="background-image:URL(<?php the_field('imageleft'); ?>)"> <div class="cp2_subcontainer"> <h3 style="text-align:center;">Title for Text Box 1</h3> <p>Text in box # 1</p> </div> </div> <div class="cp2_container2" style="background-image:URL(<?php the_field('imageright'); ?>)"> <div class="cp2_subcontainer2"> <h3 style="text-align:center;">Title for Text Box 2</h3> <p>Text in box #2</p> </div> </div> </div> <div class="sectionbreak" align="center"></div> 

You gave the divs a width in percentage. 您为div设置了宽度百分比。 That means that as their container grows smaller, so do they, and they never have a reason to break. 这意味着随着它们的容器变得越来越小,它们也越来越小,而且它们从来没有理由破坏。 The obvious solution is to give them a fixed width (px, em). 显而易见的解决方案是为它们提供固定的宽度(px,em)。

If for some reason you need the percentages to work up to a certain point (for the purpose of bigger screens for example), two options come to mind: 如果出于某种原因,您需要将百分比提高到一定水平(例如,用于更大的屏幕),那么您会想到两个选择:

  1. Give the divs a min-width , so that once they hit that width the percentages will be ignored and the line will break. 给div一个min-width ,以便一旦它们达到该宽度,百分比将被忽略并且该行将断开。
  2. Using media queries, define a different width for them based on screen size. 使用媒体查询,根据屏幕尺寸为其定义不同的宽度。

 .cp2_maincontainer { width:100%; height:300px; font-size: 0px; display:flex; justify-content: space-between; padding: 10px 50px 20px 50px; } .cp2_container { width:47%; height:100%; background: no-repeat center; display:inline-block; position: relative; } .cp2_subcontainer { background-color: rgba(211, 211, 211, 0.8); height:100%; width:100%; padding:10px 15px; font-size:16px; font-family: playfair display; position: absolute; outline:solid 2px darkgrey; outline-offset: -10px; } .cp2_subcontainer ul{ margin-left:20px; } .cp2_subcontainer p{ margin:10px; } .cp2_subcontainer h3{ padding:10px 0px; } .cp2_container2 { background-color: darkgrey; background: no-repeat center; width:47%; display:inline-block; position: relative; min-width: 300px; position: absolute; right: 0; height:300px; } .cp2_subcontainer2 { background-color: rgba(211, 211, 211, 0.8); height:100%; width:100%; padding:10px 15px; font-size:16px; font-family: playfair display; position: absolute; outline:solid 2px darkgrey; outline-offset: -10px; } .cp2_subcontainer2 ul{ margin-left:20px; } .cp2_subcontainer2 p{ margin:10px; } .cp2_subcontainer2 h3{ padding:10px 0px; } .addtextarea { color: black; padding: 10px; width: 100%; font-size: 16px; text-align: justify; } 
 <div class="cp2_maincontainer"> <div class="cp2_container" style="background-image:URL(<?php the_field('imageleft'); ?>)"> <div class="cp2_subcontainer"> <h3 style="text-align:center;">Title for Text Box 1</h3> <p>Text in box # 1</p> </div> </div> <div class="cp2_container2" style="background-image:URL(<?php the_field('imageright'); ?>)"> <div class="cp2_subcontainer2"> <h3 style="text-align:center;">Title for Text Box 2</h3> <p>Text in box #2</p> </div> </div> </div> <div class="sectionbreak" align="center"> </div> 

I think this is what you are trying to accomplish. 我认为这就是您要完成的工作。

I have added/edited the following on .cp2_container2 我在.cp2_container2上添加/编辑了以下内容

min-width: 300px;
position: absolute;
right: 0;
height:300px;

You need a min-width in order to have the 2 boxes overlap, otherwise they will always be half the width of the page and never overlap. 您需要最小宽度才能使2个框重叠,否则它们将始终是页面宽度的一半且永不重叠。

Positioning absolutely allows the div to freely pass over the statically positioned one. 绝对定位可以使div自由通过静态定位的div。

Right just tells the div to position at the right edge of whatever it is relative to, in this case the body. Right只是告诉div相对于相对位置(在此情况下为body)位于右侧。

By positioning absolutely the height 100% becomes relative to the entire window, I have solved by using a pixel height, although you could also position the cp2_maincontainer relative and given it a height, making the height 100% of cp2_container2 relative the height of cp2_maincontainer. 通过绝对定位相对于整个窗口的高度100%,我已经解决了使用像素高度的问题,尽管您也可以相对于cp2_maincontainer定位并赋予其高度,使cp2_container2的高度相对于cp2_maincontainer的高度为100%。

Good luck. 祝好运。

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

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