简体   繁体   English

文本和元素不会随着浏览器窗口大小而缩放?

[英]Text and elements won't scale with browser window size?

I am working on my final project for my intro to HTML and CSS class.我正在为我的 HTML 和 CSS 类介绍做最后的项目。

I am building a very simple website, and everything seemed to be going alright until I wanted to see how the page looks when I resize the browser window.我正在构建一个非常简单的网站,一切似乎都很顺利,直到我想查看调整浏览器窗口大小时页面的外观。 The page looks fine until any sort of resizing is done, and then all of the elements start to get really messed up.页面看起来很好,直到完成任何类型的调整大小,然后所有元素开始变得非常混乱。

I am a complete noob and have been stuck trying to fix this for almost an hour now.我是一个完整的菜鸟,并且一直试图解决这个问题将近一个小时。 I'm not really sure what to do.我真的不知道该怎么做。 I'm trying to make the elements scale until a certain minimum width, but I fear ive built the whole website wrong from the beginning..我试图让元素缩放到某个最小宽度,但我担心我从一开始就错误地构建了整个网站..

Here is the code, if someone could give some insight I would be very grateful..这是代码,如果有人能提供一些见解,我将不胜感激..

 #wrapper { height: 1000px; margin: 0 auto; background-color: black; background-image: url(images/background.jpg); background-repeat: no-repeat; background-size: 100%; position: relative; } #header { height: 150px; position: absolute; background-color: red; width: 100%; } #welcome { left: 75px; position: absolute; bottom: 0; width: 420px; height: 100px; background-color: green; } .w { font-family: courier; position: absolute; font-size: 64pt; left: 20px; bottom: 0px; color: #fff; margin: 0; line-height: 1; } #main-nav { position: absolute; bottom: 0; right: 0; } #main-nav ul li { float: left; } #main-nav ul li a { color: #fff; display: inline-block; padding: 20px; text-decoration: none; font-family: courier; font-size: 24pt; white-space: no-wrap; } #main-nav ul li a.current { font-weight: bold; font-size: 28pt; } #main-nav ul li a:hover { color: #ffb0ac; } #main-content { position: relative; width: 100%; height: 500px; } .intro { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-family: courier; font-size: 36pt; color: #fff; } .button { position: absolute; top: 65%; left: 50%; transform: translate(-50%, -50%); }
 <div id="wrapper"> <header id="header"> <div id="welcome"> <h1 class="w">Welcome</h1> </div> <nav id="main-nav"> <ul> <li><a class="current" href="#">Home</a></li> <li><a href="#">Introduction</a></li> <li><a href="#">Keys</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </header> <div id="main-content"> <h1 class="intro">Basic Music Theory Introdution</h1> <img class="button" src="images/button.jpg" alt="button"> </div> </div> <!-- End of wrapper-->

Your elements are all using fixed pixel sizes in css.您的元素都在 css 中使用固定的像素大小。 you have other options there, you can set elements to percentage of viewport width (50vw) or height (50vh), or just a percentage (50%).您还有其他选项,您可以将元素设置为视口宽度 (50vw) 或高度 (50vh) 的百分比,或者只是一个百分比 (50%)。 If percentage is used inside of another element it will be scaled to that element not the browser window.如果在另一个元素内部使用百分比,它将缩放到该元素而不是浏览器窗口。

Also - you have pt sizes in your css.另外 - 你的 css 中有 pt 大小。 those dont exist in css and would need to be changed to px. css 中不存在这些,需要将其更改为 px。 There are various calculations out there for converting pt size to other units.有各种计算方法可以将 pt 大小转换为其他单位。

for example I've copied your snippets below and changed a few sizes to use viewport width (vw) viewport height (vh) and percentages.例如,我在下面复制了您的片段并更改了一些尺寸以使用视口宽度 (vw) 视口高度 (vh) 和百分比。 your css could be like this:你的 css 可能是这样的:

 #wrapper { height: 100vh; margin: 0 auto; background-color: black; background-image: url(images/background.jpg); background-repeat: no-repeat; background-size: 100%; position: relative; } #header { height: 30vh; position: absolute; background-color: red; width: 100%; } #welcome { left: 5vw; position: absolute; bottom: 0; width: 420px; height: 100px; background-color: green; } .w { font-family: courier; position: absolute; font-size: 15vw; left: 20px; bottom: 0px; color: #fff; margin: 0; line-height: 1; } #main-nav { position: absolute; bottom: 0; right: 0; } #main-nav ul li { float: left; } #main-nav ul li a { color: #fff; display: inline-block; padding: 20px; text-decoration: none; font-family: courier; font-size: 4vw; white-space: no-wrap; } #main-nav ul li a.current { font-weight: bold; font-size: 28pt; } #main-nav ul li a:hover { color: #ffb0ac; } #main-content { position: relative; width: 100%; height: 500px; } .intro { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-family: courier; font-size: 36pt; color: #fff; } .button { position: absolute; top: 65%; left: 50%; transform: translate(-50%, -50%); }
 <div id="wrapper"> <header id="header"> <div id="welcome"> <h1 class="w">Welcome</h1> </div> <nav id="main-nav"> <ul> <li><a class="current" href="#">Home</a></li> <li><a href="#">Introduction</a></li> <li><a href="#">Keys</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </header> <div id="main-content"> <h1 class="intro">Basic Music Theory Introdution</h1> <img class="button" src="images/button.jpg" alt="button"> </div> </div> <!-- End of wrapper-->

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM