关于CSS设置水平以及垂直居中的解决方案

想要水平居中?

  • 内联的元素(文字)?

    .center-children { text-align: center;}
  • 块级元素?

    .center-me { margin: 0 auto;}
  • 如果有多个块级元素呢?

    .inline-block-center {
      text-align: center;
      }
    .inline-block-center div {
      display: inline-block;
      text-align: left;
    }
    .flex-center {
    display: flex;
    justify-content: center;
    }

想要垂直居中?

  • 内联的元素?

    • 如果只有一行的话,可以设置上下内边距为相同的值。
    • 如果是多行文本的话,可以模拟表格。
      .center-table {
      display: table;
      height: 250px;
      background: white;
      width: 240px;
      margin: 20px;
      }
      .center-table p {
      display: table-cell;
      margin:;
      background: black;
      color: white;
      padding: 20px;
      border: 10px solid white;
      vertical-align: middle;
      }
      .flex-center-vertically {
      display: flex;
      justify-content: center;
      flex-direction: column;
      height: 400px;
      }
      块级元素?
      
      知道元素高度?
      .parent {
      position: relative;
      }
      .child {
      position: absolute;
      top: 50%;
      height: 100px;
      margin-top: -50px; /* account for padding and border if not using box-sizing: border-box; */
      }
  • 元素高度未知?
    .parent {
    position: relative;
    }
    .child {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    }
  • flex布局
    .parent {
    display: flex;
    flex-direction: column;
    justify-content: center;
    }

    原文:

    http://www.jianshu.com/p/14d519bc8c30

css设置水平垂直居中的更多相关文章

  1. CSS实现水平|垂直居中漫谈

    利用CSS进行元素的水平居中,比较简单,手到擒来:行级元素设置其父元素的text-align center,块级元素设置其本身的left 和 right margins为auto即可.而撸起垂直居中, ...

  2. 纯CSS制作水平垂直居中“十字架”

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. CSS制作水平垂直居中对齐

    作为前端攻城师,在制作Web页面时都有碰到CSS制作水平垂直居中,我想大家都有研究过或者写过,特别的其中的垂直居中,更是让人烦恼.这段时间,我收集了几种不同的方式制作垂直居中方法,但每种方法各有千秋呀 ...

  4. CSS实现水平垂直居中的1010种方式

    转载自:CSS实现水平垂直居中的1010种方式 划重点,这是一道面试必考题,很多面试官都喜欢问这个问题,我就被问过好几次了 要实现上图的效果看似很简单,实则暗藏玄机,本文总结了一下CSS实现水平垂直居 ...

  5. 你知道CSS实现水平垂直居中的第10种方式吗?

    你知道CSS实现水平垂直居中的第10种方式吗? 仅居中元素定宽高适用: absolute + 负 margin absolute + margin auto absolute + calc 居中元素不 ...

  6. css实现水平 垂直居中

    css实现水平居中 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  7. CSS制作水平垂直居中对齐 多种方式各有千秋

    作为前端攻城师,在制作Web页面时都有碰到CSS制作水平垂直居中,我想大家都有研究过或者写过,特别的其中的垂直居中,更是让人烦恼.这段时间,我收 集了几种不同的方式制作垂直居中方法,但每种方法各有千秋 ...

  8. css各种水平垂直居中

    css各种水平垂直居中,网上查了一下,总结以下几种 line-height垂直居中 缺点,仅限于单行文字 .item{ text-align: center; height: 100px; line- ...

  9. CSS如何水平垂直居中?

    CSS如何水平垂直居中? 1.CSS如何实现水平居中? margin: 0 auto 2.CSS如何实现水平垂直居中? 首先设置一个div元素,设置背景颜色以便看出变化.代码如下: <!DOCT ...

随机推荐

  1. java排序算法-选择排序

    public class SelectionSort { private static void selectSortTest() { int[] sortArray = { 5, 2, 4, 1, ...

  2. Android中如何将dp,dip,sp与px相互转化

    Android中有很多度量单位:比如常用的dp,dip,sp,px等,有时候需要将他们相互转换,有下面非常方便的方法: 比如sp转换成px: TypedValue.applyDimension(Typ ...

  3. iOS CoreBluetooth 教程

    去App Store搜索并下载“LightBlue”这个App,对调试你的app和理解Core Bluetooth会很有帮助. ================================ Cor ...

  4. Android BaseAdapter ListView (明星简介列表)

    1.搭建布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" andro ...

  5. SQL给查询结果加序号

    情境:在用delphi7编程时,想要给查询出的结果一个编号,比方有一万条结果,就自己主动从1编号到10000 显示数据时用的是DBGrid控件,可是它的第一列无法非常好的显示编号,找了非常多方法都不能 ...

  6. [每日一题] 11gOCP 1z0-052 :2013-09-2 ADDM(Automatic Database Diagnostic Monitor)...................A28

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/10951207 正确答案:BC AWR简称Automatic Workload Reposi ...

  7. [Android]android.graphics.Camera实现图像的旋转、缩放,配合Matrix实现图像的倾斜

    android.graphics.Camera可以对图像执行一些比较复杂的操作,诸如旋转与绽放,与Matrix可实现图像的倾斜. 个人总结Camera与Matrix的一些区别如下: Camera的ro ...

  8. Linux之make 、makefile的使用方法

    ◊make是什么? make是一个命令工具,是一个解释makefile中指令的命令工具.它可以简化编译过程里面所下达的指令,当执行 make 时,make 会在当前的目录下搜寻 Makefile (o ...

  9. java中的异常机制(编译时异常)

    / * 1 异常机制的原理 * 异常是什么:就是错误的另外一种说法; * 在java中,有一个专门模拟所有异常的类,所有的异常都必须继承这个类:Throwable; * 本质是:当程序出错以后,jvm ...

  10. GridView、Repeater获取当前行号

    GridView: <%# Container.DataItemIndex+1 %> Repeater:<%# Container.ItemIndex+1%>