如何在 HTML 中以移动优先方式使用媒体查询?
我们可以组织我们的布局以适应各种不同的设备及其屏幕尺寸,并使用媒体查询使其响应。这种多样性是使用两种类型的参数定义的,屏幕宽度和方向。
您可以采用两种方法来创建响应式网站:
- 移动优先方法:在这种方法中,现有 CSS 用于移动视图,然后您使用媒体查询覆盖现有 CSS 以适应不断增加的宽度大小。
- 桌面优先方法:在这种方法中,现有 CSS 用于桌面视图,然后您使用媒体查询覆盖现有 CSS 以适应不断减小的宽度大小。
在这里,我们专注于如何使用前一种方法使我们的网站具有响应性,即移动优先方法。这种方法使用最小宽度媒体查询和横向。
句法:
@media (min-width: 640px) {
// CSS properties
}
示例 1:这是最初的网站,它没有响应。
HTML:此文件仅包含 HTML 部分
HTML
Page Title
Jenny Doe
Geeks For Geeks Courses
Complete preparation package will help you
learn 4 years' worth of programming knowledge
in just 6 months!
CSS
* {
box-sizing: border-box;
font-family: "Mukta", sans-serif;
color: rgb(10, 146, 10);
}
main {
overflow-y: scroll;
height: 100vh;
padding: 40px;
}
body {
margin: 0;
display: grid;
grid-template-rows: 260px 1fr;
max-height: 100vh;
overflow: hidden;
}
h1 {
margin-top: 0;
font-size: 24px;
line-height: 1;
text-transform: uppercase;
margin-bottom: 12px;
}
p {
margin: 0;
font-size: 16px;
font-weight: 300;
}
section {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin-left: 7vw;
border-bottom: solid 1px #dbdce1;
border-right: none;
align-items: center;
}
section img {
border-radius: 50%;
width: 150px;
}
.projects img {
width: 100%;
}
.projects {
margin-top: 32px;
display: grid;
grid-template-columns: repeat(1, 1fr);
gap: 30px;
align-items: center;
}
HTML
Page Title
Jenny Doe
Geeks For Geeks Courses
Complete preparation package will help you
learn 4 years' worth of programming knowledge
in just 6 months!
CSS
/* Initial layout for desktop first */
* {
box-sizing: border-box;
font-family: "Mukta", sans-serif;
color: rgb(10, 146, 10);
}
main {
overflow-y: scroll;
height: 100vh;
padding: 40px;
}
body {
margin: 0;
display: grid;
grid-template-rows: 260px 1fr;
max-height: 100vh;
overflow: hidden;
}
h1 {
margin-top: 0;
font-size: 24px;
line-height: 1;
text-transform: uppercase;
margin-bottom: 12px;
}
p {
margin: 0;
font-size: 16px;
font-weight: 300;
}
section {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
/* display: block; */
margin-left: 7vw;
border-bottom: solid 1px #dbdce1;
border-right: none;
align-items: center;
}
section img {
border-radius: 50%;
width: 150px;
}
.projects img {
width: 100%;
}
.projects {
margin-top: 32px;
display: grid;
grid-template-columns: repeat(1, 1fr);
gap: 30px;
align-items: center;
}
/* Media Queries */
@media (min-width: 640px) {
.projects {
grid-template-columns: repeat(2, 1fr);
gap: 40px;
}
}
@media (min-width: 768px) {
.projects {
grid-template-columns: repeat(3, 1fr);
gap: 50px;
}
h1 {
font-size: 40px;
}
p {
font-size: 18px;
}
}
@media (min-width: 1024px) {
.projects {
grid-template-columns: repeat(4, 1fr);
gap: 60px;
}
}
@media (min-width: 640px) and (orientation: landscape) {
body {
grid-template-columns: 160px 1fr;
grid-template-rows: none;
}
section {
border-bottom: none;
margin-left: -4px;
border-right: solid 1px #dbdce1;
}
section img {
width: 140px;
}
}
CSS:此文件包含没有媒体查询的 CSS。
CSS
* {
box-sizing: border-box;
font-family: "Mukta", sans-serif;
color: rgb(10, 146, 10);
}
main {
overflow-y: scroll;
height: 100vh;
padding: 40px;
}
body {
margin: 0;
display: grid;
grid-template-rows: 260px 1fr;
max-height: 100vh;
overflow: hidden;
}
h1 {
margin-top: 0;
font-size: 24px;
line-height: 1;
text-transform: uppercase;
margin-bottom: 12px;
}
p {
margin: 0;
font-size: 16px;
font-weight: 300;
}
section {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin-left: 7vw;
border-bottom: solid 1px #dbdce1;
border-right: none;
align-items: center;
}
section img {
border-radius: 50%;
width: 150px;
}
.projects img {
width: 100%;
}
.projects {
margin-top: 32px;
display: grid;
grid-template-columns: repeat(1, 1fr);
gap: 30px;
align-items: center;
}
输出:
示例 2:这是最初使用媒体查询的网站。
HTML:此文件仅包含 HTML 部分
HTML
Page Title
Jenny Doe
Geeks For Geeks Courses
Complete preparation package will help you
learn 4 years' worth of programming knowledge
in just 6 months!
CSS:此文件包含带有媒体查询的 CSS。
CSS
/* Initial layout for desktop first */
* {
box-sizing: border-box;
font-family: "Mukta", sans-serif;
color: rgb(10, 146, 10);
}
main {
overflow-y: scroll;
height: 100vh;
padding: 40px;
}
body {
margin: 0;
display: grid;
grid-template-rows: 260px 1fr;
max-height: 100vh;
overflow: hidden;
}
h1 {
margin-top: 0;
font-size: 24px;
line-height: 1;
text-transform: uppercase;
margin-bottom: 12px;
}
p {
margin: 0;
font-size: 16px;
font-weight: 300;
}
section {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
/* display: block; */
margin-left: 7vw;
border-bottom: solid 1px #dbdce1;
border-right: none;
align-items: center;
}
section img {
border-radius: 50%;
width: 150px;
}
.projects img {
width: 100%;
}
.projects {
margin-top: 32px;
display: grid;
grid-template-columns: repeat(1, 1fr);
gap: 30px;
align-items: center;
}
/* Media Queries */
@media (min-width: 640px) {
.projects {
grid-template-columns: repeat(2, 1fr);
gap: 40px;
}
}
@media (min-width: 768px) {
.projects {
grid-template-columns: repeat(3, 1fr);
gap: 50px;
}
h1 {
font-size: 40px;
}
p {
font-size: 18px;
}
}
@media (min-width: 1024px) {
.projects {
grid-template-columns: repeat(4, 1fr);
gap: 60px;
}
}
@media (min-width: 640px) and (orientation: landscape) {
body {
grid-template-columns: 160px 1fr;
grid-template-rows: none;
}
section {
border-bottom: none;
margin-left: -4px;
border-right: solid 1px #dbdce1;
}
section img {
width: 140px;
}
}
输出: