3.22总结(移动端flex布局)

​ 一个检测css属性支持性的小工具,如下:Can I use... Support tables for HTML5, CSS3, etc

1.flex布局和传统布局的区别

1.1 传统布局

  • 兼容性好

  • 布局繁琐

  • 不适合移动端​

1.2 flex布局:

  • 适合移动端

  • 布局简单

  • pc端兼容较差

  • IE11或更低版本不支持flex或仅支持部分

1.3 建议

​ 如果是pc端还是使用传统布局,如果是移动端使用flex布局,当然,如果pc端不考虑兼容性的情况下也可以大量使用flex布局。

2.flex布局原理

2.1原理

​ flex 是 flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性,任何一个容器都可以指定为 flex 布局。

​ 当我们为父盒子指定display:flex;时,子元素的 float、clear 和 vertical-align 属性将失效。

​ 原因是因为,flex布局可以让元素一行显示,不区分块级和行内,行内块元素。所以就用不到float属性,也就不用清除浮动了。

总结就是通过给父盒子添加display:flex属性,来控制子盒子的位置和排列方式

2.2 Flex布局的组成部分

容器(父级盒子)项目(子元素)主轴,默认的主轴是X轴侧轴/交叉轴

3.父级元素常见属性

Flex布局容器(父级盒子)常见的属性设置

flex-direction

设置Flex布局的主轴方向

Flex布局中默认的主轴是row,x轴,默认值是从左到右

flex-direction:column;将主轴改为y轴,纵轴,从上到下;

flex-direction:row;将主轴改为x轴,横轴,从左到右;

flex-direction:row- reverse;主轴为x轴,并且翻转,从右到左翻转排列;

flex-direction:column- reverse;主轴为y轴,并且翻转,从下到上翻转排列 ;

justify-content

通过justify-content能够设置主轴子元素排列形式,但是注意使用这个属性之前一定要确定好主轴

justify-content:flex-start;默认值,所有子元素在主轴头部开始显示,如果是X轴,则从左到右,如果是Y轴,则从上到下;

justify-content:flex-end ;所有子元素在主轴尾部开始显示,如果是X轴,则所有元素靠右显示,如果是Y轴,则所有元素靠下显示;

justify-content:center;所有子元素在主轴居中对齐

justify-content:space-around ;所有子元素平分剩余空间

justify-content:space-between;所有子元素中,先两边两个元素贴边,剩余的元素平分剩余空间(常用);

justify-content:space-evenly ;所有子元素之间的距离都一致

flex-wrap

规定子元素是否换行显示,flex布局如果子元素的总宽度加起来超出了父级的宽,默认是不会自动换行的,会压缩一行显示;

flex-wrap:nowrap;

开启flex布局后项目(子元素)默认不换行,默认情况下项目(子元素)都排在一条线上(轴线),如果子元素的总宽度加起来超出了父元素的宽度,子元素就会压缩显示,不会换行;

align-items

单行的子元素,父级没有设置flex-wrap:wrap;换行属性的时候,设置侧轴子元素的排列

align-content

多行的子元素,父级设置flex-wrap:wrap;换行属性的时候,设置侧轴子元素的排列align-items:flex-start ;交叉轴的起点对齐align-items:flex-end ;交叉轴的终点对齐align-items:stretch ;默认值(拉升),如果项目(子元素)未设置高度或设为auto,将占满整个容器(父元素)的高度

align-content 和align-items区别:

  • align-items 适用于单行情况下, 只有上对齐、下对齐、居中和 拉伸

  • align-content适应于换行(多行)的情况下(单行情况下无效), 可以设置 上对齐、下对齐、居中、拉伸以及平均分配剩余空间等属性值。

  • 总结:单行找align-items 多行找 align-content

flex-flow

是 flex-direction 和 flex-wrap 属性的复合属性

flex-flow:row wrap; /*x主轴,换行 */

4.子级元素常见属性

align-self

设置子项自己在侧轴的排列方式

