盒模型练习
css盒模型练习
继续熟练练习css,并且更清楚认识到css的好处
完成以下布局:
要求:熟练使用border(边框),padding(内边距),margin(外边距)
分析:上图中第一张图需要单独写样式,后面四张图仅需要一个样式即可,并且个块都有上边框(border),外间距间距一样
代码实现:
html部分:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>小米0.1</title>
<link rel="stylesheet" type="text/css" href="xiaomi0.1.css">
</head>
<body>
<div class="box">
<div class="boxjs">
<span class="span1">14:00场</span>
<img src="./jishi1.png" alt="???" class="img1">
<div class="boxjs-p">距离结束还有</div>
<div class="jishia">
<div class="jishi1">4</div><div class="jishi3">:</div>
<div class="jishi2">21</div><div class="jishi3">:</div>
<div class="jishi2">00</div>
</div>
</div>
<div class="box-a">
<img src="./01.png" alt="???" class="img2">
<div class="box-a1">小米多功能运动休闲胸包 黑...</div>
<div class="box-a2">小而精致,精简出行</div>
<div class="box-a3"><span>59元</span><s>69元</s></div>
</div>
<div class="box-a box-b">
<img src="./02.png" alt="???" class="img2">
<div class="box-a1">最生活浴巾·Air(4条装)白...</div>
<div class="box-a2">享用一条洁净好浴巾</div>
<div class="box-a3"><span>169元</span><s>269元</s></div>
</div>
<div class="box-a box-c">
<img src="./03.png" alt="???" class="img2">
<div class="box-a1">米家扫地机器人1S白色</div>
<div class="box-a2">看的明白扫得彻底</div>
<div class="box-a3"><span>1699元</span><s>1999元</s></div>
</div>
<div class="box-a box-b">
<img src="./04.png" alt="???" class="img2">
<div class="box-a1">小米手环4 NFC版 黑色</div>
<div class="box-a2">大屏显彩,可刷公交,门禁</div>
<div class="box-a3"><span>179元</span><s>299元</s></div>
</div>
</div>
</body>
</html>
css部分:
.box{
width:1564px;
height:448px;
margin:0 auto;
background-color:#f5f5f5;
padding-top:11px;
}
.boxjs{
width:293px;
height:351px;
background-color:#f1eded;
border-top:solid 1px #e53935;
margin:0 0 0 11px;
padding-top:73px;
float:left;
}
.span1{
text-align:center;
font-size:25px;
color:#ef3a3b;
margin-left:99px;
float:left;
}
.img1{
width:41px;
height:63px;
float:left;
margin:40px 0 0 123px;
padding-bottom:40px;
}
.boxjs-p{
font-size:20px;
color:#6f6d6d;
float:left;
height:22px;
width:293px;
text-align:center;
padding-bottom:40px
}
.jishia{
color:#605751;
font-size:22px;
text-align:center;
float:left;
}
.jishi1{
width:57px;
height:57px;
color:#fff;
line-height:57px;
margin:0 0 0 42px;
float:left;
background-color:#605751;
}
.jishi2{
width:57px;
height:57px;
color:#fff;
line-height:57px;
float:left;
background-color:#605751;
}
.jishi3{
width:19px;
height:57px;
float:left;
line-height:57px;
}
.box-a{
width:293px;
height:424px;
background-color:#fafafa;
border-top:solid 1px #ffac13;
margin-left:17px;
float:left;
}
.img2{
width:293px;
height:280px;
float:left;
}
.box-a1{
color:#3b3b3b;
font-size:18px;
text-align:center;
line-height:29px;
}
.box-a2{
color:#b2b2b2;
font-size:13px;
text-align:center;
line-height:29px;
}
.box-a3{
color:#fa6509;
font-size:16px;
text-align:center;
line-height:29px;
}
.box-a3 s{
color:#adadad;
margin-left:5px;
}
.box-b{
border-top:solid 1px #83c44e;
}
.box-c{
border-top:solid 1px #2196f3;
}
.box-d{
border-top:solid 1px #e53935;
}
实现效果:
总结:这里用到了中划线<s></s>
,为了实现效果就把一图中的计时器改成了背景颜色为黑色,字体颜色为白色
上图中的盒子均使用了padding-top: 因为之前为了把文字顶下去都是使用的本方法(在上面加一个有高度的盒子)
但是注意:这里面如果加入padding-top盒子会被撑大,需要把盒子原本height的高度减去padding-top的值才可以;边框同理,边框厚度不算在盒子的宽(width),高(height)里若是在练习里追求精准尺寸 需要减去border的厚度。
也可以直接加入 **box-sizing:border-box;**原理是通过减去width+height的px值来使整个盒子保持一致