Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I place two images side by side with HTML and CSS to optimally fill up a given width?

Tags:

html

css

image

I want to place two arbitrary images side by side inside a DIV element, which is exactly 800 px wide (width=800px). The images can be a variety of sizes, in width and height too. Sometimes width greater than height and sometimes width lesser than height. I put both images inside a div element, and a third div which contains both. I tried this code, but doesn't work properly. How should I do it? JSFiddle: http://jsfiddle.net/gUT43/

<!DOCTYPE html>
<html>
<head>
<style>
img {
     max-width: 100%;
     height: auto;
     width: auto\9; /* ie8 */
    }
div {
 display: inline-block;
 white-space: nowrap;
}
</style>
</head>
<body>

<div style="max-width: 800px; border:2px black solid">

 <div style="height: auto; border:1px green solid"">
  <img src=http://i.imgur.com/Xt6vUQD.jpg>
 </div>

 <div style="height: auto; border:1px blue solid"">
  <img src=http://i.imgur.com/BqFMNlq.jpg >
 </div>

</div>

</body>
</html>
like image 439
Konstantin Avatar asked Oct 21 '22 08:10

Konstantin


2 Answers

It seems to me that you would want to float: left the divs that contain the images, and then add another div after those elements to clear the float.

A JSFiddle of your code would be excellent, so that we could help in a more effcient manner :)

like image 106
mituw16 Avatar answered Oct 24 '22 03:10

mituw16


Here is the HTML:

<div class="main_block">

    <div class="inner_block">
        <img src=http://i.imgur.com/Xt6vUQD.jpg>
    </div>

    <div class="inner_block">
        <img src=http://i.imgur.com/BqFMNlq.jpg >
    </div>    
</div>

Here is the CSS:

.main_block {
    width: 800px;
    border: 2px black solid;
}

.main_block: before, .main_block: after {
    overflow: hidden;
    content: "";
    display: table;
}

.main_block: after {
    clear: both;
}

.inner_block {
    display: inline-block;
    float: left;
    width: 50%;
}

.inner_block img {
    width: 100%;
    height: auto;
    vertical-align: middle;
}

Here is the fiddle link: http://jsfiddle.net/Mohinder/vv9M6/1/

Is this what you are looking for?

like image 42
Minder Saini Avatar answered Oct 24 '22 04:10

Minder Saini