Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the style of an image in a div class?

Tags:

html

css

I want to do something like this:

    .even {
        float: left;
        text-align: left;
    }

    .even img {
        float: right;
    }

It's not working though. I'm hoping my problem is just a syntax one, but I can't see to figure out how to do this. I've dug around google, but I'm having trouble finding the right set of keywords.

The associated html I'd like to use should look something like ths:

<div class="linkBlock even">
    <img class="linkThumbnail" src="graphics/links/thumbnail_oilcan.jpg" width="267" height="200"/> 
    <h2><a href="http://www.etsy.com/shop/oilcanpress" rel="nofollow">oilcan press</a></h2>
    <p>
        oilcan press is a purveyor of handmade books & word-related artefacts.
        if you'd like to commission work of a particular size or shape or theme let him 
        know via etsy or email: oilcanpress [!at] gmail.com
        they will gladly make custom boxes as per your general requests of colors/concepts.
        if you are desirous of sevral items a bundle can be arranged at reduced prices.
    </p>
    <p>
        you can view much of his work on <a href="http://www.etsy.com/shop/oilcanpress">flickr</a>.
    </p>
</div>

I'd like for the text of that block to float and align left, and the image to float right. With the code I have written above, the image is being centered in the page above the text; it's not floating, it looks like it's still using the static layout.

like image 615
JoshuaD Avatar asked Mar 28 '11 00:03

JoshuaD


People also ask

Can I put style in div?

To specify the size, color, and other properties of a <div> element, you can assign it style rules using CSS.

How do I control an image in a div?

To auto-resize an image or a video to fit in a div container use object-fit property. It is used to specify how an image or video fits in the container. object-fit property: This property is used to specify how an image or video resize and fit the container.

How do you call an image inside a div in CSS?

To select a image inside a div you need to first select the class of the div you want selected, like this . logo-img then add img to select the img element inside the . logo-img div, like this . logo-img img .

Can you style an image in CSS?

Using CSS to style images allows you to uniformly specify how images should appear across your website with only a few rulesets.


3 Answers

Maybe you have a clear attribute defined for the paragraph or some other style sheets? Your code works fine for me.

Use Firebug to debug your css.

like image 45
Peter Zeller Avatar answered Sep 21 '22 15:09

Peter Zeller


I'm only a CSS dabbler but I think this should work:

div.even>img {
    float: right;
}

This matches child img elements inside a div with class even.

like image 135
A Lee Avatar answered Sep 23 '22 15:09

A Lee


<img> & <p> are both block-level elements and so they line-break unless other wise specified. You need to position the <img> & <p> inside the parent div, instead of trying to position the text on the parent div. You also need to specify a width for the parent div and the <p> tag. This is probably why you are seeing the text appear below the image, because it is defaulting to the width of the parent div and cannot fit side by side with the image even though it is floated. You probably want something like this:

.even {
  width: 400px;
  display: block; /*or inline-block*/
  padding: 10px; /*just for a little visual spacing*/
}
.even img {
  position: relative;
  display: inline-block;
  float: right;
  margin-right: 25px;
}
.even p {
  display: inline-block;
  position: relative;
  width: 250px;
  float: right;
}

You should also move your <img> tag below your h2 tag as it might interfere with the float. (I'm assuming you want this to appear above the thumbnail and the text.)

like image 28
biggles Avatar answered Sep 23 '22 15:09

biggles