css3的@media属性实现页面响应式布局示例代码
程序员文章站
2023-11-01 16:55:10
使用css3的@media属性可以实现页面响应式布局,下面有个不错的示例,大家可以参考下... 14-02-10...
以最简单的header为例 直接上代码
<!doctype html>
<head>
<script src="jquery-1.8.2.min.js"></script>
<style>
* {
margin:0;
padding:0;
}
.ul {
background-color:rgb(134, 170, 209);
height: 55px;
}
.ul li {
float:left;
list-style: none;
background-color:rgb(134, 170, 209);
width: 20%;
height: 100%;
}
.item {
display: block;
text-align:center;
line-height: 49px;
height: 100%;
transition: all 0.5s;
-moz-transition: all 0.5s; /* firefox 4 */
-webkit-transition: all 0.5s; /* safari 和 chrome */
-o-transition: all 0.5s;
cursor:pointer;
}
.item:hover {
background-color:rgb(168, 209, 253);
}
@media only screen and (max-width: 400px) {
.ul li {
width: 100%;
height: 100%;
}
}
</style>
<script>
</script>
</head>
<body class="sapuibody">
<header>
<ul class="ul">
<li><a class="item">home</a></li>
<li><a class="item">first</a></li>
<li><a class="item">second</a></li>
<li><a class="item">thirdly</a></li>
<li><a class="item">fourth</a></li>
</ul>
</header>
</body>
注意的是@media属性必须写在下面,从而覆盖上面的css
复制代码
代码如下:<!doctype html>
<head>
<script src="jquery-1.8.2.min.js"></script>
<style>
* {
margin:0;
padding:0;
}
.ul {
background-color:rgb(134, 170, 209);
height: 55px;
}
.ul li {
float:left;
list-style: none;
background-color:rgb(134, 170, 209);
width: 20%;
height: 100%;
}
.item {
display: block;
text-align:center;
line-height: 49px;
height: 100%;
transition: all 0.5s;
-moz-transition: all 0.5s; /* firefox 4 */
-webkit-transition: all 0.5s; /* safari 和 chrome */
-o-transition: all 0.5s;
cursor:pointer;
}
.item:hover {
background-color:rgb(168, 209, 253);
}
@media only screen and (max-width: 400px) {
.ul li {
width: 100%;
height: 100%;
}
}
</style>
<script>
</script>
</head>
<body class="sapuibody">
<header>
<ul class="ul">
<li><a class="item">home</a></li>
<li><a class="item">first</a></li>
<li><a class="item">second</a></li>
<li><a class="item">thirdly</a></li>
<li><a class="item">fourth</a></li>
</ul>
</header>
</body>
注意的是@media属性必须写在下面,从而覆盖上面的css
下一篇: 养生饮食禁忌 春季吃八种食物易中毒