关于flex布局的一些疑惑
来源:3-17 编程练习
慕仰2255090
2019-10-10 11:26:10
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
/* 在此处补充代码*/
* {
margin: 0;
padding: 0;
list-style: none;
}
header{display:flex; background:#000; height:100px; width:100%;justify-content:space-around;align-items:center;flex-direction:row;}
header img{ flex-grow:1; height:100px;}
header .nav{ flex-grow:2; color:#fff; display:flex; flex-direction:row; justify-content:space-between; width:300px;}
.nav li {
display: inline-block;
font-size: 20px;
color: white;
}
.login {
width: 100px;
display: flex;
justify-content: space-between;
}
.login input {
/*display: inline-block;*/
border: none;
border-radius: 4px;
background: orange;
width: 40px;
height: 24px;
font-size: 15px;
color: white;
}
section{display:flex;
width:100%;
flex-wrap:wrap;
justify-content:space-around;
align-items:center; }
.section {
margin-top: 20px;
width: 45%;
height: 150px;
background: lightblue;
border-radius: 10px;
font-size: 20px;
display: flex;
align-items: center;
justify-content: space-around;
}
.section .box{
display: flex;
flex-direction: column;
justify-content: space-between;
}
.shopping{
display: flex;
justify-content: center;
align-items: center;
}
.btn {
width: 70px;
height: 30px;
background: orange;
color: white;
text-align: center;
line-height: 30px;
border-radius: 8px;
font-size:10px
}
.btn:hover,input:hover{
color: red;
}
</style>
</head>
<body>
<!-- 头部 -->
<header>
<div class="logo">
<img 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>《前端小白入门手册》</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>
</section>
</body>
</html>
1。请老师检查一下这个练习是否需要改进的
2.在做这个练习的时候,我总觉得不应该给项目添加固定的宽度,觉得既然他都可以自己平分剩余空间,为什么要去给固定宽度呢?
3.我们实际工作中,开发手机端的网页是不是基本都靠flex来布局?
1回答
同学你好,
1、优化建议:
顶部中间导航项有些分散:

建议将扩展比率去掉,将宽度设置大一些,如下:

2、同学说的项目添加固定宽度,指的是section下每个div容器设置宽度为45%吗?项目会自动均分剩余空间,但是不会自动换行,如果不设置宽度,4个项目就会在一行显示:

所以需要通过设置宽度,再加上父容器中设置flex-wrap: wrap;,这样项目就实现了换行。
3、是的哦,移动端flex布局是很常用的,因为移动端大部分是布局相同的块,很方便。
祝学习愉快!
相似问题