1、创建一个响应式网格视图,首先确保所有的 HTML 元素都有 box-sizing 属性且设置为 border-box。并确保边距和边框包含在元素的宽度和高度间。其代码为
* {
box-sizing: border-box;
}
<!DOCTYPE html>
<html>
<title>box-sizing</title>
<meta charset="utf-8">
<head>
<style type="text/css">
* {
box-sizing: border-box;
}
.header{
border: 1px solid #5D086A;
padding: 15px;
}
.left{
width: 30%;
float: left;
padding: 15px;
border: 1px solid #5D086A;
}
.right{
width: 70%;
float: left;
padding: 15px;
border: 1px solid #5D086A;
}
</style>
</head>
<body>
<div class="header">
<h1>我爱你中国!</h1>
</div>
<div class="left">
<p>五十六个民族,五十六支花。</p>
</div>
<div class="right">
<p>五十六个民族,五十六支花。</p>
<ul>
<li>汉族</li>
<li>苗族</li>
<li>壮族</li>
<li>维吾尔族</li>
</ul>
</div>
</body>
</html>
效果图
2、12 列的网格系统可以更好的控制响应式网页。首先我们可以计算每列的百分比: 100% / 12 列 = 8.33%。在每列中指定 class, class="col-" 用于定义每列有几个 span :
css代码
.col-1 {width: 8.33%;}
.col-2 {width: 16.66%;}
.col-3 {width: 25%;}
.col-4 {width: 33.33%;}
.col-5 {width: 41.66%;}
.col-6 {width: 50%;}
.col-7 {width: 58.33%;}
.col-8 {width: 66.66%;}
.col-9 {width: 75%;}
.col-10 {width: 83.33%;}
.col-11 {width: 91.66%;}
.col-12 {width: 100%;}
所有的列向左浮动,间距(padding) 为 20px:
[class*="col-"] {
float: left;
padding: 20px;
border: 1px solid #5D086A;
}
每一行使用 <div> 包裹。所有列数加起来应为 12:
<div class="conter">
<div class="col-3">
<ul>
<li>汉族</li>
<li>苗族</li>
<li>壮族</li>
<li>维吾尔族</li>
</ul>
</div>
<div class="col-9">
<p>五十六个民族,五十六支花。</p>
<p>五十六个民族,五十六支花。</p>
<p>五十六个民族,五十六支花。</p>
<p>五十六个民族,五十六支花。</p>
</div>
</div>
列中行为左浮动,并添加清除浮动:
.conter:after {
content: "";
clear: both;
display: block;
}
css与html代码
<!DOCTYPE html>
<html>
<title>CSS响应式 Web 设计(二) - 网格视图</title>
<meta charset="utf-8">
<head>
<style type="text/css">
* {
box-sizing: border-box;
}
.header{
border: 1px ;
padding: 20px;
background: #9933cc;
}
.conter:after {
content: "";
clear: both;
display: block;
}
[class*="col-"] {
float: left;
padding: 20px;
border: 1px ;
}
.col-1 {width: 8.33%;}
.col-2 {width: 16.66%;}
.col-3 {width: 25%;}
.col-4 {width: 33.33%;}
.col-5 {width: 41.66%;}
.col-6 {width: 50%;}
.col-7 {width: 58.33%;}
.col-8 {width: 66.66%;}
.col-9 {width: 75%;}
.col-10 {width: 83.33%;}
.col-11 {width: 91.66%;}
.col-12 {width: 100%;}
.col-3 ul {
list-style-type: none;
margin: 0;
padding: 0;
}
.col-3 li {
padding: 10px;
margin-bottom: 8px;
background-color :#33b5e5;
color: #ffffff;
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}
.col-9{
padding: 10px;
margin-bottom: 8px;
}
.col-3 li:hover {
background-color: #0099cc;
}
body{
font-family: '楷体';
}
p{
font-size: 24px;
}
</style>
</head>
<body>
<div class="header">
<h1>我爱你中国!</h1>
</div>
<div class="conter">
<div class="col-3">
<ul>
<li>汉族</li>
<li>苗族</li>
<li>壮族</li>
<li>维吾尔族</li>
</ul>
</div>
<div class="col-9">
<h2>我爱你,中国!</h2>
<p>五十六个民族,五十六支花。五十六族兄弟姐妹是一家。五十六种语言,汇成一句话,爱我中华爱我中华爱我中华!
五十六个民族,五十六支花。五十六族兄弟姐妹是一家。五十六种语言,汇成一句话,爱我中华爱我中华爱我中华!
</p>
</div>
</div>
</body>
</html>
效果图