美文网首页
常用的居中方法

常用的居中方法

作者: EWL | 来源:发表于2017-10-08 22:20 被阅读0次

水平居中
根据子元素为行内元素还是块状元素,宽度一定还是宽度未定的变化,采取的布局方案不同。

  1. 子元素是行内元素

HTML:

<div class="father">
    <span class="child-inline">行内元素水平居中</span>
</div>

CSS:

.father{
      width: 100%;
       height: 100px;
       text-align: center;
       background: #666;
}
  1. 子元素是无宽度块级元素

HTML:

<div class="father">
    <div class="child-noWidth">我是不定宽的块级元素</div>
</div>

CSS:

.father{
text-align: center;
}

.father .child-noWidth{
display: inline;
}
  1. 子元素是有宽度的块级元素

HTML:

<div class="father">
    <div class="child-width">定宽块级元素水平居中</div>
</div>

CSS:

.child-width{
width: 200px;
margin:0 auto;
}
  1. 对以上情况都通用的方法:flex布局(可爱的自适应布局)

HTML:

<div class="father">
    <div class="child ">我是子元素</div>
</div>

CSS:

.father{
            display: flex;
            justify-content: center;
            background: lightblue;
            margin-top: 20px;
        }

垂直居中:
分为多行内联文本,单行内联文本以及块级元素。

  1. 单行内联文本(保证行高与元素高度一致)

HTML:

<div class="container">
    <span>我是只有一行的小文本</span>
</div>

CSS:

.container{
height: 30px;
line-height: 30px;
}
  1. 多行内联文本

HTML:

<div class="container">
    <span>我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本我是有多行的大文本</span>
</div>

CSS:

.container{
            width: 100%;
            height: 200px;
            background: lightblue;
            display: table-cell;
            vertical-align: middle;
        }

3.块级元素(借助position)

HTML:

<div class="f">
    <div class="c">hello</div>
</div>

CSS:

        .f{
            position: relative;
            width: 100%;
            height:200px;
            background: lightblue;
        }
        .c{
            position: absolute;
            width: 100%;
            height: 100px;
            top: -50px;
            margin-top: 100px;
            background: lightpink;
        }

先以父元素高度的一半作为margin-top的值,再将top设置为负的自身高度的一半,即可对准父元素的中心基准线

  1. 对以上情况都通用的方法: flex布局(可爱的自适应布局)

HTML:

<div class="father">
    <div class="child">hello,this is child-element</div>
</div>

<div class="father">
    <span class="child">hello,this is child-element</span>
</div>

CSS:

        .father
           {
            margin-top: 10px;
            height: 50px;
            width: 100%;
            /*通用的垂直居中*/
            display: flex;
            align-items:center;
            background: lightblue;
          }
        .child
          {
            background: lightpink;
          }

相关文章

  • css居中总结

      css居中是布局中常见的方法,现将常用方法总结如下: 1.水平居中 1.1.使用inline-block + ...

  • css垂直水平居中显示的解决办法

    前端经常遇到居中定位的问题,今天总结集中水平垂直居中的方法,我常用的方法有5种,这5种方法针对不同情况做居中,这些...

  • css居中问题

    常用居中方法 居中在布局中很常见,我们假设DOM文档结构如下,子元素要在父元素中居中: 水平居中 子元素为行内元素...

  • CSS垂直居中,你会多少种写法?

    CSS控制居中是前端开发中非常常用的布局技能,本文列出几种CSS控制元素居中的几种方法。  谈及HTML元素居中展...

  • web前端教程:CSS 布局十八般武艺都在这里了

    CSS布局 布局是CSS中一个重要部分,本文总结了CSS布局中的常用技巧,包括常用的水平居中、垂直居中方法,以及单...

  • CSS居中的方法总结

    CSS水平和垂直居中在开发中经常用到,在此加以总结。 水平居中方法 1.行内元素水平居中,设置父元素的text-a...

  • 常用的居中方法

    水平居中:根据子元素为行内元素还是块状元素,宽度一定还是宽度未定的变化,采取的布局方案不同。 子元素是行内元素 H...

  • 常用的居中方法

    本文将介绍的居中方法有 ,水平居中,垂直居中和水平垂直居中。 一水平居中 二水平垂直居中

  • 常见的CSS居中及布局方法

    css居中是前端开发中常用方法,掌握各种居中的方法可以提高开发效率,对准确的还原设计页面有很大的帮助。本文章整理了...

  • CSS div居中的几种方法

    CSS实现div垂直居中的方法有很多,下面div居中的几种方法是自己平时写网页中经常用到的,最常见的例子就是登录注...

网友评论

      本文标题:常用的居中方法

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