几道 CSS 布局题目

157 阅读1分钟

用三种方法实现两列布局,左边固定宽度,右边自适应剩余的宽度。

1. flex 方式

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
      .box {
        height: 200px;
        display: flex;
      }
      .left {
        background-color: red;
        height: 100%;
        width: 200px;
      }
      .content {
        background-color: yellow;
        height: 100%;
        flex: 1;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="left"></div>
      <div class="content"></div>
    </div>
  </body>
</html>

2. 绝对定位方式

<style>
      .box {
        height: 200px;
        position: relative;
      }
      .left {
        background-color: red;
        height: 100%;
        width: 200px;
        position: absolute;
        left: 0;
      }
      .content {
        background-color: yellow;
        height: 100%;
        margin-left: 200px;
      }
</style>

3. 浮动方式

<style>
      .box {
        height: 200px;
        position: relative;
      }
      .left {
        background-color: red;
        height: 100%;
        width: 200px;
        float: left;
      }
      .content {
        background-color: yellow;
        height: 100%;
        margin-left: 200px;
      }
</style>

CSS 实现三栏布局,左右固定,中间自适应。

1. flex 方式

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
      .box {
        height: 200px;
        display: flex;
      }
      .left {
            width: 200px;
            background-color: red;
            height: 100%;
        }
        .content {
            background-color: yellow;
            flex: 1;
        }
        .right {
            width: 200px;
            background-color: green;
        }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="left"></div>
      <div class="content"></div>
      <div class="right"></div>
    </div>
  </body>
</html>

2. 绝对定位方式

<style>
      .box {
            position: relative;
            height: 200px;
        }
        .left {
            width: 200px;
            background-color: red;
            left: 0;
            height: 100%;
            position: absolute;
        }
        .content {
            background-color: yellow;
            left: 200px;
            right: 200px;
            height: 100%;
            position: absolute;
        }
        .right {
            width: 200px;
            background-color: green;
            right: 0;
            height: 100%;
            position: absolute;
        }
</style>

3. 浮动方式

<style>
      .box {
            height: 200px;
        }
        .left {
            width: 200px;
            background-color: red;
            float: left;
            height: 100%;
        }
        .content {
            background-color: yellow;
            height: 100%;
        }
        .right {
            width: 200px;
            background-color: green;
            float: right;
            height: 100%;
        }
</style>

让一个div垂直居中。

宽度和高度已知的

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            width: 400px;
            height: 200px;
            position: relative;
            background: red;
        }
        .content {
            width: 200px;
            height: 100px;
            position: absolute;
            top: 50%;
            left: 50%;
            margin-left: -100px;
            margin-top: -50px;
            background: green;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="content"></div>
    </div>
</body>
</html>

宽度和高度未知的

<style>
        .box {
            width: 400px;
            height: 200px;
            position: relative;
            background: red;
        }
        .content {
            width: 200px;
            height: 100px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: green;
        }
    </style>

flex 方式

<style>
        .box {
            width: 400px;
            height: 200px;
            background: red;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .content {
            width: 200px;
            height: 100px;
            background: green;
        }
    </style>