Centering in CSS Grid

后端 未结 7 874
半阙折子戏
半阙折子戏 2020-11-21 07:11

I\'m trying to create a simple page with CSS Grid.

What I\'m failing to do is center the text from the HTML to the respective grid cells.

I\'ve tried placing

相关标签:
7条回答
  • 2020-11-21 07:19

    Do not even try to use flex; stay with css grid!! :)

    https://jsfiddle.net/ctt3bqr0/

    place-self: center;
    

    is doing the centering work here.

    If you want to center something that is inside div that is inside grid cell you need to define nested grid in order to make it work. (Please look at the fiddle both examples shown there.)

    https://css-tricks.com/snippets/css/complete-guide-grid/

    Cheers!

    0 讨论(0)
  • 2020-11-21 07:21

    The CSS place-items shorthand property sets the align-items and justify-items properties, respectively. If the second value is not set, the first value is also used for it.

    .parent {
      display: grid;
      place-items: center;
    }
    
    0 讨论(0)
  • 2020-11-21 07:28

    You want this?

    html,
    body {
      margin: 0;
      padding: 0;
    }
    
    .container {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-template-rows: 100vh;
      grid-gap: 0px 0px;
    }
    
    .left_bg {
      display: subgrid;
      background-color: #3498db;
      grid-column: 1 / 1;
      grid-row: 1 / 1;
      z-index: 0;
    }
    
    .right_bg {
      display: subgrid;
      background-color: #ecf0f1;
      grid-column: 2 / 2;
      grid_row: 1 / 1;
      z-index: 0;
    }
    
    .text {
      font-family: Raleway;
      font-size: large;
      text-align: center;
    }
    <div class="container">
      <!--everything on the page-->
    
      <div class="left_bg">
        <!--left background color of the page-->
        <div class="text">
          <!--left side text content-->
          <p>Review my stuff</p>
        </div>
      </div>
    
      <div class="right_bg">
        <!--right background color of the page-->
        <div class="text">
          <!--right side text content-->
          <p>Hire me!</p>
        </div>
      </div>
    </div>

    0 讨论(0)
  • 2020-11-21 07:31

    This answer has two main sections:

    1. Understanding how alignment works in CSS Grid.
    2. Six methods for centering in CSS Grid.

    If you're only interested in the solutions, skip the first section.


    The Structure and Scope of Grid layout

    To fully understand how centering works in a grid container, it's important to first understand the structure and scope of grid layout.

    The HTML structure of a grid container has three levels:

    • the container
    • the item
    • the content

    Each of these levels is independent from the others, in terms of applying grid properties.

    The scope of a grid container is limited to a parent-child relationship.

    This means that a grid container is always the parent and a grid item is always the child. Grid properties work only within this relationship.

    Descendants of a grid container beyond the children are not part of grid layout and will not accept grid properties. (At least not until the subgrid feature has been implemented, which will allow descendants of grid items to respect the lines of the primary container.)

    Here's an example of the structure and scope concepts described above.

    Imagine a tic-tac-toe-like grid.

    article {
      display: inline-grid;
      grid-template-rows: 100px 100px 100px;
      grid-template-columns: 100px 100px 100px;
      grid-gap: 3px;
    }
    

    You want the X's and O's centered in each cell.

    So you apply the centering at the container level:

    article {
      display: inline-grid;
      grid-template-rows: 100px 100px 100px;
      grid-template-columns: 100px 100px 100px;
      grid-gap: 3px;
      justify-items: center;
    }
    

    But because of the structure and scope of grid layout, justify-items on the container centers the grid items, not the content (at least not directly).

    article {
      display: inline-grid;
      grid-template-rows: 100px 100px 100px;
      grid-template-columns: 100px 100px 100px;
      grid-gap: 3px;
      justify-items: center;
    }
    
    section {
        border: 2px solid black;
        font-size: 3em;
    }
    <article>
        <section>X</section>
        <section>O</section>
        <section>X</section>
        <section>O</section>
        <section>X</section>
        <section>O</section>
        <section>X</section>
        <section>O</section>
        <section>X</section>
    </article>

    Same problem with align-items: The content may be centered as a by-product, but you've lost the layout design.

    article {
      display: inline-grid;
      grid-template-rows: 100px 100px 100px;
      grid-template-columns: 100px 100px 100px;
      grid-gap: 3px;
      justify-items: center;
      align-items: center;
    }
    

    article {
      display: inline-grid;
      grid-template-rows: 100px 100px 100px;
      grid-template-columns: 100px 100px 100px;
      grid-gap: 3px;
      justify-items: center;
      align-items: center;
    }
    
    section {
        border: 2px solid black;
        font-size: 3em;
    }
    <article>
        <section>X</section>
        <section>O</section>
        <section>X</section>
        <section>O</section>
        <section>X</section>
        <section>O</section>
        <section>X</section>
        <section>O</section>
        <section>X</section>
    </article>

    To center the content you need to take a different approach. Referring again to the structure and scope of grid layout, you need to treat the grid item as the parent and the content as the child.

    article {
      display: inline-grid;
      grid-template-rows: 100px 100px 100px;
      grid-template-columns: 100px 100px 100px;
      grid-gap: 3px;
    }
    
    section {
      display: flex;
      justify-content: center;
      align-items: center;
      border: 2px solid black;
      font-size: 3em;
    }
    

    article {
      display: inline-grid;
      grid-template-rows: 100px 100px 100px;
      grid-template-columns: 100px 100px 100px;
      grid-gap: 3px;
    }
    
    section {
      display: flex;
      justify-content: center;
      align-items: center;
      border: 2px solid black;
      font-size: 3em;
    }
    <article>
      <section>X</section>
      <section>O</section>
      <section>X</section>
      <section>O</section>
      <section>X</section>
      <section>O</section>
      <section>X</section>
      <section>O</section>
      <section>X</section>
    </article>

    jsFiddle demo


    Six Methods for Centering in CSS Grid

    There are multiple methods for centering grid items and their content.

    Here's a basic 2x2 grid:

    grid-container {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-auto-rows: 75px;
      grid-gap: 10px;
    }
    
    
    /* can ignore styles below; decorative only */
    grid-container {
      background-color: lightyellow;
      border: 1px solid #bbb;
      padding: 10px;
    }
    grid-item {
      background-color: lightgreen;
      border: 1px solid #ccc;
    }
    <grid-container>
      <grid-item>this text should be centered</grid-item>
      <grid-item>this text should be centered</grid-item>
      <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
      <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
    </grid-container>

    Flexbox

    For a simple and easy way to center the content of grid items use flexbox.

    More specifically, make the grid item into a flex container.

    There is no conflict, spec violation or other problem with this method. It's clean and valid.

    grid-item {
      display: flex;
      align-items: center;
      justify-content: center;
    }
    

    grid-container {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-auto-rows: 75px;
      grid-gap: 10px;
    }
    
    grid-item {
      display: flex;            /* new */
      align-items: center;      /* new */
      justify-content: center;  /* new */
    }
    
    /* can ignore styles below; decorative only */
    grid-container {
      background-color: lightyellow;
      border: 1px solid #bbb;
      padding: 10px;
    }
    grid-item {
      background-color: lightgreen;
      border: 1px solid #ccc;
    }
    <grid-container>
      <grid-item>this text should be centered</grid-item>
      <grid-item>this text should be centered</grid-item>
      <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
      <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
    </grid-container>

    See this post for a complete explanation:

    • How to Center Elements Vertically and Horizontally in Flexbox

    Grid Layout

    In the same way that a flex item can also be a flex container, a grid item can also be a grid container. This solution is similar to the flexbox solution above, except centering is done with grid, not flex, properties.

    grid-container {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-auto-rows: 75px;
      grid-gap: 10px;
    }
    
    grid-item {
      display: grid;            /* new */
      align-items: center;      /* new */
      justify-items: center;    /* new */
    }
    
    /* can ignore styles below; decorative only */
    grid-container {
      background-color: lightyellow;
      border: 1px solid #bbb;
      padding: 10px;
    }
    grid-item {
      background-color: lightgreen;
      border: 1px solid #ccc;
    }
    <grid-container>
      <grid-item>this text should be centered</grid-item>
      <grid-item>this text should be centered</grid-item>
      <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
      <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
    </grid-container>


    auto margins

    Use margin: auto to vertically and horizontally center grid items.

    grid-item {
      margin: auto;
    }
    

    grid-container {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-auto-rows: 75px;
      grid-gap: 10px;
    }
    
    grid-item {
      margin: auto;
    }
    
    /* can ignore styles below; decorative only */
    grid-container {
      background-color: lightyellow;
      border: 1px solid #bbb;
      padding: 10px;
    }
    grid-item {
      background-color: lightgreen;
      border: 1px solid #ccc;
    }
    <grid-container>
      <grid-item>this text should be centered</grid-item>
      <grid-item>this text should be centered</grid-item>
      <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
      <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
    </grid-container>

    To center the content of grid items you need to make the item into a grid (or flex) container, wrap anonymous items in their own elements (since they cannot be directly targeted by CSS), and apply the margins to the new elements.

    grid-item {
      display: flex;
    }
    
    span, img {
      margin: auto;
    }
    

    grid-container {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-auto-rows: 75px;
      grid-gap: 10px;
    }
    
    grid-item {
      display: flex;
    }
    
    span, img {
      margin: auto;
    }
    
    /* can ignore styles below; decorative only */
    grid-container {
      background-color: lightyellow;
      border: 1px solid #bbb;
      padding: 10px;
    }
    grid-item {
      background-color: lightgreen;
      border: 1px solid #ccc;
    }
    <grid-container>
      <grid-item><span>this text should be centered</span></grid-item>
      <grid-item><span>this text should be centered</span></grid-item>
      <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
      <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
    </grid-container>


    Box Alignment Properties

    When considering using the following properties to align grid items, read the section on auto margins above.

    • align-items
    • justify-items
    • align-self
    • justify-self

    https://www.w3.org/TR/css-align-3/#property-index


    text-align: center

    To center content horizontally in a grid item, you can use the text-align property.

    grid-container {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-auto-rows: 75px;
      grid-gap: 10px;
      text-align: center;  /* new */
    }
    
    
    /* can ignore styles below; decorative only */
    grid-container {
      background-color: lightyellow;
      border: 1px solid #bbb;
      padding: 10px;
    }
    grid-item {
      background-color: lightgreen;
      border: 1px solid #ccc;
    }
    <grid-container>
      <grid-item>this text should be centered</grid-item>
      <grid-item>this text should be centered</grid-item>
      <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
      <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
    </grid-container>

    Note that for vertical centering, vertical-align: middle will not work.

    This is because the vertical-align property applies only to inline and table-cell containers.

    grid-container {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-auto-rows: 75px;
      grid-gap: 10px;
      text-align: center;     /* <--- works */
      vertical-align: middle; /* <--- fails */
    }
    
    
    /* can ignore styles below; decorative only */
    grid-container {
      background-color: lightyellow;
      border: 1px solid #bbb;
      padding: 10px;
    }
    grid-item {
      background-color: lightgreen;
      border: 1px solid #ccc;
    }
    <grid-container>
      <grid-item>this text should be centered</grid-item>
      <grid-item>this text should be centered</grid-item>
      <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
      <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
    </grid-container>

    One might say that display: inline-grid establishes an inline-level container, and that would be true. So why doesn't vertical-align work in grid items?

    The reason is that in a grid formatting context, items are treated as block-level elements.

    6.1. Grid Item Display

    The display value of a grid item is blockified: if the specified display of an in-flow child of an element generating a grid container is an inline-level value, it computes to its block-level equivalent.

    In a block formatting context, something the vertical-align property was originally designed for, the browser doesn't expect to find a block-level element in an inline-level container. That's invalid HTML.


    CSS Positioning

    Lastly, there's a general CSS centering solution that also works in Grid: absolute positioning

    This is a good method for centering objects that need to be removed from the document flow. For example, if you want to:

    • Center text over an image, or
    • Place asterisks (*) above required form fields

    Simply set position: absolute on the element to be centered, and position: relative on the ancestor that will serve as the containing block (it's usually the parent). Something like this:

    grid-item {
      position: relative;
      text-align: center;
    }
    
    span {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }
    

    grid-container {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-auto-rows: 75px;
      grid-gap: 10px;
    }
    
    grid-item {
      position: relative;
      text-align: center;
    }
    
    span, img {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }
    
    
    /* can ignore styles below; decorative only */
    
    grid-container {
      background-color: lightyellow;
      border: 1px solid #bbb;
      padding: 10px;
    }
    
    grid-item {
      background-color: lightgreen;
      border: 1px solid #ccc;
    }
    <grid-container>
      <grid-item><span>this text should be centered</span></grid-item>
      <grid-item><span>this text should be centered</span></grid-item>
      <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
      <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
    </grid-container>

    Here's a complete explanation for how this method works:

    • Element will not stay centered, especially when re-sizing screen

    Here's the section on absolute positioning in the Grid spec:

    • https://www.w3.org/TR/css3-grid-layout/#abspos
    0 讨论(0)
  • 2020-11-21 07:33

    I know this question is a couple years old, but I am surprised to find that no one suggested:

       text-align: center;
    

    this is a more universal property than justify-content, and is definitely not unique to grid, but I find that when dealing with text, which is what this question specifically asks about, that it aligns text to the center with-out affecting the space between grid items, or the vertical centering. It centers text horizontally where its stands on its vertical axis. I also find it to remove a layer of complexity that justify-content and align-items adds. justify-content and align-items affects the entire grid item or items, text-align centers the text without affecting the container it is in. Hope this helps.

    0 讨论(0)
  • 2020-11-21 07:36

    You can use flexbox to center your text. By the way no need for extra containers because text is considered as anonymous flex item.

    From flexbox specs:

    Each in-flow child of a flex container becomes a flex item, and each contiguous run of text that is directly contained inside a flex container is wrapped in an anonymous flex item. However, an anonymous flex item that contains only white space (i.e. characters that can be affected by the white-space property) is not rendered (just as if it were display:none).

    So just make grid items as flex containers (display: flex), and add align-items: center and justify-content: center to center both vertically and horizontally.

    Also performed optimization of HTML and CSS:

    html,
    body {
      margin: 0;
      padding: 0;
    }
    
    .container {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-template-rows: 100vh;
      
      font-family: Raleway;
      font-size: large;
    }
    
    .left_bg,
    .right_bg {
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    .left_bg {
      background-color: #3498db;
    }
    
    .right_bg {
      background-color: #ecf0f1;
    }
    <div class="container">
      <div class="left_bg">Review my stuff</div>
      <div class="right_bg">Hire me!</div>
    </div>

    0 讨论(0)
提交回复
热议问题