html - 如何让另一个 div 中的 2 个 div 居中并向右浮动?

标签 html css css-float

我的页面上有 2 个 div,我希望其中一个相对于页面居中(页面标题),另一个 float 到右侧(导航栏)。我无法让标题相对于页面居中。我能做的最好的事情就是让它相对于导航栏左侧剩余的空间居中。

这说明了我遇到的问题。

<style>
  .page {
    width: 400px;
    margin: 0 auto;
    background: red;
  }
  .navbar {
     float: right;
   }
  .title {
      text-align: center;
  }
</style>


<div class='page'>
  <div class='navbar'>
    navbar
  </div>
  <div class='title'>
    <h1>TITLE GOES HERE</h1>
  </div>
</div>

有没有简单的解决办法?

最佳答案

如何this

<style>
  .page {
    width: 400px;
    margin: 0 auto;
    background: red;
    position:relative;
  }
  .navbar {
    position:absolute;
    right:2px;
   }
  .title {
      text-align: center;
  }
</style>


<div class='page'>
  <div class='navbar'>
    navbar
  </div>
  <div class='title'>
    <h1>TITLE GOES HERE</h1>
  </div>
</div>

关于html - 如何让另一个 div 中的 2 个 div 居中并向右浮动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4481542/

相关文章:

javascript - 移动到悬停内容时保持悬停

html - 如何使父属性不覆盖后代

html - 自动换行不适用于 float 文本

css - 在 IE10 中列出不向左浮动的元素

html - 垂直展开 Div 以适应内容

html - 我该如何修复这个响应式导航错误?

php - 在php中隐藏图像并实现css

jquery - 如何使用 AngularJS 中的 ng click 获取 <div> 元素的文本值并将其显示在另一个 div 中

html - 响应式 HTML 电子邮件模板对齐问题

html - Bootstrap 标题不能始终在我的模式窗口上工作......想法?