1

Here's my code. I'm trying to add an image called lg.png into the HTML and be able to edit the length/width in the css file. The lg.png is located in the same folder as the index.html and styles.css

Tried looking online for this answer but can't seem to get any luck.

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<head>
    <title>yournetid</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css"/>   
</head>
<img id="my_image" src="lg.png" alt="image error"/>

        &nbsp;
<body>
    <p>
        Here's some awesome pictures!
    </p>
</body>

CSS:

body {

}

img#lg background:url(lg.png); width:200px; height:100px;

3
  • background image of image? take a div instead of the image and do that. Commented Jan 28, 2014 at 17:29
  • Is it a valid vanilla CSS? You can't nest styles (img#lg is inside body) BTW img#lg won't match your image (because it has not lg id). Commented Jan 28, 2014 at 17:29
  • You need to fix the opening body tag and I see no link in the HTML file to the CSS file. Commented Jan 28, 2014 at 17:31

2 Answers 2

4

Use a div and set the background property:

HTML:

<div class="my_image"></div>

CSS:

.my_image
{
    background:URL('path/to/img.png');
    width:100px;
    height:100px;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Made some changes to what I'm currently working with, still stumped on where to go in the right direction since the image still isn't showing up
3

You are trying to use CSS selector img#lg which makes no sense. You are telling CSS to look for an image with id of 'lg' but you did not set any id to your image.

Also, setting the background-image:ur(lg.png) is not the same as <img src='lg.png'>.

To fix it:

  1. Add id to your image
  2. Target the id in your CSS.

Change your HTML:

 <img id="my_image" src="lg.png" alt="image error">

CSS:

#my_image {width:200px; height:100px; }

If you wanted to change CSS properties of ALL images, you'd use the following:

img {width:200px; height:100px; }

Hope this helps!

7 Comments

Thank you for your help, I'm still getting a couple errors, as it's not displaying the image. Error: "In XHTML 1.0 Strict the tag <html> cannot contain tag <img>
@user3245569 where is the image located? is it in the same folder of the HTML or a subfolder?
It's in the same folder as the html file.
I also updated the original post with what I'm currently working with.
@user3245569 why are you using strict xhtml? and not simply <!DOCTYPE html>?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.