html - border-bottom 没有按预期工作

标签 html css css-position

我正在创建一个导航栏。我想让底部边框线出现在导航栏的底部,像这样:

Desired output

但是我从我的代码中得到的是这样的:

Actual output

我的 HTML:

<!DOCTYPE html>

<html>
    <head>
        <title>batman</title>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>

    <body>
        <div id="header">
            <a href="#" class="logo">
                <img src="./img/bat_logo.png" alt="batman" />
            </a>
            <nav>
                <ul style="list-style-type: none;">
                    <li><a href="#villains"> Villains </a></li>
                    <li><a href="#identity"> Identity </a></li>
                    <li><a href="#movies"> Movies </a></li>
                </ul>
            </nav>
        </div>
    </body>
</html>

我的 CSS:

.logo {
    float: left;
    height: 50px;
}
#header {
    padding: 5px 0px;
    border-bottom: 1px solid;
}

最佳答案

使用 flexjustify-content: space-between在 parent 身上,你也可以使用 align-items垂直对齐的东西。也改了<div id="header"><header id="header"> ,因为它似乎是一个语义上更合适的元素。

img {
  max-width: 50px;
}
.logo {
  height: 50px;
  display: block;
}
#header {
  padding: 5px 0px;
  border-bottom: 1px solid;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

nav li {
  display: inline-block;
}
<header id="header">
  <a href="#" class="logo">
    <img src="https://images.coplusk.net/projects/19697/steps/33014/normal_big_cartoon_batman_symbol_1250787261.jpg" alt="batman" />
  </a>
  <nav>
    <ul style="list-style-type: none;">
      <li><a href="#villains"> Villains </a></li>
      <li><a href="#identity"> Identity </a></li>
      <li><a href="#movies"> Movies </a></li>
    </ul>
  </nav>
</header>

关于html - border-bottom 没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44474878/

相关文章:

python - BeautifulSoup 在父 DIV 的子 DIV 中添加新标签

javascript - 将自定义图标添加到选择菜单

html - 自动化 CSS 图像内联的工具

javascript - HTML Canvas : text doesn't appear at center of circle

html - 关键帧动画在不可见时也会渲染吗?它们会消耗资源吗?

css - 文本超出包含 div

css - 仅限 Chrome : Fixed Div w/Google Map scrolls when other div scrolls

css - 定位更多固定元素的问题

javascript - HTML 图表显示问题 - 将图表移动到卡片内

javascript - 如何选择和操作表 jQuery 中的列