【问题标题】:Vertical border/divider centered in header垂直边框/分隔线以标题为中心
【发布时间】:2016-09-28 10:21:26
【问题描述】:

是否有可能,并且相当容易,在我的标题中创建一个居中的垂直边框?我希望它划分我的 logo/h1-text 和我的导航栏。

我知道 css 很乱,我上周刚学了 html 和 css!我可能会删除一些 css。

        header {
            height: 60px;
            width: 85%;
            /background-color: white;
            margin: 0 auto;
            /box-shadow: 0px 2px 31px -2px rgba(0, 0, 0, 0.86);
            /border: 2px solid #333;
            left: 0;
            right: 0;
            /border-sizing: border-box;
            /-moz-box-sizing: border-box;
            /-webkit-box-sizing: border-box;
            z-index: 999999;
        }


        header #kage > *, header li {
            display: inline-block;
        }

        header li {
            padding: 0 8px 0 8px;
            float:left;
        }

        #kage {
            width: 99%;
            margin: 0 auto;
            height: 100%;
            line-height: 59px;
            text-align: center;
        }

        .button1:hover {
            background-color: #f2f2f2;
        }
        .button2:hover {
            background-color: #f2f2f2;
        }
        .button3:hover {
            background-color: #f2f2f2;
        }

        .active {
            text-decoration: underline;
        }

        header a {
            text-decoration: none;
            color: #333;
            position: relative;
        }

        header h1 {
            margin: 0;
            float: left;
            height: 100%;
            text-shadow: 1px 2px lightgrey;
        }

        header h1:hover {
            color: #f2f2f2;
        }

        #topnav {
            height: 100%;
            /*float: right;*/
            font-weight: 700;
            font-size: 1.3em;
            width: 310.95px;
        }

        header ul {
            list-style-type: none;
            margin: 0;
        }
    <header>
      <div id="kage">
      <a href="index.html"><h1>H1 TEXT HEREEEEE</h1></a>
        <nav id="topnav">
            <ul class="menu">
                <a href="index.html"><li class="button1 active">Home</li></a>
                <a href="profil.html"><li class="button2">About</li></a>
                <a href="mdu.html"><li class="button3">MDU</li></a>
            </ul>
        </nav>
      </div>
    </header>

好像你需要全屏运行 sn-p,所以标题不会分成两行。

【问题讨论】:

  • 在包含您的徽标的 div 上设置一个右边框 :)

标签: html css header nav


【解决方案1】:

有很多方法可以做到这一点。 我认为最简单的方法是在您的#topnav 上放置一个左边框。

#topnav {
            height: 100%;
            /*float: right;*/
            font-weight: 700;
            font-size: 1.3em;
            width: 310.95px;
            border-left: 2px solid rgb(0,0,0);
            margin: 0 0 0 40px;
        }

我很容易在你的 topnav 左侧添加了一个 2 像素的边框,宽度为黑色。 并用左侧的边距对其进行了一些调整。边距:0 0 0 40px; ~(边距:右上左下)

        header {
            height: 60px;
            width: 85%;
            /background-color: white;
            margin: 0 auto;
            /box-shadow: 0px 2px 31px -2px rgba(0, 0, 0, 0.86);
            /border: 2px solid #333;
            left: 0;
            right: 0;
            /border-sizing: border-box;
            /-moz-box-sizing: border-box;
            /-webkit-box-sizing: border-box;
            z-index: 999999;
        }


        header #kage > *, header li {
            display: inline-block;
        }

        header li {
            padding: 0 8px 0 8px;
            float:left;
        }

        #kage {
            width: 99%;
            margin: 0 auto;
            height: 100%;
            line-height: 59px;
            text-align: center;
        }

        .button1:hover {
            background-color: #f2f2f2;
        }
        .button2:hover {
            background-color: #f2f2f2;
        }
        .button3:hover {
            background-color: #f2f2f2;
        }

        .active {
            text-decoration: underline;
        }

        header a {
            text-decoration: none;
            color: #333;
            position: relative;
        }

        header h1 {
            margin: 0;
            float: left;
            height: 100%;
            text-shadow: 1px 2px lightgrey;
        }

        header h1:hover {
            color: #f2f2f2;
        }

        #topnav {
            height: 100%;
            /*float: right;*/
            font-weight: 700;
            font-size: 1.3em;
            width: 310.95px;
            border-left: 2px solid rgb(0,0,0);
            margin: 0 0 0 40px;
        }

        header ul {
            list-style-type: none;
            margin: 0;
        }
    <header>
      <div id="kage">
      <a href="index.html"><h1>H1 TEXT HEREEEEE</h1></a>
        <nav id="topnav">
            <ul class="menu">
                <a href="index.html"><li class="button1 active">Home</li></a>
                <a href="profil.html"><li class="button2">About</li></a>
                <a href="mdu.html"><li class="button3">MDU</li></a>
            </ul>
        </nav>
      </div>
    </header>

