四.常见的布局技巧
1.1 margin负值运用
商品的显示框
1.解决盒子边框重合 margin:-1.....;
2.鼠标经过某个盒子的时候,提高当前盒子的层级即可(如果没有有定位,则加相对定位(保留位置),如果有定位,则加z-index)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
ul,
li {
list-style: none;
}
a {
text-decoration: none;
}
.w {
width: 1178px;
margin: 0 auto;
}
.wrapper {
margin-top: 72px;
}
.wrapper ul {
float: left;
}
.wrapper ul li {
position: relative;
float: left;
height: 376px;
width: 215px;
padding: 75px 39px 18px 39px;
margin: 0 -1px -1px 0;
border: 1px solid #e0e0e0;
text-align: center;
}
.wrapper ul li:hover {
z-index: 1;
border: 1px solid orangered;
}
.wrapper ul li>img {
width: 100%;
height: 270px;
}
.wrapper ul li a {
display: block;
margin-top: 18px;
color: #828282;
font-size: 16px;
}
.wrapper ul li h4 {
margin-top: 46px;
color: #349167;
}
</style>
</head>
<body>
<div class="wrapper w">
<ul>
<li>
<img src="images/oppo1.png" alt="">
<a href="#">
<p> 玫瑰金 前后1000万像素</p>
</a>
<h4>¥2900.00</h4>
</li>
<li>
<img src="images/oppo1.png" alt="">
<a href="#">
<p> 玫瑰金 前后1000万像素</p>
</a>
<h4>¥2900.00</h4>
</li>
<li>
<img src="images/oppo1.png" alt="">
<a href="#">
<p> 玫瑰金 前后1000万像素</p>
</a>
<h4>¥2900.00</h4>
</li>
<li>
<img src="images/oppo1.png" alt="">
<a href="#">
<p> 玫瑰金 前后1000万像素</p>
</a>
<h4>¥2900.00</h4>
</li>
<li>
<img src="images/oppo1.png" alt="">
<a href="#">
<p> 玫瑰金 前后1000万像素</p>
</a>
<h4>¥2900.00</h4>
</li>
<li>
<img src="images/oppo1.png" alt="">
<a href="#">
<p> 玫瑰金 前后1000万像素</p>
</a>
<h4>¥2900.00</h4>
</li>
<li>
<img src="images/oppo1.png" alt="">
<a href="#">
<p> 玫瑰金 前后1000万像素</p>
</a>
<h4>¥2900.00</h4>
</li>
<li>
<img src="images/oppo1.png" alt="">
<a href="#">
<p> 玫瑰金 前后1000万像素</p>
</a>
<h4>¥2900.00</h4>
</li>
</ul>
</div>
</body>
</html>
1.2 文字围绕浮动元素运用
布局示意图
巧妙运用浮动元素不会压住文字的特性
1.3 行内块巧妙运用
页码在页面中间显示:
- 把这些链接盒子转换为行内块, 之后给父级指定 text-align:center;
- 利用行内块元素中间有缝隙,并且给父级添加 text-align:center; 行内块元素会水平会居中
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: #5a5959;
}
.box {
text-align: center;
}
.box a {
display: inline-block;
width: 36px;
height: 36px;
text-align: center;
line-height: 36px;
font-size: 14px;
background-color: #cccccc;
}
.box .prev,
.box .next {
width: 85px;
}
.box .clicked,
.box .ellip {
background-color: #fff;
}
.box input {
width: 45px;
height: 36px;
outline: none;
border: 1px solid #cccccc;
}
button {
width: 55px;
height: 34px;
border: none;
outline: none;
cursor: pointer;
background-color: #cccccc;
}
</style>
</head>
<body>
<div class="box">
<a href="#" class="prev"><<上一页</a>
<a href="#">1</a>
<a href="#" class="clicked">2</a>
<a href="#">3</a>
<a href="#">4</a>
<a href="#">5</a>
<a href="#">6</a>
<a href="#" class="ellip">...</a>
<a href="#" class="next">>>下一页</a> 跳转到第
<input type="text" name="" id="">页
<button>确定</button>
</div>
</body>
</html>
1.4 三角强化
案例:
代码:京东价格标签栏
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 185px;
height: 25px;
margin: 100px auto;
line-height: 25px;
border: 1px solid orangered;
font-size: 14px;
font-weight: 700;
color: #cccccc;
}
.box .decreace {
position: relative;
float: left;
width: 100px;
height: 100%;
margin-right: 10px;
text-align: center;
background-color: orangered;
}
.decreace i {
position: absolute;
top: 0;
right: 0;
width: 0;
height: 0;
border-width: 25px 10px 0 0;
border-style: solid;
border-color: transparent white transparent transparent;
}
.box .price {
text-decoration: line-through;
}
</style>
</head>
<body>
<div class="box">
<span class="decreace">¥1256
<i></i>
</span>
<span class="price">¥3489</span>
</div>
</body>
</html>