对于作为全栈或前端 Web 开发人员工作并经常使用 css 的人,我想问你如何处理屏幕大小并使你的网站在所有平台上看起来都相同,这是我目前面临的一个真正的问题,因为我不知道什么是更好的做法,需要您的建议:您是否使用像这些@媒体屏幕和(最小宽度:npx)这样的多个媒体查询
或者您是否在代码中使边距、填充、字体大小...随着屏幕缩小而缩小。
我尝试了第一个选项,它对我来说效果很好,但我不知道这是否是一个更好的做法,第二个选项非常难,因为在设计网站并使其看起来完美之后,如果我改变一件事,整个设计就会破坏,所以我应该使用第一个选项更好还是我应该更多地学习与第二个选项相关的技能
您需要使用断点(
@media
查询)和动态调整边距、填充、字体大小等大小。仅使用后一个选项是不够的。
运行下面示例中的代码片段。调整浏览器窗口大小并注意内容和字体大小如何变化。
/* Original code from W3Schools */
.example {
font-size: 16px;
padding: 20px;
color: white;
}
/* Extra small devices (phones, 600px and down) */
@media only screen and (max-width: 600px) {
.example {
background: red;
}
}
/* Small devices (portrait tablets and large phones, 600px and up) */
@media only screen and (min-width: 600px) {
.example {
background: green;
}
}
/* Medium devices (landscape tablets, 768px and up) */
@media only screen and (min-width: 768px) {
.example {
background: blue;
}
}
/* Large devices (laptops/desktops, 992px and up) */
@media only screen and (min-width: 992px) {
.example {
background: orange;
}
}
/* Extra large devices (large laptops and desktops, 1200px and up) */
@media only screen and (min-width: 1200px) {
.example {
background: pink;
}
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h2>Typical Breakpoints</h2>
<p class="example">Resize the browser window to see how the background color of this paragraph changes on different screen sizes.</p>
</body>
</html>
/* Original code from W3Schools */
.example {
font-size: 3vw;
padding: 20px;
color: white;
}
/* Extra small devices (phones, 600px and down) */
@media only screen and (max-width: 600px) {
.example {
background: red;
}
}
/* Small devices (portrait tablets and large phones, 600px and up) */
@media only screen and (min-width: 600px) {
.example {
background: green;
}
}
/* Medium devices (landscape tablets, 768px and up) */
@media only screen and (min-width: 768px) {
.example {
background: blue;
}
}
/* Large devices (laptops/desktops, 992px and up) */
@media only screen and (min-width: 992px) {
.example {
background: orange;
}
}
/* Extra large devices (large laptops and desktops, 1200px and up) */
@media only screen and (min-width: 1200px) {
.example {
background: pink;
}
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h2>Typical Breakpoints</h2>
<p class="example">Resize the browser window to see how the background color of this paragraph changes on different screen sizes.</p>
</body>
</html>