如何在 html 和 css 中获得缩小到适合的导航栏
How do I get a shrink-to-fit navbar in html and css
我正在使用 HTML 和 CSS,但似乎无法找到一种方法让我的导航栏随 window 调整大小。当我缩小 window 时,我从我的导航栏中得到了一堆可怕的图标。有没有任何代码可以提供帮助?
这是我的代码:
navbar{position: absolute;
top: 145px;
left: 20px;
width: auto;
list-style-type: none;
background-color: #ff0000
}
navbar ul{
font-size:42px;
}
navbar li{
display: inline-block;
}
navbar a{
border-right:2px solid #000000;
color:#000000;
font-size:0.9em;
text-transform:uppercase
}
navbar li a{
text-decoration: none;
background-color: #ff0000;
color: #000001
height: 50px;
padding: 16px 12px;
border-spacing: 10px;
transition: 0.5s ease;
}
navbar li a:hover{
background-color: #000000;
color: #ff0000;
transition: 1.0s ease;
}
navbar li.active a{
background-color: #000000;
color: #ff0000;
}
可能这就是您要找的东西Link to W3 school
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.topnav {
overflow: hidden;
background-color: #333;
}
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #4CAF50;
color: white;
}
.topnav .icon {
display: none;
}
@media screen and (max-width: 600px) {
.topnav a:not(:first-child) {display: none;}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 600px) {
.topnav.responsive {position: relative;}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
</style>
</head>
<body>
<div class="topnav" id="myTopnav">
<a href="#home" class="active">Home</a>
<a href="#news">News</a>
<a href="#contact">Contact</a>
<a href="#about">About</a>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
<div style="padding-left:16px">
<h2>Responsive Topnav Example</h2>
<p>Resize the browser window to see how it works.</p>
</div>
<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
</body>
</html>
您可以从资源中获取代码,但这对您来说很好这是响应式 navbar
与 HTML 和 CSS
这是具有 HTML 和 CSS[=40 的简单响应式 Navbar =]
* {
box-sizing: border-box;
}
body {
margin: 0px;
font-family: 'segoe ui';
}
.nav {
height: 50px;
width: 100%;
background-color: #4d4d4d;
position: relative;
}
.nav > .nav-header {
display: inline;
}
.nav > .nav-header > .nav-title {
display: inline-block;
font-size: 22px;
color: #fff;
padding: 10px 10px 10px 10px;
}
.nav > .nav-btn {
display: none;
}
.nav > .nav-links {
display: inline;
float: right;
font-size: 18px;
}
.nav > .nav-links > a {
display: inline-block;
padding: 13px 10px 13px 10px;
text-decoration: none;
color: #efefef;
}
.nav > .nav-links > a:hover {
background-color: rgba(0, 0, 0, 0.3);
}
.nav > #nav-check {
display: none;
}
@media (max-width:600px) {
.nav > .nav-btn {
display: inline-block;
position: absolute;
right: 0px;
top: 0px;
}
.nav > .nav-btn > label {
display: inline-block;
width: 50px;
height: 50px;
padding: 13px;
}
.nav > .nav-btn > label:hover,.nav #nav-check:checked ~ .nav-btn > label {
background-color: rgba(0, 0, 0, 0.3);
}
.nav > .nav-btn > label > span {
display: block;
width: 25px;
height: 10px;
border-top: 2px solid #eee;
}
.nav > .nav-links {
position: absolute;
display: block;
width: 100%;
background-color: #333;
height: 0px;
transition: all 0.3s ease-in;
overflow-y: hidden;
top: 50px;
left: 0px;
}
.nav > .nav-links > a {
display: block;
width: 100%;
}
.nav > #nav-check:not(:checked) ~ .nav-links {
height: 0px;
}
.nav > #nav-check:checked ~ .nav-links {
height: calc(100vh - 50px);
overflow-y: auto;
}
}
<div class="nav">
<input type="checkbox" id="nav-check">
<div class="nav-header">
<div class="nav-title">
Logo
</div>
</div>
<div class="nav-btn">
<label for="nav-check">
<span></span>
<span></span>
<span></span>
</label>
</div>
<div class="nav-links">
<a href="//github.io/jo_geek" target="_blank">Github</a>
<a href="http://whosebug.com/users/4084003/" target="_blank">Whosebug</a>
<a href="https://in.linkedin.com/in/jonesvinothjoseph" target="_blank">LinkedIn</a>
<a href="https://codepen.io/jo_Geek/" target="_blank">Codepen</a>
<a href="https://jsfiddle.net/user/jo_Geek/" target="_blank">JsFiddle</a>
</div>
</div>
和第二个选项使用 Bootstrap 4 因为你需要使用一些 Bootstrap 4 CDN 即 Content delivery network 在 head
标签下使用此 CDN
CDN
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
这是带有 bootstrap 4
的简单示例响应式导航栏
<nav class="navbar navbar-expand-md navbar-light bg-light">
<a href="#" class="navbar-brand">
<img src="/examples/images/logo.svg" height="28" alt="CoolBrand">
</a>
<button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbarCollapse">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<div class="navbar-nav">
<a href="#" class="nav-item nav-link active">Home</a>
<a href="#" class="nav-item nav-link">Profile</a>
<a href="#" class="nav-item nav-link">Messages</a>
<a href="#" class="nav-item nav-link disabled" tabindex="-1">Reports</a>
</div>
<div class="navbar-nav ml-auto">
<a href="#" class="nav-item nav-link">Login</a>
</div>
</div>
</nav>
我正在使用 HTML 和 CSS,但似乎无法找到一种方法让我的导航栏随 window 调整大小。当我缩小 window 时,我从我的导航栏中得到了一堆可怕的图标。有没有任何代码可以提供帮助? 这是我的代码:
navbar{position: absolute;
top: 145px;
left: 20px;
width: auto;
list-style-type: none;
background-color: #ff0000
}
navbar ul{
font-size:42px;
}
navbar li{
display: inline-block;
}
navbar a{
border-right:2px solid #000000;
color:#000000;
font-size:0.9em;
text-transform:uppercase
}
navbar li a{
text-decoration: none;
background-color: #ff0000;
color: #000001
height: 50px;
padding: 16px 12px;
border-spacing: 10px;
transition: 0.5s ease;
}
navbar li a:hover{
background-color: #000000;
color: #ff0000;
transition: 1.0s ease;
}
navbar li.active a{
background-color: #000000;
color: #ff0000;
}
可能这就是您要找的东西Link to W3 school
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.topnav {
overflow: hidden;
background-color: #333;
}
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #4CAF50;
color: white;
}
.topnav .icon {
display: none;
}
@media screen and (max-width: 600px) {
.topnav a:not(:first-child) {display: none;}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 600px) {
.topnav.responsive {position: relative;}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
</style>
</head>
<body>
<div class="topnav" id="myTopnav">
<a href="#home" class="active">Home</a>
<a href="#news">News</a>
<a href="#contact">Contact</a>
<a href="#about">About</a>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
<div style="padding-left:16px">
<h2>Responsive Topnav Example</h2>
<p>Resize the browser window to see how it works.</p>
</div>
<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
</body>
</html>
您可以从资源中获取代码,但这对您来说很好这是响应式 navbar
与 HTML 和 CSS
这是具有 HTML 和 CSS[=40 的简单响应式 Navbar =]
* {
box-sizing: border-box;
}
body {
margin: 0px;
font-family: 'segoe ui';
}
.nav {
height: 50px;
width: 100%;
background-color: #4d4d4d;
position: relative;
}
.nav > .nav-header {
display: inline;
}
.nav > .nav-header > .nav-title {
display: inline-block;
font-size: 22px;
color: #fff;
padding: 10px 10px 10px 10px;
}
.nav > .nav-btn {
display: none;
}
.nav > .nav-links {
display: inline;
float: right;
font-size: 18px;
}
.nav > .nav-links > a {
display: inline-block;
padding: 13px 10px 13px 10px;
text-decoration: none;
color: #efefef;
}
.nav > .nav-links > a:hover {
background-color: rgba(0, 0, 0, 0.3);
}
.nav > #nav-check {
display: none;
}
@media (max-width:600px) {
.nav > .nav-btn {
display: inline-block;
position: absolute;
right: 0px;
top: 0px;
}
.nav > .nav-btn > label {
display: inline-block;
width: 50px;
height: 50px;
padding: 13px;
}
.nav > .nav-btn > label:hover,.nav #nav-check:checked ~ .nav-btn > label {
background-color: rgba(0, 0, 0, 0.3);
}
.nav > .nav-btn > label > span {
display: block;
width: 25px;
height: 10px;
border-top: 2px solid #eee;
}
.nav > .nav-links {
position: absolute;
display: block;
width: 100%;
background-color: #333;
height: 0px;
transition: all 0.3s ease-in;
overflow-y: hidden;
top: 50px;
left: 0px;
}
.nav > .nav-links > a {
display: block;
width: 100%;
}
.nav > #nav-check:not(:checked) ~ .nav-links {
height: 0px;
}
.nav > #nav-check:checked ~ .nav-links {
height: calc(100vh - 50px);
overflow-y: auto;
}
}
<div class="nav">
<input type="checkbox" id="nav-check">
<div class="nav-header">
<div class="nav-title">
Logo
</div>
</div>
<div class="nav-btn">
<label for="nav-check">
<span></span>
<span></span>
<span></span>
</label>
</div>
<div class="nav-links">
<a href="//github.io/jo_geek" target="_blank">Github</a>
<a href="http://whosebug.com/users/4084003/" target="_blank">Whosebug</a>
<a href="https://in.linkedin.com/in/jonesvinothjoseph" target="_blank">LinkedIn</a>
<a href="https://codepen.io/jo_Geek/" target="_blank">Codepen</a>
<a href="https://jsfiddle.net/user/jo_Geek/" target="_blank">JsFiddle</a>
</div>
</div>
和第二个选项使用 Bootstrap 4 因为你需要使用一些 Bootstrap 4 CDN 即 Content delivery network 在 head
标签下使用此 CDN
CDN
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
这是带有 bootstrap 4
的简单示例响应式导航栏 <nav class="navbar navbar-expand-md navbar-light bg-light">
<a href="#" class="navbar-brand">
<img src="/examples/images/logo.svg" height="28" alt="CoolBrand">
</a>
<button type="button" class="navbar-toggler" data-toggle="collapse" data-target="#navbarCollapse">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<div class="navbar-nav">
<a href="#" class="nav-item nav-link active">Home</a>
<a href="#" class="nav-item nav-link">Profile</a>
<a href="#" class="nav-item nav-link">Messages</a>
<a href="#" class="nav-item nav-link disabled" tabindex="-1">Reports</a>
</div>
<div class="navbar-nav ml-auto">
<a href="#" class="nav-item nav-link">Login</a>
</div>
</div>
</nav>