美文网首页
小薇学院任务四笔记2(居中)

小薇学院任务四笔记2(居中)

作者: garble | 来源:发表于2017-03-04 19:31 被阅读0次

    1.You can center inline elements horizontally, within a block-level parent element, with just:

    HTML
    <header>
      This text is centered.
    </header>
    
    <nav role='navigation'>
      <a href="#0">One</a>
      <a href="#0">Two</a>
      <a href="#0">Three</a>
      <a href="#0">Four</a>
    </nav>  
    
    CSS
    body {
      background: #f06d06;
    }
    
    header, nav {
      text-align: center;
      background: white;
      margin: 20px 0;
      padding: 10px;
    }
    
    nav a {
      text-decoration: none;
      background: #333;
      border-radius: 5px;
      color: white;
      padding: 3px 8px;
    }
    
    结果

    This will work for inline, inline-block, inline-table, inline-flex, etc.

    2.You can center a block-level element by giving it margin-left and margin-right of auto (and it has a set width, otherwise it would be full width and wouldn't need centering). That's often done with shorthand like this:

    HTML
    <main>
      <div class="center">
        I'm a block level element and am centered.
      </div>
    </main>
    
    CSS
    body {
      background: #f06d06;
    }
    
    main {
      background: white;
      margin: 20px 0;
      padding: 10px;
    }
    
    .center {
      margin: 0 auto;
      width: 200px;
      background: black;
      padding: 20px;
      color: white;
    }
    
    结果
    Note that you can't float
    an element to the center. There is a trick though.

    3.If you have two or more block-level elements that need to be centered horizontally in a row, chances are you'd be better served making them a different display type. Here's an example of making them inline-block and an example of flexbox:

    HTML
    <main class="inline-block-center">
      <div>
        I'm an element that is block-like with my siblings and we're centered in a row.
      </div>
      <div>
        I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
      </div>
      <div>
        I'm an element that is block-like with my siblings and we're centered in a row.
      </div>
    </main>
    
    <main class="flex-center">
      <div>
        I'm an element that is block-like with my siblings and we're centered in a row.
      </div>
      <div>
        I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
      </div>
      <div>
        I'm an element that is block-like with my siblings and we're centered in a row.
      </div>
    </main>
    
    CSS
    body {
      background: #f06d06;
      font-size: 80%;
    }
    
    main {
      background: white;
      margin: 20px 0;
      padding: 10px;
    }
    
    main div {
      background: black;
      color: white;
      padding: 15px;
      max-width: 125px;
      margin: 5px;
    }
    
    .inline-block-center {
      text-align: center;
    }
    .inline-block-center div {
      display: inline-block;
      text-align: left;
    }
    
    .flex-center {
      display: flex;
      justify-content: center;
    }
    
    结果

    4.Unless you mean you have multiple block level elements stacked on top of each other, in which case the auto margin technique is still fine:

    HTML
    <main>
      <div>
        I'm an element that is block-like with my siblings and we're centered in a row.
      </div>
      <div>
        I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
      </div>
      <div>
        I'm an element that is block-like with my siblings and we're centered in a row.
      </div>
    </main>
    
    CSS
    body {
      background: #f06d06;
      font-size: 80%;
    }
    
    main {
      background: white;
      margin: 20px 0;
      padding: 10px;
    }
    
    main div {
      background: black;
      margin: 0 auto;
      color: white;
      padding: 15px;
      margin: 5px auto;
    }
    
    main div:nth-child(1) {
      width: 200px;
    }
    main div:nth-child(2) {
      width: 400px;
    }
    main div:nth-child(3) {
      width: 125px;
    }
    

    Vertical centering is a bit trickier in CSS.

    5.Sometimes inline / text elements can appear vertically centered, just because there is equal padding above and below them.

    .link {
      padding-top: 30px;
      padding-bottom: 30px;
    }
    
    HTML
    <main>
      <a href="#0">We're</a>
      <a href="#0">Centered</a>
      <a href="#0">Bits of</a>
      <a href="#0">Text</a>
    </main>
    
    CSS
    body {
      background: #f06d06;
      font-size: 80%;
    }
    
    main {
      background: white;
      margin: 20px 0;
      padding: 50px;
    }
    
    main a {
      background: black;
      color: white;
      padding: 40px 30px;
      text-decoration: none;
    }
    

    If padding isn't an option for some reason, and you're trying to center some text that you know will not wrap, there is a trick were making the line-height equal to the height will center the text.

    .center-text-trick {
      height: 100px;
      line-height: 100px;
      white-space: nowrap;
    }
    
    HTML
    <main>
      <div>
        I'm a centered line.
      </div>
    </main>
    
    body {
      background: #f06d06;
      font-size: 80%;
    }
    
    main {
      background: white;
      margin: 20px 0;
      padding: 40px;
    }
    
    main div {
      background: black;
      color: white;
      height: 100px;
      line-height: 100px;
      padding: 20px;
      width: 50%;
      white-space: nowrap;
    }
    

    Equal padding on top and bottom can give the centered effect for multiple lines of text too, but if that isn't going to work, perhaps the element the text is in can be a table cell, either literally or made to behave like one with CSS. The vertical-align
    property handles this, in this case, unlike what it normally does which is handle the alignment of elements aligned on a row. (More on that.)
    HTML
    <table>
      <tr>
        <td>
          I'm vertically centered multiple lines of text in a real table cell.
        </td>
      </tr>
    </table>
    
    <div class="center-table">
      <p>I'm vertically centered multiple lines of text in a CSS-created table layout.</p>
    </div>
    
    CSS
    body {
      background: #f06d06;
      font-size: 80%;
    }
    
    table {
      background: white;
      width: 240px;
      border-collapse: separate;
      margin: 20px;
      height: 250px;
    }
    
    table td {
      background: black;
      color: white;
      padding: 20px;
      border: 10px solid white;
      /* default is vertical-align: middle; */
    }
    
    .center-table {
      display: table;
      height: 250px;
      background: white;
      width: 240px;
      margin: 20px;
    }
    .center-table p {
      display: table-cell;
      margin: 0;
      background: black;
      color: white;
      padding: 20px;
      border: 10px solid white;
      vertical-align: middle;
    }
    

    If something table-like is out, perhaps you could use flexbox? A single flex-child can be made to center in a flex-parent pretty easily.

    .flex-center-vertically {
      display: flex;
      justify-content: center;
      flex-direction: column;
      height: 400px;
    }
    
    HTML
    <div class="flex-center">
      <p>I'm vertically centered multiple lines of text in a flexbox container.</p>
    </div>
    
    CSS
    body {
      background: #f06d06;
      font-size: 80%;
    }
    
    div {
      background: white;
      width: 240px;
      margin: 20px;
    }
    
    .flex-center {
      background: black;
      color: white;
      border: 10px solid white;
      display: flex;
      flex-direction: column;
      justify-content: center;
      height: 200px;
      resize: vertical;
      overflow: auto;
    }
    .flex-center p {
      margin: 0;
      padding: 20px;
    }
    

    Remember that it's only really relevant if the parent container has a fixed height (px, %, etc), which is why the container here has a height.

    If both of these techniques are out, you could employ the "ghost element" technique, in which a full-height pseudo element is placed inside the container and the text is vertically aligned with that.

    .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;
    }
    

    It's fairly common to not know the height in web page layout, for lots of reasons: if the width changes, text reflow can change the height. Variance in the styling of text can change the height. Variance in the amount of text can change the height. Elements with a fixed aspect ratio, like images, can change height when resized. Etc.

    But if you do know the height, you can center vertically like:

    .parent {
      position: relative;
    }
    .child {
      position: absolute;
      top: 50%;
      height: 100px;
      margin-top: -50px; /* account for padding and border if not using box-sizing: border-box; */
    }
    
    HTML
    <main>
      
      <div>
         I'm a block-level element with a fixed height, centered vertically within my parent.
      </div>
      
    </main>
    
    CSS
    body {
      background: #f06d06;
      font-size: 80%;
    }
    
    main {
      background: white;
      height: 300px;
      margin: 20px;
      width: 300px;
      position: relative;
      resize: vertical;
      overflow: auto;
    }
    
    main div {
      position: absolute;
      top: 50%;
      left: 20px;
      right: 20px;
      height: 100px;
      margin-top: -70px;
      background: black;
      color: white;
      padding: 20px;
    }
    

    It's still possible to center it by nudging it up half of it's height after bumping it down halfway:

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

    No big surprise, this is a lot easier in flexbox.

    .parent {
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
    
    HTML
    <main>
      
      <div>
         I'm a block-level element with an unknown height, centered vertically within my parent.
      </div>
      
    </main>
    
    CSS
    body {
      background: #f06d06;
      font-size: 80%;
    }
    
    main {
      background: white;
      height: 300px;
      width: 200px;
      padding: 20px;
      margin: 20px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      resize: vertical;
      overflow: auto;
    }
    
    main div {
      background: black;
      color: white;
      padding: 20px;
      resize: vertical;
      overflow: auto;
    }
    

    You can combine the techniques above in any fashion to get perfectly centered elements. But I find this generally falls into three camps:
    Using negative margins equal to half of that width and height, after you've absolutely positioned it at 50% / 50% will center it with great cross browser support:

    .parent {
      position: relative;
    }
    
    .child {
      width: 300px;
      height: 100px;
      padding: 20px;
    
      position: absolute;
      top: 50%;
      left: 50%;
    
      margin: -70px 0 0 -170px;
    }
    
    HTML
    <main>
      
      <div>
         I'm a block-level element a fixed height and width, centered vertically within my parent.
      </div>
      
    </main>
    
    CSS
    body {
      background: #f06d06;
      font-size: 80%;
      padding: 20px;
    }
    
    main {
      position: relative;
      background: white;
      height: 200px;
      width: 60%;
      margin: 0 auto;
      padding: 20px;
      resize: both;
      overflow: auto;
    }
    
    main div {
      background: black;
      color: white;
      width: 200px;
      height: 100px;
      margin: -70px 0 0 -120px;
      position: absolute;
      top: 50%;
      left: 50%;
      padding: 20px;
    }
    

    If you don't know the width or height, you can use the transform property and a negative translate of 50% in both directions (it is based on the current width/height of the element) to center:

    .parent {
      position: relative;
    }
    .child {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
    
    HTML
    <main>
      
      <div>
         I'm a block-level element of an unknown height and width, centered vertically within my parent.
      </div>
    
    CSS
    body {
      background: #f06d06;
      font-size: 80%;
      padding: 20px;
    }
    
    main {
      position: relative;
      background: white;
      height: 200px;
      width: 60%;
      margin: 0 auto;
      padding: 20px;
      resize: both;
      overflow: auto;
    }
    
    main div {
      background: black;
      color: white;
      width: 50%;
      transform: translate(-50%, -50%);
      position: absolute;
      top: 50%;
      left: 50%;
      padding: 20px;
      resize: both;
      overflow: auto;
    }
    

    To center in both directions with flexbox, you need to use two centering properties:

    .parent {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    HTML
    <main>
      
      <div>
         I'm a block-level-ish element of an unknown width and height, centered vertically within my parent.
      </div>
      
    </main>
    
    CSS
    body {
      background: #f06d06;
      font-size: 80%;
      padding: 20px;
    }
    
    main {
      background: white;
      height: 200px;
      width: 60%;
      margin: 0 auto;
      padding: 20px;
      display: flex;
      justify-content: center;
      align-items: center;
      resize: both;
      overflow: auto;
    }
    
    main div {
      background: black;
      color: white;
      width: 50%;
      padding: 20px;
      resize: both;
      overflow: auto;
    }
    

    转载自<a href="https://css-tricks.com/centering-css-complete-guide/">CSS-TRICKS</a>

    相关文章

      网友评论

          本文标题:小薇学院任务四笔记2(居中)

          本文链接:https://www.haomeiwen.com/subject/ambrgttx.html