Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<img> element and display:table-cell

Tags:

html

css

I am trying to replace images in a single horizontal row - as cells in a table row.

That layout works with any other elements but not with <img> for some reason.

Check this:

div { display: table; border: 1px solid red; }
div > img { display: table-cell; }
<p>These shall be replaced in single row but they are not:</p>

<div>
  <img src="http://lorempixel.com/output/city-q-c-78-50-6.jpg" />
  <img src="http://lorempixel.com/output/people-q-c-78-50-5.jpg" />
  <img src="http://lorempixel.com/output/animals-q-c-78-50-5.jpg" />
</div>

Any idea?

UPDATE: FF follows CSS spec and replaces them in single row. All other browsers are not. Heil Firefox!

like image 918
c-smile Avatar asked Nov 22 '15 21:11

c-smile


People also ask

Can you insert an image in a table cell?

Click inside the cell where you want to position the picture file (first cell in the first row for this example). Click the Insert tab. Click Pictures in the Illustrations group. Use the Insert Pictures dialog to find and insert the picture.

How do I insert an image in a table cell in HTML?

Use the img Tag to Add Image Inside the td Element in HTML We can use the img tag inside the td element, the table data, to add an image inside the table cell. A td tag defines each cell in the table. Any data inside <td></td> are the content of table cell. We can specify the image source in the img tag.

Can we insert an image in a table cell using the IMG tag?

Answer: Yes, we can insert image in a table cell by using <img> tag within the <td> tag.


1 Answers

EDIT

img is a replaced element, it's measured calculations and box model are different. See this ARTICLE


If you insist on using table and are concerned about spacing look at this fork of @StevenThomas's PenCode

I removed all the divs

.container { display: table; table-layout: auto; width: 100%; }

img { display: inline-table; margin: .33em; width: 30%; height: auto; }

Use margin: .125em if you want 4px; border-spacing.


Change div to inline-block

Change img to inline-block

div {
  display: inline-block;
  border: 1px solid red;
}
div > img {
  display: inline-block;
}
<p>These shall be replaced in single row but they are not:</p>

<div>
  <img src="http://lorempixel.com/output/city-q-c-78-50-6.jpg">
  <img src="http://lorempixel.com/output/people-q-c-78-50-5.jpg">
  <img src="http://lorempixel.com/output/animals-q-c-78-50-5.jpg">
</div>
like image 189
zer00ne Avatar answered Oct 04 '22 09:10

zer00ne