美文网首页前端开发JS
居中解决方案荟萃

居中解决方案荟萃

作者: 前端进阶之旅 | 来源:发表于2016-11-15 09:48 被阅读188次

原文链接 http://blog.poetries.top/2016/11/12/CSS-center-methods/

在线预览 http://sandbox.runjs.cn/show/oljhupi9

水平居中方案


方案一:text-align + inline-block


<div id="parent1">
    <div class="child">水平居中</div>
</div>
#parent1{
    text-align: center;
    background:#ddd;
    margin-bottom:20px;
}
#parent1 .child{
    display: inline-block;
    background:#666;
    color:#fff;
}

方案二:margin:0 auto


<div id="parent2">
    <div class="child">水平居中</div>
</div>
#parent2{
    text-align: center;
    background:#ddd;
    margin-bottom:20px;
}
#parent2 .child{
    display: table;
    margin: 0 auto;
    background:#666;
    color:#fff;
}

方案三:absolute+transform


<div id="parent3">
    <div class="child">水平居中</div>
</div>
#parent3{
    position: relative;
    background:#ddd;
    margin-bottom:20px;
}
#parent3 .child{
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    background:#666;
    color:#fff;
}


方案四:justify-content


<div id="parent4">
    <div class="child">水平居中</div>
</div>
#parent4{
    display: flex;
    justify-content: center;
    background:#ddd;
    margin-bottom:20px;
}
#parent4 .child{
    margin:0 auto;
    background:#666;
    color:#fff;
}

垂直居中方案


方案一: 利用 line-height 实现垂直居中


  • 这种方法适用于单行文本垂直居中,如果文本内容太长,出现了换行,换行后的内容会溢出
<div id="example1">
    单行文字垂直居中
</div>
#example1 {
    height: 100px;
    line-height: 100px;
    background: #161616;
    color: #fff;
    width: 200px;
}

方案二 利用 display: table 实现垂直居中


<div id="example2">
    <div class="inner">块区域垂直居中</div>
</div>
#example2 {
    height: 100px;
    background: #161616;
    color: #fff;
    width: 400px;
    overflow: hidden;
    display: table;
            margin-bottom:20px;
}
#example2 .inner{
    display: table-cell;
    vertical-align: middle;
    height: 50px;
    background:#999;
}

方案三 margin 填充 这种方法需要知道内外容器的大小


<div id="example3">
    <div class="inner">块区域垂直居中</div>
</div>
#example3 {
    height: 100px;
    background: #161616;
    color: #fff;
    width: 400px;
    overflow: hidden;
            margin-bottom:20px;
}
#example3 .inner{
    margin-left: auto;
    margin-right: auto;
    margin-top: calc((100px - 50px)/2);
    height: 50px;
    background:#999;
}

方案四:经典 absolute 布局上下文垂直居中


<div id="example4">
    <div class="inner">块区域垂直居中</div>
</div>
#example4 {
    width: 400px;
    height: 100px;
    background: #161616;
    color: #fff;
    position: relative;
        margin-bottom:20px;
}
#example4 .inner{
    height: 50px;
    width: 200px;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-top: -25px;
    margin-left: -100px;
    background:#999;
}

方案五:absolute+transform


<div id="example5">
    <div class="inner">块区域垂直居中</div>
</div>
#example5 {
    width: 400px;
    height: 100px;
    background: #161616;
    color: #fff;
    position: relative;
    margin-bottom:20px;
}
#example5 .inner{
    position: absolute;
    left: 50%;
    top: 50%;
    background: #999;
    transform: translateX(-50%) translateY(-50%);
}

方案六 利用margin:auto 居中


<div id="expample6">
    <div class="inner">Content here</div>
</div>
#expample6 {
    width: 400px;
    height: 100px;
    background: #eee;
    position: relative;
        margin-bottom:20px;
}

#expample6 .inner {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
    height: 50px;
    width: 70%;
    background: #aaa;
    color:#222;
}

方案七 利用 Flex布局 居中


<div id="expample7">
    <div class="inner">Content here</div>
</div>
#expample7 {
    width: 400px;
    height: 100px;
    background: #eee;
    display: flex;
    justify-content: center;
    align-items: center;
}

#expample7 .inner {
    height: 50px;
    width: 70%;
    background: #aaa;
    color:#222;
}

相关文章

  • 居中解决方案荟萃

    原文链接 http://blog.poetries.top/2016/11/12/CSS-center-metho...

  • 元素居中解决方案

    html: css:1水平居中:解决方案1: 解决方案2: 解决方案3: 2垂直居中:解决方案1: 解决方案2: ...

  • 前端之布局解决方案

    总结在前,代码在后 文章讲解了水平居中、垂直居中和居中布局解决方案,其实不难发现,在所有这些解决方案里面,都是利用...

  • 页面架构

    布局解决方案 水平居中布局 垂直居中布局 水平垂直都居中的布局 多列布局 多列等分布局 多列等高布局 在多列布局的...

  • 前端常用布局

    1、水平居中 方法一:margin:0 auto; 是最常见的水平居中解决方案,但有其局限性:块级元素,已知宽度,...

  • css 的 伸缩盒布局

    1、垂直居中[1] 垂直居中的实现很常见,下面介绍三种比较便捷的实现方案: 1.1 基于绝对定位的解决方案[2] ...

  • 面试之道 - 元素实现水平垂直居中的CSS解决方案

    声明:文章转载自《面试之道 - 元素实现水平垂直居中的CSS解决方案》 写出几种常见的水平垂直居中方式。 这是一个...

  • 3、单行文字垂直居中的代码

    单行文字垂直居中的代码 解决方案:让文字的行高等于盒子的高度 就可以让文字在当前盒子内垂直居中。

  • CSS居中解决方案

    整理一下常用的CSS居中方式~~~ 1、水平居中 块级元素:margin: 0 auto行内元素:text-ali...

  • 元素div 上下左右居中方法总结

    元素div 上下左右居中方法总结 情景一:一个浏览器页面中,只有一个div模块,让其上下左右居中 解决方案: { ...

网友评论

    本文标题:居中解决方案荟萃

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