老师,感觉做的不太对,帮忙看下哪里需要修改或者优化一下,还有flex布局和数值布局一般是怎么搭配的

来源:3-17 编程练习

多盐少糖

2021-08-22 13:54:42

问题描述:

   是不是有了flex还是要搭配百分比或者固定数值来布局呢,单靠flex应该不能完成布局吧?

相关截图:

http://img.mukewang.com/climg/6121e6280968d67019120470.jpg

问题描述:

​    ​当设置了项目flex:auto自动放大缩小后,在放大或者缩小时,padding,margin,border是一起等比例放大消耗剩余空间吗?

相关代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
/* 在此处补充代码*/
*{
margin: 0;
padding: 0;
}
header{
display: flex;
align-items: center;
justify-content: space-around;
width: 100%;
background-color: black;
}
header .logo{
display: flex;
flex:1;
justify-content: center;
align-items: center;
}
header ul{
flex:1;
display: flex;
justify-content: space-around;
align-items: center;
width: 30%;
}
header ul li{
list-style: none;
color: white;
}
.login{
flex:1;
display: flex;
justify-content: center;
align-items: center;
width: 10%;
}
.login input{
padding: 2px;
outline: none;
border-style: none;
background-color: orange;
border-radius: 2px;
color: white;
}
.login input:first-child{
margin-right: 10px;
}
section{
width: 100%;
height: 300px;
display: flex;
flex-flow: row wrap;
justify-content: space-around;
align-content: space-around;
}
.section{
padding: 0 20px;
display: flex;
justify-content: space-between;
align-items: center;
width:40%;
background-color: paleturquoise;
height:120px;
border-radius: 10px;
}
.section .box{
height: 100%;
display: flex;
flex-direction: column;
justify-content: space-around;
}
.section .shopping{
display: flex;
color: red;
justify-content:flex-end;
}
.section .shopping .btn{
display: flex;
justify-content: center;
align-items: center;
flex-wrap: nowrap;
font-weight: bolder;
color: white;
background-color: orange;
padding: 4px;
border-radius:4px;
}
</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>《从H5网页开发到移动端响应式开发》</p>
<div>适用人群:有html、css、js、jQ基础的童鞋</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>费用:¥599</div>
</div>
<div class="shopping">
<div class="btn">加入购物车</div>
</div>
</div>
</section>
</body>
</html>


写回答

1回答

好帮手慕然然

2021-08-22

同学你好,代码整体实现效果是没问题的,以下几处位置的代码多余,可以优化一下,如图

http://img.mukewang.com/climg/6121f79b099a5c4211820719.jpg

http://img.mukewang.com/climg/6121f9080908570a10470800.jpg

问题解答:

1、设置flex布局的盒子,应该根据实际需要设置宽度和高度,既可以使用%,也可以使用固定值px。大多情况下需要给父容器设置宽度和高度,此时子项会按照父容器设置的排列规则在父容器所在空间内排列。

2、当设置了项目flex:auto后,padding,margin,border不会一起等比例放大消耗剩余空间,而是保持原数值不变,如图

http://img.mukewang.com/climg/6121fc8c098ba44a14960655.jpg

通常在移动端开发时,会在css开头设置以下代码

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

其中box-sizing: border-box属性将设置的边框和内边距的值包含在width内。

祝学习愉快!

0

0 学习 · 15276 问题

查看课程