<style>
        div {
            display: flex;
            width: 80%;
            height: 300px;
            background-color: pink;
            /* 让三个子盒子沿着侧轴底侧对齐 */
            /* align-items: flex-end; */
           
        }
        
        div span {
            width: 150px;
            height: 100px;
            background-color: purple;
            margin-right: 5px;
        }
        
        div span:nth-child(3) {
             /* 我们想只让3号盒子下来底侧 */
            align-self: flex-end;
        }
    </style>
</head>
​
<body>
    <div>
        <span>1</span>
        <span>2</span>
        <span>3</span>
    </div>

效果:

 

older

定义项目的排列顺序,数值越小,排列越靠前,默认为0,可以是负数。

flex

定义子盒子所占父盒子的份数,可以用数字,也可以用百分比。

5.知识点

垂直水平居中的方法:

margin: auto–完美的居中​ 使用弹性盒子(display: flex),居中变的很简单,只想要设置 margin: auto; 可以使得弹性子元素在两上轴方向上完全居中。

6、案例

效果图(因为没有图片素材自己截图做的不太美观,见谅):

 

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- 视口完善:禁止用户缩放,最小缩放为1,最大缩放为0-->
    <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no,minimum-scale=1.0,maximum-scale=1.0,">
    <!-- 注意一定要把normalize.css放在上面,因为浏览器从上往下解析,会有层叠性 -->
    <link rel="stylesheet" href="./css/normalize.css">
    <link rel="stylesheet" href="./css/index.css">
    <title>消息中心</title>
</head>

<body>
    <!-- 头部 -->
    <header>
        <div class="hd">消息中心</div>
    </header>
    <!-- 内容区域 -->
    <article>
        <div class="content">
            <div class="photo-1"></div>
            <div class="vip">
                <div class="notice">
                    <h3>会员通知 </h3>
                    <span>上午5:00</span>
                </div>
                <div class="prize">
                    您领取的“完善生日信息”任务已经完成,奖
                </div>
            </div>
        </div>
        <div class="content">
            <div class="photo-2"></div>
            <div class="vip">
                <div class="notice">
                    <h3>物流通知</h3>
                    <span>昨天12:30</span>
                </div>
                <div class="prize">
                    暂无消息,点此查看订单信息
                </div>
            </div>
        </div>
        <div class="content">
            <div class="photo-3"></div>
            <div class="vip">
                <div class="notice">
                    <h3>我的资产 </h3>
                    <span>星期六</span>
                </div>
                <div class="prize">
                    暂无消息,点击查看我的礼券
                </div>
            </div>
        </div>
        <div class="content">
            <div class="photo-4"></div>
            <div class="vip">
                <div class="notice">
                    <h3>社区消息</h3>
                    <span>星期五</span>
                </div>
                <div class="prize">
                    暂无消息,点击查看个人主页
                </div>
            </div>
        </div>
        <div class="content">
            <div class="photo-5"></div>
            <div class="vip">
                <div class="notice">
                    <h3>商品问答</h3>
                    <span>去年</span>
                </div>
                <div class="prize">
                    暂无消息,点击查看我的问答
                </div>
            </div>
        </div>
        <div class="content">
            <div class="photo-6"></div>
            <div class="vip">
                <div class="notice">
                    <h3>服务通知</h3>
                    <span>去年</span>
                </div>
                <div class="prize">
                    暂无消息,点击查看订单信息
                </div>
            </div>
        </div>
        <div class="content">
            <div class="photo-7"></div>
            <div class="vip">
                <div class="notice">
                    <h3>社区消息</h3>
                    <span>去年</span>
                </div>
                <div class="prize">
                    暂无消息,点击查看收藏商品
                </div>
            </div>
        </div>
    </article>
</body>

</html>

