Deprecated: Assigning the return value of new by reference is deprecated in /home/shan/public_html/mahudsblog/wp-content/plugins/sem-recent-comments/sem-recent-comments.php on line 971
mahud’s blog: Paint Shop Pro Tutorials and Proboard Templates » Displaying Images

mahud's blog

Paint Shop Pro Tutorials, ProBoards Templates, Downloads and More Stuff

< Ordered and unordered lists (create a horizontal menu/navi bar) | Create a User bar siggy (Paint Shop Pro Tutorial) >

Displaying Images

19 Nov 2006 > 4 Comments

Creating a <div> based Template using CSS (Part: 4)

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.

XHTML

In XHTML the <img> tag must be closed.

Note: I’m using XHTML markup.

<img src="images/image.jpg" alt="my image" />

HTML

In HTML <img> has no closing tag

<img src="images/image.jpg" alt="my image">

Images in block level elements

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;}
yodatemp.jpg

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.

Using a class selector

I can create a .class selector in the stylesheet, and add the class attribute to the image <div> tag:

Markup
<div class="image"><img src="images/yoda.jpg" alt="Yoda" /><p>yoda</p></div>
CSS
.image {text-align: center;}

That’s exactly what I intend to do next, in order to float: the image right; and left;

Adding the float property to the image div

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.

Markup
<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>
CSS
.imgfl {float: left;}

.imgfr {float: right;}

Here’s the result:

yodafloat.jpg

Using an image as a hyperlink

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>
linkyyoda.jpg

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 :D

< Back to everything
Keep going… >

Related Posts

4 Comments (Have your say)

  1. Alvin James

    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.


  2. mahud

    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?


  3. buzz

    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?


  4. mahud

    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?


  1. smile
  2. happy
  3. sad
  4. wink
  5. url
  6. bquote
  7. bold
  8. acronym
  9. abbr
  10. cite
  11. code
  12. em

Why Markup when you can Markdown?

Menu

logopv.jpg

Mahud Version 5 © 2006-2008 > powered by Mr Whippy Wordpress