我正在尝试正确对齐导航链接和徽标

时间:2017-06-20 22:44:20

标签: html css

我试图在绝对中心对齐徽标,但也有导航链接。这就是现在看来enter image description here

我的CSS:

    .logo {
      display: inline-block;
      text-align: center;
    }

    nav {
      margin-top: 2em;
      display: inline-block;
      opacity: 0;
      transition: 0.8s opacity;
      float: right;
      margin-right: 4em;
    }

    nav ul {
      margin: 0;
      padding: 0;
      list-style: none;
    }

    nav li {
      display: inline-block;
      margin: 1em;
    }

    nav a {
      font-weight: 900;
      text-decoration: none;
      text-transform: uppercase;
      font-size: 1rem;
      padding: .75;
      color: white;
      transition: 0.3s color;
    }

HTML: (extremely basic right now)
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Placeholder Name</title>
    <link rel="stylesheet" href="css/style.css" type="text/css">
    <link href='https://fonts.googleapis.com/css?family=Fira Sans' rel='stylesheet'>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  </head>
  <body>

    <header>
      <img class="logo" src="images/placeholder.png" alt="Placeholder Logo">
      <nav>
        <ul>
          <li class="home"><a href="#">Home</a></li>
          <li><a href="#">Services</a></li>
          <li><a href="#">About</a></li>
          <li><a href="#">Contact</a></li>
        </ul>
      </nav>
    </header>

    <section class="home-hero">
      <div class="container">
        <h1 class="title">Turning your Tennis dreams into reality
          <span>with professional interaction and planning</span>
        </h1>
        <a href="#" class="button button-outline">See Our Schedule</a>
      </div>
    </section>
  </body>

  <script>
   $(document).ready(function() {
     $("nav").css("opacity", '1');
   });
   $(document).ready(function() {
     $(".title").css("transform", 'translateX(0%)');
   });
   $(function() {
     $('nav li.home a').addClass('active');
   });
  </script>
</html>

如果您需要其他任何内容,请告诉我,而不是立即低估,谢谢&lt; 3

2 个答案:

答案 0 :(得分:0)

编辑响应式标题:

将您的徽标图片放入其自己的<div>,然后:

div.logo {
  display: block;
  width: 100%;
  text-align: center;
}

/* Edit for whatever size you need */
@media screen and (min-width: 64em) {
    .logo {
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
    }
}

答案 1 :(得分:0)

有很多方法可以做到这一点。这是一个 - 但实际上,你需要考虑响应式布局......

https://jsfiddle.net/sheriffderek/31p8dL1d/


标记

<header class="site-header">
  <div class="image-w logo">
    <img src="https://placehold.it/300x200" alt="Logo">
  </div>
  <nav class="site-navigation">
    <ul class="item-list">
      <li class="home"><a href="#">Home</a></li>
      <li><a href="#">Services</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </nav>
</header>


样式

.image-w img {
  display: block;
  width: 100%;
  height: auto;
}

a {
  text-decoration: none;
  color: inherit;
}

.site-header {
  background: black;
  color: white;
  text-align: right; /* you'll likely use float or flex */
  position: relative;
}

.site-header .logo {
  max-width: 80px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.site-navigation {
  /* float: right; if not using align right for inline-block */
  display: inline-block;
}

.site-navigation .item-list {
  list-style: none;
  margin: 0;
  padding: 0
}

.site-navigation .item-list li {
  display: inline-block;
}

.site-navigation .item-list a {
  display: block;
  padding: 1.4rem;
}