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>
Without knowing the
width
/height
of the positioned1 element, it is still possible to align it as follows:EXAMPLE HERE
It's worth noting that CSS Transform is supported in IE9 and above. (Vendor prefixes omitted for brevity)
Explanation
Adding
top
/left
of50%
moves the top/left margin edge of the element to the middle of the parent, andtranslate()
function with the (negative) value of-50%
moves the element by the half of its size. Hence the element will be positioned at the middle.This is because a percentage value on
top
/left
properties is relative to the height/width of the parent element (which is creating a containing block).While a percentage value on
translate()
transform function is relative to width/height of the element itself (Actually it refers to the size of bounding box).For unidirectional alignment, go with
translateX(-50%)
ortranslateY(-50%)
instead.1. An element with a
position
other thanstatic
. I.e.relative
,absolute
,fixed
values.I'm not sure what you want to accomplish, but in this case just adding
width: 100%;
to yourul#slideshow li
will do the trick.Explanation
The
img
tags are inline-block elements. This means that they flow inline like text, but also have a width and height like block elements. In your css there are twotext-align: center;
rules applied to the<body>
and to the#slideshowWrapper
(which is redundant btw) this makes all inline and inline-block child elements to be centered in their closest block elements, in your code these areli
tags. All block elements havewidth: 100%
if they are the static flow (position: static;
), which is default. The problem is that when you tellli
tags to beposition: absolute;
, you take them out of normal static flow, and this causes them to shrink their size to just fit their inner content, in other words they kind of "lose" theirwidth: 100%
property.A simple CSS trick, just add:
This works on both images and text.
to center a a position:absolute attribute you need to set left:50% and margin-left: -50% of the width of the div.
for vertical center absolute you need to do the same thing bud not with left just with top. ( NOTE: html and body must have min-height 100%; )
and can be combined for both
Here is easy and best solution for center element with “position: absolute”
Position absolute takes it out of the flow, and places it at 0x0 to the parent ( Last block element to have a position absolute or position relative ).
I'm not sure what exactly you what you are trying to accomplish, It might be best to set the li to a
position:relative
and that will center them. Given your current CSSCheck out http://jsfiddle.net/rtgibbons/ejRTU/ to play with it