html - 将一个 div 水平对齐到左侧,一个标题对齐到中心,一个 div 水平对齐到右侧

标签 html css

我有一个包含 div、h1 和 div 的 css header 。我面临着将 div 内容向左对齐、标题向中间对齐以及第二个 div 向右水平对齐并且第二个 div 的内容相互重叠的挑战。

CSS 片段

#header {
            height: 166px;

          background-color: #ccc;
          font-weight: bold;
          color: black;
          margin-bottom:3px;
          padding: 2px;
          text-align: center;
          width: 100%x;

        }
        #footer {
        height: auto;
        background-color: #ccc;
          font-size: 20px;
          font-weight: bold;
          color: black;
          margin-top: 3px;
          padding: 2px;
          text-align: center;
          width: 100%x;
        }

html 片段

<div id="header">
<div style="float:left; margin-left:40px;">
<img src="${contextPath}/resources/images/chat1.png" width="496" height="90"/>

</div>
<h3 align="center"><strong>Chat Panel</strong></h3>
<div align="right">
<img src="${contextPath}/resources/images/chat2.png" />
<form method="post" action="/logout">
        <input type="submit" value="Leave Chat"/>
</form>
</div>
</div>

下面是我正在尝试的示例

div content                h1 content                          second div

请协助!

最佳答案

也许像这样,你使用 flex 来排列它们

#header {
  height: 166px;
  background-color: #ccc;
  font-weight: bold;
  color: black;
  margin-bottom: 3px;
  padding: 2px;
  text-align: center;
  width: 100%x;
  display: flex;
}
#header * {
  flex: 1;
}
#header div:last-child {
  text-align: right;
}
#header h3 {
  margin-top: 35px;
}

#footer {
  height: auto;
  background-color: #ccc;
  font-size: 20px;
  font-weight: bold;
  color: black;
  margin-top: 3px;
  padding: 2px;
  text-align: center;
  width: 100%x;
}
<div id="header">
  <div>
    <img src="http://placehold.it/200x50/f00" width="296" height="90" />
  </div>
  <h3 align="center"><strong>Chat Panel</strong></h3>
  <div>
    <img src="http://placehold.it/50x50/ff0" />
    <form method="post" action="/logout">
      <input type="submit" value="Leave Chat" />
    </form>
  </div>
</div>

如果您需要针对较旧的浏览器,可以使用 display: table-cell 来完成

#header {
  height: 166px;
  background-color: #ccc;
  font-weight: bold;
  color: black;
  margin-bottom: 3px;
  padding: 2px;
  text-align: center;
  width: 100%;
  display: table;
}
#header * {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}

#footer {
  height: auto;
  background-color: #ccc;
  font-size: 20px;
  font-weight: bold;
  color: black;
  margin-top: 3px;
  padding: 2px;
  text-align: center;
  width: 100%x;
}
<div id="header">
  <div>
    <img src="http://placehold.it/200x50/f00" width="296" height="90" />
  </div>
  <h3 align="center"><strong>Chat Panel</strong></h3>
  <div>
    <img src="http://placehold.it/50x50/ff0" />
    <form method="post" action="/logout">
      <input type="submit" value="Leave Chat" />
    </form>
  </div>
</div>

关于html - 将一个 div 水平对齐到左侧,一个标题对齐到中心,一个 div 水平对齐到右侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38703676/

相关文章:

javascript - 如何通过 jquery 提交表单并检索 JSON 数据?

javascript - 如何使用箭头键和悬停事件将 css 应用于下拉表中的链接?

css - 没有 CSS 和 JS 的 Svg 动画

html - 将工具提示尾部添加到 CSS 下拉菜单?

jquery - Bootstrap 的导航栏下拉菜单无法在自定义断点下方正确打开

css - 表格TD内强制DIV全高

html - Div 未正确 float 。我不确定我错过了什么

jquery - 跨浏览器 HTML5 存储库

css - npm bootstrap 版本 (4.1.0) 没有将所有 css 属性添加到 css 'background' 标签

javascript - 当我尝试以编程方式调整时,为什么 div 和 span 为空值?