css

    /* 引入icommon字体 */
    
    @font-face {
        font-family: 'icomoon';
        src: url('fonts/icomoon.eot?yt1na2');
        src: url('fonts/icomoon.eot?yt1na2#iefix') format('embedded-opentype'), url('fonts/icomoon.ttf?yt1na2') format('truetype'), url('fonts/icomoon.woff?yt1na2') format('woff'), url('fonts/icomoon.svg?yt1na2#icomoon') format('svg');
        font-weight: normal;
        font-style: normal;
        font-display: block;
    }
    
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
    /* 设置body样式 */
    
    body {
        /* 设置宽度与浏览器一样宽 */
        width: 100%;
        /* 限制最大宽度最小宽度 */
        max-width: 640px;
        min-width: 320px;
        margin: 0 auto;
        font: normal 14px/1.5 Tahoma, "Lucida Grande", Verdana, "Microsoft Yahei", STXihei, hei;
        line-height: 1.5;
        color: #000;
        background: #b8b8b8;
        /* 设置X轴的滚动条永远不显示 */
        overflow-x: hidden;
        /* 设置高亮为透明 */
        -webkit-tap-highlight-color: transparent;
    }
    /* 设置ul样式 */
    
    ul {
        /* 清除ul原有样式 */
        list-style: none;
        /* 清除间距 */
        margin: 0;
        padding: 0;
    }
    /* 设置a链接样式 */
    
    a {
        /* 清除a的下划线 */
        text-decoration: none;
        color: #222;
    }
    
    div {
        /* 设置div为css 3盒模型 */
        box-sizing: border-box;
    }
    /* ------头部------ */
    
    header {
        position: fixed;
        top: 0;
        /* 固定定位一定要有宽度 */
        width: 100%;
        /* 因为固定定位参考浏览器,所以要限制他的最大和最小宽度 */
        max-width: 640px;
        min-width: 320px;
        padding: 0px 22px;
        text-align: center;
    }
    
    .hd {
        position: relative;
        font-size: 27px;
        color: #474747;
    }
    
    .hd::before {
        position: absolute;
        top: 0;
        left: 0;
        font-family: 'icomoon';
        content: '\ea40';
        font-size: 26px;
        color: #000;
    }
    /* 内容区域 */
    
    article {
        padding: 0px 22px;
    }
    
    .content {
        display: flex;
        padding-top: 50px;
        height: 125px;
    }
    
    .content div[class^=photo-] {
        width: 67px;
        height: 67px;
        background-color: green;
        border-radius: 50%;
        align-self: center;
        background: url(../images/photo1.png) no-repeat;
        background-size: 67px 67px;
    }
    
    .content div.photo-2 {
        background: url(../images/photo2.png) no-repeat;
        background-size: 67px 67px;
    }
    
    .content div.photo-3 {
        background: url(../images/photo3.png) no-repeat;
    }
    
    .content div.photo-4 {
        background: url(../images/photo4.png) no-repeat;
        background-size: 67px 67px;
    }
    
    .content div.photo-5 {
        background: url(../images/photo7.png) no-repeat;
        background-size: 67px 67px;
    }
    
    .content div.photo-6 {
        background: url(../images/6.png) no-repeat;
        background-size: 67px 67px;
    }
    
    .content div.photo-7 {
        background: url(../images/photo5.png) no-repeat;
        background-size: 67px 67px;
    }
    
    .vip {
        /* 设置最小宽度为0是,因为里面的子盒子设置了强制不换行,会撑大flex布局的盒子。 */
        min-width: 0;
        display: flex;
        padding-left: 10px;
        flex: 1;
        flex-direction: column;
        border-bottom: 1px solid #d8d8d8;
    }
    
    .notice {
        flex: 1;
        display: flex;
    }
    
    .notice h3 {
        flex: 1;
        font-size: 21px;
        color: #474747;
        font-weight: 400;
    }
    
    .notice span {
        flex: 1;
        text-align: right;
        font-size: 17px;
        color: #aaa;
    }
    
    .prize {
        flex: 1;
        font-size: 14px;
        color: #a7a7a7;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

normalize.css

/*! normalize.css v5.0.0 | MIT License | github.com/necolas/normalize.css */

/**
 * 1. Change the default font family in all browsers (opinionated).
 * 2. Correct the line height in all browsers.
 * 3. Prevent adjustments of font size after orientation changes in
 *    IE on Windows Phone and in iOS.
 */

/* Document
   ========================================================================== */

html {
  font-family: sans-serif; /* 1 */
  line-height: 1.15; /* 2 */
  -ms-text-size-adjust: 100%; /* 3 */
  -webkit-text-size-adjust: 100%; /* 3 */
}

/* Sections
   ========================================================================== */

/**
 * Remove the margin in all browsers (opinionated).
 */

body {
  margin: 0;
}

/**
 * Add the correct display in IE 9-.
 */

article,
aside,
footer,
header,
nav,
section {
  display: block;
}

/**
 * Correct the font size and margin on `h1` elements within `section` and
 * `article` contexts in Chrome, Firefox, and Safari.
 */

h1 {
  font-size: 2em;
  margin: 0.67em 0;
}

/* Grouping content
   ========================================================================== */

/**
 * Add the correct display in IE 9-.
 * 1. Add the correct display in IE.
 */

figcaption,
figure,
main { /* 1 */
  display: block;
}

/**
 * Add the correct margin in IE 8.
 */

figure {
  margin: 1em 40px;
}

/**
 * 1. Add the correct box sizing in Firefox.
 * 2. Show the overflow in Edge and IE.
 */

hr {
  box-sizing: content-box; /* 1 */
  height: 0; /* 1 */
  overflow: visible; /* 2 */
}

/**
 * 1. Correct the inheritance and scaling of font size in all browsers.
 * 2. Correct the odd `em` font sizing in all browsers.
 */

pre {
  font-family: monospace, monospace; /* 1 */
  font-size: 1em; /* 2 */
}

/* Text-level semantics
   ========================================================================== */

/**
 * 1. Remove the gray background on active links in IE 10.
 * 2. Remove gaps in links underline in iOS 8+ and Safari 8+.
 */

a {
  background-color: transparent; /* 1 */
  -webkit-text-decoration-skip: objects; /* 2 */
}

/**
 * Remove the outline on focused links when they are also active or hovered
 * in all browsers (opinionated).
 */

a:active,
a:hover {
  outline-width: 0;
}

/**
 * 1. Remove the bottom border in Firefox 39-.
 * 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari.
 */

abbr[title] {
  border-bottom: none; /* 1 */
  text-decoration: underline; /* 2 */
  text-decoration: underline dotted; /* 2 */
}

/**
 * Prevent the duplicate application of `bolder` by the next rule in Safari 6.
 */

b,
strong {
  font-weight: inherit;
}

/**
 * Add the correct font weight in Chrome, Edge, and Safari.
 */

b,
strong {
  font-weight: bolder;
}

/**
 * 1. Correct the inheritance and scaling of font size in all browsers.
 * 2. Correct the odd `em` font sizing in all browsers.
 */

code,
kbd,
samp {
  font-family: monospace, monospace; /* 1 */
  font-size: 1em; /* 2 */
}

/**
 * Add the correct font style in Android 4.3-.
 */

dfn {
  font-style: italic;
}

/**
 * Add the correct background and color in IE 9-.
 */

mark {
  background-color: #ff0;
  color: #000;
}

/**
 * Add the correct font size in all browsers.
 */

small {
  font-size: 80%;
}

/**
 * Prevent `sub` and `sup` elements from affecting the line height in
 * all browsers.
 */

sub,
sup {
  font-size: 75%;
  line-height: 0;
  position: relative;
  vertical-align: baseline;
}

sub {
  bottom: -0.25em;
}

sup {
  top: -0.5em;
}

/* Embedded content
   ========================================================================== */

/**
 * Add the correct display in IE 9-.
 */

audio,
video {
  display: inline-block;
}

/**
 * Add the correct display in iOS 4-7.
 */

audio:not([controls]) {
  display: none;
  height: 0;
}

/**
 * Remove the border on images inside links in IE 10-.
 */

img {
  border-style: none;
}

/**
 * Hide the overflow in IE.
 */

svg:not(:root) {
  overflow: hidden;
}

/* Forms
   ========================================================================== */

/**
 * 1. Change the font styles in all browsers (opinionated).
 * 2. Remove the margin in Firefox and Safari.
 */

button,
input,
optgroup,
select,
textarea {
  font-family: sans-serif; /* 1 */
  font-size: 100%; /* 1 */
  line-height: 1.15; /* 1 */
  margin: 0; /* 2 */
}

/**
 * Show the overflow in IE.
 * 1. Show the overflow in Edge.
 */

button,
input { /* 1 */
  overflow: visible;
}

/**
 * Remove the inheritance of text transform in Edge, Firefox, and IE.
 * 1. Remove the inheritance of text transform in Firefox.
 */

button,
select { /* 1 */
  text-transform: none;
}

/**
 * 1. Prevent a WebKit bug where (2) destroys native `audio` and `video`
 *    controls in Android 4.
 * 2. Correct the inability to style clickable types in iOS and Safari.
 */

button,
html [type="button"], /* 1 */
[type="reset"],
[type="submit"] {
  -webkit-appearance: button; /* 2 */
}

/**
 * Remove the inner border and padding in Firefox.
 */

button::-moz-focus-inner,
[type="button"]::-moz-focus-inner,
[type="reset"]::-moz-focus-inner,
[type="submit"]::-moz-focus-inner {
  border-style: none;
  padding: 0;
}

/**
 * Restore the focus styles unset by the previous rule.
 */

button:-moz-focusring,
[type="button"]:-moz-focusring,
[type="reset"]:-moz-focusring,
[type="submit"]:-moz-focusring {
  outline: 1px dotted ButtonText;
}

/**
 * Change the border, margin, and padding in all browsers (opinionated).
 */

fieldset {
  border: 1px solid #c0c0c0;
  margin: 0 2px;
  padding: 0.35em 0.625em 0.75em;
}

/**
 * 1. Correct the text wrapping in Edge and IE.
 * 2. Correct the color inheritance from `fieldset` elements in IE.
 * 3. Remove the padding so developers are not caught out when they zero out
 *    `fieldset` elements in all browsers.
 */

legend {
  box-sizing: border-box; /* 1 */
  color: inherit; /* 2 */
  display: table; /* 1 */
  max-width: 100%; /* 1 */
  padding: 0; /* 3 */
  white-space: normal; /* 1 */
}

/**
 * 1. Add the correct display in IE 9-.
 * 2. Add the correct vertical alignment in Chrome, Firefox, and Opera.
 */

progress {
  display: inline-block; /* 1 */
  vertical-align: baseline; /* 2 */
}

/**
 * Remove the default vertical scrollbar in IE.
 */

textarea {
  overflow: auto;
}

/**
 * 1. Add the correct box sizing in IE 10-.
 * 2. Remove the padding in IE 10-.
 */

[type="checkbox"],
[type="radio"] {
  box-sizing: border-box; /* 1 */
  padding: 0; /* 2 */
}

/**
 * Correct the cursor style of increment and decrement buttons in Chrome.
 */

[type="number"]::-webkit-inner-spin-button,
[type="number"]::-webkit-outer-spin-button {
  height: auto;
}

/**
 * 1. Correct the odd appearance in Chrome and Safari.
 * 2. Correct the outline style in Safari.
 */

[type="search"] {
  -webkit-appearance: textfield; /* 1 */
  outline-offset: -2px; /* 2 */
}

/**
 * Remove the inner padding and cancel buttons in Chrome and Safari on macOS.
 */

[type="search"]::-webkit-search-cancel-button,
[type="search"]::-webkit-search-decoration {
  -webkit-appearance: none;
}

/**
 * 1. Correct the inability to style clickable types in iOS and Safari.
 * 2. Change font properties to `inherit` in Safari.
 */

::-webkit-file-upload-button {
  -webkit-appearance: button; /* 1 */
  font: inherit; /* 2 */
}

/* Interactive
   ========================================================================== */

/*
 * Add the correct display in IE 9-.
 * 1. Add the correct display in Edge, IE, and Firefox.
 */

details, /* 1 */
menu {
  display: block;
}

/*
 * Add the correct display in all browsers.
 */

summary {
  display: list-item;
}

/* Scripting
   ========================================================================== */

/**
 * Add the correct display in IE 9-.
 */

canvas {
  display: inline-block;
}

/**
 * Add the correct display in IE.
 */

template {
  display: none;
}

/* Hidden
   ========================================================================== */

/**
 * Add the correct display in IE 10-.
 */

[hidden] {
  display: none;
}

7.思维导图

最后附上一份思维导图~

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值