在屏幕上居中导航菜单

时间:2017-12-08 17:13:28

标签: html css css3

嘿,我对网络开发很陌生,想知道是否有人可以帮助我解决我的问题,我似乎无法让这个菜单集中在中心相当多的代码在这里,我不确定关注哪些元素以及准确关注哪些内容,如果你能解释我或快速解决,我会非常感谢一些帮助

nav ul ul {
  display: none;
  text-align: center;
}
	
nav ul li:hover>ul {
  display: block;
  text-align: center;
}

nav ul {
  background: #efefef;
  background: linear-gradient(top, #efefef 0%, #bbbbbb 100%);
  background: -moz-linear-gradient(top, #efefef 0%, #bbbbbb 100%);
  background: -webkit-linear-gradient(top, #efefef 0%, #bbbbbb 100%);
  box-shadow: 0px 0px 9px rgba(0, 0, 0, 0.15);
  padding: 0 20px;
  border-radius: 10px;
  list-style: none;
  position: relative;
  display: inline-table;
  text-align: center;
}

nav ul:after {
  content: "";
  clear: both;
  display: block;
  text-align: center;
}

nav ul li {
  float: left;
}

nav ul li:hover {
  background: #4b545f;
  background: linear-gradient(top, #4f5964 0%, #5f6975 40%);
  background: -moz-linear-gradient(top, #4f5964 0%, #5f6975 40%);
  background: -webkit-linear-gradient(top, #4f5964 0%, #5f6975 40%);
}

nav ul li:hover a {
  color: #fff;
}

nav ul li a {
  display: block;
  padding: 25px 40px;
  color: #757575;
  text-decoration: none;
}

nav ul ul {
  background: #5f6975;
  border-radius: 0px;
  padding: 0;
  position: absolute;
  top: 100%;
}

nav ul ul li {
  float: none;
  border-top: 1px solid #6b727c;
  border-bottom: 1px solid #575f6a;
  position: relative;
}

nav ul ul li a {
  padding: 15px 40px;
  color: #fff;
}

nav ul ul li a:hover {
  background: #4b545f;
}

nav ul ul ul {
  position: absolute;
  right: 100%;
  top: 0;
}
<nav>
    <ul>
      <li><a href="#">1</a></li>
      <li><a href="#">2</a>
        <ul>
          <li><a href="#">21</a></li>
          <li><a href="#">22</a></li>
          <li><a href="#">23</a>
          <li><a href="#">24</a>
              <li><a href="">25</a>
                <ul>
                  <li><a href="#">251</a></li>
                  <li><a href="#">252</a></li>
                </ul>
              </li>
        </ul>
        </li>
        <li><a href="#">3</a>
          <ul>
            <li><a href="#">31</a></li>
            <li><a href="#">32</a></li>
            <li><a href="#">33</a></li>
          </ul>
        </li>
        <li><a href="#">4</a>
          <ul>
            <li><a href="#">41</a></li>
            <li><a href="#">42</a></li>
            <li><a href="#">43</a></li>
            <li><a href="#">44</a></li>
          </ul>
        </li>
        <li><a href="#">5</a>
        </li>
    </ul>
  </nav>
    

2 个答案:

答案 0 :(得分:1)

由于您在display: inline-table;标记内的ul上使用了nav,因此您可以将其添加到全局ulnav的中心位置:

nav {
  text-align: center;
}

&#13;
&#13;
nav ul ul {
  display: none;
  text-align: center;
}

nav ul li:hover>ul {
  display: block;
  text-align: center;
}
nav {
  text-align: center;
}
nav ul {
  background: #efefef;
  background: linear-gradient(top, #efefef 0%, #bbbbbb 100%);
  background: -moz-linear-gradient(top, #efefef 0%, #bbbbbb 100%);
  background: -webkit-linear-gradient(top, #efefef 0%, #bbbbbb 100%);
  box-shadow: 0px 0px 9px rgba(0, 0, 0, 0.15);
  padding: 0 20px;
  border-radius: 10px;
  list-style: none;
  position: relative;
  display: inline-table;
}

nav ul:after {
  content: "";
  clear: both;
  display: block;
  text-align: center;
}

nav ul li {
  float: left;
}

nav ul li:hover {
  background: #4b545f;
  background: linear-gradient(top, #4f5964 0%, #5f6975 40%);
  background: -moz-linear-gradient(top, #4f5964 0%, #5f6975 40%);
  background: -webkit-linear-gradient(top, #4f5964 0%, #5f6975 40%);
}

nav ul li:hover a {
  color: #fff;
}

nav ul li a {
  display: block;
  padding: 25px 40px;
  color: #757575;
  text-decoration: none;
}

nav ul ul {
  background: #5f6975;
  border-radius: 0px;
  padding: 0;
  position: absolute;
  top: 100%;
}

nav ul ul li {
  float: none;
  border-top: 1px solid #6b727c;
  border-bottom: 1px solid #575f6a;
  position: relative;
}

nav ul ul li a {
  padding: 15px 40px;
  color: #fff;
}

nav ul ul li a:hover {
  background: #4b545f;
}

nav ul ul ul {
  position: absolute;
  right: 100%;
  top: 0;
}
&#13;
<nav>
  <ul>
    <li><a href="#">1</a></li>
    <li><a href="#">2</a>
      <ul>
        <li><a href="#">21</a></li>
        <li><a href="#">22</a></li>
        <li><a href="#">23</a>
          <li><a href="#">24</a>
            <li><a href="">25</a>
              <ul>
                <li><a href="#">251</a></li>
                <li><a href="#">252</a></li>
              </ul>
            </li>
      </ul>
      </li>
      <li><a href="#">3</a>
        <ul>
          <li><a href="#">31</a></li>
          <li><a href="#">32</a></li>
          <li><a href="#">33</a></li>
        </ul>
      </li>
      <li><a href="#">4</a>
        <ul>
          <li><a href="#">41</a></li>
          <li><a href="#">42</a></li>
          <li><a href="#">43</a></li>
          <li><a href="#">44</a></li>
        </ul>
      </li>
      <li><a href="#">5</a>
      </li>
  </ul>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

检查一下: 如果没问题,我会解释。

&#13;
&#13;
 nav{
     text-align:center;
 }
nav ul ul {
  display: none;
  text-align: center;
}

nav ul li:hover>ul {
  display: block;
  text-align: center;
  width: 88px;
}

nav ul {
  background: #efefef;
  background: linear-gradient(top, #efefef 0%, #bbbbbb 100%);
  background: -moz-linear-gradient(top, #efefef 0%, #bbbbbb 100%);
  background: -webkit-linear-gradient(top, #efefef 0%, #bbbbbb 100%);
  box-shadow: 0px 0px 9px rgba(0, 0, 0, 0.15);
  padding: 0 20px;
  border-radius: 10px;
  list-style: none;
  position: relative;
  display: inline-table;
  text-align: center;
}

nav ul:after {
  content: "";
  clear: both;
  display: block;
  text-align: center;
}

nav ul li {
  float: left;
}

nav ul li:hover {
  background: #4b545f;
  background: linear-gradient(top, #4f5964 0%, #5f6975 40%);
  background: -moz-linear-gradient(top, #4f5964 0%, #5f6975 40%);
  background: -webkit-linear-gradient(top, #4f5964 0%, #5f6975 40%);
}

nav ul li:hover a {
  color: #fff;
}

nav ul li a {
  display: block;
  padding: 25px 40px;
  color: #757575;
  text-decoration: none;
}

nav ul ul {
  background: #5f6975;
  border-radius: 0px;
  padding: 0;
  position: absolute;
  top: 100%;
}

nav ul ul li {
  float: none;
  border-top: 1px solid #6b727c;
  border-bottom: 1px solid #575f6a;
  position: relative;
}

nav ul ul li a {
  padding: 15px 40px;
  color: #fff;
}

nav ul ul li a:hover {
  background: #4b545f;
}

nav ul ul ul {
  position: absolute;
  right: 100%;
  top: 0;
}
&#13;
<nav>
<ul>
  <li><a href="#">1</a></li>
  <li><a href="#">2</a>
    <ul>
      <li><a href="#">21</a></li>
      <li><a href="#">22</a></li>
      <li><a href="#">23</a>
      <li><a href="#">24</a>
          <li><a href="">25</a>
            <ul>
              <li><a href="#">251</a></li>
              <li><a href="#">252</a></li>
            </ul>
          </li>
    </ul>
    </li>
    <li><a href="#">3</a>
      <ul>
        <li><a href="#">31</a></li>
        <li><a href="#">32</a></li>
        <li><a href="#">33</a></li>
      </ul>
    </li>
    <li><a href="#">4</a>
      <ul>
        <li><a href="#">41</a></li>
        <li><a href="#">42</a></li>
        <li><a href="#">43</a></li>
        <li><a href="#">44</a></li>
      </ul>
    </li>
    <li><a href="#">5</a>
    </li>
</ul>
</nav>
&#13;
&#13;
&#13;