18

I have two HTML elements inside a div. I want to display them in a row or a horizontal line. Let's say I have two images with the code shown below. How would I make it so there is no line break after the first element so they will be displayed one after the other horizontally. Right now the second image is displayed below the first. I want the second image to be displayed to the right of the first. I am pretty sure this can be done in CSS. Please Help.

<img src="image one.jpg">
<img src="image two.jpg">
Web_Designer
  • 72,308
  • 93
  • 206
  • 262
  • 1
    possible duplicate of [Display two images side by side on an HTML Page](http://stackoverflow.com/questions/2839318/display-two-images-side-by-side-on-an-html-page) – Nikita Rybak Dec 28 '10 at 22:26
  • 3
    Add `display : flex` or `display : inline-flex` to `img` – Choxmi Aug 20 '19 at 07:15

4 Answers4

22

Option 1

img {
 display:inline;
}

Option 2

img {
 display:block;
 float:left;
}

Updated to reflect current browser capabilities

Option 3

img {
 display:inline-block;
}

However, this will only work if there is enough horizontal space for the images in question.

Sandwich
  • 2,304
  • 3
  • 24
  • 30
7

The hack is to set position: relative; on the parent div and

position: absolute; 
top: 0px; 
left: {left image's computed width}px; 

on the second one. Other wise you simple increase the div size.

JP Hellemons
  • 5,977
  • 11
  • 63
  • 128
Babiker
  • 18,300
  • 28
  • 78
  • 125
3

The image elements are inline elements, so they display horizontally, unless you changed the "display" CSS rule. I think you don't have enough space for them to fit horizontally.

Pik'
  • 6,819
  • 1
  • 28
  • 24
0

You can do this with CSS (position attribute) or with a table. It will not have a line break by default unless the pictures are too wide to fit on one line. In that case, it is questionable design to force them to be on the same line.

Jonathan
  • 1,487
  • 11
  • 12