导航栏在缩小时不会停留在 header 中。我怎样才能做到这一点?
Nav bar won't stay in header when made smaller. How can I achieve this?
我遇到的问题是我的菜单栏不会保留在 header。
我正在尝试设计单个页面,但我不知道如何在 window 变小时防止导航菜单掉出蓝色 header。
我试过使用 min-height
、overflow
等,但我想我可能遗漏了一些明显的东西。
可以给我一些help/advice吗?
谢谢,
安尼施
HTML
<header>
<div class="containernav">
<img src="Images/VAC_Modern_LOGO_V3_optometrists.jpg" alt="logo" class="logo">
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#eyecare">Eye Care</a></li>
<li><a href="#contactus">Contact Us</a></li>
</ul>
</nav>
</div>
</header>
<body>
<div id="home">
<div class="textbox">
<h3>lorem</h3>
<p>Lorem</p>
</body>
CSS
@charset "utf-8";
body {
margin: 0;
font-family: 'Roboto', sans-serif;
background: rgba(4, 26, 125, 0.5);
}
.containernav {
display: block;
width: 80%;
margin: 0;
}
img {
float: left;
height: 200px;
width: 250px;
}
header {
background: rgb(4, 26, 125);
}
header::after {
content: '';
display: table;
clear: both;
}
.logo {
float: left;
padding: 5px 0;
}
nav {
float: left;
margin-top: 10%;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
position: absolute;
}
nav li {
display: inline-block;
margin-left: 70px;
padding-top: 33px;
position: relative;
}
nav a {
color: rgba(255, 255, 255, 0.5);
text-decoration: none;
text-transform: uppercase;
font-size: 16px;
}
nav a:hover {
color: rgba(249, 29, 0, 0.5);
}
nav a::before {
content: '';
display: block;
height: 5px;
width: 0%;
background-color: rgb(255, 255, 255);
position: absolute;
top: 0;
transition: all ease-in-out 250ms;
}
nav a:hover::before {
width: 100%;
}
button {
background-color: rgba(85, 85, 85, 0.5);
color: rgba(255, 255, 255, 0.7);
height: auto;
width: auto;
padding 100px 50px;
cursor: pointer;
float: right;
font-size: 22px;
}
您的导航栏非常混乱,我建议您使用 width % 和 float 创建它。这是一个基本的导航栏,它会调整大小并始终保持在一行上。
您可以使用纯 CSS 来做到这一点,只需确保您要显示的元素直接在父元素之后。
.navTitle {
width: 20%;
float: left;
background-color: lightGrey;
cursor: pointer;
border-bottom: 1px solid black;
}
.navTitle:hover .moreInfo {
display: inline-block;
}
.moreInfo {
margin: 0;
background-color: grey;
display: none;
width: 100%;
}
<div class="navTitle">
<p>Section 1</p>
<p class="moreInfo">More info 1</p>
</div>
<div class="navTitle">
<p>Section 2</p>
<p class="moreInfo">More info 2</p>
</div>
<div class="navTitle">
<p>Section 3</p>
<p class="moreInfo">More info 3</p>
</div>
<div class="navTitle">
<p>Section 4</p>
<p class="moreInfo">More info 4</p>
</div>
<div class="navTitle">
<p>Section 5</p>
<p class="moreInfo">More info 5</p>
</div>
查看下面的代码。
@charset "utf-8";
body {
margin: 0;
font-family: 'Roboto', sans-serif;
background: rgba(4, 26, 125, 0.5);
}
.containernav {
display: flex;
align-items: center;
width: 100%;
margin: 0;
}
img {
height: 200px;
width: 250px;
}
header {
background: rgb(4, 26, 125);
}
header::after {
content: '';
display: table;
clear: both;
}
.logo {
padding: 5px 0;
}
nav {
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
margin-left: 70px;
padding-top: 33px;
position: relative;
}
nav a {
color: rgba(255, 255, 255, 0.5);
text-decoration: none;
text-transform: uppercase;
font-size: 16px;
}
nav a:hover {
color: rgba(249, 29, 0, 0.5);
}
nav a::before {
content: '';
display: block;
height: 5px;
width: 0%;
background-color: rgb(255, 255, 255);
position: absolute;
top: 0;
transition: all ease-in-out 250ms;
}
nav a:hover::before {
width: 100%;
}
button {
background-color: rgba(85, 85, 85, 0.5);
color: rgba(255, 255, 255, 0.7);
height: auto;
width: auto;
padding 100px 50px;
cursor: pointer;
float: right;
font-size: 22px;
}
<header>
<div class="containernav">
<img src="Images/VAC_Modern_LOGO_V3_optometrists.jpg" alt="logo" class="logo">
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#eyecare">Eye Care</a></li>
<li><a href="#contactus">Contact Us</a></li>
</ul>
</nav>
</div>
</header>
<body>
<div id="home">
<div class="textbox">
<h3>lorem</h3>
<p>Lorem</p>
</body>
我遇到的问题是我的菜单栏不会保留在 header。
我正在尝试设计单个页面,但我不知道如何在 window 变小时防止导航菜单掉出蓝色 header。
我试过使用 min-height
、overflow
等,但我想我可能遗漏了一些明显的东西。
可以给我一些help/advice吗?
谢谢,
安尼施
HTML
<header>
<div class="containernav">
<img src="Images/VAC_Modern_LOGO_V3_optometrists.jpg" alt="logo" class="logo">
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#eyecare">Eye Care</a></li>
<li><a href="#contactus">Contact Us</a></li>
</ul>
</nav>
</div>
</header>
<body>
<div id="home">
<div class="textbox">
<h3>lorem</h3>
<p>Lorem</p>
</body>
CSS
@charset "utf-8";
body {
margin: 0;
font-family: 'Roboto', sans-serif;
background: rgba(4, 26, 125, 0.5);
}
.containernav {
display: block;
width: 80%;
margin: 0;
}
img {
float: left;
height: 200px;
width: 250px;
}
header {
background: rgb(4, 26, 125);
}
header::after {
content: '';
display: table;
clear: both;
}
.logo {
float: left;
padding: 5px 0;
}
nav {
float: left;
margin-top: 10%;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
position: absolute;
}
nav li {
display: inline-block;
margin-left: 70px;
padding-top: 33px;
position: relative;
}
nav a {
color: rgba(255, 255, 255, 0.5);
text-decoration: none;
text-transform: uppercase;
font-size: 16px;
}
nav a:hover {
color: rgba(249, 29, 0, 0.5);
}
nav a::before {
content: '';
display: block;
height: 5px;
width: 0%;
background-color: rgb(255, 255, 255);
position: absolute;
top: 0;
transition: all ease-in-out 250ms;
}
nav a:hover::before {
width: 100%;
}
button {
background-color: rgba(85, 85, 85, 0.5);
color: rgba(255, 255, 255, 0.7);
height: auto;
width: auto;
padding 100px 50px;
cursor: pointer;
float: right;
font-size: 22px;
}
您的导航栏非常混乱,我建议您使用 width % 和 float 创建它。这是一个基本的导航栏,它会调整大小并始终保持在一行上。
您可以使用纯 CSS 来做到这一点,只需确保您要显示的元素直接在父元素之后。
.navTitle {
width: 20%;
float: left;
background-color: lightGrey;
cursor: pointer;
border-bottom: 1px solid black;
}
.navTitle:hover .moreInfo {
display: inline-block;
}
.moreInfo {
margin: 0;
background-color: grey;
display: none;
width: 100%;
}
<div class="navTitle">
<p>Section 1</p>
<p class="moreInfo">More info 1</p>
</div>
<div class="navTitle">
<p>Section 2</p>
<p class="moreInfo">More info 2</p>
</div>
<div class="navTitle">
<p>Section 3</p>
<p class="moreInfo">More info 3</p>
</div>
<div class="navTitle">
<p>Section 4</p>
<p class="moreInfo">More info 4</p>
</div>
<div class="navTitle">
<p>Section 5</p>
<p class="moreInfo">More info 5</p>
</div>
查看下面的代码。
@charset "utf-8";
body {
margin: 0;
font-family: 'Roboto', sans-serif;
background: rgba(4, 26, 125, 0.5);
}
.containernav {
display: flex;
align-items: center;
width: 100%;
margin: 0;
}
img {
height: 200px;
width: 250px;
}
header {
background: rgb(4, 26, 125);
}
header::after {
content: '';
display: table;
clear: both;
}
.logo {
padding: 5px 0;
}
nav {
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
margin-left: 70px;
padding-top: 33px;
position: relative;
}
nav a {
color: rgba(255, 255, 255, 0.5);
text-decoration: none;
text-transform: uppercase;
font-size: 16px;
}
nav a:hover {
color: rgba(249, 29, 0, 0.5);
}
nav a::before {
content: '';
display: block;
height: 5px;
width: 0%;
background-color: rgb(255, 255, 255);
position: absolute;
top: 0;
transition: all ease-in-out 250ms;
}
nav a:hover::before {
width: 100%;
}
button {
background-color: rgba(85, 85, 85, 0.5);
color: rgba(255, 255, 255, 0.7);
height: auto;
width: auto;
padding 100px 50px;
cursor: pointer;
float: right;
font-size: 22px;
}
<header>
<div class="containernav">
<img src="Images/VAC_Modern_LOGO_V3_optometrists.jpg" alt="logo" class="logo">
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#eyecare">Eye Care</a></li>
<li><a href="#contactus">Contact Us</a></li>
</ul>
</nav>
</div>
</header>
<body>
<div id="home">
<div class="textbox">
<h3>lorem</h3>
<p>Lorem</p>
</body>