I'm trying to vertically and horizontally center some content overlaying an image slide (flexslider). There were some similar questions to this one, but I couldn't find a satisfactory solution that applied directly to my specific problem. Because of the limitations of FlexSlider, I cannot use position: absolute;
on the img tag in my implementation.
I almost have workaround below working. The only problem is I cannot get the width & height declarations to work on inner-wrapper
div with the display: table-cell
property.
Is this standard behavior, or am I missing something with my code? If it's standard behavior, what's the best solution to my problem?
HTML
<ul>
<li>
<img src="#">
<div class="outer-wrapper">
<div class="inner-wrapper">
<h1>My Title</h1>
<h5>Subtitle</h5>
</div>
</div>
</li>
</ul>
CSS
html, body {
margin: 0; padding: 0;
width: 100%; height: 100%;
}
ul {
background: #CCC;
height: 100%;
width: 100%;
list-style-position: outside;
margin: 0; padding: 0;
}
li {
width: 100%;
display: table;
}
img {
width: 100%;
height: 410px;
}
.outer-wrapper {
position: absolute;
width: 100%;
height: 100%;
top: 0;
margin: 0; padding: 0;
}
.inner-wrapper {
display: table-cell;
vertical-align: middle;
text-align: center;
width: 100%;
height: 100%;
}
Note: the centered content will be more than 1 element, so I can't use the line-height trick.
This question is related to
html
css
vertical-alignment
flexslider
css-tables
Just add min-height:100% and min-width:100% and it will work. I had the same problem. You don't need a 3th wrapper
I figured this one out. I know this will help someone someday.
How to Vertically & Horizontally Center a Div Over a Relatively Positioned Image
The key was a 3rd wrapper. I would vote up any answer that uses less wrappers.
HTML
<div class="wrapper">
<img src="my-slide.jpg">
<div class="outer-wrapper">
<div class="table-wrapper">
<div class="table-cell-wrapper">
<h1>My Title</h1>
<p>Subtitle</p>
</div>
</div>
</div>
</div>
CSS
html, body {
margin: 0; padding: 0;
width: 100%; height: 100%;
}
ul {
width: 100%;
height: 100%;
list-style-position: outside;
margin: 0; padding: 0;
}
li {
width: 100%;
display: table;
}
img {
width: 100%;
height: 100%;
}
.outer-wrapper {
width: 100%;
height: 100%;
position: absolute;
top: 0;
margin: 0; padding: 0;
}
.table-wrapper {
width: 100%;
height: 100%;
display: table;
vertical-align: middle;
text-align: center;
}
.table-cell-wrapper {
width: 100%;
height: 100%;
display: table-cell;
vertical-align: middle;
text-align: center;
}
You can see the working jsFiddle here.
How about this? (jsFiddle link)
CSS
ul {
background: #CCC;
height: 1000%;
width: 100%;
list-style-position: outside;
margin: 0; padding: 0;
position: absolute;
}
li {
background-color: #EBEBEB;
border-bottom: 1px solid #CCCCCC;
border-right: 1px solid #CCCCCC;
display: table;
height: 180px;
overflow: hidden;
width: 200px;
}
.divone{
display: table-cell;
margin: 0 auto;
text-align: center;
vertical-align: middle;
width: 100%;
}
img {
width: 100%;
height: 410px;
}
.wrapper {
position: absolute;
}
Your 100% means 100% of the viewport, you can fix that using the vw unit besides the % unit at the width. The problem is that 100vw is related to the viewport, besides % is related to parent tag. Do like that:
.table-cell-wrapper {
width: 100vw;
height: 100%;
display: table-cell;
vertical-align: middle;
text-align: center;
}
I discovered that the higher the value of 'width' is, the smaller the box width is made and vice versa. I found this out because I had the same problem earlier. So:
.inner-wrapper {
width: 1%;
}
solves the problem.
Welcome to 2017 these days will using vW
and vH
do the trick
html, body {_x000D_
margin: 0; padding: 0;_x000D_
width: 100%; height: 100%;_x000D_
}_x000D_
_x000D_
ul {_x000D_
background: #CCC;_x000D_
height: 100%;_x000D_
width: 100%;_x000D_
list-style-position: outside;_x000D_
margin: 0; padding: 0;_x000D_
}_x000D_
_x000D_
li {_x000D_
width: 100%;_x000D_
display: table;_x000D_
}_x000D_
_x000D_
img {_x000D_
width: 100%;_x000D_
height: 410px;_x000D_
}_x000D_
_x000D_
.outer-wrapper {_x000D_
position: absolute;_x000D_
width: 100%;_x000D_
height: 100%;_x000D_
top: 0;_x000D_
margin: 0; padding: 0;_x000D_
}_x000D_
_x000D_
.inner-wrapper {_x000D_
display: table-cell;_x000D_
vertical-align: middle;_x000D_
text-align: center;_x000D_
width: 100vw; /* only change is here "%" to "vw" ! */_x000D_
height: 100vh; /* only change is here "%" to "vh" ! */_x000D_
}
_x000D_
<ul>_x000D_
<li>_x000D_
<img src="#">_x000D_
<div class="outer-wrapper">_x000D_
<div class="inner-wrapper">_x000D_
<h1>My Title</h1>_x000D_
<h5>Subtitle</h5>_x000D_
</div>_x000D_
</div>_x000D_
</li>_x000D_
</ul>
_x000D_
Source: Stackoverflow.com