XML technologies / CSS / Advanced websites with CSS / Putting CSS in perspective / What is CSS actually for? / Images and CSS

(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)

Images are inserted into HTML using the tag <img>. CSS offers limited control over an image’s appearance, but stylesheets still provide some interesting possibilities.

Like any other object on a web page, an image can be placed anywhere using the tag <div>. The tag <div> also lets you influence an image’s border and alignment, while a style rule can change the border’s appearance.

The following figure shows text flow beside an image without CSS. The image appears at the page’s left edge, aligned with the first text line’s baseline. Most of the paragraph appears below the image.

  Image and adjacent text without CSS

Figure: Image and adjacent text without CSS.

CSS is also particularly useful for wrapping text around images. The property float (described further in A simple CSS layout) lets you integrate an image neatly with its adjacent text. The following figure shows what happens when we position the image correctly using the property float. Text first flows around the image’s edge, then below it. This fits our design much better than the rendering in the preceding figure.

  Image and adjacent text using float

Figure: Image and adjacent text using float.

To let text wrap around every image on your page, add the following rule to your stylesheet:

img {
   float: left;
}

  

 

 

 

back to top