flex

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />

<style type="text/css">

div{

width: 100%;

height: 500px;

background-color: antiquewhite;

}

.box {

display: flex;

flex-direction: column;

/*主轴的方向 */

/*flex-wrap: nowrap | wrap | wrap-reverse

可使得主轴上的元素不折行、折行、反向折行。*/

flex-wrap: nowrap;

/*一个复合属性

                              flex-flow = flex-drection + flex-wrap

                              flex-flow相当于规定了flex布局的“工作流(flow)”

                            flex-flow: row nowrap;*/

/*交叉轴上的对齐方式align-items

                          默认值是stretch,当元素没有设置具体尺寸时会将容器在交叉轴方向撑满。*/

align-items: flex-start;

                          /*justify-content属性定义了项目在主轴上的对齐方式。*/

                            justify-content: space-between;

                            align-content: center;

}

.item {

flex: none; /*0 不放大 none(0 0 auto)。  1 放大*/

text-align: left;

height: 42px;

line-height: 40px;

width: 300px;

list-style: none;

background: green;

border-right: 3px solid grey;

}

.red {

background-color: red;

}

.blue {

background-color: blue;

}

</style>

</head>

<body>

<div >

<ul class="box">

<li class="item">音乐</li>

<li class="item">旅游</li>

<li class="item">电影</li>

<li class="item">综艺</li>

<li class="item">音乐</li>

<li class="item">旅游</li>

</ul>

</div>

</body>

</html>

1、flex设置元素水平垂直居中

<div class="box">

  <h3>Flex实现水平垂直居中</h3>

  <p>

  flex-direction决定主轴的方向:row|row-reverse|column|column-reverse<br/>

  justify-content决定主轴的对齐方式:flex-start|flex-end|center|space-between|space-around<br/>

  align-items决定交叉轴的对齐方式:flex-start|flex-end|center|baseline|stretch

  </p>

</div>

.box{

display: flex;

justify-content: center;

align-items: center;

border: 1px solid black;

width:500px;

height:500px;

}

实现效果:

need-to-insert-img

2、用flex布局制作导航栏

以前写导航栏的时候,总是用float或display:inline-block实现,但是这两种方法都会有各种问题,比如浮动会影响父元素以及兄弟元素的样式,需要清除浮动,现在用flex会很方便,并且是弹性布局。

<ul>

<li>音乐</li>

<li>旅游</li>

<li>电影</li>

<li>综艺</li>

</ul>

ul{

display: flex;

}li{

flex:1;

text-align: center;

line-height: 100px;

list-style: none;

background: green;

border-right: 1px solid grey;

}

实现效果:

need-to-insert-img

3、图文并排的样式:左边是图片,右边是文字

<div class="box">

  <div class="left"></div>

  <div class="right">

<p>第一行</p>

<p>说明1&nbsp;&nbsp;&nbsp;&nbsp;说明2</p>

<div><input type="button" value="确认"></div>

  </div>

</div>

.box{

display: flex;

justify-content: space-between;

width: 350px;

height: 150px;

border: 1px solid grey;

}.left{

width: 100px;

height: 100px;

background: grey;

}.right{

width: 150px;

height: 100px;

}

实现效果:

need-to-insert-img

4、固定百分比布局

<div class="demo">

  <div class="item item1">1/4</div>

  <div class="item item2">1/4</div>

  <div class="item item3">1/4</div>

  <div class="item item4">1/4</div>

</div>

.demo{

display: flex;           

}.item{

flex: 1;

background: green;

border-right:1px solid grey;

line-height: 100px;

}

实现效果:

need-to-insert-img

5、某一个固定

<div class="demo">

  <div class="item item1">auto</div>

  <div class="item item2">1/2</div>

  <div class="item item3">auto</div>

  <div class="item item4">auto</div>

</div>

.demo{

display: flex;           

}.item{

flex: 1;

background: green;

border-right:1px solid grey;

line-height: 100px;

color: #fff;

text-align: center;

}.item2{

    flex: 0 0 50%;

}

实现效果:

need-to-insert-img

6、圣杯布局

    圣杯布局指的是一种最常见的网站布局。页面从上到下,分成三部分:头部(header),躯干(body),尾部(footer)。

其中躯干又水平分成三栏:从左到右为:导航、主栏、副栏。

<div class="demo">

  <div class="header">头部</div>

  <div class="body">

    <div class="left">left</div>

    <div class="center">center</div>

    <div class="right">right</div>

  </div>

  <div class="footer">底部</div>

</div>

.demo{

  display: flex;

flex-direction: column;           

}.demo div{

flex: 1;

}.body{

display: flex; 

} .header, .footer{

background: grey;

line-height: 80px;

text-align: center;

}.left, .right{

background: pink;

line-height: 200px;

text-align: center;

}.header,.footer,.left,.right{

flex: 0 0 20%!important;

}

实现效果:

need-to-insert-img

7、双飞翼布局

    双飞翼布局,就是两端固定宽高,中间自适应的三栏布局。

<div id="container">

  <div id="left" class="column">#left</div>

    <div id="center" class="column">#center</div>

    <div id="right" class="column">#right</div>

</div>

    body {

        min-width: 550px;

  }

    #container{

        display: flex;

        justify-content: center;

        align-items: flex-start;

    }

    .column{

        height: 200px;

        color:white;

    }

    #center{

        flex-grow: 1;

        background-color: black;

    }

    #left{

        flex-basis: 200px;

        background-color: red;

    }

    #right{

        flex-basis: 200px;

        background-color: blue;

    }

实现效果:

need-to-insert-img

8、底部fooer固定在底部,但是不是fixed定位

页面会经常用到固定的底栏,但是当页面内容过少时,footer会到页面中间,下面用flex来解决:

<div class="demo">

  <div class="main">这是主要内容</div>

  <div class="footer">这是底部</div>

</div>

.demo{

    display: flex;

  flex-direction: column;

  width: 300px;

  height: 200px;

}.main{

  flex: 1;

  background: pink;

}.footer{

  width: 100%;

height: 30px;

background: grey;

}

实现效果:

need-to-insert-img

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,755评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,305评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,138评论 0 355
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,791评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,794评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,631评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,362评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,264评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,724评论 1 315
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,900评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,040评论 1 350
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,742评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,364评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,944评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,060评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,247评论 3 371
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,979评论 2 355

推荐阅读更多精彩内容

  • 作者: 阮一峰日期: 2015年7月14日 上一篇文章介绍了Flex布局的语法,今天介绍常见布局的Flex写法。你...
    vimtest阅读 303评论 0 3
  • 转载自:http://www.ruanyifeng.com/blog/2015/07/flex-examples....
    天字一等阅读 609评论 0 1
  • flex 布局是 css3 中使用最频繁也是最出色的功能,有点复杂,分为应用在容器上的属性和项目上的属性,即父元素...
    LaBaby_阅读 366评论 0 0
  • 上一篇文章介绍了Flex布局的语法,今天介绍常见布局的Flex写法。你会看到,不管是什么布局,Flex往往都可以几...
    君临天下夜未央阅读 3,755评论 1 12
  • 东平湖畔的"喜丧" 在东平湖畔的一个小渔村里,传来一阵欢快的鼓乐声,现在既不是春会,也不是捕鱼节,吹的啥乐? “嗯...
    东平糊粥儿阅读 775评论 7 7