Sofia Lazrak
Sofia Lazrak

Reputation: 314

CSS column responsive design

I have a sidebar that i need to be responsive depending on the viewport. Here is how it looks responsive sidebar

I was wondering if there was a way to write one piece of code and some css without having to write different html for every device.

Here is what I wrote so far for desktop view

<div class="mycontainer">
    <div>
        <p>A</p>
        <p>B</p>
    </div>
        <div>C</div>
        <div>D</div>
    </div>
    <div>E</div>
    <div>F</div>
    <div>
        <p>G</p>
        <p>H</p>
    </div>
</div>

the css:

.mycontainer{
  width:194px; 
  height:291px; 
  border-radius: 8px; 
  box-shadow: 0px 3px 6px #00000029; 
  margin-left: 30px;
  background: #FFF; 
  z-index:100;
}
.mycontainer div{
  border-bottom:  1px solid #E5E5E5;
  text-align: center;
}

.mycontainer div:first-child, .mycontainer div:nth-child(2){
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 10px 0;
}

.mycontainer div:nth-child(3), .mycontainer div:nth-child(4){
  display: flex;
  flex-direction: row;
  align-items: center;
  
}
.mycontainer div:nth-child(5){
  border:  none;
}

Any help would be appreciated

Upvotes: 0

Views: 71

Answers (1)

Nikola Pavicevic
Nikola Pavicevic

Reputation: 23490

One of the ways is with media queries and css grid

.mycontainer{
  width:194px; 
  height:291px; 
  border-radius: 8px; 
  box-shadow: 0px 3px 6px #00000029; 
  margin-left: 30px;
  background: #FFF; 
  z-index:100;
  display: grid;
  justify-items: center;
  align-items: center;
}
.ef {
  justify-self: left;
}
@media (max-width:801px) { 
  .mycontainer{
    grid-template-columns: auto auto;
  }
  .ab, .cd, .ef, .gh {
    display: flex;
  }
  .ef {
    justify-self: center;
  }
}
@media (max-width:481px)  { 
  .mycontainer{
    grid-template-columns: auto auto;
  }
  .ab {
    display: grid;
  }
  .cd, .gh {
    display: flex;
  }
  .ef {
    justify-self: center;
    grid-area: 2 / 1 / 3 / 3;
  }
  .gh {
     grid-area: 3 / 1 / 4 / 3;
  }
}
<div class="mycontainer">
    <div class="ab">
      <p>A</p>
      <p>B</p>
    </div>
    <div class="cd">
      <div>C</div>
      <div>D</div>
    </div>
    <div class="ef">
      <div>E</div>
      <div>F</div>
    </div>
    <div class="gh">
      <p>G</p>
      <p>H</p>
    </div>
</div>

Upvotes: 2

Related Questions