• css flex实现同行div根据内容高度自适应且保持一致


    有情况如下:三个div的高度是随着内容自适应的,但希望每列的高度都相同,即,都与最高的相同。

    在这里插入图片描述

    DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Documenttitle>
        <style>
            .container {
                display: flex;
                justify-content: space-between;
            }
    
            .item {
                width: 100px;
            }
    
            .aqua {
                background-color: rgb(97, 231, 231);
    
            }
    
            .green {
                background-color: rgb(135, 226, 150);
            }
    
            .yellow {
                background-color: rgb(240, 240, 131);
            }
        style>
    head>
    
    <body>
        <div class="container">
            <div class="item aqua">
                <p>1p>
                <p>1p>
                <p>1p>
                <p>1p>
            div>
            <div class="item">
                <div class="item1 green">
                    <p>2p>
                div>
                <div class="item1 yellow">
                    <p>2p>
                    <p>2p>
                div>
            div>
            <div class="item">
                <div class="item1 green">
                    <p>2p>
                div>
                <div class="item1 yellow">
                    <p>2p>
                    <p>2p>
                div>
                <div class="item aqua">
                    <p>1p>
                    <p>1p>
                    <p>1p>
                    <p>1p>
                div>
            div>
        div>
    body>
    
    html>
    
    • 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
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    实现方法:令父元素flex,且align-items: stretch;

    在这里插入图片描述
    想要让子元素下面的元素贴底,可以令子元素:

    display: flex;
    flex-direction: column;
    justify-content: space-between;
    
    • 1
    • 2
    • 3

    在这里插入图片描述
    可以把这段css放到一个新的类里,命名为alignHeight,在任何想要实现此效果的div中加入此类。

    修改后的代码:

    DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Documenttitle>
        <style>
            .container {
                display: flex;
                justify-content: space-between;
                align-items: stretch;
            }
    
            .alignHeight {
                display: flex;
                flex-direction: column;
                justify-content: space-between;
            }
    
            .item {
                width: 100px;
    
            }
    
            .aqua {
                background-color: rgb(97, 231, 231);
    
            }
    
            .green {
                background-color: rgb(135, 226, 150);
            }
    
            .yellow {
                background-color: rgb(240, 240, 131);
            }
        style>
    head>
    
    <body>
        <div class="container">
            <div class="item aqua alignHeight">
                <p>1p>
                <p>1p>
                <p>1p>
                <p>1p>
            div>
            <div class="item alignHeight">
                <div class="item1 green">
                    <p>2p>
                div>
                <div class="item1 yellow">
                    <p>2p>
                    <p>2p>
                div>
            div>
            <div class="item alignHeight">
                <div class="item1 green">
                    <p>2p>
                div>
                <div class="item1 yellow">
                    <p>2p>
                    <p>2p>
                div>
                <div class="item aqua">
                    <p>1p>
                    <p>1p>
                    <p>1p>
                    <p>1p>
                div>
            div>
        div>
    body>
    
    html>
    
    • 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
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
  • 相关阅读:
    WebGPU 中消失的 FBO 和 RBO
    机器学习模型的“可解释性”
    1542. 找出最长的超赞子字符串 哈希+状态压缩
    Jenkins插件开发——提供对外访问接口
    使用docker搭建nacos单机、集群 + mysql
    BeeWare官方教程中文版
    TiDB 乐观事务模型
    风光储一体化能源中心 | 数字孪生智慧能源
    高速花炮筒纸筒剪切机分切机设计(说明书+CAD图纸)
    大话AI绘画技术原理与算法优化
  • 原文地址:https://blog.csdn.net/karshey/article/details/133985639