• 响应式布局(3种) + flex计算


    响应式布局是指同一个页面在不同屏幕尺寸下有不同的布局。

    1.媒体查询

    媒体查询是最基础的实现响应式的方式
    使用@media关键字
    媒体查询是阶梯性的,不连续,没有完全响应

    2.使用百分比、rem、vw、vh等相对单位来设置元素的宽度、高度、字体大小等

    1.rem与em

    rem(root em) 是相对于根元素(通常是 标签)的字体大小。适用于全局缩放和响应式布局.
    em 是相对于父元素的字体大小。具有继承性,适用于局部尺寸的调整。

    2.vw、vh、vmax、vmin

    先来了解下网页视口相关:

    屏幕的宽度和高度:window.screenWidth、window.screenHeight
    网页视口的宽度和高度:window.innerWidth 、 window.innerHeight
    body的宽度和高度:document.body.clientWidth、document.body.clientHeight

    在这里插入图片描述

    1. 网页视口的高度=100vh
    2. 网页视口的宽度=100vw
    3. vmax 取vh和vw两者的最大值
    4. vmin 取vh和vw两者的最小值 所以1vh是网页视高度的百分之一,1vw是网页视口宽度的百分之一

    3.Flexbox

    主轴(Main Axis)、交叉轴(Cross Axis)、Flex容器(Flex Container):包含Flex项目的容器。、Flex项目(Flex Item)

    设置Flex属性:使用Flex属性来控制Flex项目在主轴和交叉轴上的行为。

    /* 容器属性 */
    
    .container {
        /* 定义为flex布局 */
        display: flex; /* 或 inline-flex */
    
        /* 主轴的方向 */
        flex-direction: row; /* 默认值,从左到右。还可以是row-reverse、column或column-reverse */
    
        /* 是否换行 */
        flex-wrap: nowrap; /* 默认值,不换行。还可以是wrap或wrap-reverse:当项目无法放在同一行时,它们会从容器的底部开始并向上堆叠 */
    
        /* flex-direction 和 flex-wrap 的简写 */
        flex-flow: row nowrap; 
    
        /* 主轴上的对齐方式 */
        justify-content: flex-start; /* 默认值,左对齐。还可以是flex-end(右对齐)、center(中心对齐)、space-between(平分空间,左右顶边)、space-around(两侧空间是中间一半)或space-evenly(两侧空间等于中间) */
    
        /* 交叉轴上的对齐方式 */
        align-items: stretch; /* 默认值,项目被拉伸以适应容器的交叉轴宽度。还可以是flex-start、flex-end(从交叉轴底部排列)、center(交叉轴中间排列)或baseline */
    
        /* 多行的交叉轴上的对齐方式 */
        align-content: stretch; /* 默认值,行之间拉伸。也可为flex-start、flex-end、center、space-between或space-around */
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    /* 项目属性 */
    
    .item {
        /* 排列顺序 */
        order: 0; /* 默认值。可以是任何整数 */
    
        /* 放大比例 */
        flex-grow: 0; /* 默认值。定义在分配多余空间时,项目的放大比例 */
    
        /* 缩小比例 */
        flex-shrink: 1; /* 默认值。定义在减少空间时,项目的缩小比例 */
    
        /* 初始大小 */
        flex-basis: auto; /* 默认值。初始大小 */
    
        /* 上述三个属性的简写 */
        flex: 0 1 auto; /* 使用flex-grow, flex-shrink 和 flex-basis的默认值 */
    	flex: 1 1 0%; /* flex:1缩写 让所有Item平均分配剩余空间*/
        /* 单个项目的对齐方式 */
        align-self: auto; /* 默认值,与继承容器的align-items值。还可以是flex-start、flex-end、center、baseline或stretch。控制交叉轴单独布局,没有与之对应的单独控制主轴的布局,除非换grid方法或加margin */
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    flexbox计算题

    问题1:
    你有四个项目,宽度分别为100px、150px、200px和250px,其flex-shrink值分别为2、1、3和0。
    如果需要在这四个项目之间减少总共300px的空间,每个项目会减少多少像素?
    解:
    项目1的缩小比例为 2/6 = 1/3
    项目2的缩小比例为 1/6
    项目3的缩小比例为 3/6 = 1/2
    项目4不会缩小,因为它的flex-shrink值为0。

    项目1会减少:300px * 1/3 = 100px
    项目2会减少:300px * 1/6 = 50px
    项目3会减少:300px * 1/2 = 150px
    项目4不会缩小。

    最后宽度加上flex-basis

    实现一个响应式布局:一个父盒子,里面三个子盒子,左右固定在两侧,固定宽高200px ,中间盒子响应式,有多少种实现方法,给出代码

    响应式布局可以通过多种方式实现。以下列举了几种常用的方法来实现所描述的布局:

    1. 使用Flexbox

    .parent {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
    
    .child-left,
    .child-right {
        width: 200px;
        height: 200px;
        flex-shrink: 0; /* 防止缩小 */
    }
    
    .child-center {
        flex-grow: 1; /* 允许增长以填充剩余空间 */
        height: 200px;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    2. 使用Grid

    .parent {
        display: grid;
        grid-template-columns: 200px 1fr 200px;
    }
    
    .child-left,
    .child-right,
    .child-center {
        height: 200px;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3. 使用浮动

    .child-left,
    .child-right {
        width: 200px;
        height: 200px;
        float: left;
    }
    
    .child-center {
        margin-left: 200px;
        margin-right: 200px;
        height: 200px;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    4. 使用定位 (不推荐,因为中间内容可能会被两边的内容遮挡)

    .parent {
        position: relative;
    }
    
    .child-left,
    .child-right {
        width: 200px;
        height: 200px;
        position: absolute;
        top: 0;
    }
    
    .child-left {
        left: 0;
    }
    
    .child-right {
        right: 0;
    }
    
    .child-center {
        padding-left: 200px;
        padding-right: 200px;
        height: 200px;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    以上提供的是几种不同的实现方法,每种方法有其适用的场景。现代Web开发中,Flexbox和Grid是比较推荐的方法,因为它们提供了更多的灵活性和对响应式设计的支持。

  • 相关阅读:
    Mybatis-Plus 自定义SQL注入器,实现真正的批量插入![MyBatis-Plus系列]
    C# 抽象类和接口
    跨境电商卖家只青睐亚马逊?其实你不知道,“备胎”早已选好!(Starday)
    2023/10/24 MySQL学习
    【cocos creator】编辑器里自动播放spine动画
    实现高并发内存池(C++)
    Day11:文件和异常
    【随笔】无题
    golang validator 提示消息本地化(中英文案例)
    成都瀚网科技有限公司:怎么优化抖店体验分?
  • 原文地址:https://blog.csdn.net/weixin_43850639/article/details/132664941