Элементы CSS Grid не идут, как ожидалось - PullRequest
0 голосов
/ 27 января 2019

Я пытаюсь выровнять элементы во вложенной сетке с помощью назначения grid-column / grid-row после объявления шаблона с помощью grid-template-row: / grid-template-columns. Но когда я иду, чтобы поместить вещи, где я хотел бы их, они оказываются в неожиданных областях. Элементы в $ 50 и Give Now должны отображаться в строке 3, один из которых - в столбце 1, а другой - в столбце 2. Вместо этого они отображаются в столбце 2 в строках 1 и 2. Я не могу понять, почему они отображаются именно так. Буду признателен за любую помощь в этом вопросе.

Спасибо

.mockup {
  display: grid;
  max-width: 303px;
  position: relative;
  margin: auto;
  grid-gap: 10px;
  grid-template-areas: "infobar infobar" "progbar progbar" "main       main" "button2 button3";
  grid-template-columns: 50% 50%;
  grid-template-rows: 65px 20px 250px 50px 50px;
}

.infobar {
  grid-area: infobar;
  padding: 5px 18px;
  border-radius: 4px;
  color: white;
  background-color: #424242;
  font-family: "rooney-web", "AmericanTypewriter";
  white-space: nowrap;
  text-align: center;
}

.infobar:before {
  content: ' ';
  display: block;
  position: relative;
  bottom: -48px;
  width: 14px;
  height: 18px;
  right: -247px;
  background-color: #424242;
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
}

.progbar {
  grid-area: progbar;
  border: 1px solid #777;
  margin: none;
  background: linear-gradient(to right, #EF5F3C 75%, #eaeaea 25%);
}

.main {
  grid-area: main;
  border: 1px solid #777;
  display: grid;
  grid-template-columns: 50% 50%;
  grid-template-rows:  repeat(4, 25%)
}



.main .input p4{
  border: 1px solid #777;
  border-radius: 4px;
  grid-column: 1;
  grid-row: 3;
  font-size: 20px;
  z-index: 1;
  
}

.main .button1 p3 {
  grid-column: 2;
  grid-row: 3;
  white-space: nowrap;
  background-color: Green;
  border: 1px solid #777;
  border-radius: 4px;
  text-align: center;
  color: white;
  z-index: 1;
}

.main p1 {
  grid-column: 1 / 2;
  grid-row: 1;
}

.main p2 {
  grid-column:1 / 2;
  grid-row: 2;
  
}

.main p5 {
  grid-column: 1 / 2;
  grid-row: 4;
  color: #20A1D4;
}

.button2 {
  grid-area: button2;
  border: 1px solid #777;
  border-radius: 4px;
  text-align: center;
  background-color: #eaeaea;
}

.button3 {
  grid-area: button3;
  white-space: nowrap;
  border: 1px solid #777;
  border-radius: 4px;
  text-align: center;
  background-color: #eaeaea;
}
<!DOCTYPE html>
<html>

  <head>
    <link rel='stylesheet' type='text/css' href='style.css'>
  </head>

  <body>
    <div class="mockup">
      <section class="infobar">$167 still needed for this project</section>
      <section class="progbar"></section>
      <section class="main">
        <p1>
          <span style="color: #EF5F3C">Only 3 days           left</span> to fund this project.
        </p1>
        <p2>
          Join the <strong>42</strong> others who             have already supported this project.               Every dollar helps.
        </p2>
      <section class="input">
        <p4>
          $ <strong>50</strong>
        </p4>
      </section>
      <section class="button1">
        <p3>
          Give Now
        </p3>
      </section>
        <p5>
          Why give $50?
        </p5>
      </section>
      <section class="button2">
        <p>
          Save for later
        </p>
      </section>
      <section class="button3">
        <p>
          Tell your friends
        </p>
      </section>
    </div>
  </body>

</html>

Вот оно в JSFIddle

1 Ответ

0 голосов
/ 27 января 2019

Вы перепутали свои числа сетки.Вы не хотите считать столбцы, вы хотите считать строки.Возьмите кусок макулатуры и отметьте 3 вертикальные линии (для ваших двух столбцов), а затем горизонтальные линии (для ваших строк).Когда вы отметили p1, p2 и p5 для указания диапазона столбцов, вы (вполне логично) сказали 1/2 для столбцов один и два.Правильный способ сделать это - посчитать строки.Посмотрите на свой клочок бумаги, эти занятия должны начинаться с первой строки и заканчиваться на третьей.Итак, быстрое изменение вашего CSS, и вы должны быть "золотым".(примечание: ссылка ниже для получения дополнительной информации)

.main p1 {
  grid-column: 1 / 3;
  grid-row: 1;
}

.main p2 {
  grid-column:1 / 3;
  grid-row: 2;
}

.main p5 {
  grid-column: 1 / 3;
  grid-row: 4;
  color: #20A1D4;
}

Элемент сетки CSS w3schools

...