对于不同的屏幕分辨率,我该如何保持Div的比例



我目前已经开始在一个网站上玩跳棋,但我面临着在显示器上缩放的问题,在学校,我用笔记本电脑对网站进行编码,一旦我回到家在桌面上尝试,侧导航和主内容就错位了,看起来不像是被集成的样子。。。所以我只是困惑于该做什么以及如何保持它们的大小,无论是使用笔记本电脑还是台式机。

我试着让侧导航占屏幕的25%,主内容占75%。。。但那没用。。。所以我调整了主要内容以占据85%的屏幕,这很有效,但仅在PC上,在笔记本电脑上,这又很奇怪。。。我真的被困在这里了哈哈

.divMain {
background-color: rgba(0, 0, 0, 0.596);
width: 85%;
float: right;
padding: 10px;
}

/* The side navigation menu */
.sidenav {
height: 100%;
/* 100% Full-height */
width: 25%;
position: fixed;
/* Stay in place */
z-index: 1;
/* Stay on top */
top: 0;
/* Stay at the top */
left: 0;
background-color: #111;
overflow-x: hidden;
/* Disable horizontal scroll */
padding-top: 60px;
/* Place content 60px from the top */
padding-left: 10px;
padding-right: 10px;
transition: 0.5s;
/* 0.5 second transition effect to slide in the sidenav */
}

/* The navigation menu links */
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #b4b4b4;
display: block;
transition: 0.3s;
}
.sidenav .Logo {
padding: 8px 0px 8px 0px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s;
}

/* When you mouse over the navigation links, change their color */
.sidenav a:hover {
color: #f1f1f1;
}

/* Position and style the close button (top right corner) */
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}

/* Style page content
#main {
transition: margin-left .5s;
padding: 20px;
}
/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
@media screen and (max-height: 450px) {
.sidenav {
padding-top: 15px;
}
.sidenav a {
font-size: 18px;
}
}

/* The sidenav */
.sidenav {
right: 0;
height: 100%;
width: 200px;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: rgba(0, 0, 0, 0.705);
overflow-x: hidden;
padding-top: 20px;
}

/* Page content */
.main {
margin-left: 200px;
/* Same as the width of the sidenav */
}
<!DOCTYPE html>
<html>
<head>
<style>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@600&display=swap');
</style>
<!-- Font Awesome icons !-->
<script src="https://use.fontawesome.com/cb792220ec.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Home | Checkers.place</title>
<link href="style.css" rel="stylesheet">
</head>
<body>
<div style="overflow-y: hidden;" id="mySidenav" class="sidenav">
<a class="Logo" href="index.html"><img style="width:200px;" src="logoYellow.png"></a>
<a href="#"><i class="fa fa-play-circle" aria-hidden="true"></i> Play</a>
<a href="learn.html"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learn</a>
<a href="social.html"><i class="fa fa-users" aria-hidden="true"></i> Socialize</a>
<a style="font-size:22px;" href="plus.html"><i class="fa fa-plus-circle" style="color:#FBCD08;" aria-hidden="true"></i> <span style="color:#adadad;">Checkers</span> <strong><span style="color: #FBCD08;">plus</span></strong></a>
<a href="plus.html">
<div class="plusAd">
<h1>Tired of ads?</h1>
<p>why not subscribe to Checkers Plus?</p>
<p style="font-size: 18px;"><i class="perkYes fa fa-check" aria-hidden="true"></i> Ad free</p>
<p style="font-size: 16px;"><i class="perkYes fa fa-check" aria-hidden="true"></i> Exclusive Items</p>
<p>... + More!</p>
<h2> $4.99/month </h2>
</div>
</a>
</div>
<div class="divMain" id="divMain">
<strong><p style="font-family: 'Poppins', sans-serif;" class="userNameText"><img src="anon.png" class="userIcon">xJEMAx</p></strong>
</div>
</body>
</html>

是的,还有更多的CSS,但CSS是用于用户图标和其他与divs lol 无关的东西

Flexbox是您的朋友。您可能想了解更多信息,我无法在这里解释,但通过在父元素上设置display: flex;,您可以将宽度设置为25%和75%,以便将它们并排排列。

您在.divMain.sidenav的多个位置也有一些代码,所以我清理了这些代码。您希望只在一个位置为每个选择器编写代码。

/* where the magic happens */
body {
display: flex;
}
/* Page content */
.divMain {
background-color: rgba(0, 0, 0, 0.596);
width:75%; /* WIDTH 75% */
padding:10px;
}
/* The side navigation menu */
.sidenav {
width: 25%; /* WIDTH 25% */
background-color: rgba(0, 0, 0, 0.705);
padding-top: 20px;
padding-left:10px;
padding-right:10px;
transition: 0.5s;
}
/* The navigation menu links */
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #b4b4b4;
display: block;
transition: 0.3s;
}
.sidenav .Logo {
padding: 8px 0px 8px 0px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s;
}
/* When you mouse over the navigation links, change their color */
.sidenav a:hover {
color: #f1f1f1;
}
/* Position and style the close button (top right corner) */
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}

/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
@media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}
<html>
<head>
<style>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@600&display=swap');
</style>
<!-- Font Awesome icons !-->
<script src="https://use.fontawesome.com/cb792220ec.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Home | Checkers.place</title>
<link href="style.css" rel="stylesheet">
</head>
<body>
<div style="overflow-y: hidden;"id="mySidenav" class="sidenav">
<a class="Logo" href="index.html"><img style="width:200px;" src="logoYellow.png"></a>
<a href="#"><i class="fa fa-play-circle" aria-hidden="true"></i> Play</a>
<a href="learn.html"><i class="fa fa-graduation-cap" aria-hidden="true"></i> Learn</a>
<a href="social.html"><i class="fa fa-users" aria-hidden="true"></i> Socialize</a>
<a style="font-size:22px;"href="plus.html"><i class="fa fa-plus-circle" style="color:#FBCD08;" aria-hidden="true"></i> <span style="color:#adadad;">Checkers</span> <strong><span style="color: #FBCD08;">plus</span></strong></a>
<a href="plus.html"><div class="plusAd">
<h1>Tired of ads?</h1>
<p>why not subscribe to Checkers Plus?</p>
<p style="font-size: 18px;"><i class="perkYes fa fa-check" aria-hidden="true"></i> Ad free</p>
<p style="font-size: 16px;"><i class="perkYes fa fa-check" aria-hidden="true"></i> Exclusive Items</p>
<p>... + More!</p>
<h2> $4.99/month </h2>
</div></a>
</div>

<div class="divMain" id="divMain">
<strong><p style="font-family: 'Poppins', sans-serif;" class="userNameText"><img src="anon.png" class="userIcon">xJEMAx</p></strong>
</div>
</body>
</html>

最新更新