美文网首页
css等高布局

css等高布局

作者: sun_hl | 来源:发表于2021-08-05 17:24 被阅读0次

一、CSS等高布局的7种方式
https://www.cnblogs.com/xiaohuochai/p/5457127.html

1、 负margin
  因为背景是在padding区域显示的,设置一个大数值的padding-bottom,再设置相同数值的负的margin-bottom,使背景色铺满元素区域,又符合元素的盒模型的计算公式,实现视觉上的等高效果
[注意]如果页面中使用<a>锚点跳转时,将会隐藏部分文字信息
[注意]如果页面中的背景图片定位到底部,将会看不到背景图片

<style>
body,p{margin: 0;}
.parent{
    overflow: hidden;
}
.left,.centerWrap,.right{
    float: left;
    width: 50%;
    padding-bottom: 9999px;
    margin-bottom: -9999px;
}
.center{
    margin: 0 20px;
}
.left,.right{
    width: 25%;
}
</style>

<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>  
    <div class="centerWrap">
        <div class="center" style="background-color: pink;">
            <p>center</p>
            <p>center</p>
        </div>         
    </div>
    <div class="right" style="background-color: lightgreen;">
        <p>right</p>
    </div>        
</div>

2 table
  table元素中的table-cell元素默认就是等高的

<style>
body,p{margin: 0;}
.parent{
    display: table;
    width: 100%;
    table-layout: fixed;
}
.left,.centerWrap,.right{
    display: table-cell;
}
.center{
    margin: 0 20px;
}
</style>

<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>  
    <div class="centerWrap">
        <div class="center" style="background-color: pink;">
            <p>center</p>
            <p>center</p>
        </div>         
    </div> 
    <div class="right" style="background-color: lightgreen;">
        <p>right</p>
    </div>        
</div>

3 flex
  flex中的伸缩项目默认都拉伸为父元素的高度,也实现了等高效果

<style>
body,p{margin: 0;}
.parent{
    display: flex;
}
.left,.center,.right{
    flex: 1;
}
.center{
    margin: 0 20px;
}
</style>

<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>  
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>          
    <div class="right" style="background-color: lightgreen;">
        <p>right</p>
    </div>        
</div>

4 grid

<style>
body,p{margin: 0;}
.parent{
    display: grid;
    grid-auto-flow: column;
    grid-gap:20px;
}
</style>

<div class="parent" style="background-color: lightgrey;">
    <div class="left" style="background-color: lightblue;">
        <p>left</p>
    </div>  
    <div class="center" style="background-color: pink;">
        <p>center</p>
        <p>center</p>
    </div>          
    <div class="right" style="background-color: lightgreen;">
        <p>right</p>
    </div>        
</div>

5、absolute(真等高)

设置子元素的top:0;bottom:0;使得所有子元素的高度都和父元素的高度相同,实现等高效果

<style>
body,p{margin: 0;}
.parent{
    position: relative;
    height: 40px;
}
.left,.center,.right{
    position: absolute;
    top: 0;
    bottom: 0;
}
.left{
    left: 0;
    width: 100px;
}
.center{
    left: 120px;
    right: 120px;
}
.right{
    width: 100px;
    right: 0;
}
</style>

相关文章

  • css等高布局

    首先页面template如下: 1.tabletable元素中的table-cell元素默认就是等高的 2.fle...

  • css等高布局

    一、CSS等高布局的7种方式https://www.cnblogs.com/xiaohuochai/p/54571...

  • 几种常见css布局

    单列布局 第一种 给定宽度,margin:auto即可实现 html css 第二种 html css 等高布局 ...

  • css之等高布局

    1. 在父元素中不设置高度,使用overflow:hidden; 2. 使用很大的正padding-bottom,...

  • css实现等高布局

    等高布局是指在同一个父容器里,子元素的高度相等的布局方式,等高布局的实现有伪等高和真等高,伪等高只是在视觉上给人感...

  • 等分布局

    提供一种flex实现html代码如下 CSS代码 等高布局用table flex

  • CSS奇技淫巧之负边距的应用

    最近在学习flex布局,在赞叹其便捷性的同时,回想起之前使用css2的时候实现等高或者等宽布局的麻烦。同时也看到[...

  • 等高布局

  • 等高布局

    1、 使用数值非常大正padding-bottom与负margin-bottom 首先,两列都是左浮动,且都设置了...

  • 等高布局

    一、padding&margin的叠加 原理:,使用足够高的padding-bottom 来显示高度的落差部分,设...

网友评论

      本文标题:css等高布局

      本文链接:https://www.haomeiwen.com/subject/gzxcvltx.html