简体   繁体   English

如何在不使用 flexbox 的情况下使用 CSS 将多个项目垂直居中放置在 div 中?

[英]How can I center multiple items inside a div vertically using CSS without using flexbox?

I've been looking all over the internet and I've stumbled upon very similar problems but the solutions I've found vary so slightly that they don't work.我一直在寻找整个互联网,我偶然发现了非常相似的问题,但我发现的解决方案差异很小,以至于它们不起作用。 I'm trying to create a banner for a webpage that has all items vertically aligned to the center without using a flexbox .我正在尝试为一个网页创建一个横幅,其中所有项目都垂直对齐到中心而不使用 flexbox

I need to align the logo and the name to the left and some buttons to the right.我需要将徽标和名称向左对齐,将一些按钮向右对齐。 All I need to do now is align them vertically.我现在需要做的就是垂直对齐它们。 I created a minimal snippet just to show my problem.我创建了一个最小的片段来展示我的问题。

 .banner { background-color: gray; width: 100%; height: 80px; } .banner img { float: left; width: 40px; height: 40px; } .banner h1 { float: left; } .banner button { float: right; width: 80px; height: 40px; }
 <div class="banner"> <img src="https://cdn.sstatic.net/Sites/stackoverflow/Img/favicon.ico"/> <h1>Title</h1> <button>Button 2</button> <button>Button 1</button> </div>

Hope, this solutions solve your problem but it's not actual solutions, it's better you move flex box, or also you can solve it using pading top and bottom.希望,这个解决方案可以解决您的问题,但它不是实际的解决方案,您最好移动 flex box,或者您也可以使用填充顶部和底部来解决它。

 .banner { background-color: gray; width: 100%; height: 80px; display: table; } .left-col { width: 75%; } .right-col { min-width: 200px; } .banner-col { display: table-cell; vertical-align: middle; overflow: hidden; } .banner img { display: inline-block; width: 40px; height: auto; } .banner h1 { display: inline-block; } .banner button { width: 80px; height: 40px; float: right; }
 <div class="banner"> <div class="banner-col left-col"> <img src="https://cdn.sstatic.net/Sites/stackoverflow/Img/favicon.ico"/> <h1>Title</h1> </div> <div class="banner-col right-col"> <button>Button 2</button> <button>Button 1</button> </div> </div>

This can be achieved using a table, which is very supported by any browser.这可以通过使用任何浏览器都非常支持的表格来实现。 If we don't want to use flexbox如果我们不想使用flexbox

Working example here .工作示例在这里

Html - html -

<section class="banner">
  <table>
    <tr>
    <td> 
      <div>
    <img class="logo" src="https://cdn.sstatic.net/Sites/stackoverflow/Img/favicon.ico"/>
    <h1>Title</h1>
    </div>
  </td>
    <td>
    <nav>
    <button>Button 2</button>
    <button>Button 1</button>
  </nav>
    </td>
    </tr>
  </table>
</section>

Css - css -

.banner {
  background-color: gray;
  width: 100%;
  height: 80px;
}

.banner table{
  height: 100%;
  width: 100%;
}

table tr td:last-of-type {
  text-align: right;
}

table h1{
  margin: 0;
  display: inline-block;
  vertical-align: middle;
}

table img.logo {
  vertical-align: middle;
}

To center multiple items just try this out, I have given 2 solutions.Try both要将多个项目居中,请尝试一下,我给出了 2 个解决方案。尝试两个

 display: flex; align-items: center; justify-content: center;

 .Aligner { display: flex; align-items: center; justify-content: center; } .Aligner-item { max-width: 50%; } .Aligner-item--top { align-self: flex-start; } .Aligner-item--bottom { align-self: flex-end; }

A way of positioning vertically in its container is to move the whole banner down its container 50% and then back up by half its own height.在其容器中垂直定位的一种方法是将整个横幅向下移动 50%,然后再向上移动其自身高度的一半。

 body { height: 100vh; } .banner { background-color: gray; width: 100%; height: 80px; position: relative; top: 50%; transform: translateY(-50%); } .banner img { float: left; width: 40px; height: 40px; } .banner h1 { float: left; } .banner button { float: right; width: 80px; height: 40px; }
 <section class="banner"> <img src="img.png" /> <h1>Title</h1> <nav> <button>Button 2</button> <button>Button 1</button> </nav> </section>

Note that this method relies on the banner's parent (or at least the first ancestor which has a position set or implied) having some height set.请注意,此方法依赖于具有一定高度设置的横幅的父级(或至少具有位置设置或隐含位置的第一个祖先)。 In this case the body has been given the full viewport height.在这种情况下,身体被赋予了完整的视口高度。 Depending on your use case you may want to put an additional container with 100vh height.根据您的用例,您可能想要放置一个高度为 100vh 的额外容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM