Apolo Radomer Apolo Radomer - 1 month ago 9
CSS Question

Align hr vertically behind div with text

I am trying to create a banner/tittle that will have a line behind the main div. I want the line and the text of the tittle to be in the middle (vertically) as shown :

enter image description here

The problem is that if I change the size of the browser, the

hr
and the text are not aligned (vertically). Below is the first version :



.section {
clear: both;
padding: 0px;
margin: 0px;
}

.col {
text-align:center;
display: block;
float:left;
margin: 1% 0 1% 0%;
}
.col:first-child { margin-left: 0; }

.group:before,
.group:after { content:""; display:table; }
.group:after { clear:both;}
.group { zoom:1; /* For IE 6/7 */ }

.span_3_of_3 { width: 100%; }
.span_2_of_3 { width: 66.66%; }
.span_1_of_3 { width: 33.33%; }

@media only screen and (max-width: 480px) {
.col { margin: 1% 0 1% 0%; }
.span_3_of_3, .span_2_of_3, .span_1_of_3 { width: 100%; }
}

#middle
{
background:#CCC;
color:#FC3699;
height:100px;
margin-top:0;
line-height:100px;
}

hr {
margin-top:40px;
border:2px solid #FC3699;
}

<div class="section group">
<div class="col span_1_of_3">
<div class="topsection">
<div class="header"><hr /></div>
</div>
</div>
<div id="middle" class="col span_1_of_3">
aaaaaaa
</div>
<div class="col span_1_of_3">
<div class="topsection">
<div class="header"><hr /></div>
</div>
</div>
</div>





And here is the second version. Instead of having 3 columns I could use something like the snippet below. The problem is in this line :

height:100px;
line-height: 100px;

where I want the height to be 100%. But the line-height cannot:



.main {
height:100%;
min-height:100px;
display: block;
text-align: center;
overflow: hidden;
white-space: nowrap;
}

.main > span {
width:200px;
height:100px;
line-height: 100px;
background:#F1F1F1;
position: relative;
display: inline-block;
}

.main > span:before,
.main > span:after {
content: "";
position: absolute;
top: 50%;
width: 9999px;
height: 3px;
background: #FC3699;
}

.main > span:before {
right: 100%;
margin-right: 15px;
}

.main > span:after {
left: 100%;
margin-left: 15px;
}

<div class="main">
<span style="vertical-align: middle;">Text</span>
</div>





Here is the third and best version where I only have to
text-aling:center;
the text. But even if I add it in the CSS, it does not work for a reason:



.main {
height:100%;
min-height:100px;
display: block;
text-align: center;
overflow: hidden;
white-space: nowrap;
}

.main > span {
width:200px;
height:100px;
line-height: 100px;
background:#F1F1F1;
position: relative;
display: inline-block;
}

.main > span:before,
.main > span:after {
content: "";
position: absolute;
top: 50%;
width: 9999px;
height: 3px;
background: #FC3699;
}

.main > span:before {
right: 100%;
}

.main > span:after {
left: 100%;
}

#child {
display: table-cell;
vertical-align: middle;
}

<div class="main">
<span><div id="child">Text</div></span>
</div>




Answer

If flexbox is an option, this is easy - you just have to give

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

and you can do away with all the positioning and the width calculations - see demo below:

.main {
  height: 100%;
  min-height: 100px;
  display: flex;
  justify-content:center;
  align-items:center;
  width: 100%;
  text-align: center;
  white-space: nowrap;
}
.main > span {
  width: 200px;
  height: 100px;
  background: #F1F1F1;
  display: inline-flex;
  justify-content:center;
  align-items:center;
}
.main:before,
.main:after {
  content: "";
  height: 3px;
  background: #FC3699;
  flex:1;
}
<div class="main">
  <span>Text</span>
</div>