display:flex 弹性布局
用法:
在父级元素上设置 display:flex
- 设置排列方向:
flex-decrition:columns(垂直)/row(默认:水平)
- 设置换行:
flex-warp:warp/no-warp(默认:不换行)
- 设置对齐方式
- 水平:
justify-content/justify-items
- 垂直:
align-content(多行)/align-items(单行)
- 水平:
在子元素上设置属性:
- align-self:自身交叉轴上的对齐方式(如果父级上也设置交叉轴上的对齐方式,就近原则)
- justify-self:自身主轴上的对齐方式(但是在弹性盒子上不作用,在display:grid上有效果)
- flex-grow:等分主轴上的间隙
- flex-shrink:等分多余溢出部分按比重等分缩小
- flex-basis:效果等同于width ,权重大于width
- flex :flex-grow flex-shrink flex-basis (简写)
对齐方式常用属性值
- flex-start 从开始的地方水平/垂直对齐 ,默认是左
- flex-end 从结束的地方水平/垂直对齐 ,默认是右
- center 居中
- space-between 水平/垂直方向首尾紧贴父元素,中间间隙等分
- space-around 水平/垂直方向的子元素之间的左右/上下间距相等,相邻元素之间的间距为两倍间距
实例神马搜索布局 —主要是适应移动端
body {
background-color: #F1F1F3;
}
body,
div,
ul,
li,
span,
a,
i,p{
padding: 0;
margin: 0;
}
.main {
background: #eee;
overflow: hidden;
min-width: 320px;
min-height: 600px;
}
.top {
width: 100%;
height: 80px;
/*父级上设置display:flex*/
display: flex;
flex-direction: row;
justify-content: flex-end;
align-items: center;
}
.top a {
margin-right: 20px;
margin-top: 20px;
width: 40px;
height: 40px;
background-image: url(../img/navs4.png);
background-size: 50px;
background-repeat-x: no-repeat;
background-position: 0px 34px;
}
.logo {
margin-top: 20px;
width: 100%;
height: 90px;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
.logo a {
width: 150px;
height: 82px;
background-repeat: no-repeat;
background-image: url(../img/07.png);
}
.search {
width: 100%;
height: 60px;
display: flex;
justify-content: flex-start;
align-items: center;
background-color: #fff;
border-radius: 5px;
}
.search_logo {
width: 60px;
height: 60px;
background-size: 23px;
background-repeat: no-repeat;
background-image: url(../img/06.png);
background-position: 20px 18px;
padding-left: 5px;
}
.search textarea {
flex: 1 1 auto;
height: 56px;
color: #222;
font-size: 25px;
line-height: 54px;
border: none;
outline: none;
resize: none;
padding-bottom: 0;
}
.search textarea::-webkit-input-placeholder {
color: #ccc;
font-size: 25px;
}
.microphone_img {
width: 60px;
height: 60px;
background-size: 20px;
background-repeat: no-repeat;
background-image: url(../img/04.png);
background-position: 12px 15px;
}
.scan_img {
width: 60px;
height: 60px;
background-size: 23px;
background-repeat: no-repeat;
background-image: url(../img/03.png);
background-position: 12px 18px;
}
nav {
width: 100%;
height: 100px;
display: flex;
justify-content: space-around;
align-items: center;
margin-top: 20px;
}
a {
text-decoration: none;
}
nav li {
list-style: none;
}
nav li a {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
color: #666666;
width: 100px;
height: 100px;
}
nav li a span {
width: 40px;
height: 40px;
border-radius: 40%;
background-image: url(../img/navs4.png);
background-repeat-x: no-repeat;
background-size: 46px;
}
nav li:nth-child(1) a span {
background-position: -3px -96px;
background-color: #FFA97F;
}
nav li:nth-child(2) a span {
background-position: -3px -3px;
background-color: #60D1D1;
}
nav li:nth-child(3) a span {
background-position: -3px -326px;
background-color: #56CFDD;
}
nav li:nth-child(4) a span {
background-position: -3px -279px;
background-color: #DCAEF3;
}
nav li:nth-child(5) a span {
background-position: -3px -188px;
background-color: #7FCEFF;
}
nav li:nth-child(6) a span {
background-position: -3px 321px;
background-color: #A4B7C3;
}
.ads {
margin-top: 40px;
width: 100%;
height: 70px;
background-color: #FFD153;
display: flex;
justify-content: center;
align-items: center;
}
.ads img {
width: 400px;
height: 70px;
}
.hotspot {
width: 100%;
height: 50px;
display: flex;
justify-content: space-between;
align-items: center;
}
.hotspot span:nth-child(1) {
flex: 1 1 auto;
padding-left: 30px;
color: #CCCCCC;
font-size: 20px;
}
.hotspot a {
width: 50px;
height: 44px;
display: flex;
justify-content: center;
align-items: center;
color: #CCCCCC;
font-size: 30px;
padding-right: 5px;
}
section {
margin-top: 10px;
box-shadow: 0 0 2px #ccc;
background-color: #fff;
}
section ul li {
height: 40px;
display: flex;
justify-content: center;
align-items: center;
list-style: none;
padding-top: 10px;
padding-bottom: 10px;
}
section ul {
border-bottom: 1px solid #ccc;
}
.sign_num {
height: 40px;
width: 40px;
font-weight: bold;
color: #999;
font-size: 20px;
padding-left: 35px;
display: flex;
align-items: center;
}
.text {
flex: 1 1 auto;
height: 40px;
display: flex;
align-items: center;
}
.hot_new::after {
content: "新";
width: 18px;
height: 18px;
background: #ff6f3d;
color: #fff;
font-size: 12px;
line-height: 18px;
text-align: center;
display: block;
}
.num {
height: 40px;
color: #aaa;
font-size: 15px;
padding-right: 20px;
align-self: center;
display: flex;
align-items: center;
}
.num1 {
color: #FF3200;
}
.num2 {
color: #FF7D52;
}
.num3 {
color: #2D83FF;
}
.search_more {
width: 100%;
height: 75px;
display: flex;
justify-content: center;
align-items: center;
}
.search_more a {
font-size: 22px;
color: #0033E6;
display: flex;
justify-content: center;
align-items: center;
}
.search_more a i {
width: 20px;
height: 20px;
background-image: url(../img/02.png);
background-repeat: no-repeat;
background-size: 11px;
margin-left: 10px;
}
.content {
margin-top: 10px;
background-color: #FFFFFF;
width: 100%;
}
.content_ul{
border-bottom: 1px solid #ccc;
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px;
}
.content_ul li{
height: 80px;
list-style: none;
display: flex;
align-items: center;
justify-content: center;
}
.content_ul li a{
width: 60px;
height: 40px;
font-size: 12px;
padding-top: 40px;
display: flex;
align-items: center;
justify-content: center;
color: #666;
background-image: url(../img/014.png);
background-repeat-y: no-repeat;
background-size: 321px;
}
.content_ul li:nth-child(1) a{
background-position: 0px 0px;
}
.content_ul li:nth-child(2) a{
background-position: -57px 0px;
}
.content_ul li:nth-child(3) a{
background-position: -115px 0px;
}
.content_ul li:nth-child(4) a{
background-position: -172px 0px;
}
.content_ul li:nth-child(5) a{
background-position: -232px 0px;
}
.foot{
padding: 10px;
}
.commodity1{
width: 100%;
height: 90px;
display: flex;
margin-top: 12px;
}
.img{
width: 90px;
height: 90px;
min-width: 90px;
background-repeat: no-repeat;
background-size: 90px;
margin-right: 15px;
}
.img1{
background-image: url(../img/015.jpg);
}
.img2{
background-image: url(../img/016.jpg);
}
.img3{
background-image: url(../img/017.jpg);
}
.aside{
flex: 1 1 auto;
height: 90px;
display: flex;
flex-direction: column;
}
.p_context{
display: flex;
flex: 1 1 auto;
min-height:22px;
max-height: 44px;
font-size: 16px;
overflow: hidden;
text-overflow: ellipsis;
}
.place,.price{
font-size: 16px;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
color: rgb(0,0,86);
/*min-height:22px;*/
}
.price{
color: #E55555;
}
.place{
display: flex;
color: #CCCCCC;
justify-content: flex-start;
}
.place span:nth-child(1){
margin-right: 10px;
}
.see_more,.administration{
width: 100%;
height: 50px;
margin-top: 1px;
display: flex;
background-color: #fff;
justify-content: center;
align-items: center;
}
.see_more a{
color: #666666;
height: 50px;
line-height:50px;
font-size: 16px;
padding-right: 24px;
background-image: url(../img/012.png);
background-position:100px center;
background-size: 10px;
background-repeat: no-repeat;
}
.administration {
margin-top: 20px;
}
.administration a{
color: #666666;
height: 50px;
line-height:50px;
font-size: 16px;
padding-left: 35px;
background-image: url(../img/01.png);
background-position: 0px center;
background-size: 25px;
background-repeat: no-repeat;
}
footer{
width: 100%;
height: 110px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
footer p{
display: flex;
color: #D3D3D3;
}
footer p a:nth-child(1){
width: 80px;
height: 30px;
color: #666666;
margin-right:10px;
}
footer p a:nth-child(2){
width: 80px;
height: 30px;
color: #D3D3D3;
margin-left:10px ;
}
<div class="main">
<div class="top"><a></a></div>
<div class="logo"><a></a></div>
<div class="search">
<span class="search_logo"></span>
<textarea placeholder="请输入要搜索的内容"></textarea>
<a class="microphone_img"></a>
<a class="scan_img"></a>
</div>
<nav>
<li> <a href="#"><span></span>新闻</a></li>
<li> <a href="#"><span></span>小说</a></li>
<li> <a href="#"><span></span>知医</a></li>
<li> <a href="#"><span></span>导航</a></li>
<li> <a href="#"><span></span>应用</a></li>
<li> <a href="#"><span></span>更多</a></li>
</nav>
<div class="ads">
<img src="img/011.jpg" />
</div>
<section>
<div class="hotspot">
<span>今日热搜</span>
<a href="#">...</a>
</div>
<ul>
<li>
<span class="sign_num num1">1</span>
<div class="text"></div>
<span class="num">11111人关注</span>
</li>
<li>
<span class="sign_num num2">2</span>
<div class="text"></div>
<span class="num">11110人关注</span>
</li>
<li>
<span class="sign_num num3">3</span>
<div class="text hot_new"></div>
<span class="num">1111人关注</span>
</li>
<li>
<span class="sign_num ">4</span>
<div class="text"></div>
<span class="num">111人关注</span>
</li>
<li>
<span class="sign_num ">5</span>
<div class="text"></div>
<span class="num">11人关注</span>
</li>
<li>
<span class="sign_num ">6</span>
<div class="text hot_new"></div>
<span class="num">1人关注</span>
</li>
</ul>
<div class="search_more"><a href="#">查看更多<i></i></a></div>
</section>
<div class="content">
<div class="hotspot">
<span>神马好东西</span>
<a href="#">...</a>
</div>
<ul class="content_ul">
<li><a href="#" >白菜价</a></li>
<li><a href="#" >清仓</a></li>
<li><a href="#" >品牌特惠</a></li>
<li><a href="#" >优惠价</a></li>
<li><a href="#" >9.9包邮</a></li>
</ul>
<div class="foot">
<div class="commodity1">
<div class="img img1"></div>
<div class="aside">
<p class="p_context">MECHREVO 机械革命 S1 Pro 14英寸笔记本电脑(i5-8265U、8GB、512GB、MX250) 3799元包邮(前2小时)</p>
<p class="price">3799元 包邮 前2小时</p>
<p class="place">
<span>京东商城</span>
<span>2019年11月12日</span>
</p>
</div>
</div>
<div class="commodity1">
<div class="img img2"></div>
<div class="aside">
<p class="p_context">MECHREVO 机械革命 S1 Pro 14英寸笔记本电脑(i5-8265U、8GB、512GB、MX250) 3799元包邮(前2小时)</p>
<p class="price">3799元 包邮 前2小时</p>
<p class="place">
<span>京东商城</span>
<span>2019年11月12日</span>
</p>
</div>
</div>
<div class="commodity1">
<div class="img img3"></div>
<div class="aside">
<p class="p_context">MECHREVO 机械革命 S1 Pro 14英寸笔记本电脑(i5-8265U、8GB、512GB、MX250) 3799元包邮(前2小时)</p>
<p class="price">3799元 包邮 前2小时</p>
<p class="place">
<span>京东商城</span>
<span>2019年11月12日</span>
</p>
</div>
</div>
</div>
<!--content___end-->
</div>
<div class="see_more">
<a href="#">查看更多优惠</a>
</div>
<div class="administration">
<a href="#">管理我的首页</a>
</div>
<!--mian____end-->
<footer>
<p><a href="#">触屏版</a>
<a href="#">极速版</a></p>
<p>©SM.CN 粤ICP备13072090号-2</p>
</footer>
</div>
效果图如下:调节移动端页面的大小,会自适应改变页面的样式