在页面布局中,怎么实现水平居中和垂直居中?

摘要:
首先,给出DOM结构一个:如果水平中心是内联元素,请将文本align:center设置为其父元素,以实现内联元素的水平中心是块级元素,将margin:0Auto设置为块级元素、将父元素设置为灵活布局,并将margin:0Auto设置为子元素。1.使用flex2012版本布局,可以轻松实现水平居中,子元素设置如下:。box{width:200px;height:200px;display:flex;//使内部flex项水平居中对齐content:center;background-color:pin;}2。使用绝对定位和CSS3的新属性进行转换。框{width:200px;height:200px;position:relative;background-color:pin;}。box center{position:absolute;left:50%;//width:50%;height:100%;//通过transfer slate()方法,元素从其当前位置移动,并根据给定的左侧(x坐标)和顶部(y坐标)位置p定义2D转换arameters://translate(x,y)。

先给出DOM结构

<div class="box"><div class="box-center">
     </div>
</div>

一:水平居中

若是行内元素,给其父元素设置text-align:center即可实现行内元素水平居中

若是块级元素,该元素设置margin:0 auto即可(元素需要定宽)

若是块级元素,设置父元素为flex布局,子元素设置margin:0 auto即可(子元素不需要定宽)

1.使用flex 2012年版本布局,可以轻松的实现水平居中,子元素设置如下:

.box {
      width: 200px;
      height: 200px;
      display: flex;
      // 使内部的flex项目水平居中
      justify-content: center;
      background-color: pink;
}

2.使用绝对定位和CSS3新增的属性transform(这个属性还和GPU硬件加速、固定定位相关)

.box {
      width: 200px;
      height: 200px;
      position: relative;
      background-color: pink;
    }
    
    .box-center {
      position: absolute;
      left:50%;
      // width: 50%;
      height: 100%;
      // 通过 translate() 方法,元素从其当前位置移动,根据给定的 left(x 坐标) 和 top(y 坐标) 位置参数:
      // translate(x,y)    定义 2D 转换。
      // translateX(x)    定义转换,只是用 X 轴的值。
      // translateY(y)    定义转换,只是用 Y 轴的值。
      // left: 50% 先整体向父容器的左侧偏移50%,此时是不能居中的,因为元素本身有大小
      // 接着使用transform使用百分比向左偏移本身的宽度的一半实现水平居中(这里的百分比以元素本身的宽高为基准)
      transform:translate(-50%,0);
      background-color: greenyellow;
    }

3.使用绝对定位和margin-left(元素定宽)

.box {
      width: 200px;
      height: 200px;
      position: relative;
      background-color: pink;
    }
    
    .box-center {
      position: absolute;
      left:50%;
      height: 100%;
      // 类似于transform// width: 50%;// margin-left: -25%;
      width: 100px;
      margin-left: -50px;
      background-color: greenyellow;
    }

二:垂直居中

若元素是单行文本, 则可设置line-height等于父元素高度

若是块级元素,设置父元素为flex布局,子元素设置margin: auto 0即可(子元素不需要定宽)

1若元素是行内块级元素,基本思想是使用display: inline-block, vertical-align: middle和一个伪元素让内容块处于容器中央:

    .box {
      height: 100px;
    }
    
    .box::after, .box-center{
      display:inline-block;
      vertical-align:middle;
    }
    .box::after{
      content:'';
      height:100%;
    }

2.当居中元素高度不定时,

2.1可用 vertical-align 属性(vertical-align只有在父层为 td 或者 th 时才会生效,,对于其他块级元素,例如 div、p 等,默认情况是不支持的),为了使用vertical-align,我们需要设置父元素display:table, 子元素 display:table-cell;vertical-align:middle

.box {
      height: 100px;
      display: table;
    }
    
     .box-center{
        display: table-cell;
        vertical-align:middle;
    }

2.2.可用 Flex 2012版, 这是CSS布局未来的趋势。Flexbox是CSS3新增属性,设计初衷是为了解决像垂直居中这样的常见布局问题:优点:内容块的宽高任意, 优雅的溢出.  可用于更复杂高级的布局技术中.    缺点:IE8/IE9不支持、需要浏览器厂商前缀、渲染上可能会有一些问题。↓

.box {
      height: 100px;
      display: flex;
      align-items: center;
    }

2.3.可用 transform ,设置父元素相对定位:缺点:IE8不支持, 属性需要追加浏览器厂商前缀,可能干扰其他 transform 效果,某些情形下会出现文本或元素边界渲染模糊的现象。

  .box {
      height: 100px;
      position: relative;
      background-color: pink;
    }
    
    .box-center {
      position: absolute;
      top: 50%;
      transform: translate(0, -50%);
      background-color: greenyellow;
    }

3.居中元素高度固定时

3.1设置父元素相对定位,子元素如下css样式:

.box {
      position:relative;
      height: 100px;
      background-color: pink;
    }
    
    .box-center{
      position:absolute;
      top:50%;
      // 注意不能使用百分比// margin的百分比计算是相对于父容器的width来计算的,甚至包括margin-top和margin-bottom
      height: 50px;
      margin-top: -25px;
    }

3.2设置父元素相对定位, 子元素如下css样式:

.box {
      position:relative;
      width: 200px;
      height: 200px;
      background-color: pink;
    }
    
    .box-center{
      position:absolute;
      top: 0;
      bottom: 0;
      margin: auto 0;
      height: 100px;
      background-color: greenyellow;
    }

水平垂直居中

Flex布局(子元素是块级元素)

.box {
      display: flex;
      width: 100px;
      height: 100px;
      background-color: pink;
    }
    
    .box-center{
      margin: auto;
      background-color: greenyellow;
    }
```css

- Flex布局
```css
    .box {
      display: flex;
      width: 100px;
      height: 100px;
      background-color: pink;
      justify-content: center;
      align-items: center;
    }
    
    .box-center{
      background-color: greenyellow;
    }

绝对定位实现(定位元素定宽定高)

.box {
      position: relative;
      height: 100px;
      width: 100px;
      background-color: pink;
    }
    
    .box-center{
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
      top: 0;
      margin: auto;
      width: 50px;
      height: 50px;
      background-color: greenyellow;
    }

---------------------------

免责声明:文章转载自《在页面布局中,怎么实现水平居中和垂直居中?》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇hadoop三个配置文件的参数含义说明the import javax.jms cannot be resolved问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

css实现步骤条

实现效果 html <ul class="steps"> <li class="active">申请完成</li> <li class="active">资料上传</li> <li>专员审核</...

web打印,web打印控件的三种实现方法

做管理系统的时候,打印一直是个棘手的问题,做B/S的系统这个问题就更加突出了!下面举出三种常用的web打印处理方式1、利用word或者excel来实现web打印(如果不修改ie设置,可以在web服务器端生成xls文件,然后通过xlBook = xls.Workbooks.Open(remotePath) 获取对象打印)   实现过程:先将需要打印的数据导入...

CSS之显示天气

 这个可以有,自从有了这个,以后查询天气就方便多了,哈哈。 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <head> <titl...

z-index 应用简单总结

做过页面布局的同学对z-index属性应该是很熟悉了,z-index是针对网页显示中的一个特殊属性。因为显示器是显示的图案是一个二维平面,拥有x轴和y轴来表示位置属性。为了表示三维立体的概念如显示元素的上下层的叠加顺序引入了z-index属性来表示z轴的区别。表示一个元素在叠加顺序上的上下立体关系。 z-index值较大的元素将叠加在z-index值较小的...

学习CSS之用CSS绘制一些基本图形

一、三角形 如下图,通过设置 border 的大小和颜色可以形成四个三角形: 上图对应的代码为: /*三角形*/ .triangle{ 0; height:0; border-top:100pxsolidgreen; border-right:100pxsolidred; border-bottom:100pxsolidblue; border-lef...

4、HTML和CSS进阶知识

HTML和CSS进阶知识 一、HTML中常用的块元素(block)和行内元素(inline)   常见的块元素(block):p、h1--h6、div、ul、ol、dl、hr   常见的行内元素(inline):b、i、em、strong、small、sub、sup、a、span、br、   在css中,可以通过display属性实现block元素和inl...