CSS常用布局实现03-水平垂直居中

1. 简介

其实,水平居中和垂直居中都是水平垂直居中的一部分,所以这一章节所讲到的方法稍微改下就可用于前面两章。说道水平垂直居中,那么居中的元素肯定是有宽度和高度的,要么是指定宽高,要么就是自适应的宽高。

2. 已知宽高

这种情况比较简单,因为已知宽高,根据宽高就能做很多处理。这里我只记录一种方法。
使用 -margin + absolute(relative):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>负margin + absolute</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .wrap {
            position: relative;
            background: gray;
            height: 300px;
        }
        .inner {
            background: blue;
            width: 200px;
            height: 100px;
            position: absolute;
            top: 50%;
            left: 50%;
            margin-top: -50px;
            margin-left: -100px;
        }
    </style>
</head>
<body>

<div class="wrap">
    <div class="inner">inner content</div>
</div>
</body>
</html>
image.png

当然,这里使用这个方法的简化方法也可以,就是不设置父元素为absolute,设置子元素为relative,其余保持一样。

3. 未知宽高

3.1 使用 -margin + absolute:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>负margin + absolute</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .wrap {
            position: relative;
            background: gray;
            height: 300px;
        }
        .inner {
            background: blue;
            width: 200px;
            height: 100px;
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            margin: auto auto;
        }
    </style>
</head>
<body>

<div class="wrap">
    <div class="inner">inner content</div>
</div>
</body>
</html>

这种方法很巧妙,使用也很多。IE7及之前版本不支持。

3.2 flex

是的,flex,flex,flex。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>flex</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .wrap {
            background: gray;
            display: flex;
            height: 60px;
            align-items: center;
            justify-content: center;
        }
        .inner {
            background: blue;
        }
    </style>
</head>
<body>

<div class="wrap">
    <div class="inner">inner content</div>
</div>
</body>
</html>

3.3 grid

还有grid,grid,grid。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>grid</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .wrap {
            background: gray;
            display: grid;
            height: 60px;
            align-items: center;
            justify-content: center;
        }
        .inner {
            background: blue;
        }
    </style>
</head>
<body>

<div class="wrap">
    <div class="inner">inner content</div>
</div>
</body>
</html>

3.4 table

当然,这种情景使用table也很方便。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>table</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .wrap {
            display: table-cell;
            background: gray;
            height: 100px;
            width: 300px;
            text-align: center;
            vertical-align: middle;
        }
        .inner {
            background: blue;
        }
    </style>
</head>
<body>

<div class="wrap">
    <div class="inner">
        inner content
    </div>
</div>
</body>
</html>

ie8以后兼容。


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,816评论 1 92
  • 收听音频,戳链接,旧号itclan已暂停使用,欢迎关注微信itclanCoder公众号可收听更多音频 前言 关于网...
    itclanCoder阅读 8,222评论 3 30
  • 前言 温馨提示:本文较长,图片较多,本来是想写一篇 CSS 布局方式的,但是奈何 CSS 布局方式种类太多并且实现...
    sunshine小小倩阅读 3,182评论 0 59
  • 1.绝对定位居中技术 我们一直用margin:auto实现水平居中,而一直认为margin:auto不能实现垂直居...
    DecadeHeart阅读 1,631评论 0 3
  • 一开始没什么心情看的,后来看完了,完全被郭富城和王千源征服了。 先了解下剧情: 在不普通的一天里,高见翔警官先是历...
    大脸猩猩阅读 421评论 0 0