我如何将导航中心化?

时间:2016-05-10 02:56:37

标签: html css

我是HTML和CSS的新手很抱歉,如果这是一个相当明显的问题我的导航栏居中,直到我添加了引导类,任何人都可以帮助我解决造成这种冲突的原因吗?

ul {
  margin: 0;
  padding: 0;
  text-align: center;
  position: relative;
  display: inline-block;
}
li {
  display: inline;
  list-style: none;
  height: 100px;
  line-height: 100px;
}
ul li {
  height: 100px;
}
a:link,
a:visited {
  display: inline-block;
  margin-right: -4px;
  width: 120px;
  font-weight: bold;
  color: black;
  text-align: center;
  padding: 4px;
  text-decoration: none;
  text-transform: uppercase;
}
#circle {
  background-color: #AAAAAA;
  border-radius: 50%;
  display: inline-block;
  width: 100px;
  height: 100px;
}
#nav {
  padding-top: 10px;
  padding-bottom: 10px;
}
.line {
  text-decoration: overline;
  text-align: center;
  vertical-align: middle;
  display: inline;
}
#full {
  width: 100%;
  text-align: center;
}
#K {
  color: white;
  font-size: 48px;
  font-family: 'Lucida Sans Typewriter', 'Lucida Console', monaco, 'Bitstream Vera Sans Mono', monospace;
}
.box {
  display: inline;
  padding-left: 30px;
  padding-right: 30px;
}
<div id="full" class="row">
  <ul id="nav">
    <li>
      <div class="box col-md-2 col-sm-2 col-xs-12">
        <a href=csshomepage.html>
          <p class="line">About&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
        </a>
      </div>
    </li>
    <li>
      <div class="box col-md-2 col-sm-2 col-xs-12">
        <a href="#images">
          <p class="line">Projects&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
        </a>
      </div>
    </li>
    <li>
      <div class="box col-md-4 col-sm-4 hidden-xs" id="circle">
        <a href="#images">
          <p id="K">K</p>
        </a>
      </div>
    </li>
    <li>
      <div class="box col-md-2 col-sm-2 col-xs-12">
        <a href="#links">
          <p class="line">Examples&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
        </a>
      </div>
    </li>
    <li>
      <div class="box col-md-2 col-sm-2 col-xs-12">
        <a href="#links">
          <p class="line">Contact &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
        </a>
      </div>
    </li>
  </ul>
</div>

1 个答案:

答案 0 :(得分:0)

当你使用bootstrap css时,你为什么要编写自己的CSS? 您的代码中的主要问题是&nbsp;的使用,当您在不同的屏幕尺寸上检查您的网站时,这会导致很多麻烦。

将您的代码更改为:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <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="#">Brand</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 class="active"><a href="#">About Us<span class="sr-only">(current)</span></a>
        </li>
        <li><a href="#">Project</a>
        </li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Examples<span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a>
            </li>
            <li><a href="#">Another action</a>
            </li>
            <li><a href="#">Something else here</a>
            </li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a>
            </li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a>
            </li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-left" role="search">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Contact</a>
        </li>
        
      </ul>
    </div>
    <!-- /.navbar-collapse -->
  </div>
  <!-- /.container-fluid -->
</nav>

不要使用你的CSS,只需更改必要的html内容。

参考:Bootstrap