1

I want to align some div's with same class name in a row using css grid layout. when i tried all div are aligned in top of another one. How to achieve this using css grid layout.

   .front{
        grid-area: front;
    }
    .accountcontainer{
        display: grid;
        margin: 0 10px;
        background: #ffffff;
        grid-template-columns: 1fr 1fr 1fr 1fr;
        grid-template-areas: 
            "front   front   front  front";
        margin-bottom: 20px;
    }
2
  • Show us what effort have you made so far by adding the problematic code. Commented Dec 8, 2017 at 8:00
  • @Highdef, i add the css what i tried. Commented Dec 8, 2017 at 8:04

1 Answer 1

4

Don't use grid-area

Just specify the grid-row for that class.

Note the number of divs with that class MUST be the same as (or less than) the number of columns.

.container {
  display: grid;
  margin: 10px auto;
  background: #ffffff;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-auto-flow: rows;
  margin-bottom: 20px;
  border: 1px solid grey;
}

.item {
  height: 60px;
  background: pink;
  display: flex;
  justify-content: center;
  align-items: center;
  color: whitesmoke;
  border: 1px solid red;
}

.item.front {
  background: rebeccapurple;
}

.front {
  grid-row: 1;
}
<div class="container">
  <div class="item front">1</div>
  <div class="item front">2</div>
  <div class="item"></div>
  <div class="item front">3</div>
  <div class="item front">4</div>
  <div class="item"></div>
  <div class="item"></div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.