简体   繁体   English

如何根据屏幕宽度调整边距大小?

[英]How do I Resize Margins Based on the Width of Screen?

I have this HTML and CSS code for a webpage. 我有此HTML和CSS代码的网页。 I am trying to make the website mobile-friendly and resizes itself with the size of screen viewed. 我正在尝试使该网站适合移动设备使用,并根据查看的屏幕大小来调整自身大小。 I want the margins to become very small when viewed on a narrow screen like a smartphone and readjusts itself gradually when the screen is bigger and margins become larger and larger until it is a full screen of, say, a desktop computer. 我希望在像智能手机这样的狭窄屏幕上观看时,边距会变得很小,并且当屏幕更大时,边距会逐渐调整,并且边距会越来越大,直到它变成台式机的全屏为止。 However, this code isn't really working. 但是,此代码并没有真正起作用。 (I didn't include all the other CSS parts of this code, but please ask for it if needed!) (我没有在代码中包含所有其他CSS部分,但是如果需要,请提出要求!)

My attempt to resize margins due to the width of the screen: 由于屏幕的宽度,我试图调整边距的大小:

        @media (max-width: 1100px, min-width: 800px) {
            body {
                margin-right: 20px;
                margin-left: 20px;
            }

        @media (max-width: 750px, min-width: 501) {
            body {
                margin-right: 5vw;
                margin-left: 5vw;
            }
        }   
        @media (max-width: 500px) {
            body {
                margin-right: 2vw;
                margin-left: 2vw;
            }
        }
    </style>
</head>
<body>
    <header>

        <h1>Blog</h1>
        <ul> <!-- Menu Tabs -->
            <li><a href="#">Home</a></li>
            <li><a href="#">Art</a></li>
            <li><a href="#">Music</a></li>
        </ul>
    </header>
</body>

Thanks, I would really appreciate your help! 谢谢,非常感谢您的帮助!

You are missing a closing brace around your first media query. 您在第一个媒体查询中缺少左括号。 Also, you have some extra bits in your media queries making them invalid. 另外,媒体查询中还有一些额外的位,使它们无效。 The way media queries work makes the min-width parts you were trying to add unnecessary. 媒体查询的工作方式使您要添加的min-width部分变得不必要。 The following code, at large screens, creates a 20px left/right margin. 在大屏幕上,以下代码创建了20px左/右页边距。 When the threshold of 750px is hit, 5vw kicks in, and so on. 达到750px的阈值时, 5vw启动5vw ,依此类推。

/* Invalid: 
   @media (max-width: 1100px, min-width: 800px) 
*/

@media (max-width: 1100px) {
  body {
    margin-right: 20px;
    margin-left: 20px;
  }
}

@media (max-width: 750px) {
  body {
    margin-right: 5vw;
    margin-left: 5vw;
  }
}

@media (max-width: 500px) {
  body {
    margin-right: 2vw;
    margin-left: 2vw;
  }
}

If your intention is to start with a default 20px right/left margin, for screens even larger than 1100px , you could create a default margin in your CSS which will be overridden by your media query rules. 如果您打算以默认的20px左右边距开始,则对于大于1100px屏幕,您可以在CSS中创建默认的边距,该默认边距将被媒体查询规则覆盖。 Then, you can begin your media queries at a narrower screen size. 然后,您可以以较小的屏幕尺寸开始媒体查询。

/* default size */
body {
  margin-left: 20px;
  margin-rights: 20px;
}

@media (max-width: 750px) {
  body {
    margin-right: 5vw;
    margin-left: 5vw;
  }
}

@media (max-width: 500px) {
  body {
    margin-right: 2vw;
    margin-left: 2vw;
  }
}

https://jsfiddle.net/5vez3rdc/ https://jsfiddle.net/5vez3rdc/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CSS HTML为什么当我以px为单位指定宽度时,东西会根据屏幕宽度调整大小? - CSS HTML why do things resize based on screen width when I specify width in px? 如何根据屏幕尺寸调整表格宽度 - How to resize table width based on screen size 如何根据 Angular/Ionic 中的屏幕尺寸使 CSS/HTML 边距响应 - How do I make CSS/HTML margins responsive based on screen size in Angular/Ionic 如何在水平窗口调整大小时缩小网页的边距? - How do I make the margins of a webpage shrink on horizontal window resize? 我如何居中 <svg> 在 <body> 根据屏幕的宽度/高度 - How do I center a <svg> in <body> based on screen width/height 如何让媒体查询根据屏幕宽度调整 div 的大小? - How can I get media query to resize div based on the width of the screen? 如何创建具有100%宽度以及左右边界的div? - How do I create a div with 100% width AND left and right margins? Bootstrap 如何根据屏幕宽度自动更改边距 - Bootstrap how to change margins automatically with screen width 如何调整导航栏按钮的大小并使其适合屏幕宽度? - How do I resize the buttons of a navigation bar and make it fit the width of the screen? 根据屏幕宽度调整 div 高度 - Resize div height based on screen width
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM