美文网首页web前端Web前端之路程序员
网页布局各种居中问题的详解

网页布局各种居中问题的详解

作者: fanyank | 来源:发表于2017-03-07 22:11 被阅读97次

水平居中

行内元素水平居中

div{
  text-align: center;
}

块级元素水平居中

div{
  margin: 0 auto;
}

多个块级元素水平居中

解决方法之一:

<div class="wrap">
        <div class="center-box">
            <h1>1</h1>
            <p>不定长的文字</p>
            <p>不定长的文字</p>
            <p>不定长的文字</p>
        </div>
        <div class="center-box">
            <h1>2</h1>
            <p>不定长的文字</p>
            <p>不定长的文字</p>
            <p>不定长的文字</p>
        </div>
        <div class="center-box">
            <h1>3</h1>
            <p>不定长的文字</p>
            <p>不定长的文字</p>
            <p>不定长的文字</p>
        </div>
</div>
.wrap{
    text-align: center;
}

.center-box{
    background-color: #e0e0e0;
    display: inline-block;
    text-align: left;
}

解决方法之二:

<div class="wrap">
        <div class="center-box">
            <h1>1</h1>
            <p>不定长的文字</p>
            <p>不定长的文字</p>
            <p>不定长的文字</p>
        </div>
        <div class="center-box">
            <h1>2</h1>
            <p>不定长的文字</p>
            <p>不定长的文字</p>
            <p>不定长的文字</p>
        </div>
        <div class="center-box">
            <h1>3</h1>
            <p>不定长的文字</p>
            <p>不定长的文字</p>
            <p>不定长的文字</p>
        </div>
</div>
.wrap{
    display:flex;
    justify-content:center;
}

垂直居中

单行行内元素垂直居中

方法之一:

div p{
  padding: 30px 0;
}

方法之二:

div{
  line-height: 30px;
}

多行行内元素垂直居中

应用场景:外围div高度固定
方法之一:

<div class="container">
    <div class="table-container">
        <span>这里是文字</span>
        <span>这里是文字</span>
        <span>这里是文字</span>
        <span>这里是文字</span>
        <span>这里是文字</span>
    </div>
</div>
.container{
    width: 200px;
    background-color: #f2dede;
    margin-top: 50px;
}
.table-container{
    width: 200px;
    height: 200px;
    display: table-cell;
    vertical-align: middle;
}

方法之二应用场景:在可以使用flex布局的情况下

<div class="wrap">
        <div class="center-box">
            <h1>1</h1>
            <p>不定长的文字</p>
            <p>不定长的文字</p>
            <p>不定长的文字</p>
        </div>
</div>
.wrap{
    display:flex;
    flex-direction: column;
    justify-content:center;
    height: 100vh;
}

方法之三:

<div class="container ghost-center">
    <p>I'm vertically centered multiple lines of text in a container. Centered with a ghost pseudo element</p>
</div>
.container{
    width: 200px;
    background-color: #f2dede;
    margin-top: 50px;
}
.ghost-center{
    height: 200px;
    position: relative;
}
.ghost-center::before{
    content: " ";
    display: inline-block;
    width: 1%;
    height: 100%;
    vertical-align: middle;
}
.ghost-center p{
  /*宽度务必要计算准确哦!*/
    width: 190px;
    vertical-align: middle;
    display: inline-block;
    margin: 0;
}

块级元素垂直居中

方法之一应用场景:块级元素高度固定

<div class="container parent">
    <div class="child">

    </div>
</div>
.container{
    width: 200px;
    background-color: #f2dede;
    margin-top: 50px;
}
.parent{
    height: 200px;
    position: relative;
    background-color: lightgrey;
}
.child{
    width: 100%;
    height: 100px;
    position: absolute;
    margin-top: -50px;
    top: 50%;
    background-color: lightblue;
}

方法之二应用场景: 块级元素高度不固定

<div class="container parent">
    <div class="child">

    </div>
</div>
.container{
    width: 200px;
    background-color: #f2dede;
    margin-top: 50px;
}
.parent{
    height: 200px;
    position: relative;
    background-color: lightgrey;
}
.child{
    width: 100%;
    position: absolute;
    transform: translateY(-50%);
    top: 50%;
    background-color: lightblue;
}

方法之三应用场景:可以使用flex布局的情况

<div class="container parent">
    <div class="child">

    </div>
</div>
.container{
    width: 200px;
    background-color: #f2dede;
    margin-top: 50px;
}
.parent{
  display: flex;
  flex-direction: column;
  justify-content: center;
}

绝对居中

高度宽度都固定

<div class="container parent">
    <div class="child">

    </div>
</div>
.container{
    width: 200px;
    height: 200px;
    background-color: #f2dede;
    margin-top: 50px;
}
.parent{
  position: relative;
}
.child{
  position: absolute;
  width: 100px;
  height: 100px;
  top: 50%;
  left: 50%;
  margin-top: -50px;
  margin-left: -50px;
  background-color: lightblue;
}

高度宽度不固定

<div class="container parent">
    <div class="child">

    </div>
</div>
.container{
    width: 200px;
    height: 200px;
    background-color: #f2dede;
    margin-top: 50px;
}
.parent{
  position: relative;
}
.child{
  position: absolute;
  width: 100px;
  height: 100px;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  background-color: lightblue;
}

使用flex布局

<div class="container parent">
    <div class="child">

    </div>
</div>
.container{
    width: 200px;
    height: 200px;
    background-color: #f2dede;
    margin-top: 50px;
}
.parent{
  display: flex;
  justify-content: center;
  align-items: center;
}

相关文章

  • 网页布局各种居中问题的详解

    水平居中 行内元素水平居中 块级元素水平居中 多个块级元素水平居中 解决方法之一: 解决方法之二: 垂直居中 单行...

  • CSS布局技巧总结

    目录 详解 CSS 七种三栏布局技巧 16种方法实现水平居中垂直居中 详解 CSS 七种三栏布局技巧 三栏布局,顾...

  • CSS 系列——Grid布局学习笔记

    Grid 布局,就是网格布局。 简单的需求,垂直居中、水平居中等,有 Flex 布局。 网格布局,对应网页设计或者...

  • CSS边用边学(一):一站式各种布局实践

    目录 布局相关属性displaypositionfloat 各种布局两栏布局三栏布局水平居中垂直居中 总结 概述 ...

  • 页面布局居中问题

    css页面布局水平垂直居中问题 居中问题

  • 如何实现垂直居中

    我们在进行网页布局的时候总会遇到需要把一个元素居中的问题,水平居中很容易就可以实现,但是垂直居中的话相对来说就会有...

  • 居中到底有多少种方法

    前言 居中可以说是网页布局中非常常见的布局了,垂直居中,水平居中,其中又分为块级元素和行内元素,没有系统的整理过还...

  • css实现居中的方法总结

    在页面布局中经常会涉及到各种居中问题,下面统计下经常用到的各种居中方法: 1.行内元素的水平居中 如果父元素是块级...

  • 前端面试重点——居中问题

    前端面试重点——居中问题 在页面布局开发中,居中问题是我们经常碰到的问题,掌握居中问题对于解决页面布局非常重要,同...

  • 2018-04-22

    最近在练习网页布局,遇到一个问题如何将 中 标签垂直居中的新认识了一种布局方式display:flex 编译之后的...

网友评论

本文标题:网页布局各种居中问题的详解

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