I am making a lightweight responsive jQuery slider for my site, and am using the very common markup:
HTML:
<div id="slider">
<ul>
<li><img class="active" src="slide1.jpg" alt=""></li>
<li><img src="slide2.jpg" alt=""></li>
<li><img src="slide3.jpg" alt=""></li>
</ul>
</div>
CSS:
#slider {
width:80%;
max-width:800px;
margin:0 auto;
}
#slider ul {
margin:0px;
padding:0px;
list-style:none;
width:100%;
}
#slider ul li {
width:100%;
position:relative;
}
#slider img {
width:100%;
position:absolute;
top:0px;
box-shadow:0px 20px 40px rgba(0,0,0,.66);
}
#slider img.active {
z-index:1;
}
The problem I am having is that my absolute positioned slides are doing what they are supposed to and removing them self from the structure of the page. (This is not what I want them to do, but it was the easy way to stack them).
I have been reading about CSS Pseudo-Elements today and all the cool stuff people are doing with them. And it made me think... Could they be used to "clearfix" these absolute-positioned elements? My thought was that there might be a way to do something like the following:
#slider img:before {
content:"";
display:block;
position:static;
}
It will obviously take more than those styles to make this happen (because it isn't rendering things how I want it to... I know by this point you may be thinking "Why not just specify the height of the container and be done with it?". Well, the slides are meant to be responsive in that when they shrink, so does the height of the container.
I really want to do as much of this as possible without resorting to jQuery to set the container's height relative to the image height... It's tempting, but I think there's ways around that...
I can't find anything online (yet) on how to do this (or if it is possible). Can some CSS gurus step up and help me out here? I would love to hear alternative ways to stack my slides, or (mainly) if it is possible to use the pseudo-elements to retain the container height around my absolutely-positioned images...
Update: jsFiddle here: http://jsfiddle.net/derekmx271/Hu6Yf/
Update: Getting close to a solution with this (just need to lock down the same dimensions as the slides) :) http://jsfiddle.net/derekmx271/T7A7M/