简体   繁体   English

如何使用纯CSS,HTML和JavaScript自动布局网格?

[英]How to auto grid layout using pure css, HTML and javascript?

Need to show a grid layout system 3 by 3 row and column card >=990px . 需要显示一个3×3行和列卡 >=990px的网格布局系统。 And at >=760px This one screenshot and after <760 This screenshot . 而在>=760px 此屏幕截图<760 此屏幕截图之后

However, I tried below code snippet: 但是,我尝试了以下代码片段:

<div id="plat-listing-main">
    <div class="section group">
        <div class="col span_1_of_3">
            This is column 1
        </div>
        <div class="col span_1_of_3">
            This is column 2
        </div>
        <div class="col span_1_of_3">
            This is column 3
        </div>
    </div>
</div> 

CSS: CSS:

/*  SECTIONS  */
.section {
    clear: both;
    padding: 0px;
    margin: 0px;
}

/*  COLUMN SETUP  */
.col {
    display: block;
    float:left;
    margin: 1% 0 1% 1.6%;
}
.col:first-child { margin-left: 0; }


/*  GROUPING  */
.group:before,
.group:after {
    content:"";
    display:table;
}
.group:after {
    clear:both;
}
.group {
    zoom:1; /* For IE 6/7 */
}

/*  GRID OF THREE  */
.span_1_of_3 {
  width: 32.2%;
  height: 200px;
  background-color: red;
}

/*  GRID OF THREE  */
.span_3_of_3 {
    width: 100%;
}
.span_2_of_3 {
    width: 66.1%;
}
/* .span_1_of_3 {
    width: 32.2%;
} */

/*  GO FULL WIDTH AT LESS THAN 480 PIXELS */

@media only screen and (max-width: 990px) {
    .col { margin: 1% 0 1% 1.6%;}
  .span_1_of_3 { width: 49.2%; }
}

@media screen and (max-width: 760px) {
    .col { margin: 1% 0 1% 1.6%;}
  .span_1_of_3 { width: 100%; }
}

Need to make this grid as much as responsive. 需要使此网格尽可能地响应。 Do not want to use Bootstrap or any other framework just to learning purpose. 不想仅出于学习目的而使用Bootstrap或任何其他框架。

ANY HELP WOULD BE APPRECIATED!! 任何帮助,将不胜感激!!

I recommend that you use padding instead of margin. 我建议您使用填充而不是边距。 In order to make it work with padding, you need to put another div that has a padding and use your original div just to set the height and width of your content. 为了使其能够使用填充,您需要放置另一个具有填充的div并使用原始div来设置内容的高度和宽度。

Here's an example of your code adapted with my use of padding and some width and height to adjust the div. 这是您的代码示例,该代码适应了我使用填充和一些宽度和高度来调整div的情况。

 .section { clear: both; padding: 0px; margin: 0px; } .col { display: block; float: left; } .span_1_of_3 { width: 32.2%; height: 200px; } .padding-div { padding: 1% 0 1% 1.6%; /*padding to make text look right*/ background-color: red; /*height and width so the divs are separated*/ height: 90%; width: 95%; } /* GO FULL WIDTH AT LESS THAN 480 PIXELS */ @media only screen and (max-width: 990px) { .span_1_of_3 { width: 49.2%; } } @media screen and (max-width: 760px) { .span_1_of_3 { width: 100%; } } 
 <div id="plat-listing-main"> <div class="section group"> <div class="col span_1_of_3"> <div class="padding-div"> This is column 1 </div> </div> <div class="col span_1_of_3"> <div class="padding-div"> This is column 2 </div> </div> <div class="col span_1_of_3"> <div class="padding-div"> This is column 3 </div> </div> </div> </div> 

Also you can check this working example of JsFiddle here 您也可以在这里查看JsFiddle的这个工作示例

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

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