gpt4 book ai didi

html - 具有 Logo 图像和固定高度的响应式导航栏

转载 作者:太空宇宙 更新时间:2023-11-04 08:51:17 25 4
gpt4 key购买 nike

我的页面中有一个固定的顶部导航栏,导航栏设置为具有固定高度的图像并且导航栏本身具有高度,我想要做的是将导航栏品牌 Logo 与列表上的列表垂直对齐在响应式大屏幕的右侧,我该怎么做?这是我的代码:

.navbar{
height:150px;
}

.navbar-brand img{
height:95px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<html>
<body><!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">
<img src="http://placehold.it/150x50&text=Logo" alt="">
</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a href="#">About</a>
</li>
<li>
<a href="#">Services</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

</body>
</html>

最佳答案

你可以使用flex。我已经编辑了您的代码段。

我还为响应行为编写了代码,以便在小屏幕上 Logo 和切换图标也能正确排列。

我已经为 .navbar-brand 使用了 height: auto !important 你可以在你的真实代码中删除 !important 因为它不是必需的.我写这个只是因为在这个片段中,bootstrap 风格压倒了我的风格。

对于小屏幕移除 .navbar 的高度

.navbar{
height:auto;
}

如果需要,可以使用 min-height 代替。这是因为 Bootstrap 使用折叠作为静态元素而不是绝对元素,所以导航栏将在菜单打开时扩展。

.navbar{
height:150px;
}

.navbar-brand img{
height:95px;
}

.navbar > .container {
display: flex;
align-items: center;
}

.navbar-brand {
height: auto !important;
}

.navbar-toggle {
margin-left: auto;
order: 2;
}

@media (max-width: 767px) {
.navbar > .container {
display: block;
}
.navbar-header {
display: flex;
align-items: center;
}
.navbar{
height: auto;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<html>
<body><!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">
<img src="http://placehold.it/150x50&text=Logo" alt="">
</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a href="#">About</a>
</li>
<li>
<a href="#">Services</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

</body>
</html>

关于html - 具有 Logo 图像和固定高度的响应式导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43440942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com