简体   繁体   English

如何垂直居中 HR 元素

[英]How to vertically center an HR element

How do I vertically center an hr element?如何垂直居中hr元素?

I've tried to place the element in some div elements...我试图将元素放在一些div元素中......

<div class="table">
    <div class="table-cell"><hr /></div>
</div>
<div class="table">
    <div class="table-cell">Some text Here!</div>
</div>
<div class="table">
    <div class="table-cell"><hr /></div>
</div>

I've left out that I am using bootstrap and have these three div elements with the class of table in the same row using the col-size-number format.我忽略了我正在使用引导程序并使用 col-size-number 格式将这三个div元素与 table class放在同一行中。


So I'm looking for inline HR element Some Text inline HR element所以我正在寻找内联 HR 元素 Some Text inline HR element

--------------SOME TEXT-------------- --------------一些文字--------------

Thank you again css guru masters!再次感谢css大师高手!

For a simple, generic way to vertically center an hr element:对于垂直居中hr元素的简单通用方法:

html: html:

<div class="container">
  <hr />
</div>

css: css:

.container {
    display: flex;
    align-items: center;
}

.container hr {
    width: 100%;
}

The hr element needs a width given to it (or flex-grow ), otherwise it will be only a dot in the center of the container. hr元素需要给它一个width (或flex-grow ),否则它将只是容器中心的一个点。

 .container { display: flex; align-items: center; height: 100px; background: yellow; } .container hr { flex-grow: 1; }
 <div class="container"> <hr /> </div>

我在我的 css 中使用了以下解决方案,在每个设备CSS上将元素边距居中: margin: 0 auto;

To anyone who's stumbled upon this in the future, I have updated the answer to what I feel is a more modern, efficient approach using Flexbox对于将来偶然发现此问题的任何人,我已将答案更新为我认为使用Flexbox的更现代、更有效的方法

HTML: HTML:

<div class="title">
  <hr />
  <h3>Some text</h3>
  <hr />
</div>

CSS: CSS:

.title {
  display:flex;
  flex-flow:row;
  justify-content:center;
  align-items:center;
}
hr {
  flex:1;
}
h3 {
   padding:0px 48px;
}

DEMO HERE演示在这里

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

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