【问题标题】:Why isn't grid item shifting to the end?为什么网格项目没有移动到最后?
【发布时间】:2020-07-11 03:14:49
【问题描述】:

我正在尝试将“联系人”一词发送到网格的末尾,但它似乎根本没有移动。

我是使用 CSS Grid 属性的新手,所以我不确定我做错了什么。我有一个用于页面中所有元素的网格容器,还有一个“绿色”类,其中包含所有具有绿色背景的单词。我给了“Contact”一个 id,这样我就可以只移动那个词,但还是没有任何反应。

.container {
  display: grid;
  grid-gap: 0px;
  grid-template-columns: auto;
  grid-template-rows: auto;
}

.green {
  grid-column-start: 1;
  grid-column-end: 4;
}

#Contact {
  justify-self: end;
}

.zone {
  padding: 30px 50px;
  cursor: pointer;
  display: inline-block;
  color: #FFF;
  font-size: 2em;
  border-radius: 4px;
  border: 1px solid #bbb;
  transition: all 0.3s linear;
}

.zone:hover {
  -webkit-box-shadow: rgba(0, 0, 0, 0.8) 0px 5px 15px, inset rgba(0, 0, 0, 0.15) 0px -10px 20px;
  -moz-box-shadow: rgba(0, 0, 0, 0.8) 0px 5px 15px, inset rgba(0, 0, 0, 0.15) 0px -10px 20px;
  -o-box-shadow: rgba(0, 0, 0, 0.8) 0px 5px 15px, inset rgba(0, 0, 0, 0.15) 0px -10px 20px;
  box-shadow: rgba(0, 0, 0, 0.8) 0px 5px 15px, inset rgba(0, 0, 0, 0.15) 0px -10px 20px;
}


/*https://paulund.co.uk/how-to-create-shiny-css-buttons*/


/***********************************************************************
 *  Green Background
 **********************************************************************/

