skip to Main Content

Using this code:

.grid-container {
  display: grid;
  grid-template-columns: auto auto auto;
  background-color: #2196F3;
  padding: 10px;
}

.grid-item {
  background-color: rgba(255, 255, 255, 0.8);
  border: 1px solid rgba(0, 0, 0, 0.8);
  padding: 20px;
  font-size: 30px;
  text-align: center;
}
<div class="grid-container">
  <div class="grid-item">1</div>
  <div class="grid-item">2</div>
  <div class="grid-item">3</div>
  <div class="grid-item">4</div>
  <div class="grid-item">5</div>
  <div class="grid-item">6</div>
  <div class="grid-item">7</div>
  <div class="grid-item">8</div>
  <div class="grid-item">9</div>
</div>

Which can be seen and run here: https://www.w3schools.com/css/tryit.asp?filename=trycss_grid

Would it be possible to add a css style to one of the div elements (or by some other way) to vertically offset one of the numbers by say 15 pixels.

Say specifically, to offset the 5 in the center cell by say 15 pixels vertically?

(If the attached image works then I’ve mocked up what I mean).

Thanks.

enter image description here
Picture

2

Answers


  1. Sure, you’re free to apply most of the CSS rules to grid children.

    You can add a separate class to specific elements you want offset, i.e. grid-item__offset, and a rule with 15px of bottom padding:

    .grid-container {
      display: grid;
      grid-template-columns: auto auto auto;
      background-color: #2196F3;
      padding: 10px;
    }
    
    .grid-item {
      background-color: rgba(255, 255, 255, 0.8);
      border: 1px solid rgba(0, 0, 0, 0.8);
      padding: 20px;
      font-size: 30px;
      text-align: center;
    }
    
    .grid-item__offset {
      padding: 0 0 15px;
     }
    <div class="grid-container">
      <div class="grid-item">1</div>
      <div class="grid-item">2</div>
      <div class="grid-item">3</div>
      <div class="grid-item">4</div>
      <div class="grid-item grid-item__offset">5</div>
      <div class="grid-item">6</div>
      <div class="grid-item">7</div>
      <div class="grid-item">8</div>
      <div class="grid-item">9</div>
    </div>
    Login or Signup to reply.
  2. Just use the nth-child pseudo class to select the 5th element and adjust the padding on that element. See marked up code below.

    .grid-container {
      display: grid;
      grid-template-columns: auto auto auto;
      background-color: #2196F3;
      padding: 10px;
    }
    
    .grid-item {
      background-color: rgba(255, 255, 255, 0.8);
      border: 1px solid rgba(0, 0, 0, 0.8);
      padding: 20px;
      font-size: 30px;
      text-align: center;
    }
    
    .grid-item:nth-child(5) {
      padding-bottom: 35px;
      padding-top: 5px;
    }
    <div class="grid-container">
      <div class="grid-item">1</div>
      <div class="grid-item">2</div>
      <div class="grid-item">3</div>
      <div class="grid-item">4</div>
      <div class="grid-item">5</div>
      <div class="grid-item">6</div>
      <div class="grid-item">7</div>
      <div class="grid-item">8</div>
      <div class="grid-item">9</div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search