html - 使用网格时的响应式布局
问题描述
该网站设计有 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>
解决方案
您可能需要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
推荐阅读
- javascript - 如何使用 jaredpalmer/presspack 与 Google 地图集成构建主题?
- android - 为什么 target="_blank" 在三星设备上的 Chrome 中不起作用?
- javascript - 完全使用 JavaScript 替换 html 元素(无库)
- angular - Angular 编译警告:“在 '@angular/core' 中找不到导出 'ɵɵdefineInjectable'
- c# - 方法调用中没有异常 UIImage.LoadFromData(null)
- python - 将python中从列到行的输出更改为csv
- tsql - 从字符串中吐出第一部分和最后一部分
- http - 使用 IIS 在服务器和客户端之间建立持续的 HTTPS 连接
- clipboard - QA 测试剪贴板操作
- php - 以模型为参数的资源控制器方法不起作用