zoukankan      html  css  js  c++  java
  • css让子元继承父元素的高度

    情景描述:父元素由一个设置了高度的元素撑起,另一个元素自动继承父元素的高,并且两元素在一行显示。

    现在情况

     <style>
            .far{
                border: 1px solid bisque;
            }
            .s1{
                height: 150px;
                width: 100px;
                background-color: blue;
                display: inline-block;
            }
            .s2{
                height: 100%;
                width: 50px;
                background-color: brown;
                display: inline-block;
            }
    
        </style>
    </head>
    <body>
        <div class="far">
            <div class="s1"></div>
            <div class="s2"></div>
        </div>
    </body>

    方法1:

      设置父元素flex,子元素不设置高度

    <style>
            .far{
                border: 1px solid bisque;
                display: flex;
            }
            .s1{
                height: 150px;
                width: 100px;
                background-color: blue;
                display: inline-block;
            }
            .s2{
                width: 50px;
                background-color: brown;
                display: inline-block;
            }
    
        </style>
    </head>
    <body>
        <div class="far">
            <div class="s1"></div>
            <div class="s2"></div>
        </div>
    </body>

    方法2:

      父元素相当定位,子元素绝对定位,设置100%高

     .far{
                border: 1px solid bisque;
                position: relative;
                font-size: 0;
            }
            .s1{
                height: 150px;
                 100px;
                background-color: blue;
                display: inline-block;
            }
            .s2{
                 50px;
                background-color: brown;
                display: inline;
                position: absolute;
                height: 100%;
    
            }
    
        </style>
    </head>
    <body>
        <div class="far">
            <div class="s1"></div>
            <div class="s2"></div>
        </div>
    </body>

    s1和s2元素都要设置inline或者inline-block属性,否则不法自动在一行,要在设置left,top等属性

    龙丘居士亦可怜,谈空说有夜不眠。 忽闻河东狮子吼,拄杖落手心茫然。 多有画面感
  • 相关阅读:
    vue向组件传递数据
    微信会员卡,开卡组件开发遇到总汇
    siteserver安装向导完成后,显示System.Web.UI.page render System.ArgumentNullException
    spring mvc中的valid
    django 命名空间详解
    spring MVC 如何查找URL对应的处理类
    【HDU 2010】水仙花数
    【CSU 1079】树上的查询
    【Poj 1330】Nearest Common Ancestors
    【Dairy】2016.10.20 生日记
  • 原文地址:https://www.cnblogs.com/ybhome/p/14367285.html
Copyright © 2011-2022 走看看