首页 > 解决方案 > 在标题/导航栏中居中徽标

问题描述

我正在尝试创建一个站点并从导航栏开始。我正在尝试将徽标居中并给 subs/links(?不知道它们叫什么,a-tags)更多的空间。

我尝试遵循一些视频教程和 Stackoverflow 答案,但它们导致左侧 3 个链接更向下,而右侧 3 个链接高于其他所有链接,并导致整体标题大小几乎翻了一番。如果有人可以帮助我,我将不胜感激!:) 可能有一些不需要的垃圾代码,或者总体上是一种不好的做法,但如果不清楚我是网页设计的初学者:p

body { 
  margin: 0;
  font-family: 'Open Sans', sans-serif;
}

.header {
  overflow: hidden;
  background-color: #262e28;
  padding: 10px 10px;
  box-shadow: 0px 2px 8px #888888;
}

.header a {
  color: white;
  text-align: center;
  padding: 12px;
  text-decoration: none;
  font-size: 14px; 
  line-height: 25px;
  border-radius: 4px;
}

.header a.logo {
  font-size: 25px;
  color: white;
    display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
}

.header a:hover {
  color: #aeaeae;
}

.header a.active {
  color: #aeaeae;
}

.header-right {
  float: right;
}

.header-left {
  float: left;
}

@media screen and (max-width: 500px) {
  .header a {
    float: none;
    display: block;
    text-align: left;
  }
  
  .header-right {
    float: none;
  }
}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="assets/style.css">
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300&display=swap" rel="stylesheet"></head>
<body>

<div class="header">
  <div class="header-right">
    <a href="#"><b>1</b></a>
    <a href="#"><b>2</b></a>
    <a href="#"><b>3</b></a>
  </div>
  <a href="#" class="logo">LOGO</a>
  <div class="header-left">
    <a href="#"><b>4</b></a>
    <a href="#"><b>5</b></a>
    <a href="#"><b>6</b></a>
  </div>
</div>

</body>
</html>

标签: htmlcss

解决方案


您可以在课堂上使用display:flex属性。align-items: centerheader

如今,使用flex属性是一种推荐的方式,因为它在现代浏览器上的响应速度非常快

将此代码添加到您的标头类中

.header {
  overflow: hidden;
  background-color: #262e28;
  padding: 10px 10px;
  box-shadow: 0px 2px 8px #888888;
  display: flex;
  align-items: center;
  flex-direction: row-reverse;
}

现场工作示例:

body {
  margin: 0;
  font-family: 'Open Sans', sans-serif;
}

.header {
  overflow: hidden;
  background-color: #262e28;
  padding: 10px 10px;
  box-shadow: 0px 2px 8px #888888;
  display: flex;
  align-items: center;
  flex-direction: row-reverse;
}

.header a {
  color: white;
  text-align: center;
  padding: 12px;
  text-decoration: none;
  font-size: 14px;
  line-height: 25px;
  border-radius: 4px;
}

.header a.logo {
  font-size: 25px;
  color: white;
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
}

.header a:hover {
  color: #aeaeae;
}

.header a.active {
  color: #aeaeae;
}

.header-right {
  float: right;
}

.header-left {
  float: left;
}


@media screen and (max-width: 500px) {
  .header a {
    float: none;
    display: block;
    text-align: left;
  }
  .header-right {
    float: none;
  }
}
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" type="text/css" href="assets/style.css">
  <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300&display=swap" rel="stylesheet">
</head>

<body>

  <div class="header">
    <div class="header-right">
      <a href="#"><b>1</b></a>
      <a href="#"><b>2</b></a>
      <a href="#"><b>3</b></a>
    </div>
    <a href="#" class="logo">LOGO</a>
    <div class="header-left">
      <a href="#"><b>4</b></a>
      <a href="#"><b>5</b></a>
      <a href="#"><b>6</b></a>
    </div>
  </div>

</body>

</html>


推荐阅读