【讨论】:

  • 好的,伙计,谢谢。虽然我发现了一些我认为会更聪明的东西。在我的 H1 和 topnav 之间,我放了一个空 div,宽度:4px,背景颜色:黑色。这实际上也起到了作用。
  • 虽然,正如您自己所说,您必须调整 h1 margin-left på 40px 以使其适合。怎么会?我的 topnav 似乎有一个看不见的边距。 margin-left: 0 left: 0 不起作用?
  • ^^ 不客气。我没有从 h1 调整 margin-left,这没有任何意义 :) 我调整了 #topnav 的 margin-left
  • 如果你想调整h1,你必须设置margin-right。但请注意,

  • 哦,我现在明白了——我只是误解了你在说什么。我现在解决了我的问题 - 非常感谢队友!
【解决方案2】:

您可以简化您的 HTML 和 CSS,并且仍然实现您正在寻找的布局。

使用header 元素作为您的徽标和导航链接的父容器。

header 中,有两个子元素h1nav,并且让这两个元素都是inline-block 元素。

如果您希望子元素在header 中居中,请使用text-align: center 调整定位。

header 可能会包裹在小屏幕尺寸上,您可以使用 white-space: nowrap 来防止这种情况发生(您的设计选择)。

然后您可以调整边距、内边距和边框值来控制各个链接之间的间距。

请注意,对于 nav,请应用 vertical-align: top,否则您会在基线上方或下方获得一些额外的空白。

另外,如果您为header 指定height,还要指定line-height,以便内联子元素垂直居中(如果这是您想要的)。

header {
  height: 60px;
  line-height: 60px;
  width: 85%;
  margin: 0 auto;
  border: 1px dotted blue; /* for demo only */
  text-align: center; /* optional? */
  white-space: nowrap; /* to prevent line from wrapping, optional */
}
header h1 {
  display: inline-block;
  border-right: 1px solid black;
  margin: 0;
  padding-left: 10px; /* adjust to taste */
  padding-right: 10px; /* add whitespace before vertical border */
  height: 100%;
}
header h1 a {
  text-shadow: 1px 2px lightgrey;
}
header h1:hover a {
  color: #f2f2f2;
}

header a {
  text-decoration: none;
  color: #333;
}
.button:hover {
  background-color: #f2f2f2;
}
.active {
  text-decoration: underline;
}
nav {
  display: inline-block;
  vertical-align: top;
  padding-left: 10px; /* whitespace after vertical border */
  font-weight: bold;
  font-size: 1.3em;
  border: 1px dotted gray; /* for demo only */
}
nav a {
  margin: 0 20px;
}
<header>
  <h1><a href="index.html">H1 TEXT HEREEEEE</a></h1>
  <nav>
    <a href="index.html" class="button active">Home</a>
    <a href="profil.html" class="button">About</a>
    <a href="mdu.html" class="button">MDU</a>
  </nav>
</header>

【讨论】:

  • 哇,这对我帮助很大!谢谢你。我的 css 和 html 开始变得如此混乱 - 当你知道并且对任何事情都没有 100% 的把握时,我不会删除任何东西。感谢您的宝贵时间!
猜你喜欢
  • 2019-10-24
  • 2014-10-13
  • 2020-05-14
  • 1970-01-01
  • 2018-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
相关资源
最近更新 更多