.green {
  background: #56B870;
  /* Old browsers */
  background: -moz-linear-gradient(top, #56B870 0%, #a5c956 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #56B870), color-stop(100%, #a5c956));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, #56B870 0%, #a5c956 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, #56B870 0%, #a5c956 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(top, #56B870 0%, #a5c956 100%);
  /* IE10+ */
  background: linear-gradient(top, #56B870 0%, #a5c956 100%);
  /* W3C */
}


/***********************************************************************
 *  Red Background
 **********************************************************************/

.red {
  background: #C655BE;
  /* Old browsers */
  background: -moz-linear-gradient(top, #C655BE 0%, #cf0404 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #C655BE), color-stop(100%, #cf0404));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, #C655BE 0%, #cf0404 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, #C655BE 0%, #cf0404 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(top, #C655BE 0%, #cf0404 100%);
  /* IE10+ */
  background: linear-gradient(top, #C655BE 0%, #cf0404 100%);
  /* W3C */
}


/***********************************************************************
 *  Yellow Background
 **********************************************************************/

.yellow {
  background: #F3AAAA;
  /* Old browsers */
  background: -moz-linear-gradient(top, #F3AAAA 0%, #febf04 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #F3AAAA), color-stop(100%, #febf04));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, #F3AAAA 0%, #febf04 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, #F3AAAA 0%, #febf04 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(top, #F3AAAA 0%, #febf04 100%);
  /* IE10+ */
  background: linear-gradient(top, #F3AAAA 0%, #febf04 100%);
  /* W3C */
}


/***********************************************************************
 *  Blue Background
 **********************************************************************/

.blue {
  background: #7abcff;
  /* Old browsers */
  background: -moz-linear-gradient(top, #7abcff 0%, #60abf8 44%, #4096ee 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #7abcff), color-stop(44%, #60abf8), color-stop(100%, #4096ee));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, #7abcff 0%, #60abf8 44%, #4096ee 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, #7abcff 0%, #60abf8 44%, #4096ee 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(top, #7abcff 0%, #60abf8 44%, #4096ee 100%);
  /* IE10+ */
  background: linear-gradient(top, #7abcff 0%, #60abf8 44%, #4096ee 100%);
  /* W3C */
}
<div class="container">
  <div class="zone green">
    <span>About</span>
    <span>Products</span>
    <span>Our Team</span>
    <span id="Contact">Contact</span>
  </div>
  <div class="zone red">Cover</div>
  <div class="zone blue">Project Grid</div>
  <div class="zone yellow">Footer</div>
</div>

【问题讨论】:

    标签: html css css-grid


    【解决方案1】:

    网格应用于.container,不会影响.zone.green。将 .zone.green 改为 flexbox:

    .zone.green{
      display: flex;
    }
    .zone.green span{
      margin-left: 10px;
    }
    
    .zone.green span#Contact{
      margin-left: auto;
    }
    

    .zone.green{
      display: flex;
    }
    .zone.green span{
      margin-left: 10px;
    }
    
    .zone.green span#Contact{
      margin-left: auto;
    }
    
    .container {
      display: grid;
      grid-gap: 0px;
      grid-template-columns: auto;
      grid-template-rows: auto;
    }
    
    .green {
      grid-column-start: 1;
      grid-column-end: 4;
    }
    
    #Contact {
      justify-self: end;
    }
    
    .zone {
      padding: 30px 50px;
      cursor: pointer;
      display: inline-block;
      color: #FFF;
      font-size: 2em;
      border-radius: 4px;
      border: 1px solid #bbb;
      transition: all 0.3s linear;
    }
    
    .zone:hover {
      -webkit-box-shadow: rgba(0, 0, 0, 0.8) 0px 5px 15px, inset rgba(0, 0, 0, 0.15) 0px -10px 20px;
      -moz-box-shadow: rgba(0, 0, 0, 0.8) 0px 5px 15px, inset rgba(0, 0, 0, 0.15) 0px -10px 20px;
      -o-box-shadow: rgba(0, 0, 0, 0.8) 0px 5px 15px, inset rgba(0, 0, 0, 0.15) 0px -10px 20px;
      box-shadow: rgba(0, 0, 0, 0.8) 0px 5px 15px, inset rgba(0, 0, 0, 0.15) 0px -10px 20px;
    }
    
    
    /*https://paulund.co.uk/how-to-create-shiny-css-buttons*/
    
    
    /***********************************************************************
     *  Green Background
     **********************************************************************/
    
    .green {
      background: #56B870;
      /* Old browsers */
      background: -moz-linear-gradient(top, #56B870 0%, #a5c956 100%);
      /* FF3.6+ */
      background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #56B870), color-stop(100%, #a5c956));
      /* Chrome,Safari4+ */
      background: -webkit-linear-gradient(top, #56B870 0%, #a5c956 100%);
      /* Chrome10+,Safari5.1+ */
      background: -o-linear-gradient(top, #56B870 0%, #a5c956 100%);
      /* Opera 11.10+ */
      background: -ms-linear-gradient(top, #56B870 0%, #a5c956 100%);
      /* IE10+ */
      background: linear-gradient(top, #56B870 0%, #a5c956 100%);
      /* W3C */
    }
    
    
    /***********************************************************************
     *  Red Background
     **********************************************************************/
    
    .red {
      background: #C655BE;
      /* Old browsers */
      background: -moz-linear-gradient(top, #C655BE 0%, #cf0404 100%);
      /* FF3.6+ */
      background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #C655BE), color-stop(100%, #cf0404));
      /* Chrome,Safari4+ */
      background: -webkit-linear-gradient(top, #C655BE 0%, #cf0404 100%);
      /* Chrome10+,Safari5.1+ */
      background: -o-linear-gradient(top, #C655BE 0%, #cf0404 100%);
      /* Opera 11.10+ */
      background: -ms-linear-gradient(top, #C655BE 0%, #cf0404 100%);
      /* IE10+ */
      background: linear-gradient(top, #C655BE 0%, #cf0404 100%);
      /* W3C */
    }
    
    
    /***********************************************************************
     *  Yellow Background
     **********************************************************************/
    
    .yellow {
      background: #F3AAAA;
      /* Old browsers */
      background: -moz-linear-gradient(top, #F3AAAA 0%, #febf04 100%);
      /* FF3.6+ */
      background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #F3AAAA), color-stop(100%, #febf04));
      /* Chrome,Safari4+ */
      background: -webkit-linear-gradient(top, #F3AAAA 0%, #febf04 100%);
      /* Chrome10+,Safari5.1+ */
      background: -o-linear-gradient(top, #F3AAAA 0%, #febf04 100%);
      /* Opera 11.10+ */
      background: -ms-linear-gradient(top, #F3AAAA 0%, #febf04 100%);
      /* IE10+ */
      background: linear-gradient(top, #F3AAAA 0%, #febf04 100%);
      /* W3C */
    }
    
    
    /***********************************************************************
     *  Blue Background
     **********************************************************************/
    
    .blue {
      background: #7abcff;
      /* Old browsers */
      background: -moz-linear-gradient(top, #7abcff 0%, #60abf8 44%, #4096ee 100%);
      /* FF3.6+ */
      background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #7abcff), color-stop(44%, #60abf8), color-stop(100%, #4096ee));
      /* Chrome,Safari4+ */
      background: -webkit-linear-gradient(top, #7abcff 0%, #60abf8 44%, #4096ee 100%);
      /* Chrome10+,Safari5.1+ */
      background: -o-linear-gradient(top, #7abcff 0%, #60abf8 44%, #4096ee 100%);
      /* Opera 11.10+ */
      background: -ms-linear-gradient(top, #7abcff 0%, #60abf8 44%, #4096ee 100%);
      /* IE10+ */
      background: linear-gradient(top, #7abcff 0%, #60abf8 44%, #4096ee 100%);
      /* W3C */
    }
    <div class="container">
      <div class="zone green">
        <span>About</span>
        <span>Products</span>
        <span>Our Team</span>
        <span id="Contact">Contact</span>
      </div>
      <div class="zone red">Cover</div>
      <div class="zone blue">Project Grid</div>
      <div class="zone yellow">Footer</div>
    </div>

    【讨论】:

    • 嘿,谢谢这工作。你能解释一下为什么我应该在这里使用 flex 吗?有没有办法只用 CSSgrid 来做到这一点?
    • @Cris 你可以使用网格,但你必须使用嵌套网格或子网格。由于.zone.green 只是单行,我认为只使用 flex 会更容易。
    【解决方案2】:

    首先,HTML 中的“联系人”是一个 ID。但在 CSS 中它是一个类。

    其次,#contact { justify-self: end } 不起作用,因为the parent isn't a grid container

    这是一个使用嵌套弹性容器的简单解决方案:

    .container {
      display: grid;
    }
    
    .green {
      grid-column-start: 1;
      grid-column-end: 4;
      display: flex; /* new */
    }
    
    #Contact {
      margin-left: auto; /* new */
    }
    
    .zone {
      padding: 30px 50px;
      cursor: pointer;
      /* display: inline-block; */ /* unnecessary; also, interferes with specificity */
      color: #FFF;
      font-size: 2em;
      border-radius: 4px;
      border: 1px solid #bbb;
      transition: all 0.3s linear;
    }
    
    .zone:hover {
      -webkit-box-shadow: rgba(0, 0, 0, 0.8) 0px 5px 15px, inset rgba(0, 0, 0, 0.15) 0px -10px 20px;
      -moz-box-shadow: rgba(0, 0, 0, 0.8) 0px 5px 15px, inset rgba(0, 0, 0, 0.15) 0px -10px 20px;
      -o-box-shadow: rgba(0, 0, 0, 0.8) 0px 5px 15px, inset rgba(0, 0, 0, 0.15) 0px -10px 20px;
      box-shadow: rgba(0, 0, 0, 0.8) 0px 5px 15px, inset rgba(0, 0, 0, 0.15) 0px -10px 20px;
    }
    
    
    /*https://paulund.co.uk/how-to-create-shiny-css-buttons*/
    
    
    /***********************************************************************
     *  Green Background
     **********************************************************************/
    
    .green {
      background: #56B870;
      /* Old browsers */
      background: -moz-linear-gradient(top, #56B870 0%, #a5c956 100%);
      /* FF3.6+ */
      background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #56B870), color-stop(100%, #a5c956));
      /* Chrome,Safari4+ */
      background: -webkit-linear-gradient(top, #56B870 0%, #a5c956 100%);
      /* Chrome10+,Safari5.1+ */
      background: -o-linear-gradient(top, #56B870 0%, #a5c956 100%);
      /* Opera 11.10+ */
      background: -ms-linear-gradient(top, #56B870 0%, #a5c956 100%);
      /* IE10+ */
      background: linear-gradient(top, #56B870 0%, #a5c956 100%);
      /* W3C */
    }
    
    
    /***********************************************************************
     *  Red Background
     **********************************************************************/
    
    .red {
      background: #C655BE;
      /* Old browsers */
      background: -moz-linear-gradient(top, #C655BE 0%, #cf0404 100%);
      /* FF3.6+ */
      background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #C655BE), color-stop(100%, #cf0404));
      /* Chrome,Safari4+ */
      background: -webkit-linear-gradient(top, #C655BE 0%, #cf0404 100%);
      /* Chrome10+,Safari5.1+ */
      background: -o-linear-gradient(top, #C655BE 0%, #cf0404 100%);
      /* Opera 11.10+ */
      background: -ms-linear-gradient(top, #C655BE 0%, #cf0404 100%);
      /* IE10+ */
      background: linear-gradient(top, #C655BE 0%, #cf0404 100%);
      /* W3C */
    }
    
    
    /***********************************************************************
     *  Yellow Background
     **********************************************************************/
    
    .yellow {
      background: #F3AAAA;
      /* Old browsers */
      background: -moz-linear-gradient(top, #F3AAAA 0%, #febf04 100%);
      /* FF3.6+ */
      background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #F3AAAA), color-stop(100%, #febf04));
      /* Chrome,Safari4+ */
      background: -webkit-linear-gradient(top, #F3AAAA 0%, #febf04 100%);
      /* Chrome10+,Safari5.1+ */
      background: -o-linear-gradient(top, #F3AAAA 0%, #febf04 100%);
      /* Opera 11.10+ */
      background: -ms-linear-gradient(top, #F3AAAA 0%, #febf04 100%);
      /* IE10+ */
      background: linear-gradient(top, #F3AAAA 0%, #febf04 100%);
      /* W3C */
    }
    
    
    /***********************************************************************
     *  Blue Background
     **********************************************************************/
    
    .blue {
      background: #7abcff;
      /* Old browsers */
      background: -moz-linear-gradient(top, #7abcff 0%, #60abf8 44%, #4096ee 100%);
      /* FF3.6+ */
      background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #7abcff), color-stop(44%, #60abf8), color-stop(100%, #4096ee));
      /* Chrome,Safari4+ */
      background: -webkit-linear-gradient(top, #7abcff 0%, #60abf8 44%, #4096ee 100%);
      /* Chrome10+,Safari5.1+ */
      background: -o-linear-gradient(top, #7abcff 0%, #60abf8 44%, #4096ee 100%);
      /* Opera 11.10+ */
      background: -ms-linear-gradient(top, #7abcff 0%, #60abf8 44%, #4096ee 100%);
      /* IE10+ */
      background: linear-gradient(top, #7abcff 0%, #60abf8 44%, #4096ee 100%);
      /* W3C */
    }
    <div class="container">
      <div class="zone green">
        <span>About</span>
        <span>Products</span>
        <span>Our Team</span>
        <span id="Contact">Contact</span>
      </div>
      <div class="zone red">Cover</div>
      <div class="zone blue">Project Grid</div>
      <div class="zone yellow">Footer</div>
    </div>

    【讨论】:

    • 谢谢,这行得通。你能解释一下为什么吗?更具体地说,比如你为什么使用 flex 以及你为什么注释掉 display: inline-block;
    • 如果您不删除display: inline-block,它将覆盖display: flex,因为它在级联中稍后出现并且具有相同的specificity level
    • 这也是不必要的,因为display: flex 默认排列所有子元素。
    • 我使用 flex 只是因为它是一个简单易用的解决方案。有关更完整的解释,请参阅此帖子:stackoverflow.com/q/45536537/3597276
    猜你喜欢
    • 2018-10-15
    • 2022-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    相关资源
    最近更新 更多