Make row stretch across all columns in CSS Grid

前端 未结 3 589
借酒劲吻你
借酒劲吻你 2020-11-30 15:23

I\'ve been having trouble getting my nav to stretch across my columns completely, despite the fact that I\'ve specified what columns I wanted the nav to stretch over, if you

相关标签:
3条回答
  • 2020-11-30 15:54

    I've been having trouble getting my nav to stretch across my columns completely, despite the fact that I've specified what columns I wanted the nav to stretch over, if you could take a look at my code and point out my error that would be awesome.

    Actually, you have not specified the columns you want the nav to stretch over.

    You have specified the column lines.


    When you tell a grid item to do the following:

    grid-column: 1 / 2
    

    That breaks down to this:

    grid-column-start: 1
    grid-column-end: 2
    

    This is called line-based placement and it defines boundary lines for the columns.

    In this case, you're telling the grid item to extend from grid column line 1 to grid column line 2. That covers one column.

    Except in your grid there are two columns.

    .container {
      display: grid;
      grid-template-columns: 4fr 1fr;
    }
    

    Keep in mind that in a two-column grid, there are three grid column lines.

    In fact, in every grid the number of column / row lines is equal to the number of columns / rows + 1, because the last column / row has an extra (final) line.

    Firefox offers a useful tool for seeing this.

    In Firefox dev tools, when you inspect the grid container, there is a tiny grid icon in the CSS declaration. On click it displays an outline of your grid.

    enter image description here

    More details here: https://developer.mozilla.org/en-US/docs/Tools/Page_Inspector/How_to/Examine_grid_layouts


    There are several ways to solve this problem.

    One would be to adjust your grid-column rule from:

    grid-column: 1 / 2
    

    to

    grid-column: 1 / 3
    

    But, when working with explicit grids, you could also do this:

    grid-column: 1 / -1
    

    Positive integers begin the count from the start of the grid.

    Negative integers begin the count from the end of the grid (source).

    body {
      margin: 0;
      padding: 0;
    }
    
    .container {
      max-width: 960px;
      width: 100%;
      margin: 0 auto;
      display: grid;
      grid-template-columns: 4fr 1fr;
      grid-template-rows: 50px auto;
    }
    
    .mainnav {
      grid-column: 1 / -1;       /* adjustment (was 1 / 2) */
      grid-row: 1 / 2;           /* adjustment (was 1 / 1) */
      background-color: #5eccc0;
    }
    
    .navul {
      list-style-type: none;
      margin: 0;
      padding: 0;
    }
    
    .navli {
      float: left;
      text-align: center;
    }
    
    .navli a {
      display: block;
      text-decoration: none;
    }
    
    main {
      grid-column: 1 / 1;
      grid-row: 2 / 2;
      background-color: #ffff77;
    }
    
    .sidebar {
      background-color: #b200a4;
      grid-column: 2 / 2;
      grid-row: 2 / 2;
    }
    <div class="container">
      <nav class="mainnav">
        <ul class="navul">
          <li class="navli"><a href="index.html">Home</a></li>
          <li class="navli"><a href="contact.html">Contact</a></li>
          <li class="navli"><a href="about.html">About</a></li>
        </ul>
      </nav>
    
      <main>
        <h1>Html Example Header</h1>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean dignissim congue sapien, faucibus ultricies metus pulvinar nec. Vestibulum vitae mattis nibh. Sed nibh mauris, rhoncus id urna rutrum, dignissim luctus ligula. Pellentesque quis nulla
          eget elit egestas dignissim. Vivamus blandit congue sem. Fusce eget condimentum massa, sit amet convallis erat. Proin in justo ac nisl vulputate vulputate id et sem. Donec consectetur mollis mi, ut tempor sem scelerisque in. Nulla facilisi.
          <br>
          <br> Nunc tempus lectus mi, ac posuere metus elementum vitae. Praesent non elit at lacus bibendum ornare. Sed pretium ultricies justo, fringilla sagittis ligula aliquam quis. Aenean aliquet euismod fringilla. Curabitur sit amet pharetra nisi. Donec
          ac suscipit arcu, ac accumsan tortor. Ut facilisis egestas augue, laoreet tempus ligula. Morbi pulvinar arcu et lectus tincidunt condimentum. Mauris ut nisl at lectus condimentum suscipit.
          <br>
          <br> Vestibulum vel magna id lacus ullamcorper pretium vitae a eros. Sed vestibulum metus eu justo volutpat, nec facilisis lacus tempus. Nulla at maximus sapien, eget finibus massa. Phasellus pulvinar iaculis augue rhoncus aliquet. Vestibulum vitae
          massa bibendum, pretium justo ultrices, fermentum turpis. Vestibulum id scelerisque lacus. In mattis viverra justo, vel suscipit enim egestas ac. Ut fringilla lobortis nulla, vitae tempus nibh fermentum ac. Vestibulum ante ipsum primis in faucibus
          orci luctus et ultrices posuere cubilia Curae; Phasellus ac urna vitae arcu varius suscipit. Pellentesque nec nunc ligula. Sed sollicitudin pretium iaculis. Aliquam ut ex molestie, vulputate lacus vitae, dapibus lorem. Phasellus nec bibendum tortor.
        </p>
      </main>
      <div class="sidebar">
        <h2>Sidebar Example</h2>
        <ul>
          <li>List item 1</li>
          <li>List item 2</li>
          <li>List item 3</li>
        </ul>
      </div>
    </div>


    0 讨论(0)
  • 2020-11-30 16:00
    .mainnav {
      grid-column: 1 / -1;
    }
    
    0 讨论(0)
  • 2020-11-30 16:09

    You should change .mainnav { grid-column: 1 / 2; }

    to .mainnav { grid-column: 1 / 3; }.

    Also you can change to .mainnav { grid-column: 1 / span 2; }

    You are specifying grid line numbers, not column numbers in grid-column. From A Complete Guide to Grid:

    grid-column: <start-line> / <end-line> | <start-line> / span <value>;

    Demo:

    body {
      margin: 0;
      padding: 0;
    }
    
    .container {
      max-width: 960px;
      width: 100%;
      margin: 0 auto;
      display: grid;
      grid-template-columns: 4fr 1fr;
      grid-template-rows: 50px auto;
    }
    
    .mainnav {
      grid-column: 1 / 3; /* or grid-column: 1 / span 2; */
      grid-row: 1 / 1;
      background-color: #5eccc0;
    }
    
    .navul {
      list-style-type: none;
      margin: 0;
      padding: 0;
    }
    
    .navli {
      float: left;
      text-align: center;
    }
    
    .navli a {
      display: block;
      text-decoration: none;
    }
    
    main {
      grid-column: 1 / 1;
      grid-row: 2 / 2;
      background-color: #ffff77;
    }
    
    .sidebar {
      background-color: #b200a4;
      grid-column: 2 / 2;
      grid-row: 2 / 2;
    }
    <div class="container">
      <nav class="mainnav">
        <ul class="navul">
          <li class="navli"><a href="index.html">Home</a></li>
          <li class="navli"><a href="contact.html">Contact</a></li>
          <li class="navli"><a href="about.html">About</a></li>
        </ul>
      </nav>
    
      <main>
        <h1>Html Example Header</h1>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean dignissim congue sapien, faucibus ultricies metus pulvinar nec. Vestibulum vitae mattis nibh. Sed nibh mauris, rhoncus id urna rutrum, dignissim luctus ligula. Pellentesque quis nulla
          eget elit egestas dignissim. Vivamus blandit congue sem. Fusce eget condimentum massa, sit amet convallis erat. Proin in justo ac nisl vulputate vulputate id et sem. Donec consectetur mollis mi, ut tempor sem scelerisque in. Nulla facilisi.
          <br>
          <br> Nunc tempus lectus mi, ac posuere metus elementum vitae. Praesent non elit at lacus bibendum ornare. Sed pretium ultricies justo, fringilla sagittis ligula aliquam quis. Aenean aliquet euismod fringilla. Curabitur sit amet pharetra nisi. Donec
          ac suscipit arcu, ac accumsan tortor. Ut facilisis egestas augue, laoreet tempus ligula. Morbi pulvinar arcu et lectus tincidunt condimentum. Mauris ut nisl at lectus condimentum suscipit.
          <br>
          <br> Vestibulum vel magna id lacus ullamcorper pretium vitae a eros. Sed vestibulum metus eu justo volutpat, nec facilisis lacus tempus. Nulla at maximus sapien, eget finibus massa. Phasellus pulvinar iaculis augue rhoncus aliquet. Vestibulum vitae
          massa bibendum, pretium justo ultrices, fermentum turpis. Vestibulum id scelerisque lacus. In mattis viverra justo, vel suscipit enim egestas ac. Ut fringilla lobortis nulla, vitae tempus nibh fermentum ac. Vestibulum ante ipsum primis in faucibus
          orci luctus et ultrices posuere cubilia Curae; Phasellus ac urna vitae arcu varius suscipit. Pellentesque nec nunc ligula. Sed sollicitudin pretium iaculis. Aliquam ut ex molestie, vulputate lacus vitae, dapibus lorem. Phasellus nec bibendum tortor.
        </p>
      </main>
      <div class="sidebar">
        <h2>Sidebar Example</h2>
        <ul>
          <li>List item 1</li>
          <li>List item 2</li>
          <li>List item 3</li>
        </ul>
      </div>
    </div>

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