Позиционирование / заполнение объектов внутри ячеек CSS Grid - PullRequest
0 голосов
/ 16 апреля 2019

Я создаю оценочные карты, где каждая карта представляет собой сетку с 1 столбцом и 6 строками.

enter image description here

Мне нужно поместить отступы вокруг текстовых ячеек, чтобы их содержимое не касалось краев. Я могу применить атрибут встроенного стиля, но это не работает:

  .plan-cost-details {
    text-align: center;
    padding: 20px;
  }

Также не вложенный div:

  .plan-cost-details > div {
    padding: 20px;
  }

Я пытался добавить отступы к ячейкам сетки, но безрезультатно. Я попытался вставить второй div внутри и применить к нему отступ - тоже ничего.

Я пытаюсь использовать сетку вместо флексбокса, чтобы все элементы располагались по карточкам.

Что мне здесь не хватает? У меня также есть Codepen с 2 картами в сетке.

карта:

<div class="pricing-plan-card">
      <div class="plan-title">Professional</div>
      <div class="plan-summary">
        Try out features that will get you up and running.
      </div>

      <div class="plan-price">
        <span class="currency">$</span>25
        <div class="plan-price-interval">per month</div>
      </div>
      <div class="plan-price-details">
        Billed annually or $12 month-to-month
      </div>
      <button class="btn btn-primary">Choose</button>

      <div class="plan-features">
        <p><strong>Free plus:</strong></p>
        <ul>
          <li>Feature item one and the benefits</li>
          <li>Feature item two and the benefits</li>
          <li>Feature item three and the benefits</li>
        </ul>

      </div>
    </div>

SCSS:


.pricing-plan-card {
  display: grid;
  padding: 1.5rem;
  background-color: #fff;
  grid-template-areas: 'title' 'summary' 'price' 'pricedetail' 'button' 'features';
  grid-template-columns: 1fr;
  grid-template-rows: 60px 60px 80px 60px 75px 1fr;

  .plan-title {
    color: magenta;
    font-family: helvetica;
    font-size: 1.5rem;
    text-align: center;
    grid-area: title;
  }

  .plan-summary {
    font-size: 1rem;
    text-align: center;
    grid-area: summary;
  }

  .plan-price {
    text-align: center;
    color: blue;
    font-family: helvetica;
    font-size: 3rem;
    letter-spacing: -.05em;
    grid-area: price;

    .currency {
      font-size: 2rem;
      vertical-align: text-top;
    }

    .plan-price-interval {
      display: block;
      font-family: arial;
      font-size: 14px;
      letter-spacing: normal;
    }

    .plan-price-details {
      text-align: center;
      grid-area: pricedetail;
    }

    button {
      display: block;
      grid-area: button;
    }

    .plan-features {
      grid-area: features;

      div {
        padding: 20px;
      }
    }
  }
}

Ответы [ 3 ]

1 голос
/ 16 апреля 2019

Поместите кнопку внутри элемента div и задайте размер кнопки (высота и ширина) на 100%.(Вы даже можете сделать это с помощью Flex)

При необходимости добавьте отступ к внешнему элементу.

Пример Codepen

.pricing-plan-cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
}

.pricing-plan-card {
  display: grid;
  padding: 1.5rem;
  background-color: #fff;
  grid-template-areas: 'title' 'summary' 'price' 'pricedetail' 'button' 'features';
  grid-template-columns: 1fr;
  grid-template-rows: 60px 60px 80px 60px 75px 1fr;
}

.pricing-plan-card .plan-title {
  color: magenta;
  font-family: helvetica;
  font-size: 1.5rem;
  text-align: center;
  grid-area: title;
}

.pricing-plan-card .plan-summary {
  font-size: 1rem;
  text-align: center;
  grid-area: summary;
}

.pricing-plan-card .button-container {
  border: 2px solid red !important;
  padding: 20px;
}

.pricing-plan-card .button-container button {
  width: 100%;
  height: 100%;
}

.pricing-plan-card .plan-price {
  text-align: center;
  color: blue;
  font-family: helvetica;
  font-size: 3rem;
  letter-spacing: -0.05em;
  grid-area: price;
}

