我刚刚使用旧技术(浮动元素和嵌入式元素)创建了一个网格布局。它足够好用。简要地说,我有一个顶级包装器类来居中和限制大小内容。有2个主要部分,每个部分都有标题(20%),其余部分(80%)以不同的方式细分。甚至还有一个设置有填充物的弹性排水沟(在em中)。我在设计布局时考虑到了内容与表示之间的良好分隔,以实现较高的代码可维护性,并且在这种情况下,存在一些多余的标记(尤其是div)。
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Float layout plus wrapping rows using inline-block</title>
<!-- the base styles and "housekeeping" styles are in here: -->
<link rel="stylesheet" href="css/grid-base.css">
<!-- the HTML5 shiv,to help older browsers understand styling
on newer HTML5 elements: -->
<script src="js/html5shiv.min.js"></script>
<style>
/* grid styling */
.row {
margin: 0 -.6875em;
padding: 0;
list-style: none;
}
.row:after {
content: '';
display: block;
clear: both;
}
.row-quartet > * {
width: 25%;
}
.row-trio > * {
width: 33.3333%;
}
.col {
float: left;
-moz-box-sizing: border-box;
box-sizing: border-box;
margin: 0;
padding: 0 .6875em 1.375em;
}
.col:last-child {
float: right;
}
.row-wrapping {
font-size: 0;
margin: 0 -11px;
margin: 0 -.6875rem;
}
.row-wrapping > * {
float: none;
vertical-align: top;
display: inline-block;
font-size: 16px;
font-size: 1rem;
}
/* content styling */
.subcategory {
margin-top: 1.5em;
border-bottom: 1px solid #8e3339;
}
.subcategory-featured {
width: 50%;
}
.subcategory-content {
width: 80%;
}
.subcategory-header {
width: 20%;
}
.story {
padding: .6875em;
background-color: #eee;
}
.story + .story {
margin-top: 1.375em;
}
.story img {
width: 100%;
}
</style>
</head>
<body>
<header class="masthead">
<div class="wrapper">
<h1>Important News</h1>
</div>
</header>
<nav role="navigation" class="navbar">
<div class="wrapper">
<ul class="navlist">
<li><a href="#">Home</a></li>
<li><a href="#">World</a></li>
<li><a href="#">Local</a></li>
<li><a href="#">Sports</a></li>
</ul>
</div>
</nav>
<main class="wrapper">
<section class="subcategory">
<div class="row">
<header class="col subcategory-header">
<h2>Lorem ipsum</h2>
</header>
<div class="col subcategory-content">
<div class="row row-quartet">
<div class="col subcategory-featured">
<article class="story">
<img src="http://placehold.it/600x300" alt="Dummy image">
<h3><a href="#">Cras suscipit nec leo id.</a></h3>
<p>Autem repudiandae aliquid tempora quos reprehenderit architecto,sequi repellat.</p>
</article>
</div>
<div class="col">
<article class="story">
<img src="http://placehold.it/600x300" alt="Dummy image">
<h3><a href="#">Perferendis,ipsam!</a></h3>
<p>Neque magnam vero obcaecati facere nobis sint dolore accusamus vitae consequuntur ad necessitatibus,laborum molestiae.</p>
</article>
</div>
<div class="col">
<article class="story">
<img src="http://placehold.it/600x300" alt="Dummy image">
<h3><a href="#">Curabitur mattis purus nec velit.</a></h3>
<p>Neque magnam vero obcaecati facere nobis sint dolore accusamus vitae consequuntur ad necessitatibus,laborum molestiae.</p>
</article>
</div>
</div>
<ul class="row row-quartet row-wrapping">
<li class="col">
<div class="story">
<h3><a href="#">Perferendis,ipsam! Dolor sit amet consectetur</a></h3>
</div>
</li>
<li class="col">
<div class="story">
<h3><a href="#">Aliquam mattis eros id posuere.</a></h3>
</div>
</li>
<li class="col">
<div class="story">
<h3><a href="#">Proin leo felis,semper nec</a></h3>
</div>
</li>
<li class="col">
<div class="story">
<h3><a href="#">Aliquam vitae risus tortor. Sed!</a></h3>
</div>
</li>
<li class="col">
<div class="story">
<h3><a href="#">Perferendis,ipsam!</a></h3>
</div>
</li>
<li class="col">
<div class="story">
<h3><a href="#">Aliquam mattis eros id posuere.</a></h3>
</div>
</li>
<li class="col">
<div class="story">
<h3><a href="#">Proin leo felis,semper nec</a></h3>
</div>
</li>
<li class="col">
<div class="story">
<h3><a href="#">Aliquam vitae risus tortor. Sed!</a></h3>
</div>
</li>
</div>
</div>
</div>
</section>
<section class="subcategory">
<div class="row">
<header class="col subcategory-header">
<h2>Dolor sit amet</h2>
</header>
<div class="col subcategory-content">
<div class="row row-trio">
<div class="col">
<article class="story">
<img src="http://placehold.it/600x300" alt="Dummy image">
<h3><a href="#">Ut sit amet mi massa</a></h3>
<p>Neque magnam vero obcaecati facere nobis sint dolore accusamus vitae consequuntur ad necessitatibus,laborum molestiae.</p>
</article>
</div>
<div class="col">
<article class="story">
<h3><a href="#">Nunc mollis sit amet nunc</a></h3>
<p>Neque magnam vero obcaecati facere nobis sint dolore accusamus vitae consequuntur ad necessitatibus,laborum molestiae.</p>
</article>
<article class="story">
<h3><a href="#">Duis sed ante enim. Cras</a></h3>
<p>Neque magnam vero obcaecati facere nobis sint dolore accusamus vitae consequuntur ad necessitatibus,laborum molestiae.</p>
</article>
</div>
<div class="col">
<article class="story">
<img src="http://placehold.it/600x300" alt="Dummy image">
<h3><a href="#">Animi,explicabo,ipsum</a></h3>
<p>Neque magnam vero obcaecati facere nobis sint dolore accusamus vitae consequuntur ad necessitatibus,laborum molestiae.</p>
</article>
</div>
</div>
</div>
</div>
</section>
</main>
</body>
</html>
和外部.css:
body {
margin: 0;
line-height: 1.375;
font-family: Georgia,Times New Roman,Times,serif;
}
h1,h2,h3,h4,h5,h6 {
font-family: Avenir Next,Avenir,Franklin Gothic,Trebuchet MS,Arial,sans-serif;
margin-top: 0;
}
a {
color: #8E3339;
text-decoration: none;
}
a:hover,a:focus {
text-decoration: underline;
}
/* here's our wrapper */
.wrapper {
width: 95%;
max-width: 76em;
margin: 0 auto;
}
/* masthead styling */
.masthead {
background-color: #8E3339;
}
.masthead h1 {
margin: 0;
padding: 0.5em 0;
color: #fff;
text-shadow: -.1em .1em 0 rgba(0,0.3);
}
/* navbar styling */
.navbar {
background-color: #5E2126;
margin-bottom: 1.375em;
}
nav {
display: block;
}
.navbar ul {
font-family: 'Avenir Next',Corbel,'Franklin Gothic','Century Gothic',CenturyGothic,AppleGothic,sans-serif;
list-style: none;
padding: 0;
margin: 0;
background-color: #752A2F;
display: flex;
overflow: hidden;
}
.navbar li {
float: left;
text-transform: uppercase;
text-align: center;
box-sizing: border-box;
flex: 1 1 auto;
border-left: 1px solid #8E3339;
}
.navbar li:first-child {
border-left: 0;
}
.navbar li a {
display: block;
text-decoration: none;
line-height: 1.75em;
padding: 1em 2em;
color: #fff;
}
https://jsfiddle.net/36q59rav/
为改进我稍后介绍的代码(一种渐进式增强),flexbox的功能,在等高的盒子中添加一些装饰,这是float所具有的一项棘手的功能(实际上,已经有导航栏的flexbox,带有类似后备浮动的float)。
我正在使用Modernizr来测试特定的flexbox支持(flexbox和flexwrap类)。我在Firefox中取得了惊人的成绩(由于我的有限经验),但是令我惊讶的是,当我在Google Chrome浏览器中测试代码时,出现了一个视觉错误:第一部分的第二个嵌套行与第一行重叠,能够理解为什么。
这是代码: https://jsfiddle.net/rs7n8hLm/
您可以在jsfiddle浏览器中看到相同的问题。 建议也受到赞赏,因为我在一条小巷里,如果某些代码行晦涩难懂,我非常有空。 预先感谢。
这里是问题的屏幕截图。 https://ibb.co/jkNKRnY