I'm having a problem centering an element that has the attribute position
set to absolute
.
Does anyone know why the images are not centered?
body {
text-align: center;
}
#slideshowWrapper {
margin-top: 50px;
text-align: center;
}
ul#slideshow {
list-style: none;
position: relative;
margin: auto;
}
ul#slideshow li {
position: absolute;
}
ul#slideshow li img {
border: 1px solid #ccc;
padding: 4px;
height: 450px;
}
<body>
<div id="slideshowWrapper">
<ul id="slideshow">
<li><img src="img/dummy1.JPG" alt="Dummy 1" /></li>
<li><img src="img/piano_unlicened.JPG" alt="Dummy 2" /></li>
</ul>
</div>
</body>
see demo on: http://jsfiddle.net/MohammadDayeh/HrZLC/
text-align: center
; works with aposition: absolute
element when addingleft: 0; right: 0;
The simpler, the best:
Then you need to insert your img tag into a tag that sports position:relative property, as follows:
Using
left: calc(50% - Wpx/2);
where W is the width of the element works for me.If you don't know the width of the element you can use this code:
Demo at fiddle: http://jsfiddle.net/wrh7a21r/
Source: https://stackoverflow.com/a/1777282/1136132
Use
margin-left: x%;
where x is the half of the width of the element.Div vertically and horizontally aligned center
Note : Elements should have width and height to be set