【发布时间】:2020-05-15 16:53:17
【问题描述】:
我设计了带有左侧导航的页面,在全屏上看起来不错,但是,当我将其调整为移动尺寸时,我的主要内容停留在一列中并且没有填满整个视口 - 留下了相当大的空白在左边。
我相信媒体查询中的某些东西可能会导致这种情况。
这是一个链接:https://codepen.io/sweexee/pen/abvqyOY
如果你能看到它和 CSS,请告诉我:
/* Typography imported from Google Fonts */
header {
font-family: "Taviraj", serif;
color: #257ecc;
}
p,
a {
font-family: Taviraj, serif;
}
/*Generic styles*/
html {
scroll-behavior: smooth;
}
html,
body {
min-width: 290px;
background-color: #ffffff;
line-height: 1.5;
}
body {
margin: 8px;
display: block;
}
a {
background-color: #257ecc;
text-decoration: none;
color: white;
text-align: center;
display: inline-block;
transition: all 0.3s;
}
a:hover {
opacity: 0.8;
color: #cc4d47;
}
p {
font-weight: 300;
font-size: 1.2rem;
}
blockquote {
border-left: 10px solid #257ecc;
margin: 1.5em 10px;
padding: 0.5em 10px;
}
blockquote:before {
color: #257ecc;
content: open-quote;
font-size: 4em;
line-height: 0.1em;
margin-right: 0.25em;
vertical-align: -0.4em;
}
blockquote p {
display: inline;
font-weight: 400;
font-style: italic;
font-size: 1.5rem;
color: #cc4d47;
}
ul {
display: block;
margin-block-start: 1em;
margin-block-end: 1em;
margin-inline-start: 0px;
margin-inline-end: 0px;
padding-inline-start: 40px;
}
li {
display: list-item;
}
/* Navigation styles*/
nav {
display: block;
}
#navbar {
position: fixed;
min-width: 290px;
top: 0px;
left: 0px;
width: 300px;
height: 100%;
background-color: #257ecc;
border-right: solid;
border-color: #828e99;
}
@media only screen and (max-width: 815px) {
#navbar {
max-height: 275px;
border: none;
border-bottom: 2px solid;
position: absolute;
width: 100%;
z-index: 1;
top: 0;
padding: 0;
margin: 0;
}
}
nav > header {
color: #ffffff;
margin: 10px;
text-align: center;
font-size: 1.9rem;
font-weight: 600;
display: block;
}
#navbar ul {
padding: 0;
height: 80%;
overflow-y: auto;
overflow-x: hidden;
}
@media only screen and (max-width: 815px) {
#navbar ul {
border: 1px solid;
height: 207px;
}
}
#navbar ul > li {
color: #ffffff;
border-top: 1px solid;
border-color: #cc4d47;
list-style: none;
position: relative;
width: 100%;
}
#navbar a {
display: block;
padding: 12px 30px;
text-decoration: none;
cursor: pointer;
font-size: 1.1rem;
font-family: "Roboto", sans-serif;
font-weight: 300;
}
/*Main ccontent styles*/
main {
display: block;
}
#main-doc {
position: absolute;
margin-left: 310px;
padding: 20px;
margin-bottom: 110px;
}
@media only screen and (max-width: 400px) {
#main-doc {
margin-left: -10px;
}
}
@media only screen and (max-width: 815px) {
#main-doc {
position: relative;
margin-top: 270px;
}
}
/* Section styling*/
section {
display: block;
}
#main-doc header {
display: block;
font-size: 1.6rem;
font-weight: 600;
text-align: left;
margin: 0px;
padding-top: 10px;
}
section article {
color: #3d4247;
margin: 15px;
}
article {
display: block;
}
section article > p {
display: block;
}
section article > img {
display: block;
max-width: 100%;
height: auto;
margin-left: auto;
margin-right: auto;
margin-top: 20px;
}
section article > ul {
list-style: none;
}
section article > ul li::before {
content: "\2022";
color: #257ecc;
font-weight: bold;
display: inline-block;
width: 1em;
margin-left: -1em;
}
section article > ul > li {
font-family: "Taviraj", sans-serif;
font-size: 1.2rem;
font-weight: 300;
color: #3d4247;
}
section article > ul > li > a {
color: #257ecc;
background-color: #ffffff;
}
【问题讨论】:
-
我没有在手机上看到这个问题,这是我看到的:screenshot.how/ZllqZF你能分享截图吗?
-
嗨,亲爱的,欢迎来到 SO。快速浏览后:您需要在移动模式下调整
#main-doc margin-left。当视口大小在 400-815px 之间时,它现在固定为320px。为什么 vp max-width 为 400px 时为 -10px?此外,页面看起来相当不错,但 CSS 过于复杂。基本上你有两个主要容器:导航栏和主容器。为什么不(例如)将您的<body>设为移动模式下的flex-direction: column和桌面模式下的flex-direction: row的flexbox 容器,并摆脱position: fixed/absolute等? -
查看Codepen MDN Holy Grail 获取一些示例代码。很容易理解/修改您自己的需求....
-
谢谢@RenevanderLende!我只是一个初学者,所以可能会使我的代码过于复杂:D 感谢您的建议,我会尝试一下!
-
快速 flexbox 101:一个 FBL 容器要么是一行列(FBL 默认),要么是一列行,容易记住!嵌套容器时重复这个“想法”并询问:我要插入的元素是一行……还是一列……一直有效!
标签: css responsive-design media-queries