简体   繁体   English

如何使孩子的div相对于父母具有流动性

[英]How to make child div fluid with respect to parent

I have a two/three column layout based on screen size. 我有一个基于屏幕尺寸的two/threetwo/three列布局。

If window size is greater than 1000 than I need to follow 3 column layout else I need to follow two column layout. 如果窗口大小大于1000 ,则我需要遵循3 column布局,否则我需要遵循two column布局。

I have achieved this using JS but the code is very messy. 我已经用JS实现了,但是代码很混乱。 Now I want to do it using CSS. 现在,我想使用CSS做到这一点。 But I am stuck. 但是我被困住了。

Here is JSFiddle : https://jsfiddle.net/7kuah16h/1/ 这是JSFiddle: https ://jsfiddle.net/7kuah16h/1/

Constants: 常数:

Child Min Width (148px) including padding
child Max Width (196px) including padding

If Window Size is 500px (for eg) Child must be 196px each and two column. 如果“窗口大小”为500px (例如),则“子项”必须分别为196pxtwo column.

If Window Size is 350px Child must be 175px each in two column . 如果“窗口大小”为350px ,则two column子项”必须分别为175px

If Window size becomes 1000px then It should convert to 3 column Layout. 如果窗口大小变为1000px ,则应转换为3列布局。

In case window size is less than 1000px all content should be aligned center . 如果窗口大小小于1000px的所有内容应对准中心

Child must change its width a/c to available width. 儿童必须将其宽度a / c更改为可用宽度。

My Requirements: 我的要求: 在此处输入图片说明

I've added jsfiddle with a possible solution, but I'm not sure if I understood your question correctly. 我为jsfiddle添加了可能的解决方案,但是不确定我是否正确理解了您的问题。 Can you confirm this is what you're looking for? 您可以确认这是您要找的吗?

You need to define a media query with width for a specific breakpoint: 您需要为特定的断点定义宽度的媒体查询:

@media (min-width: 1000px) {
  .child {
    width: 33.33%;
  }
}

More details in the fiddle: 小提琴中的更多细节:

https://jsfiddle.net/wz5raza3/ https://jsfiddle.net/wz5raza3/

Ofcourse, depending on your solution, you need more than this, like paddings, margins or equal column height. 当然,根据您的解决方案,您还需要更多,例如填充,边距或相等的列高。 For any real-world usage, you would be better off with flexbox. 对于任何实际使用情况,使用flexbox都会更好。

If you only want to learn basic layouting using older methods, I would suggest: http://learnlayout.com/ 如果您只想学习使用较旧方法的基本布局,建议您: http : //learnlayout.com/

If you just want to support modern browsers you can use flex boxes and avoid the use of floating boxes and overflows. 如果您只想支持现代浏览器 ,则可以使用弹性框,避免使用浮动框和溢出框。 Much better for responsive design and mobile. 响应式设计和移动性要好得多。

.parent {
    display: flex;
    flex-wrap: wrap;
}
.child {
    height: 50px;
    flex: 0 1 50%; // 2 boxes 
 }

 @media (min-width: 1000px) {
   .child {
       flex: 0 1 33.33%; // 3 boxes for screens bigger than 1000px 
   }
 }

Update your fiddle: https://jsfiddle.net/7kuah16h/9/ 更新您的小提琴: https : //jsfiddle.net/7kuah16h/9/

Here's an approximate solution to your problem using @media screen . 这是使用@media screen解决您的问题的近似解决方案。 You can adjust the values of the screen and width of your content blocks as required. 您可以根据需要调整屏幕的值和内容块的宽度。

 @media screen and (max-width: 350px) { .block { width:150px; } } @media screen and (max-width: 500px) and (min-width: 351px) { .block { width:196px; } } @media screen and (max-width: 999px) and (min-width: 501px) { .block { width:48%; } } @media screen and (min-width: 1000px) { .block { width:32%; } } .container { text-align:center; } .block { text-align:left; display:inline-block; background-color:#ddd; height:175px; margin:0px; padding:0px; } body { margin:0px; padding:0px; } 
 <div class="container"> <div class="block">block</div> <div class="block">block</div> <div class="block">block</div> <div class="block">block</div> <div class="block">block</div> <div class="block">block</div> </div> 

只需将text-align: center添加到父div。

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

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