为什么网格项没有移到末尾?

问题描述 投票:2回答:2

我正在尝试将“ Contact”一词发送到表格的末尾,但它似乎完全没有移动。

我是使用CSS Grid属性的新手,所以我不确定自己在做什么错。我有一个用于页面中所有元素的网格容器,还有一个“绿色”类,其中包含具有绿色背景的所有单词。我给“联系人”设置了一个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
2个回答
0
投票

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

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

这是使用嵌套flex容器的简单解决方案:

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

0
投票

网格已应用于.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>
© www.soinside.com 2019 - 2024. All rights reserved.