Reputation: 2101
I have a relatively position wrapper around three absolutely positioned images. How can I center these images inside the wrapper without changing the 'left' value so that I can make this module fluid responsive?
My html:
<div class="illustrations">
<img src="http://fillmurray.com/175/112" alt="" class="platform desktop">
<img src="http://fillmurray.com/143/110" alt="" class="platform tablet" style="left: 110px;">
<img src="http://fillmurray.com/40/87" alt="" class="platform mobile" style="left: 230px;">
</div>
See my fiddle here https://jsfiddle.net/gjexgw86/
Just want to always have the images centered without changing the absolute 'left' values.
Upvotes: 0
Views: 65
Reputation: 105853
You may give a try to both right
and lef
t coordonates and use margin:auto;
.illustrations {
position: relative;
text-align: left;
text-align: center;
height:140px;
/* see me and my center */
border: solid;
background:linear-gradient(to left, transparent 50%, rgba(0,0,0,0.2) 50% ),linear-gradient(to top, transparent 50%, rgba(0,0,0,0.2) 50% )
}
.platform {
transform: translatey(-50%);/*transform would be for vertical centering , optionnal */
vertical-align: middle;
position: absolute;
left: 0;
right:0;/* add this to center */
margin:70px auto;/* 70px from + transform would be for vertical centering , optionnal */
transition: all .7s ease-in-out;
}
.illustrations:hover .platform {
/* see behind */
opacity:0.5
}
<div class="illustrations">
<img src="http://fillmurray.com/175/112" alt="" class="platform desktop">
<img src="http://fillmurray.com/143/110" alt="" class="platform tablet" style="left: 110px;">
<img src="http://fillmurray.com/40/87" alt="" class="platform mobile" style="left: 230px;">
</div>
Notice that :
left:0;
+ right:0;
= in the midddleleft:110px;
+ right:0;
= 110px away from middleleft:230px;
+ right:0;
= 230px away from middlehttps://jsfiddle.net/gjexgw86/4/
Upvotes: 0
Reputation: 5985
If you can't change the left
value, you'll have to change their translate
value.
Not sure how you're scripting the whole thing, but you'd use
transform:translateX(-125px);
or however much you need to move them.
Upvotes: 1
Reputation: 4350
For a responsive result, you need to set relative positions between images instead absolute values (absolute left position) :
See in https://jsfiddle.net/gjexgw86/2/ using negative margin-left between images and let the outer container center the content.
.illustrations {
width:100%;
text-align:center;
height:140px;
}
.platform {
transition: all .7s ease-in-out;
}
<div class="illustrations">
<img src="http://fillmurray.com/175/112" alt="" class="platform desktop">
<img src="http://fillmurray.com/143/110" alt="" class="platform tablet" style="margin-left: -65px;">
<img src="http://fillmurray.com/40/87" alt="" class="platform mobile" style="margin-left: -30px;">
</div>
Upvotes: 0
Reputation: 409
Are you looking for responsive way of arranging the images, here I did with Bootstrap.
Check here : https://jsfiddle.net/my23wuxz/
<div class="container-fluid">
<div class="row">
<div class="col-sm-4" style="text-align:center;"><img src="http://fillmurray.com/175/112" alt=""> </div>
<div class="col-sm-4" style="text-align:center;"><img src="http://fillmurray.com/143/110" alt="" > </div>
<div class="col-sm-4" style="text-align:center;"><img src="http://fillmurray.com/40/87" alt="" ></div>
</div>
</div>
Upvotes: 0