tags:

views:

57

answers:

5

Consider the task of replacing this table with CSS stylings:

<table border="1">
  <tr><td align="center">
       <img src="foo" />
  </td></tr>
  <tr><td align="center">
       <img src="bar" />
    </td></tr>
  <tr><td align="center">
       <img src="bat" />
    </td></tr>
</table>

The desired output is 3 images stacked on top of each other. The images are centered on the widest of them all.

How would you style this markup with <div> around those <img> tags with CSS to achieve the same look?

+1  A: 
<p style="text-align:center;width:auto;"><img  /><br /><img /><br /><img  /></p>
vittore
its not very clean to use <br />. They are used for line breaks in text, not to style your content.
meo
@david: img basically inline element, so that's it - simplest way without additional styling
vittore
+4  A: 
<div class="images">
  <img src="foo" />
  <img src="bar" />
  <img src="bat" />
</div>

and in CSS

div.images img {
  display: block;
  margin: 0 auto;
}
Tomalak
wauww was thinking the same way
x4tje
+1  A: 
<style>
div.table img {
  display: block;
  margin: 0 auto;
}
</style>

<div class="table">
  <img src="foo" />
  <img src="bar" />
  <img src="bat" />
</div>

something like that

x4tje
margin: 0 auto; only works if you specify the with of an element.
meo
@david: a DIV is as wide as is can be.
Tomalak
+2  A: 

if you really want to use a DIV element do it like that:

html:

<div class="imgContainer">
<img src="blabla.jpg" alt="blabla" />
<img src="blabla2.jpg" alt="blabla" />
<img src="blabla3.jpg" alt="blabla" />
</div>

css:

div.imgContainer {
    width: auto;
    text-align: center;
}

div.imgContainer img{
    display: block;
}
meo
A: 

Centered on the whole page:

<html>
<style>
.centeredDiv
{
width:400px;margin-left:auto;margin-right:auto;background-color:#FF0000;
}

</style>
<body style="text-align:center;" >
<div class="centeredDiv">
<div>image</div>
<div>image</div>
<div>image</div>
</div>


</body>
</html>
DShultz