1.先上效果图
主要是flex布局和传统定位对页面的布局
2.html页面代码
<!DOCTYPE html>
<html>
<head>
<title>商城系统购物车</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="css/shop-cart.css">
</head>
<body>
<!-- 商城购物车 -->
<div class="shopcart">
<h3>我的购物车</h3>
<header>
<span>
<input type="checkbox" id="all-selected" checked="choose" >
<label for="all-selected">全选</label>
</span>
<span>商品</span>
<span>单价</span>
<span>数量</span>
<span>小计</span>
<span>操作</span>
</header>
<!-- 购物车主体 -->
<main>
<div>
<span><input type="checkbox" name="chooose" checked></span>
<!-- 图文介绍 -->
<span class="pic-intro">
<a href=""><img src="img/shop5.jpg"></a>
<a href="">商品简介商品简介商品简介。。</a>
</span>
<span>500.00</span>
<span>
<label><input type="number" name="count" value="1" min="1">
</label>
</span>
<span><i>¥</i>500.00</span>
<span><a href="">删除</a></span>
</div>
<div>
<span><input type="checkbox" name="chooose" checked></span>
<!-- 图文介绍 -->
<span class="pic-intro">
<a href=""><img src="img/shop5.jpg"></a>
<a href="">商品简介商品简介商品简介。。</a>
</span>
<span>500.00</span>
<span>
<label><input type="number" name="count" value="1" min="1">
</label>
</span>
<span><i>¥</i>500.00</span>
<span><a href="">删除</a></span>
</div>
<div>
<span><input type="checkbox" name="chooose" checked></span>
<!-- 图文介绍 -->
<span class="pic-intro">
<a href=""><img src="img/shop5.jpg"></a>
<a href="">商品简介商品简介商品简介。。</a>
</span>
<span>500.00</span>
<span>
<label><input type="number" name="count" value="1" min="1">
</label>
</span>
<span><i>¥</i>500.00</span>
<span><a href="">删除</a></span>
</div>
</main>
<!-- 底部 -->
<footer>
<span>数量:<i>3</i></span>
<span>金额:<i>1500</i></span>
<button>去结算</button>
</footer>
</div>
</body>
</html>
3.css代码
@import url(reset.css);
.shopcart {
width: 1200px;
background-color: #fff;
margin: 30px auto;
padding: 15px 0;
border-radius: 8px;
box-sizing: border-box;
}
.shopcart:hover {
box-shadow: 0 0 8px #888888;
}
/*标题*/
.shopcart > h3 {
font-size: 20px;
text-align: center;
font-weight: normal;
margin-bottom: 20px;
}
/*购物车头部*/
.shopcart > header {
height: 40px;
border-bottom: 1px solid #cccccc;
/*flex*/
display: flex;
justify-content: space-around;
align-items: center;
}
/*购物车主体*/
.shocart > main {
margin: 20px 0;
}
/*购物车中的每一条记录, 每一个商品*/
.shopcart > main > div {
height: 80px;
/*flex*/
display: flex;
justify-content: space-around;
align-items: center;
}
.shopcart > main > div:hover {
background-color: lightcyan;
}
.shopcart > main > div .pic-intro {
width: 250px;
display: flex;
margin-left: 50px;
align-items: center;
}
.shopcart > main > div .pic-intro img {
width: 60px;
height: 45px;
margin-right: 10px;
}
.shopcart > main > div a:hover {
color: lightcoral;
}
/*微调第一个复选框*/
.shopcart > main > div > span:first-of-type {
position: relative;
left: 25px;
}
.shopcart > main > div > span.pic-intro {
position: relative;
left: -40px;
}
.shopcart > main > div > span:nth-of-type(3) {
position: relative;
left: -110px;
}
.shopcart > main > div > span:nth-of-type(4) {
position: relative;
left: -80px;
}
.shopcart > main > div > span:nth-of-type(4) input {
width: 60px;
}
.shopcart > main > div > span:nth-last-of-type(2) {
position: relative;
left: -70px;
}
.shopcart > main > div > span:last-of-type {
position: relative;
left: -30px;
}
.shopcart > main > div > span:nth-last-of-type(2) i {
color: green;
font-size: 1.01rem;
margin-right: 3px;
}
/*购物车底部*/
.shopcart > footer {
height: 70px;
padding: 20px;
border-top: 1px solid #cccccc;
box-sizing: border-box;
display: flex;
justify-content: flex-end;
align-items: center;
}
.shopcart > footer > *:not(:last-child) {
width: 180px;
}
.shopcart > footer > button {
height: 40px;
width: 120px;
background-color: lightcoral;
border: none;
color: white;
font-size: 1.1rem;
}
.shopcart > footer > button:hover {
background-color: seagreen;
cursor: pointer;
}
4.小结
.一开始,我觉得直接用grid布局就可以了,后来发现用grid布局就是用错了,因为用户还要有可能还会增加或者减少商品,用grid布局是会产生局限性,我觉得grid布局适用于那些结构比较固定的页面,那些会随这用户操作变化的页面主要用flex布局比较好。