html - 对齐标题中的 div

标签 html css

.header {
 background-color: yellow;
 font-family: 'Arial';
 display: inline-block;
}

.leftheader{
 float: left;
}

#Logo{
 font-size: 40px;
 font-weight: 700;
 display: inline-block;
}

#LogoLTD{
 font-size: 20px;
 display: inline-block;
}

.rightheader{
 display: flex;
 float: right;
}

.rightheader a{
 color: black;
 font-size: 20px;
 font-weight: 700;
 text-decoration: none;
}
<section class="header">

 <div class="leftheader">
   <div id="Logo">COMPANY NAME</div>
   <div id="LogoLTD">LIMITED</div>
  </div>

 <div class="rightheader">
     <a href="/home.html">HOME</a>
     <a href="/who.html">WHO</a>
     <a href="/contact.html">CONTACT</a>
 </div>

</section>

我一直在尝试为我的网站制作标题,但无法正确对齐。

我希望我的链接位于页面右侧的黄色框内。

黄色应该在标题类中。

然后 leftheader div 和 rightheader div 应该在标题类中。它们应该位于左侧和右侧,但无论我做什么我都无法让它工作:(

我的 HTML 是:

<section class="header">

 <div class="leftheader">
   <div id="Logo">COMPANY NAME</div>
   <div id="LogoLTD">LIMITED</div>
  </div>

 <div class="rightheader">
     <a href="/home.html">HOME</a>
     <a href="/who.html">WHO</a>
     <a href="/contact.html">CONTACT</a>
 </div>

</section>

我的 CSS 是:

.header {
 background-color: yellow;
 font-family: 'Arial';
 display: inline-block;
}

.leftheader{
 float: left;
}

#Logo{
 font-size: 40px;
 font-weight: 700;
 display: inline-block;
}

#LogoLTD{
 font-size: 20px;
 display: inline-block;
}

.rightheader{
 display: flex;
 float: right;
}

.rightheader a{
 color: black;
 font-size: 20px;
 font-weight: 700;
 text-decoration: none;
}

最佳答案

您可以使用 flex 来做到这一点。查看下面的代码。

.header {
 background-color: yellow;
 font-family: 'Arial';
 display: flex;
 flex-direction: row;
 justify-content: space-between;
 align-items: center;
}

.leftheader{
 display: flex;
 align-items: flex-end;
}

#Logo{
 font-size: 40px;
 font-weight: 700;
 display: inline-block;
}

#LogoLTD{
 font-size: 20px;
 display: inline-block;
 padding-bottom: 5px;
}

.rightheader{
 display: flex;
}

.rightheader a{
 color: black;
 font-size: 20px;
 font-weight: 700;
 text-decoration: none;
}
<section class="header">

 <div class="leftheader">
   <div id="Logo">COMPANY NAME</div>
   <div id="LogoLTD">LIMITED</div>
  </div>

 <div class="rightheader">
     <a href="/home.html">HOME</a>
     <a href="/who.html">WHO</a>
     <a href="/contact.html">CONTACT</a>
 </div>

</section>

关于html - 对齐标题中的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48228188/

相关文章:

jquery - Google Chrome 上的多重渲染问题

jquery - li菜单需要 "selected"的类

HTML 正文背景图片在 iOS 5 中不起作用

javascript确保输入文件不为空

javascript - jQuery 检测已经渲染的元素?

javascript - 使 DIV 固定在可滚动的 DIV 中

javascript - 给定一个点和一个平面,当平面缩放而不缩放点时,如何平移点以匹配平面?

javascript - 带有ajax后退按钮的分页链接不会返回

html - Bootstrap 侧边栏下的内容

php - 在身份验证尝试中传递第二个参数后,记住登录页面上的用户凭据不起作用