<!DOCTYPEhtml><html><head><metacharset="utf-8"><title>浮动</title><styletype="text/css">:root{box-sizing: border-box;}*,
::before,
::after{box-sizing: inherit;}body{background-color: #eee;font-family: Arial, Helvetica, sans-serif;}body * + *{margin-top: 1.5em;}.container{/* 小视口内层容器会填满屏幕,大视口会扩展到1080px */max-width: 1080px;margin: 0 auto;}/* 头部的颜色和内边距 */header{padding: 1em 1.5em;color: white;background-color: #0072b0;border-radius: .5em;margin-bottom: 1.5em;}/* 主元素的颜色和内边距 */.main{padding: 0 1.5em;background-color: #fff;border-radius: .5em;}.media{float: left;/* 给每个媒体元素加上右侧和底部的外边距 *//* 此处特地设置了margin-top为0 是为了清除猫头鹰选择器带来的上外边距 */margin: 0 1.5em 1.5em 0;/* 从宽度里面减去外边距,防止出现不必要的换行 */width:calc(50% - 1.5em);padding: 1.5em;background-color: #eee;border-radius: 0.5em;}/* 清除浮动 */.clearfix::before,
.clearfix::after{/* 防止伪元素的外边距折叠 */display: table;content:'';}.clearfix::after{/* display: block;
content: ''; */clear: both;}/* 清除每行的第奇数个元素上的浮动 */.media:nth-child(odd){clear: left;}/* 将图片浮动到左侧 */.media-image{float: left;/* 添加外边距,与正文中间出现间隔 */margin-right: 1.5em;}/* 删除猫头鹰选择器所添加的顶部外边距 */.media-body{/* 创建一个新的BFC,这样正文就不会跟浮动的图片重叠 */overflow: auto;margin-top: 0;}/* 清除浏览器默认添加的顶部外边距 */.media-body h4{margin-top: 0;}</style></head><body><divclass="container"><header><h1>Franklin Running Club</h1></header><mainclass="main clearfix"><h2>Running tips</h2><div><divclass="media"><imgsrc="runner.png"alt=""class="media-image"><divclass="media-body"><h4>Strength</h4><p>
Strength trainning is an important part of
injury prevention. Focus on your core—
especially your abs and glutes.
</p></div></div><divclass="media"><imgsrc="shoes.png"alt=""class="media-image"><divclass="media-body"><h4>Cadence</h4><p>
Check your stride turnover. The most efficient
runners take about 180 steps per minute.
</p></div></div><divclass="media"><imgsrc="shoes.png"alt=""class="media-image"><divclass="media-body"><h4>Change it up</h4><p>
Don't run the same every time you hit the
road. Vary your pace,and vary the distance
of your runs
</p></div></div><divclass="media"><imgsrc="runner.png"alt=""class="media-image"><divclass="media-body"><h4>Focus on form</h4><p>
Run tall but relaxed. Your feet should hit the ground
beneath your hips,not out in front of you.
</p></div></div></div></main></div></body></html>
使用网格系统提高代码复用性
<!DOCTYPEhtml><html><head><metacharset="utf-8"><title>浮动</title><styletype="text/css">:root{box-sizing: border-box;}*,
::before,
::after{box-sizing: inherit;}body{background-color: #eee;font-family: Arial, Helvetica, sans-serif;}body * + *{margin-top: 1.5em;}/* 清除浮动 */.clearfix::before,
.clearfix::after{/* 防止伪元素的外边距折叠 */display: table;content:'';}.clearfix::after{/* display: block;
content: ''; */clear: both;}.container{/* 小视口内层容器会填满屏幕,大视口会扩展到1080px */max-width: 1080px;margin: 0 auto;}/* 头部的颜色和内边距 */header{padding: 1em 1.5em;color: white;background-color: #0072b0;border-radius: .5em;margin-bottom: 1.5em;}/* 主元素的颜色和内边距 */.main{padding: 0 1.5em 1.5em;background-color: #fff;border-radius: .5em;}.media{padding: 1.5em;background-color: #eee;border-radius: 0.5em;}/* 将图片浮动到左侧 */.media-image{float: left;/* 添加外边距,与正文中间出现间隔 */margin-right: 1.5em;}/* 删除猫头鹰选择器所添加的顶部外边距 */.media-body{/* 创建一个新的BFC,这样正文就不会跟浮动的图片重叠 */overflow: auto;margin-top: 0;}/* 清除浏览器默认添加的顶部外边距 */.media-body h4{margin-top: 0;}/* 添加负外边距 让其向左延伸 跟网页标题对齐*/.row{margin-left: -0.75em;margin-right: -0.75em;}.row::after{content:'';display: block;clear: both;}[class*="column-"]{float: left;/* 给每个列元素的左右内边距各赋值0.75em */padding: 0 0.75em;/* 去掉列元素顶部外边距 */margin-top: 0;}.column-1{/* 1/12 */width: 8.3333%;}.column-2{width: 16.6667%;}.column-3{width: 25%;}.column-4{width: 33.3333%;}.column-5{width: 41.6667%;}.column-6{width: 50%;}.column-7{/* 7/12 */width: 58.3333%;}.column-8{width: 56.6667%;}.column-9{width: 75%;}.column-10{width: 83.3333%;}.column-11{width: 91.6667%;}.column-12{width: 100%;}</style></head><body><divclass="container"><header><h1>Franklin Running Club</h1></header><mainclass="main clearfix"><h2>Running tips</h2><div><divclass="row"><divclass="column-6"><divclass="media"><imgsrc="runner.png"alt=""class="media-image"><divclass="media-body"><h4>Strength</h4><p>
Strength trainning is an important part of
injury prevention. Focus on your core—
especially your abs and glutes.
</p></div></div></div><divclass="column-6"><divclass="media"><imgsrc="shoes.png"alt=""class="media-image"><divclass="media-body"><h4>Cadence</h4><p>
Check your stride turnover. The most efficient
runners take about 180 steps per minute.
</p></div></div></div></div><divclass="row"><divclass="column-6"><divclass="media"><imgsrc="shoes.png"alt=""class="media-image"><divclass="media-body"><h4>Change it up</h4><p>
Don't run the same every time you hit the
road. Vary your pace,and vary the distance
of your runs
</p></div></div></div><divclass="column-6"><divclass="media"><imgsrc="runner.png"alt=""class="media-image"><divclass="media-body"><h4>Focus on form</h4><p>
Run tall but relaxed. Your feet should hit the ground
beneath your hips,not out in front of you.
</p></div></div></div></div></div></main></div></body></html>