html - 保持中间元素水平居中

标签 html css header

我正在尝试制作我的第一个自定义 header 。

我希望中间的元素(例如 Logo )保持居中,无论两边有多少内容。

我尝试使用 z-index 但我认为那是错误的。

HTML:

.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-flow: row wrap;
  justify-content: space-around;
}

.flex-item {
  text-align: center;
}

.image {
  z-index: -1;
}
<div id="header_container">
  <div id="header">

    <ul class="flex-container">
      <li class="flex-item">Menu items 1 BHello | seven | eight | nineteen | Fitiyytwooooo
      </li>
      <li class="image">
        <img src="http://www.plankdesign.com/wp-content/uploads/2011/01/html4.jpg" style="width:204px;height:128px;">
      </li>
      <li class="flex-item">Menu items 2 Blah | Blah | Blah</li>
    </ul>
  </div>
</div>

这是我的 jsfiddle demo .

最佳答案

像这样尝试: Demo

.image img {
    z-index: 1000;
    width:18%;
    height:128px;
    position:absolute;
    left:50%;
    margin-left:-9%; /* half of the width */
}

HTML:

 <li class="image">
   <img src="http://www.plankdesign.com/wp-content/uploads/2011/01/html4.jpg" />
 </li>

关于html - 保持中间元素水平居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30723570/

相关文章:

php - 使用 Header Redirect 没有成功

javascript - 有没有办法将 css 文件或 javascript 文件动态注入(inject)到 HTML 文档的 head 标签中。

html - 具有固定和百分比宽度内部列的 flex 容器

css - 我的 CSS 未应用于具有 ID 的元素

twitter-bootstrap - 是否可以只为特定设备而不是其他设备应用特定的 Bootstrap 类。?

css - 我们如何计算“flex 基准:自动和最小宽度”和“横轴宽度”?

css - 宽度为 100% 中心的背景图像以固定位置

html - PHP 文本框长度

html - 使用 :host-context() with an adjacent sibling selector

http - 在 HTTP header 或 HTTP 正文中将用户名/密码作为参数传递哪个更好?