首页 > 解决方案 > 使用网格时的响应式布局

问题描述

该网站设计有 6 个大正方形,每行 3 个,采用网格布局。

我正在努力使其具有响应性,因此如果有人缩放网站会适应......它确实如此,但方式很糟糕。

我希望正方形在缩放时放置不同;如果现在它们是每行 3 个,我希望它们每行 2 个,如果缩放足够大,最后每行 1 个。取而代之的是,正方形的宽度会变窄以适应。

/*///////////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>

标签: htmlcssresponsive-designresponsive

解决方案


您可能需要auto-fit自动流动

下面可能的例子,

https://codepen.io/gc-nomade/pen/mdeYpWK,最大列数设置为 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>

用于 IE11 测试的 jsbin :https ://jsbin.com/sifozaduso/1/edit?css,output


推荐阅读