2

I'm trying to achieve something like this:

                           [CENTER IMAGE]             [RIGHT IMAGE]

Here's an image example of what I mean: http://prntscr.com/a9vuxv

I have applied the following code, but it puts both images on the right and my aim is to put one image in the center and one on the right:

<div style="width: 100%;">
  <div style="float: right;">
    <img src="centerimage.png" />
  </div>
  <div style="float: right;">
    <img src="rightimage.png" />
  </div>
</div>
Travesty3
  • 14,608
  • 6
  • 56
  • 96
The Codesee
  • 3,628
  • 3
  • 32
  • 71

5 Answers5

1

You can use the trick with display: inline-block and text-align: center.

<div style="width: 100%; text-align: center; position: relative;">
  <div style="display: inline-block;">
    <img src="http://bit.ly/1UwB3sP" />
  </div>
  <div style="position: absolute; right: 0; top: 0">
    <img src="http://bit.ly/1UwB3sP" />
  </div>
</div>
martin
  • 85,731
  • 23
  • 174
  • 212
1

Any element in the flow will affect the margins or alignment of the center image/div. Therefore we need to remove the element from the document flow and use absolute positioning.

Then the center element can be centered per any usual method...here I remove all the floats, use inline-block / text align:center to center the middle image/div and position the right image/div absolutely.

.parent {
  text-align: center;
  position: relative;
}
.center {
  display: inline-block;
  background: red;
}
.right {
  position: absolute;
  top: 0;
  right: 0;
  background: pink;
}
<div class="parent">
  <div class="center">
    Some Image Centered
  </div>
  <div class="right">
    some image right
  </div>
</div>

Note: You could, of course, float/align the right div/image and position the center div/image absolutely....its a choice.

Paulie_D
  • 102,164
  • 10
  • 117
  • 144
0

Set the container div to text-align: center; and set the right element to absolute right.

.container{
  text-align: center;
}

.right{
  position: absolute;
  right: 0;
}
<div class="container">
  <img class="center" src="http://i.stack.imgur.com/e4z3K.jpg">
  <img class="right" src="http://i.stack.imgur.com/T5KPW.jpg">
</div>
spencer.sm
  • 17,134
  • 10
  • 72
  • 83
0

Here is the solution.

.right{
  float: right;
}
.center{
  overflow: hidden;
}
.center img{
  display: block;
  margin: 0 auto;
}
<div style="width: 100%;">
  <div  class="right">
<img src="http://lorempixel.com/150/150/nature/" />
  </div>
  
  <div  class="center">
<img src="http://lorempixel.com/150/150/nature/" />
  </div>
</div>
Felix A J
  • 6,114
  • 2
  • 25
  • 41
-2

.center{
  display: block;
  margin: auto;
  }
<div style="width: 100%;">
<div style="float: right;">
<img src="centerimage.png" width="100" height="100"/>
</div>
<img class="center" src="rightimage.png" width="100" height="100" />
</div>

Remove float:right in the center image and add the folowing rules:

margin: auto;
display: block;
The Codesee
  • 3,628
  • 3
  • 32
  • 71
silviagreen
  • 1,659
  • 1
  • 15
  • 37
  • 2
    The center image isnt actually centered...it's affected by the right div. – Paulie_D Mar 01 '16 at 16:36
  • 2
    its probably a better plan to pull the el out of flow, entirely, with absolute positioning, but if the imgs are the right size, a left float will work reasonably well. this answer should not be downvoted. – Bosworth99 Mar 01 '16 at 17:40