行内块元素

行内块元素实现单列布局

1. 前言

利用元素的行内元素特性我们可以很轻松的做到水平居中。

行内块的语法格式也很简单:display: inline-block;

2. 实例代码

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    /* 清除默认样式 */
    * { padding: ; margin: ; }

    /* 令html和body全屏显示, 并有一个灰色背景 */
    html, body { height: ; background: gray; }

    body {
      /* 令子元素水平居中 */
      text-align: center;
      
      /* 灰色背景 */
      background: gray;
    }

    .center {
      /* 令其显示为行内块元素 */
      display: inline-block;

      /* 给个宽高方便查看 */
      width: ;
      height: ;

      /* 白色背景 */
      background: white;
    }
  </style>
</head>
<body>
  <div class="center"></div>
</body>
</html>

运行结果:

编程之家

3. 小结

display: inline-block; 可以让元素既拥有行内元素的特性,同时又拥有块级元素的特性。

不过看了这么多种方式的单列布局,可是看起来依然还是很抽象。

换句话说就是不好看,那么下一小节我们就为其添油加醋一下,使其看起来更加贴合实际。