对齐此菜单的正确方法是什么?

时间:2015-05-18 19:27:56

标签: html css frontend

http://jsfiddle.net/rctdzdsm/

我试图将菜单与徽标对齐。

我必须手动保护它直到我正确对齐吗?

或者有更好/正确的方法吗?

因为手动限制,直到我正确对齐,感觉有点愚蠢。



* {
  margin: 0;
  padding: 0;
}
header {
  background-color: #e58b1a;
}
header .logo {
  float: left;
  width: 400px;
  height: 53px;
  font-family: 'Arial';
  color: white;
  font-size: 50px;
  position: relative;
  top: 20px;
  left: 50px;
}
.menu {
  float: right;
  font-family: arial;
}
.menu li {
  /** Float to the left, helps it become horizontal */
  float: left;
  /** Marigin-right allows you to space your links */
  margin-right: 30px;
}

<header class="show-when-loaded" style="height: 108px;">

  <div class="logo">
    <span class="text"> Daz Me Lulz </span>
  </div>


  <div class="menu">
    <nav>
      <ul>
        <li> <a href="aboutus.html">About Us </a>
        </li>
        <li> <a href="project.html">Projectos De Lulz </a>
        </li>
        <li> <a href="involved.html">Get Involved </a>
        </li>
      </ul>
    </nav>
  </div>

</header>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

我删除了职位:亲属。因为它不需要。
添加了一些更好的对齐边距。 试图让li元素换行,最后换成无包装解决方案 从右到左制作链接文本方向。所以他们坚持到屏幕的右侧,不会破坏流动,就像浮动:右边那样。

html, body {
  margin: 0;
  padding: 0;
}
header {
  white-space: nowrap;
  background-color: #e58b1a;
  min-width: 650px;
}
header .logo {
  display: inline-block;
  font-family: 'Arial';
  color: white;
  font-size: 50px;
  padding: 20px 0px 20px 20px;
}
.menu {
  display: inline-block;
  direction: rtl;
  font-family: arial;
  min-width: 350px;
  width: calc(100% - 350px);
  margin-top: 40px;
}
.menu ul {
  display: inline;
  direction: rtl;
  padding: 0;
}
.menu ul li {
  display: inline;
  list-style: none;
  margin-right: 10px;
}
<header class="show-when-loaded">

  <div class="logo">
    <span class="text"> Daz Me Lulz </span>
  </div>


  <nav class="menu">
    <ul>
      <li> <a href="aboutus.html">About Us </a>
      </li>
      <li> <a href="project.html">Projectos De Lulz </a>
      </li>
      <li> <a href="involved.html">Get Involved </a>
      </li>
    </ul>
  </nav>

</header>

答案 1 :(得分:0)

看起来CSS表会对此有所帮助。

header {
  background-color: #e58b1a;
  display: table;
  vertical-align: middle;
  width: 100%;
}
header .logo {
  display: table-cell;
  height: 53px;
  font-family: 'Arial';
  color: white;
  font-size: 50px;
  vertical-align: middle;
}
.menu {
  display: table-cell;
  font-family: arial;
  text-align: right;
  vertical-align: middle;
}

* {
  margin: 0;
  padding: 0;
}
header {
  background-color: #e58b1a;
  display: table;
  vertical-align: middle;
  width: 100%;
}
header .logo {
  display: table-cell;
  height: 53px;
  font-family: 'Arial';
  color: white;
  font-size: 50px;
  vertical-align: middle;
}
.menu {
  display: table-cell;
  font-family: arial;
  text-align: right;
  vertical-align: middle;
}
.menu li {
  display: inline-block;
}
<link rel="stylesheet" type="text/css" href="style.css">
<title>Daz Me Lulz - Providng Renewable Energy to the children of Latin America</title>


<body>

  <header class="show-when-loaded" style="height: 108px;">

    <div class="logo">
      <span class="text"> Daz Me Lulz </span>
    </div>


    <div class="menu">
      <nav>
        <ul>
          <li> <a href="aboutus.html">About Us </a>
          </li>
          <li> <a href="project.html">Projectos De Lulz </a>
          </li>
          <li> <a href="involved.html">Get Involved </a>
          </li>
        </ul>
      </nav>
    </div>

  </header>




</body>

答案 2 :(得分:0)

这不是一种非常灵活的方式。如果标题的高度发生变化怎么办?

比您手动调整填充和边距要多。如果您正在寻找更准确的方法,请查看显示元素和对齐它们的不同可能性。即使你的菜单存在于unordert-list中。可以将您的列表视为一个表,列表项是表格单元格。如果元素从标题继承高度,则可以轻松地在中间垂直对齐它们。这是一个片段,让您了解我的意思。

&#13;
&#13;
html, body {
  margin: 0;
  padding: 0;
}

header {
  height: 108px;
  background-color: orange;
}

header:after {
  clear: both;
}

nav {
  height: 100%;
}

nav ul {
  margin: 0;
  padding: 0;
  float: left;
  display: table;
  height: inherit;
}

nav li {
  display: table-cell;
  height: inherit;
  vertical-align: middle;
}

nav a {
  text-decoration: none;
  color: white;
  font-family: 'Arial';
}

.logo a {
  font-size: 50px;
  padding: 0 0 0 20px;
}

nav .nav-bar {
  float: right;
}

nav .nav-bar li {
  padding-right: 20px;
}
&#13;
<header>
  <nav role='navigation'>
    <ul class="logo">
      <li>
        <a href="">Daz Me Lulz</a>
      </li>
    </ul>
    <ul class="nav-bar">
      <li><a href="#">Home</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Clients</a></li>
      <li><a href="#">Contact Us</a></li>
    </ul>
  </nav>  
</header>
&#13;
&#13;
&#13;

玩得开心。