Wrap text around an image with CSS
A few of the IMG Attributes from previous version of HTML have been deprecated in HTML5. One of the most-used was the image alignment attributes. You had quite a few to choose from:
Absmiddle
Bottom
Middle
Left
Right
Texttop
Top
In HTML5 text wrapping is done with CSS. Let's see how to get the following style:
The first thing to do is to set up a STYLE in the HEAD section of the HTML. Add the highlighted code to your own HTML:
margin: 10px;
margin-right
margin-top
margin-bottom
margin-bottom: 10px;
To apply the style to the image, you need to add the CLASS attribute to the IMG tag:
Amend your own IMG tag and add CLASS="TextWrap" to your own IMG code. Before you try it out, add a paragraph of text below the image:
<P>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at ante. Mauris eleifend, quam a vulputate dictum, massa quam dapibus leo, eget vulputate orci purus ut lorem. In fringilla mi in ligula. Pellentesque aliquam quam vel dolor. Nunc adipiscing. Sed quam odio, tempus ac, aliquam molestie, varius ac, tellus. Vestibulum ut nulla aliquam risus rutrum interdum. Pellentesque lorem. Curabitur sit amet erat quis risus feugiat viverra.
</P>
Save your work and view the results in your browser.
There is, however, a problem with the above code. Suppose we want a second paragraph of text with another image floated on the left. We want to do this:
To achieve this, you might think of adding a second style and then applying it to the second image. Like this:
However, saving the work and refreshing gives you this in the browser:
The way to correct this is to use a CSS property called clear. This clears any floating elements and gets you back to the normal, default flow for browsers. The clear property can take four values: left, right, both, none. Because our first image was floated to the right, we want to clear to the right. We can add this to the second style:
Thursday, 9 July 2015
Wrap text around an image with CSS
Absbottom
So you could do this:
<IMG SRC="york_images/york_minster.jpg" ALIGN="Top">
What you were doing here was wrapping text around an image. You were not aligning an image independent of text. You were saying, "wrap the text to the top of the image".
float: right;
The CSS property we need in order to move the image is called float. The float property can take three values: left, right and none. To get some space between the image and the text we can use the margin property. We've set it to 10 pixels. This will give you space around the entire image. If you only want space on specific sides of the image you can use these:
margin-left
So we could have done this:
margin-left: 10px;
That would get us a 10 pixels margin on the left of the image and 10 pixels at the bottom.
<IMG SRC="york_images/york_minster_2.gif" CLASS="TextWrap">
The CLASS attribute doesn't have to go at the end. If you prefer, you can put it after the IMG tag:
<IMG CLASS="TextWrap" SRC="york_images/york_minster_2.gif" >
Just take note of where all the spaces are in the code above.
<IMG class="TextWrap" SRC="york_images/york_minster_2.gif">
You can, of course, use your own text, and not just the Lorem ipsum text. Make sure your IMG code is above the first P tag, however.
0 comments:
Post a Comment