简体   繁体   中英

CSS column responsive design

I have a sidebar that i need to be responsive depending on the viewport. Here is how it looks响应式侧边栏

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM