【问题标题】:Bootstrap Navbar Content Not CenteredBootstrap 导航栏内容未居中
【发布时间】:2017-12-21 01:12:28
【问题描述】:

我对 bootstrap 和 html/css 比较陌生,到目前为止我相处得很好。我一直在尝试做的是将导航栏的高度增加到 80px。我的问题是,现在我无法将导航栏的内容居中,它们太高了。

看起来像这样:https://gyazo.com/4ba22961033e9bafa13aac44a50de3f6

谢谢:)

这是我的整个 CSS 代码:

* {
	box-sizing: border-box;
}

body {
	background-color:#f6f6f6;
	background-image: url("img/bg.png");
	background-repeat: no-repeat;
	background-attachment: fixed;
	background-position: center center;
	background-size:cover;
	overflow-x:hidden;
	overflow-y:scroll;
	margin:0;
	padding:0!important;
}

.navbar {
    position: relative;
	border:1px solid transparent;
	min-height: 80px;
	border-top: 0;
	border-bottom:1px solid #e7e7e7;
	margin-bottom:0;
	z-index: 100;
}

.navbar-default {
  height: 80px;
}

.navbar-brand {
    float:left;
    font-size:18px;
    line-height:80px;
    height:80px;
    padding:0 15px;
}

.navbar-toggle {
	margin-top: 23px;
	padding: 9px 10px !important;
}


.navbar-nav > li > a {
	height: 80px;
	padding-top:10px;
    padding-bottom:10px;
    line-height:20px;
	border-left: 1px solid #e7e7e7;
}

.navbar-left {
    float:left!important;
}

.navbar-right {
    float:right!important;
    margin-right:-15px;
}

.navbar-nav>li>.dropdown-menu {
    margin-top:0;
    border-top-right-radius:0;
    border-top-left-radius:0;
}

HTML:

<!DOCTYPE html>
<html lang="en">

<head>

  <!-- Bootstrap required meta -->
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- Links to stylesheets and scripts -->  
  <link href="style.css" type="text/css" rel="stylesheet"> 
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  
  
  <title>Test</title>
  
</head>

<body>

<!-- NavBar -->
<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="#">Brand</a>
    </div>
    <ul class="nav navbar-nav navbar-right">
      <li class="active"><a href="#">Home</a></li>
      <li><a href="#">Leaderboards</a></li>
      <li><a href="#">Support</a></li>
	  
	<li class="dropdown">
	  <a class "dropdown-toggle" data-toggle="dropdown" href="#">(username&pp)
	  <span class="caret"></span></a>
	  <ul class="dropdown-menu">
	    <li><a href="#">Settings</a></li>
		<li><a href="#">Statistics</a></li>
		<li><a href="#">History</a></li>
		<li><a href="#">Logout</a></li>
		
	  </ul>
	</li>
	
    </ul>
  </div>
</nav>

</body>

</html>

【问题讨论】:

标签: html css twitter-bootstrap


【解决方案1】:

尝试在 .navbar-nav &gt; li &gt; a CSS 块中添加以下 flex 属性:

display: flex;
align-items: center; 

所以.navbar-nav &gt; li &gt; a 块现在应该如下所示:

.navbar-nav > li > a {
  display: flex;
  align-items: center;  
  height: 80px;
  padding-top: 10px;
  padding-bottom: 10px;
  line-height: 20px;
  border-left: 1px solid #e7e7e7;
}

CodePen Demo

【讨论】:

  • 没用,可惜内容还在顶部
  • 您可以发布您的 html 吗?我看到了这个问题并在 CodePen 中修复了它,但我只是将 Bootstrap 样板 HTML 用于导航栏。
  • @DerJP 我已经更新了我的答案。我不知道你的 HTML 是什么样的,但如果你在 .navbar-nav &gt; li &gt; a 块内添加这些 flex 属性,它应该居中项目。
  • 它没有解决问题,但我包含了我的 HTML,所以我希望你现在能找到问题所在。感谢您的努力,我真的很感激:)
【解决方案2】:

我认为我遇到了问题(可能是)...使用此 css 代码...我认为这正是您所需要的

.navbar.navbar-default {
        min-height: 80px;
    }
    .container-fluid {
        margin-top: 15px;
    }

要更改字体大小,请对 .navbar-header 和 .navbar-brand 进行不同的更改

【讨论】:

猜你喜欢
  • 2012-05-09
  • 1970-01-01
  • 2017-03-23
  • 2023-03-20
  • 1970-01-01
  • 2013-11-27
  • 2015-09-17
  • 2017-03-04
  • 1970-01-01
相关资源
最近更新 更多