一、圣杯布局和双飞翼布局
- 双飞翼布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>双飞翼布局</title>
<style type="text/css">
.center {
width: 100%;
float: left;
}
.center-content {
margin: 0 220px;
height: 300px;
background-color: #ffa;
}
.left {
margin-left: -100%;
float: left;
width: 200px;
height: 300px;
background-color: #de6ada;
}
.right {
margin-left: -200px;
float: right;
width: 200px;
height: 300px;
background-color: #98a3df;
}
</style>
</head>
<body>
<div class="container">
<div class="center">
<div class="center-content"></div>
</div>
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
- 圣杯布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>圣杯布局</title>
<style type="text/css">
.container {
padding: 0 220px;
}
.center {
float: left;
width: 100%;
height: 300px;
background-color: #ffa;
}
.left {
position: relative;
left: -210px;
margin-left: -100%;
float: left;
width: 200px;
height: 300px;
background-color: #de6ada;
}
.right {
position: relative;
left: 210px;
margin-left: -200px;
float: right;
width: 200px;
height: 300px;
background-color: #98a3df;
}
</style>
</head>
<body>
<div class="container">
<div class="center"></div>
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
二、去除列表的右边框
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<style>
ul,li {
margin: 0;
padding: 0;
}
.container {
margin: 100px auto;
width: 320px;
border: 1px solid #000;
overflow: hidden;
}
.container-ul {
list-style-type: none;
/* margin-right: -10px; */
}
.container-li {
float: left;
margin-right: 10px;
margin-bottom: 10px;
width: 100px;
height: 100px;
background-color: aqua;
}
</style>
</head>
<body>
<div class="container">
<ul class="container-ul">
<li class="container-li"></li>
<li class="container-li"></li>
<li class="container-li"></li>
<li class="container-li"></li>
<li class="container-li"></li>
<li class="container-li"></li>
</ul>
</div>
</body>
</html>
这里3应该在红色框这这里,就不会造成空间浪费,做法是ul:margin-right: -10px
,就是把上面的注释代码回复。
结果:
三、去除列表最后一项下边框重合问题
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<style>
ul,li {
margin: 0;
padding: 0;
list-style-type: none;
}
.container {
margin: 100px 220px;
}
ul {
width: 300px;
border: 1px solid #000;
}
li {
height: 50px;
line-height: 50px;
text-align: center;
background-color: aliceblue;
border-bottom: 1px solid #000;
}
li:nth-last-child(1) {
margin-bottom: -1px;
}
</style>
</head>
<body>
<div class="container">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</div>
</body>
</html>
在列表最后一项加:margin-bottom: -1px
四、多列等高
利用margin-bottom为负值会减少css读取元素高度的特性,加上padding-bottom和overflow:hidden,就能做一个未知高度的多列等高布局
<!DOCTYPE html>
<html>
<head>
<style>
.container {
overflow: hidden;
}
.div1 {
margin-bottom: -200px;
padding-bottom: 200px;
float: left;
width: 30%;
height: 100px;
background-color: beige;
}
.div2 {
margin-bottom: -200px;
padding-bottom: 200px;
float: left;
width: 30%;
height: 50px;
background-color: aquamarine;
}
.div3 {
margin-bottom: -200px;
padding-bottom: 200px;
float: left;
width: 30%;
height: 200px;
background-color: blue;
}
</style>
</head>
<body>
<div class="container">
<div class="div1">heigth: 100px</div>
<div class="div2">height: 50px</div>
<div class="div3">height: 200px</div>
</div>
</body>
</html>