Summarize the solutions to the css centering problem

高洛峰
Release: 2017-03-13 15:02:02
Original
1424 people have browsed it

Problems encountered in CSS centeringSummary

Horizontal centering

  • [Inline elements] Applicable to inline, inline-block, inline-table , inline-flex element

    .center {
      text-align: center;
    }
    Copy after login
  • [Block-level element] applies to block level elements

    ①One block-level element

       .center {
     margin: 0 auto;
       }
    Copy after login

    ②Multiple blocks Level element

    方法一:将块级元素变为行内块级元素
    
    html部分:
    <main class="inline-block-center">
      <p>1</p>
      <p>2</p>
      <p>3</p>
    </main>
    css部分:
    .inline-block-center {
      text-align: center;
    }
    .inline-block-center p {
      display: inline-block;
      text-align: left;
    }
    
    
    方法二:flex布局
    
    html部分:
    <main class="flex-center">
      <p>1</p>
      <p>2</p>
      <p>3</p>
    </main>
    css部分:
    .flex-center{
        display:flex;
      justify-content:center;
    }
    Copy after login

Vertically centered

  • [Inline element]

    ①Single inline element:

    Case 1: When the link or text has wrapped elements, set equal upper and lower padding

    .link {
      padding-top: 30px;
      padding-bottom: 30px;
    }
    Copy after login

    Case 2: When the link or text is not wrapped, set the line height and height to be equal

    .center-text-trick {
      height: 100px;
      line-height: 100px
    }
    Copy after login

    ②Multiple Inline elements:

    方法一:将多个行内元素分别置于table-cell中
    
    html部分:
    <table>
      <tr>
    <td>
      1
    </td>
      </tr>
    </table>
    css部分:
    table td {
      background: black;
      color: white;
      padding: 20px;
      border: 10px solid white;
      /* default is vertical-align: middle; */
    }
    
    
    方法二:将父元素设置为display:table,将自身设置为display:table-cell
    
    html部分:
    <p class="center-table">
      <p>1</p>
    </p>
    css部分:
    .center-table {
      display: table;
      height: 250px;
      width: 240px;
    }
    .center-table p {
      display: table-cell;
      vertical-align: middle;
    }
    
    
    方法三:使用felex
    
    html部分:
    <p class="flex-center">
      <p>1</p>
    </p>
    css部分:
    .flex-center{
      display: flex;
      justify-content: center;
      flex-direction: column;
      height: 400px;/*父容器必须有固定高度*/
    }
    
    
    方法四:当以上代码均不可用时,可尝试此奇淫巧技
    
    html部分:
    <p class="ghost-center">
      <p>1</p>
    </p>
    css部分:
    .ghost-center {
      position: relative;
    }
    .ghost-center::before {
      content: " ";
      display: inline-block;
      height: 100%;
      width: 1%;
      vertical-align: middle;
    }
    .ghost-center p {
      display: inline-block;
      vertical-align: middle;
    }
    Copy after login
  • [Block-level elements]

    ①Known element height (absolute positioning+negative margin)

    .parent {
      position: relative;
    }
    .child {
      position: absolute;
      top: 50%;
      height: 100px;
      margin-top: -50px; /* 为元素高度的一半,没有box-sizing时,为height+padding+border的一半*/
    }
    Copy after login

    ②I don’t know the height of the element (similar to the previous method)

    .parent {
      position: relative;
    }
    .child {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
    }
    Copy after login

    ③flex layout

    .parent {
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
    Copy after login

centered horizontally and vertically

①Yes Elements with fixed width and height

.parent {
  position: relative;
}

.child {
  width: 300px;
  height: 100px;
  padding: 20px;

  position: absolute;
  top: 50%;
  left: 50%;

  margin: -70px 0 0 -170px;/* 注意此处为height+padding+的一半*/
}
Copy after login

②Elements without fixed width and height (same as the previous elements without fixed width and height, use transform to solve)

.parent {
  position: relative;
}
.child {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
Copy after login

③Use flexbox layout

.parent {
  display: flex;
  justify-content: center;
  align-items: center;
}
Copy after login

The above is the detailed content of Summarize the solutions to the css centering problem. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template