-8

I would like to align an image in a <div>, without using any IDs. All of the solutions I've seen have used IDs or classes. I don't want to use those.

CSS

div {
    text-align: left;
 }

div img{
    //Tried many things but no luck
} 

HTML

<div>
  <img src="dd.png" />
</div>

How can I center all the images in a div?

Cody Gray
  • 230,875
  • 49
  • 477
  • 553
DarthVader
  • 49,515
  • 70
  • 199
  • 295

2 Answers2

2

I'm not sure about why your current code have default text-align: left for div element.

But please try this one: http://jsfiddle.net/18230pwa/

div img {
    display : block;
    margin : auto;
}

div {
    text-align: left;
}
Troy
  • 36
  • 3
1

div { text-align: center; }

<div> //Image link </div>

you don't need to specifically target the image. if the image is in the div, it will be centered. you could also use the <center> tag which functions similarly.

if you aren't looking to center it, just use the text-align method above but use the keyword right or left.

Thomas Tallman
  • 71
  • 1
  • 1
  • 13