Responsive Layout when using grid

假装没事ソ 提交于 2021-02-11 12:30:09

问题


The website is designed to have 6 big squares, 3 per row, in a grid layout.

I am trying to make it responsive, so if someone zooms the website would adapt... and it kind of does, but in a bad way.

I want the squares to lay different when zooming; If now they are 3 per row I want them to go 2 per row and finally 1 per row if zooming enough. Instead of that the squares narrow themselves in their width in order to fit.

/*///////////GENERAL//////////*/
* {
  margin: 0px;
  padding: 0px;
  box-sizing: border-box;
}

/*///////////HEADER//////////*/

header {
  text-align: center;
  padding: 10px;
  margin-bottom: 20px;
  border-bottom: 1px solid black;
}

#HeaderContainer {
  max-width: 1334px;
  margin-left: auto;
  margin-right: auto;
  display: grid;
  grid-template-columns: repeat(1, 1fr 2fr 1fr);
  grid-auto-rows: minmax(20px, auto);
}
header > div > p {
  padding: 15px;

  font-size: 20px;
  font-weight: lighter;
  font-family: Helvetica, Arial, Sans-serif;

  grid-column: 2/3;
  max-width: 980px;
}
/*///////////MAINSECTION//////////*/

#MainSectionContainer {
  margin-left: auto;
  margin-right: auto;
  max-width: 1000px;
  background: white;
}

section {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: minmax(150px, auto);
  gap: 10px;
}
.SectionBox {
  min-width: 324px, auto;
  display: grid;
  align-content: center;
  justify-content: center;
  border-radius: 30px;
  border: 2px solid black;
  font-family: Helvetica, Arial, Sans-serif;
}

#photo {
  grid-row: 1/3;
}
#web {
  grid-row: 1/3;
}
#coding {
  grid-row: 1/3;
}
#cv {
  grid-row: 3/5;
}
#about {
  grid-row: 3/5;
}
#contact {
  grid-row: 3/5;
}
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="MyPortfolio" content="MyPortfolio" />
    <link rel="stylesheet" href="StylesMainPage.css" />
  </head>

  <body>
    <header>
      <div id="HeaderContainer">
        <p>WELCOME TO MY PORTFOLIO</p>
      </div>
    </header>
    <div id="MainSectionContainer">
      <section>
        <p id="photo" class="SectionBox">PHOTOGRAPHY</p>
        <p id="web" class="SectionBox">WEB DESIGN</p>
        <p id="coding" class="SectionBox">CODING</p>
        <p id="cv" class="SectionBox">CURRICULUM VITAE</p>
        <p id="about" class="SectionBox">ABOUT ME</p>
        <p id="contact" class="SectionBox">CONTACT</p>
      </section>
    </div>
  </body>
</html>

回答1:


You might need auto-fit and auto-flow

Possible example below,

or https://codepen.io/gc-nomade/pen/mdeYpWK with a max numbers of columns set to 3

/*///////////GENERAL//////////*/
*{
    margin: 0px;
    padding: 0px;
    box-sizing: border-box ;
}

/*///////////HEADER//////////*/

header{
    text-align: center;
    padding: 10px;
    margin-bottom: 20px;
    border-bottom:1px solid black;
    
    
}

#HeaderContainer{

    max-width: 1334px;
    margin-left: auto;
    margin-right: auto;
    display: grid;
    grid-template-columns: repeat(1, 1fr 2fr 1fr);
    grid-auto-rows: minmax(20px, auto);
    
}
header > div > p {

    padding: 15px;

    font-size: 20px;
    font-weight: lighter;
    font-family: Helvetica, Arial, Sans-serif;

    grid-column: 2/3; 
    max-width: 980px;
    
}
/*///////////MAINSECTION//////////*/

#MainSectionContainer{
    margin-left: auto;
    margin-right: auto;
    max-width: 1000px;
    background: white;
}

section{
    display: grid;
    grid-template-columns: repeat(auto-fit,minmax(200px,1fr));
    grid-auto-rows: minmax(150px, auto);
    gap: 10px;  
}
.SectionBox{
    min-width: 324px, auto;
    display: grid;
    align-content: center;
    justify-content: center;
    border-radius: 30px;
    border: 2px solid black;
    font-family: Helvetica, Arial, Sans-serif;
}

#photo{
 
    
}
#web{
 
    
}
#coding{
 
    
}
#cv{
   
   
}
#about{
   
}
#contact{
    
    
}


/* ///////// IE11 alternative ////////////////////////*/
section{   
    display: -ms-grid;
    -ms-grid-columns:1fr 10px  1fr 10px  1fr;
    -ms-grid-rows: auto 10px  auto;
}
.SectionBox{
    min-height:150px;
    display: -ms-flexbox;
    flex-direction:column;
    align-items:center;
}


#photo{
    -ms-grid-row: 1;
    -ms-grid-column: 1;    
}
#web{
    -ms-grid-row: 1;
    -ms-grid-column: 3;    
}
#coding{
    -ms-grid-row: 1;
    -ms-grid-column: 5;    
}
#cv{
    -ms-grid-row: 3;
    -ms-grid-column: 1;   
}
#about{
    -ms-grid-row: 3;
    -ms-grid-column: 3;    
}
#contact{
    -ms-grid-row: 3;
    -ms-grid-column: 5;    
}
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="MyPortfolio" content="MyPortfolio">
    <link rel="stylesheet" href="StylesMainPage.css">

</head>

<body>
    <header>
        <div id="HeaderContainer">
            <p>WELCOME TO MY PORTFOLIO</p>
        </div>
    </header>
    <div id="MainSectionContainer">
        
        <section>
           <p id="photo" class="SectionBox">PHOTOGRAPHY</p>
           <p id="web" class="SectionBox">WEB DESIGN</p>
           <p id="coding" class="SectionBox">CODING</p>
           <p id="cv" class="SectionBox">CURRICULUM VITAE</p>
           <p id="about" class="SectionBox">ABOUT ME</p>
           <p id="contact" class="SectionBox">CONTACT</p>


        </section>
    </div>
</body>
    
</html>

jsbin for IE11 testing : https://jsbin.com/sifozaduso/1/edit?css,output



来源:https://stackoverflow.com/questions/62039214/responsive-layout-when-using-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!