如何使几乎空的<div>达到视口的大小而不会溢出?

  • 本文关键字:溢出 视口 何使几 div html css
  • 更新时间 :
  • 英文 :


我正在尝试制作我的个人页面,用分隔,其中每个页面都是视口的大小。(第一个只有我的"徽标"作为背景,第二个是我的个人信息,以此类推。(。然而,当我得到这个尺寸时,我意识到我无法对齐我的按钮(链接(,因为有溢出。当我设法得到溢出时,第一个div变得很小。我做错了什么?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="/css/style.css" />
<title>Carlos P.</title>
</head>
<body>
<main>
<div id="initialDiv">
<a href="#secondDiv"
><img
class="whiteArrowDown"
src="/img/white-arrow-down-circle.svg"
alt="arrow down"
/></a>
</div>
<div id="secondDiv">
<h1 class="title">Hi, i'm Carlos!</h1>
</div>
</main>
</body>
</html>

样式如下:

@font-face {
font-family: "232mksdroundlight";
src: url("/src/232MKSD.woff2") format("woff2");
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
padding: 0;
scroll-behavior: smooth;
}
body {
height: 100vh;
width: 100vw;
}
#initialDiv {
display: flex;
align-items: flex-end;
background-color: #383431;
background-image: url(/img/myLogo.svg);
background-position: center;
background-repeat: no-repeat;
height: 100%;
width: 100%;
}
.whiteArrowDown {
display: flex;
height: 50px;
width: 50px;
}
#secondDiv {
background-color: white;
height: 100%;
width: 100%;
}
h1,
p {
font-family: "232mksdroundlight";
color: #383431;
}
.title {
text-align: center;
margin-top: 60px;
margin-bottom: 60px;
width: 100%;
}

这里还有JSFiddle链接:

https://jsfiddle.net/CarlosPaps/v7aLcdqp/

提前感谢!

我要做的是使用JS将每个部分的宽度和高度设置为浏览器的高度和宽度,这将为任何用户提供网站的完整窗口视图。我可能还会设置一个最小高度,以防用户的窗口更短。在机身上使用overflow-x将确保不会出现水平滚动条,尽管它们不应该出现。

var contentBlocks = document.getElementsByClassName('content'); // Get all sections
var blockArray = Object.values(contentBlocks); //Convert to array for forEach
const windowWidth = document.documentElement.clientWidth; //grab client/borwser width
const windowHeight = document.documentElement.clientHeight; //grab client/browser height

//forEach to go through each item and set their width and height
blockArray.forEach(function(block) { 
block.style.width = windowWidth+"px";
block.style.height = windowHeight+"px";
});
body { 
height:100%;
width: 100%;
overflow-x: hidden;
}
.content {
background-color: #aaa;
min-height: 600px;
}
.content:nth-of-type(2) {
background-color: #bbb;
}
.content:nth-of-type(3) {
background-color: #ccc;
}
.content:nth-of-type(4) {
background-color: #ddd;
}
.content:nth-of-type(5) {
background-color: #eee;
}
.content:nth-of-type(6) {
background-color: #000;
}
<div class="content">1</div>
<div class="content">2</div>
<div class="content">3</div>
<div class="content">4</div>
<div class="content">5</div>
<div class="content">6</div>
<div class="content">7</div>

最新更新