.pricing-plan-card .plan-price .currency {
  font-size: 2rem;
  vertical-align: text-top;
}

.pricing-plan-card .plan-price .plan-price-interval {
  display: block;
  font-family: arial;
  font-size: 14px;
  letter-spacing: normal;
}

.pricing-plan-card .plan-price .plan-price-details {
  text-align: center;
  grid-area: pricedetail;
}

.pricing-plan-card .plan-price button {
  display: block;
  grid-area: button;
}

.pricing-plan-card .plan-price .plan-features {
  grid-area: features;
}

.pricing-plan-card .plan-price .plan-features div {
  padding: 20px;
}

body {
  background-color: #ccc;
  margin: 20px;
}

.container {
  max-width: 1140px;
  width: 100%;
  padding-right: 16px;
  padding-left: 16px;
  margin-right: auto;
  margin-left: auto;
}
<div class="container">

  <div class="pricing-plan-cards">


    <div class="pricing-plan-card">
      <div class="plan-title">Trial</div>
      <div class="plan-summary">
        Try out features that will get you up and running.
      </div>

      <div class="plan-price">
        FREE
      </div>
      <div class="plan-price-details">
        Billed annually or $12 month-to-month
      </div>
      <div class="button-container">
        <button class="btn btn-primary">Choose</button>
      </div>


      <div class="plan-features">
        <div>
          <p><strong>Free plus:</strong></p>
          <ul>
            <li>Feature item one and the benefits</li>
            <li>Feature item two and the benefits</li>
          </ul>
        </div>

      </div>
    </div>


    <div class="pricing-plan-card">
      <div class="plan-title">Basic</div>
      <div class="plan-summary">
        Grow with more features that deliver value.
      </div>

      <div class="plan-price">
        <span class="currency">$</span>10
        <span class="plan-price-interval">per month</span>
      </div>
      <div class="plan-price-details">
        Billed annually or $12 month-to-month
      </div>
      <div class="button-container">
        <button class="btn btn-primary">Choose</button>
      </div>

      <div class="plan-features">
        <p><strong>Free plus:</strong></p>
        <ul>
          <li>Feature item one and the benefits</li>
          <li>Feature item two and the benefits</li>
        </ul>

      </div>
    </div>


    <div class="pricing-plan-card">
      <div class="plan-title">Professional</div>
      <div class="plan-summary">
        Try out features that will get you up and running.
      </div>

      <div class="plan-price">
        <span class="currency">$</span>25
        <div class="plan-price-interval">per month</div>
      </div>
      <div class="plan-price-details">
        Billed annually or $12 month-to-month
      </div>
      <div class="button-container">
        <button class="btn btn-primary">Choose</button>
      </div>
      <div class="plan-features">
        <p><strong>Free plus:</strong></p>
        <ul>
          <li>Feature item one and the benefits</li>
          <li>Feature item two and the benefits</li>
          <li>Feature item three and the benefits</li>
        </ul>

      </div>
    </div>


    <div class="pricing-plan-card">
      <div class="plan-title">Basic</div>
      <div class="plan-summary">
        Try out features that will get you up and running.
      </div>

      <div class="plan-price">
        <span class="currency">$</span>99
        <div class="plan-price-interval">per month</div>
      </div>
      <div class="plan-price-details">
        Billed annually or $12 month-to-month
      </div>
      <div class="button-container">
        <button class="btn btn-primary">Choose</button>
      </div>

      <div class="plan-features">
        <div>
          <p><strong>Free plus:</strong></p>
          <ul>
            <li>Feature item one and the benefits</li>
            <li>Feature item two and the benefits</li>
            <li>Feature item three and the benefits</li>
            <li>Feature item four and the benefits</li>
          </ul>
        </div>

      </div>
    </div>


  </div>
</div>
0 голосов
/ 16 апреля 2019

Вот исправленный код, который, как я полагаю, вы ищете. Я добавил контейнер для кнопки, но он также исправляет ошибку вложения SCSS с помощью .plan-price div.

.pricing-plan-cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
}

.pricing-plan-card {
  display: grid;
  padding: 1.5rem;
  background-color: #fff;
  grid-template-areas: 'title' 'summary' 'price' 'pricedetail' 'button' 'features';
  grid-template-columns: 1fr;
  grid-template-rows: 60px 60px 80px 60px 75px 1fr;
}

.pricing-plan-card .plan-title {
  color: magenta;
  font-family: helvetica;
  font-size: 1.5rem;
  text-align: center;
  grid-area: title;
}

.pricing-plan-card .plan-summary {
  font-size: 1rem;
  text-align: center;
  grid-area: summary;
}

.pricing-plan-card .plan-price {
  text-align: center;
  color: blue;
  font-family: helvetica;
  font-size: 3rem;
  letter-spacing: -.05em;
  grid-area: price;
}

.pricing-plan-card .currency {
  font-size: 2rem;
  vertical-align: text-top;
}

.pricing-plan-card .plan-price-interval {
  display: block;
  font-family: arial;
  font-size: 14px;
  letter-spacing: normal;
}

.pricing-plan-card .plan-price-details {
  text-align: center;
  grid-area: pricedetail;
}

.pricing-plan-card .button-wrap {
  grid-area: button;
}

.pricing-plan-card .plan-features {
  grid-area: features;
}

.pricing-plan-card .plan-features div {
  padding: 20px;
}

body {
  background-color: #ccc;
  margin: 20px;
}

.container {
  max-width: 1140px;
  width: 100%;
  padding-right: 16px;
  padding-left: 16px;
  margin-right: auto;
  margin-left: auto;
}
<div class="container">
  <div class="pricing-plan-cards">
    <div class="pricing-plan-card">
      <div class="plan-title">Trial</div>
      <div class="plan-summary">
        Try out features that will get you up and running.
      </div>

      <div class="plan-price">
        FREE
      </div>
      <div class="plan-price-details">
        Billed annually or $12 month-to-month
      </div>
      <div class="button-wrap">
        <button class="btn btn-primary">Choose</button>
      </div>

      <div class="plan-features">
        <div>
          <p><strong>Free plus:</strong></p>
          <ul>
            <li>Feature item one and the benefits</li>
            <li>Feature item two and the benefits</li>
          </ul>
        </div>

      </div>
    </div>
  </div>
</div>

Правильный SCSS:

.pricing-plan-cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
}

.pricing-plan-card {
  display: grid;
  padding: 1.5rem;
  background-color: #fff;
  grid-template-areas: 'title' 'summary' 'price' 'pricedetail' 'button' 'features';
  grid-template-columns: 1fr;
  grid-template-rows: 60px 60px 80px 60px 75px 1fr;

  .plan-title {
    color: magenta;
    font-family: helvetica;
    font-size: 1.5rem;
    text-align: center;
    grid-area: title;
  }

  .plan-summary {
    font-size: 1rem;
    text-align: center;
    grid-area: summary;
  }

  .plan-price {
    text-align: center;
    color: blue;
    font-family: helvetica;
    font-size: 3rem;
    letter-spacing: -.05em;
    grid-area: price;


    .currency {
      font-size: 2rem;
      vertical-align: text-top;
    }

    .plan-price-interval {
      display: block;
      font-family: arial;
      font-size: 14px;
      letter-spacing: normal;
    }
 }

    .plan-price-details {
      text-align: center;
      grid-area: pricedetail;
    }

  .button-wrap {
    grid-area: button;
    button {
      // display: block;
    }
  }

    .plan-features {
      grid-area: features;

      div {
        padding: 20px;
      }
    }
}



body {
  background-color: #ccc;
  margin: 20px;
}
.container {
  max-width:1140px;
      width: 100%;
    padding-right: 16px;
    padding-left: 16px;
    margin-right: auto;
    margin-left: auto;

}
0 голосов
/ 16 апреля 2019

Я проверил ваш код и все показывает как надо. Вы правильно добавляете свой CSS-файл?

Я знаю, что это не ответ, но у меня недостаточно репутации, чтобы комментировать ваш вопрос.

Кроме того, какой браузер вы используете?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...