老师可不可以帮我检查一下代码如何优化

来源:3-17 编程练习

PADAopanda

2021-08-23 13:07:38

问题描述:

老师,加入购物车这块div怎么对齐呢?


相关截图:

http://img.mukewang.com/climg/61232d11093fed0f26840960.jpg


相关代码:

<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title></title>
<style>
*{
margin: 0;
padding: 0;
}

/* 头部header */
header {
width: 100%;
height: 80px;
margin: 0 auto;
background-color: #000;
display: flex;
justify-content: space-around;
align-items: center;
}
.nav {
width: 50%;
height: 100%;
color: white;
list-style: none;
display: flex;
justify-content: space-around;
/* justify-content: space-between; */
}
.nav li{
width: 25%;
height: 100%;
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
}
.nav li:hover{
color: black;
background-color: #ccc;
}
.login input{
padding: 5px 15px; /*扩大内边距*/
margin: 10px; /*扩大外边距*/
color: white;
background-color: rgba(255, 119, 0, 0.911);
border: none; /*去除边框*/
border-radius: 10px; /*边框圆角*/
cursor: pointer;
}

/* 主体内容section */
section{
width: 100%;
height: 500px;
display: flex;
flex-wrap: wrap;
display: flex;
justify-content: space-around;
align-content: space-around;
}
.section{
width: 40%;
height: 150px;
padding: 20px;
background-color: rgba(168, 214, 235, 0.911);
border-radius: 20px;
display: flex;
justify-content: space-around;
}
.section .box{
/* 将主轴改为水平方向 */
flex-direction: column;

display: flex;
justify-content: space-around;
}
.section .shopping{
padding-left: 20px;
flex-shrink: 0;

display: flex;
justify-content: center;
align-items: center;

flex-basis: 150px;
}
.section .shopping .btn{
padding: 5px 15px; /*扩大内边距*/
margin: 10px; /*扩大外边距*/
color: white;
background-color: rgba(255, 119, 0, 0.911);
border: none; /*去除边框*/
border-radius: 10px; /*边框圆角*/
cursor: pointer;
}
</style>
</head>

<body>
<!-- 头部 -->
<header>
<div class="logo">
<img height="80px" src="http://climg.mukewang.com/59197ab000014f1503000100.jpg" alt="">
</div>
<ul class="nav">
<li>课程</li>
<li>路径</li>
<li>猿问</li>
<li>手记</li>
</ul>
<div class="login">
<input type="button" value="登录">
<input type="button" value="注册">
</div>
</header>
<!-- 主体内容 -->
<section>
<div class="section">
<div class="box">
<p>《前端小白入门手册》</p>
<div>适用人群:没有任何前端基础的小白</div>
<div>费用:¥499</div>
</div>
<div class="shopping">
<div class="btn">加入购物车</div>
</div>
</div>
<div class="section">
<div class="box">
<p>《HTML5月CSS3实现动态网页》</p>
<div>适用人群:有html和css基础的童鞋</div>
<div>费用:¥599</div>
</div>
<div class="shopping">
<div class="btn">加入购物车</div>
</div>
</div>
<div class="section">
<div class="box">
<p>《从H5网页开发到移动端响应式开发》</p>
<div>适用人群:有html、css、js、jQ基础的童鞋</div>
<div>费用:¥699</div>
</div>
<div class="shopping">
<div class="btn">加入购物车</div>
</div>
</div>
<div class="section">
<div class="box">
<p>《Node.js全栈开发》</p>
<div>适用人群:有html、css、js、jQ、vue基础的童鞋</div>
<div>费用:¥799</div>
</div>
<div class="shopping">
<div class="btn">加入购物车</div>
</div>
</div>
</section>
</body>

</html>


写回答

1回答

好帮手慕慕子

2021-08-23

同学你好,可以给box盒子添加flex:1;属性,让其占据剩余的空间,这样就可以实现按钮左侧对齐效果了,如下:

http://img.mukewang.com/climg/612333810905133105220214.jpg

另外,效果整体实现的不错,建议优化:去掉给shopping设置的样式,给section添加align-item:center;属性,让内容垂直居中显示,然后给box添加百分百高度适应父元素高度,简化书写,示例:

http://img.mukewang.com/climg/6123342f096707e906280678.jpg

包裹btn的外层div也可以去掉了,简化代码

http://img.mukewang.com/climg/6123346b09dbfb6c07060708.jpg

记得去掉选择btn元素选择器中的shopping

http://img.mukewang.com/climg/6123349309d05b7306610388.jpg

祝学习愉快~

0

0 学习 · 15276 问题

查看课程