< Ordered and unordered lists (create a horizontal menu/navi bar) | Create a User bar siggy (Paint Shop Pro Tutorial) >
19 Nov 2006 > 4 Comments
The <img> tag doesn’t actually contain anything. Images are displayed by using the src (source) attribute, accompanied with some alternate text using the alt attribute.
In XHTML the <img> tag must be closed.
Note: I’m using XHTML markup.
<img src="images/image.jpg" alt="my image" />
In HTML <img> has no closing tag
<img src="images/image.jpg" alt="my image">
Because images display as inline elements, it’s recommended that you enclose them in a block level element such as a <p> or a <div>.
<p><img src="images/image.jpg" alt="my image" /></p>
<div><img src="images/image.jpg" alt="my image" /></div>
I’m displaying images in my template inside <div> tags, plus I’m including some accompanying text below the image as a paragraph, like so:
<div><img src="images/yoda.jpg" alt="picture of Yoda" /><p>yoda</p></div>
It’s possible to target the image <div> in the CSS, like this:
div div {text-align: center;}

However, if you have other nested divs (for instance, later on, I’ll nest a <div> to style a horizontal rule <hr />), you’ll be better taking advantage of classes.
I can create a .class selector in the stylesheet, and add the class attribute to the image <div> tag:
<div class="image"><img src="images/yoda.jpg" alt="Yoda" /><p>yoda</p></div>
.image {text-align: center;}
That’s exactly what I intend to do next, in order to float: the image right; and left;
Create two classes; one called .imgfl (image float left), and .imgfr (image float right), and —as in the example above— add them too your image divs using the class attribute.
<div class="imgfl"><img src="images/yoda.jpg" alt="Yoda" />
<p>use the force<br /> and float left</p></div>
<p>The luminous branch was concealed within the dark grove of the
moon goddess Diana, and grew -like mistletoe- upon a certain tree that,
according to Virgil (from whose epic the myth comes), was actually two
trees. Only the worthy were able to sever the golden branch, and when
severed, another would grow back in its place.</p>
<div class="imgfr"><img src="images/yoda.jpg" alt="Yoda" />
<p>use the force<br /> and float right</p></div>
<p>The luminous branch was concealed within the dark grove of the
moon goddess Diana, and grew -like mistletoe- upon a certain tree that,
according to Virgil (from whose epic the myth comes), was actually two
trees. Only the worthy were able to sever the golden branch, and when
severed, another would grow back in its place.</p>
.imgfl {float: left;}
.imgfr {float: right;}
Here’s the result:

To transform the image into a link enclose the img element in an anchor tag () and use the <dfn title="hypertext reference">href</dfn> attribute.
<div>
<img src="images/yoda.jpg" alt="Yoda" /><p>yoda</p></div>

To remove the image border add the type selector img to the stylesheet, using the property border-style:, with the value of none;
img {border-style: none;}
And that about wraps it up, I think
< Back to everything
Keep going… >
Comment on October 3, 2007 at 8:22 pm
I cannot seem to get the href(img-src=¨img-ref¨) to work please help. Thanks for sharing all this with us.
Comment on October 4, 2007 at 4:42 am
Hi, Alvin. I’d love to help, if I can, although I’m not sure what you are having a problem with.
href(img-src=¨img-ref¨)
Are you trying to make an image link?
Comment on October 7, 2007 at 7:54 pm
Mahud, I am truly puzzled by the dfn element where does it go? this is my code index.html href
yoda
use the force and float left
and my style.css .imgfl { float: left; /* a class to float img left */ } This is obviouly wrong what’s missing?
Comment on October 8, 2007 at 7:44 am
Hi, Buzz
The definition <dfn> element is used to define something, for example:
My website is a <dfn>blog<dfn>.
It should render as italic. I never use it though.
I can’t tell what the problem is with your float, from what you have posted. .imgfl { float:left; }, should work, though.
Can you post a link to your site or something, as that might help me figure out what’s wrong?
Mahud Version 5 © 2006-2008 > powered by Mr Whippy Wordpress