水平方向と垂直方向の中央に配置(テキスト、ボックス)

1.テキスト

1.1行のテキスト

コード:

<!DOCTYPE html>
<html>

<head>
  <style>

    div {
     
     
      width: 200px;
      height: 100px;
      border: 1px solid black;
      text-align: center;
      background-color: greenyellow;
    }

    span {
     
     
      line-height: 100px;
    }
  </style>
</head>

<body>

  <div><span>单行文本</span></div>

</body>

</html>


結果を示す:
ここに画像の説明を挿入

2.複数行のテキスト

コード:

<!DOCTYPE html>
<html>

<head>
  <style>

    div {
     
     
      width: 200px;
      height: 100px;
      border: 1px solid black;
      text-align: center;
      display: table;
      background-color: greenyellow;
    }

    span {
     
     
      display: table-cell;
      vertical-align: middle;
    }

  </style>
</head>

<body>

  <div><span>多行文本多行文本多行文本多行文本多行文本多行文本多行文本多行文本</span></div>

</body>

</html>

結果を示す:
ここに画像の説明を挿入

第二に、ボックス

1、フレックス

コード:

<!DOCTYPE html>
<html>

<head>
  <style>

    .container {
     
     
      width: 300px;
      height: 300px;
      border: 1px solid black;
      background-color: greenyellow;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .item {
     
     
      width: 100px;
      height: 100px;
      background-color: red;
      border: 1px solid black;
    }

  </style>
</head>

<body>

  <div class="container">
    <div class="item"></div>
  </div>

</body>

</html>

結果を示す:
ここに画像の説明を挿入

2、ポジション+マージン
(1)最初の

適用先:現在のdivの幅と高さを把握します。

コード:

<!DOCTYPE html>
<html>

<head>
  <style>

    .container {
     
     
      width: 300px;
      height: 300px;
      border: 1px solid black;
      background-color: greenyellow;
      position: relative;
    }

    .item {
     
     
      width: 100px;
      height: 100px;
      background-color: red;
      border: 1px solid black;
      position: absolute;
      top: 50%;
      left: 50%;
      margin-left: -50px;
      margin-top: -50px;
    }

  </style>
</head>

<body>

  <div class="container">
    <div class="item"></div>
  </div>

</body>

</html>

結果を示す:
ここに画像の説明を挿入

(2)2番目

コード:

<!DOCTYPE html>
<html>

<head>
  <style>

    .container {
     
     
      width: 300px;
      height: 300px;
      border: 1px solid black;
      background-color: greenyellow;
      position: relative;
    }

    .item {
     
     
      width: 100px;
      height: 100px;
      background-color: red;
      border: 1px solid black;
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      margin: auto;
    }

  </style>
</head>

<body>

  <div class="container">
    <div class="item"></div>
  </div>

</body>

</html>

結果を示す:
ここに画像の説明を挿入

3、位置+変換

コード:

<!DOCTYPE html>
<html>

<head>
  <style>

    .container {
     
     
      width: 300px;
      height: 300px;
      border: 1px solid black;
      background-color: greenyellow;
      position: relative;
    }

    .item {
     
     
      width: 100px;
      height: 100px;
      background-color: red;
      border: 1px solid black;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%,-50%)
      /* 往左,往上移动自身长度的一半 */
    }

  </style>
</head>

<body>

  <div class="container">
    <div class="item"></div>
  </div>

</body>

</html>

結果を示す:
ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/weixin_44645309/article/details/112068396