zoukankan      html  css  js  c++  java
  • CSS布局(三) 对齐方式

    本文是根据网上资料总结出来的文章

    水平居中

    行内元素的水平居中

    如果被设置元素为文本、图片等行内元素时,在父元素中设置text-align:center实现行内元素水平居中,将块级元素的display设置为inline-block,使块级元素变成行内元素,也可以水平居中。

    <div class="parent" style="background-color: gray;">
      <div class="child" style="background-color: lightblue;">DEMO</div>
    </div>
    
    <style>
    .parent{text-align: center;}    
    .child{display: inline-block;}
    </style>
    

    块状元素的水平居中

    1.当被设置元素为定宽块级元素时用 text-align:center 就不起作用了。可以通过设置“左右margin”值为“auto”来实现居中的。

    <div class="parent" style="background-color: gray;">
      <div class="child" style="background-color: lightblue;">DEMO</div>
    </div>
    
    .child{
          200px;
         margin: 0 auto;
    }
    

    2.为“不定宽度的块级元素”设置居中,可以直接给不定宽的块级元素设置text-align:center来实现,也可以给父元素加text-align:center 来实现居中效果。当不定宽块级元素的宽度不要占一行时,可以设置display 为 inline 类型或inline-block(设置为 行内元素 显示或行内块元素)。

    <div class="container">
        <ul>
            <li><a href="#">1</a></li>
            <li><a href="#">2</a></li>
            <li><a href="#">3</a></li>
        </ul>
    </div>
    
    .container{text-align:center;background: beige}
    .container ul{list-style:none;margin:0;padding:0;display:inline-block;}
    .container li{margin-right:8px;display:inline-block;}
    

    垂直居中

    父元素是盒子容器且高度已经设定

    1.子元素是行内元素,高度是由其内容撑开的

    设定父元素的行高(line-height)等于本身的高

    <div class="wrap line-height">
        <span class="span">111111</span>
    </div>
    
    .wrap{
                200px ;
                height: 300px;
                line-height: 300px;
                border: 2px solid #ccc;
            }
    .span{
                background: red;
            }
    

    2.子元素是块级元素但是子元素高度没有设定

    <div class="wrap">
        <div class="non-height ">11111</div>
    </div>
    
    .wrap{
           200px ;
           height: 300px;
           border: 2px solid #ccc;
      display: table-cell;
      vertical-align: middle;
    }
     .non-height{
           background: green;
    }
    

    3.子元素是块级元素且高度已经设定

    <div class="wrap ">
        <div class="div1">111111</div>
    </div>
    
    .wrap{
                200px ;
                height: 300px;
                border: 2px solid #ccc;
            }
    .div1{
                100px ;
                height: 100px;
                margin-top: 100px;
                background: darkblue;
            }
    

    水平垂直居中

    1.水平对齐+行高
    text-align + line-height实现单行文本水平垂直居中
    2.水平+垂直对齐
    text-align + vertical-align,在父元素设置text-align和vertical-align,并将父元素设置为table-cell元素,子元素设置为inline-block元素,若子元素是图像,可不使用table-cell,而是其父元素用行高替代高度,且字体大小设为0。子元素本身设置vertical-align:middle.
    3.相对+绝对定位

    <div class="parent" style="background-color: lightgray; 200px; height:100px; ">
      <div class="child" style="background-color: lightblue;">测试文字</div>
    </div>
    
    <style>
    .parent{
        position: relative;
    }
    .child{
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        height: 50px;
         80px;
        margin: auto;
    }
    </style>
    

    4.相对+绝对定位+transform

    <div class="parent" style="background-color: lightgray; 200px; height:100px; ">
      <div class="child" style="background-color: lightblue;">测试文字</div>
    </div>
    
    .parent{
        position: relative;
    }
    .child{
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
    }
    

    5.flex

    <div class="parent" style="background-color: lightgray; 200px; height:100px; ">
      <div class="child" style="background-color: lightblue;">测试文字</div>
    </div>
    
    .parent{
        display: flex;
        justify-content: center;
        align-items: center;
    }
    
  • 相关阅读:
    模块
    time/datetime/random/string/os/sys/shutil/zipfile/tarfile
    模块
    模块
    模块
    2.1
    1.4
    生成器 迭代器
    闭包 装饰器
    函数
  • 原文地址:https://www.cnblogs.com/jadedoo/p/10201042.html
Copyright © 2011-2022 走看看