zoukankan      html  css  js  c++  java
  • 关于用css实现文本和图片垂直水平居中

     

    关于用css实现文本和图片垂直水平居中

     

    一直相信好记性不如烂笔头,最近遇到很多用到垂直居中的,整理一下以便日后查阅。

    一、文本垂直水平居中

    1、水平居中:

      文字水平居中没什么好说的,用text-align: center;即可很容易的实现。


    2、垂直居中:

      1)简单的单行文本 

      利用line-height==height,使得单行文本垂直居中。

    1 <div>
    2     垂直水平居中
    3 </div>
    1 div{
    2      200px;
    3     height: 200px;
    4     text-align: center;
    5     line-height: 200px;
    6     background:#1AFF00;
    7 }

      

      简单点来说,用p标签就可以,就像这样 

    <p>垂直水平居中</p>
    1 p{
    2      200px;
    3     height: 200px;
    4     text-align: center;
    5     line-height: 200px;
    6     background:#00ABFF;
    7 }

       效果如下:

             


      2)多行文本

      利用表格元素的特性,块级父元素display: table;内联元素display: table-cell;vertical-align: middle; 

    (内联)

    1 <div
    2     <span>国际《儿童权利公约》界定的儿童系指18岁以下的任何人</span>
    3 </div>
     1 div{
     2      200px;
     3     height: 200px;
     4     display: table;
     5     background:#1AFF00;
     6 }
     7 span{
     8     display: table-cell;
     9     vertical-align: middle;
    10 }

    (块级)

    1 <div>
    2     <p>国际《儿童权利公约》界定的儿童系指18岁以下的任何人</p>
    3 </div>

     

      定位+transform: translateY(-50%); 

     1 div{
     2     position: relative;
     3      200px;
     4     height: 200px;
     5     background: #00ABFF;
     6 }
     7 p{
     8     position: absolute;
     9     top: 50%;
    10     left: 0;
    11      200px;
    12     height: 64px;
    13     transform: translateY(-50%);
    14 }

      定位+margin负值

     1 div{
     2     position: relative;
     3      200px;
     4     height: 200px;
     5     background:#1AFF00;
     6 }
     7 p{
     8     position: absolute;
     9     top: 50%;
    10     left: 0;
    11      200px;
    12     height: 64px;
    13     margin-top: -32px;
    14 }

      定位+margin: auto;

     1 div{
     2     position: relative;
     3      200px;
     4     height: 200px;
     5     background:#00ABFF;
     6 }
     7 p{
     8     position: absolute;
     9     top: 0;
    10     left: 0;
    11     right: 0;
    12     bottom: 0;
    13     margin: auto;
    14      200px;
    15     height: 64px;
    16 }

      两者都是定宽定高,父元素用padding值,此值为父元素高度减去子元素高度的一半

     1 div{
     2      200px;
     3     height: 64px;
     4     padding: 68px 0;
     5     background:#1AFF00;
     6 }
     7 p{
     8      200px;
     9     height: 64px;
    10 }

      两者都是定宽定高,父元素用overflow: hidden;(css hack)子元素用margin值,此值为父元素高度减去子元素高度的一半

     1 div{
     2      200px;
     3     height: 200px;
     4     overflow: hidden;
     5     background:#00ABFF;
     6 }
     7 p{
     8      200px;
     9     height: 64px;
    10     margin:68px auto;
    11 }

       效果如下:

      


    二、图片垂直水平居中

    1、水平居中

      1)img在css初始设置中是inline-block,行内块元素,此时若是要水平居中用text-align:center;

      2)给img元素设置display:block;转换为块级元素,要想水平居中用margin:0 auto;


    2、垂直居中

       1.jpg

      用这张图片做示范

    1 <div>
    2     <img alt="" src="1.jpg" />
    3 </div>

       

      line-height==height  vertical-align: middle;

     1 div{
     2      198px;
     3     height: 198px;
     4     text-align: center;
     5     line-height: 198px;
     6     border: 1px solid #8900FF;
     7 }
     8 img{
     9     vertical-align: middle;
    10 }

      display: table-cell;vertical-align: middle;

     1 div{
     2     display: table-cell;
     3     vertical-align: middle;
     4      198px;
     5     height: 198px;
     6     border: 1px solid #8900FF;
     7 }
     8 img{
     9     display: block;
    10     margin: 0 auto;
    11 }

      

      display: table-cell;vertical-align: middle; text-align: center;

    1 div{
    2     display: table-cell;
    3     vertical-align: middle;
    4     text-align: center;
    5      198px;
    6     height: 198px;
    7     border: 1px solid #8900FF;
    8 }

      定位+display: block;transform: translate(-50%,-50%);

     1 div{
     2     position: relative;
     3      198px;
     4     height: 198px;
     5     border: 1px solid #8900FF;
     6 }
     7 img{
     8     position: absolute;
     9     top: 50%;
    10     left: 50%;
    11     transform: translate(-50%,-50%);
    12     display: block;
    13 }

      定位+margin负值

     1 div{
     2     position: relative;
     3      198px;
     4     height: 198px;
     5     border: 1px solid #8900FF;
     6 }
     7 img{
     8     position: absolute;
     9     top: 50%;
    10     left: 50%;
    11     margin: -75px 0 0 -75px;
    12 }

      定位+margin: auto;

     1 div{
     2     position: relative;
     3      198px;
     4     height: 198px;
     5     border: 1px solid #8900FF;
     6 }
     7 img{
     8     position: absolute;
     9     top: 0;
    10     left: 0;
    11     right: 0;
    12     bottom: 0;
    13     margin: auto;
    14     display: block;
    15 }

       overflow: hidden;margin值

     1 div{
     2      198px;
     3     height: 198px;
     4     overflow: hidden;
     5     border: 1px solid #8900FF;
     6 }
     7 img{ 8     margin: 25px;
     9 }

      padding值

    1 div{
    2     padding: 25px;
    3      148px;
    4     height: 148px;5     border: 1px solid #8900FF;
    6 }

     


     

      用table的属性+vertical-align: middle;实现

    1 <div>
    2     <span><img alt="" src="1.jpg" /></span>
    3 </div>
     1 div{
     2     display: table;
     3      198px;
     4     height: 198px;
     5     text-align: center;
     6     border: 1px solid #8900FF;
     7 }
     8 span{
     9     display:table-cell;
    10     vertical-align: middle;
    11 }

      用background实现

    1 <div></div>
    1 div{
    2      198px;
    3     height: 198px;
    4     border: 1px dashed #8900FF;
    5     background: url(1.jpg) no-repeat center center;
    6 }

      效果如下:

      

       此篇完!!!

  • 相关阅读:
    Vue 组件之间传值(父子组件传值,vuex传值)
    利用computed和watch实现监听Vuex状态监听
    ESlint+VSCode自动格式化
    MySQL之分组查询(DQL)
    MySQL之排序查询(DQL)
    MySQL之条件查询(DQL)
    MySQL之概述
    jQuery之轮播图
    jQuery之添加删除记录
    jQuery之爱好选择
  • 原文地址:https://www.cnblogs.com/Ni-F/p/6937931.html
Copyright © 2011-2022 走看看