注意:
- 标签都是有语义的,合理的地方用合理的标签。比如产品标题就用h,大量文字段落就用p
- margin和padding大部分情况下可以进行混用,两者各有优缺点,最好根据实际情况,采取简单的方法实现,比如说,在没有设置宽度属性时,指定左右内边距不会撑开盒子大小,但是不管有没有设置高度属性,指定上下内边距总会撑开盒子大小,如果我们此时要避免撑开盒子的大小,我们可以采取margin-top
- 布局的时候,最好逐层的去实现,不要想到什么写什么,这样会造成写的样式结构很混乱,逐层实现写的时候思路会更加清晰!
我们来实现一个小米官网的简单产品模块,这是实现效果:
具体的代码:
里面写好了注释,不懂的地方可以自己看看
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 清除内外边距 */
* {
padding: 0;
margin: 0;
}
body {
background-color: #f5f5f5;
}
.box {
background-color: #ffffff;
/* 盒子的宽度和高度是根据设计图量出来的 */
width: 300px;
height: 420px;
/* 让块级元素盒子水平居中 */
margin: 50px auto;
}
.box img {
/* 让img的宽度大小是盒子宽度大小的100%这样不会溢出盒子 */
width: 100%
}
.box>.review {
height: 70px;
font: 14px/1.5 "Microsoft Yahei";
/* 给文字设置一段左右内边距 */
padding: 0 20px;
/* 让文字段落离图片上外边距30px */
margin-top: 30px;
}
.box>.appraise {
color: #b0b0b0;
font-size: 12px;
padding: 0 20px;
margin-top: 20px;
}
/* 给父级的info指定文字大小,下面的子元素会继承文字大小 14px 避免在子元素中重复写 */
.box>.info {
font-size: 14px;
margin-top: 20px;
padding: 0 20px;
}
.info h4 {
/* 将h4转换为行内块元素 这里是产品标题因此用的是h标签 */
display: inline-block;
/* 设置为不加粗 */
font-weight: 400;
font-size: 14px;
}
.info a {
text-decoration: none;
color: #333;
}
.info em {
/* 取消文字样式 */
font-style: normal;
color: #ebe4e0;
/* 顺时针方向 上 右 下 左 */
margin: 0 6px 0 15px;
}
.info span {
color: #ff6700;
}
</style>
</head>
<body>
<div class="box">
<img src="images/img.jpg" alt="">
<p class="review">真好用,戴上这款耳机我甚至可以天天摸鱼,老师再也管不住我了</p>
<div class="appraise">来自摸鱼人的评价</div>
<div class="info">
<h4><a href="#">Redmi AirDots真无线蓝...</a></h4>
<em>|</em>
<span>99.9元</span>
</div>
</div>
</body>
</html>
图片我贴在下方,有兴趣的同学可以自己拿去实现一下