Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

jQuery + Css (beginner)

Made a simple light box code: http://port-80-kp53ji7d7z.treehouse-app.com/

The problem is when I add the "P" tag. The image stops honoring the " top:30%;" found in css ID "#viewimage"..... Why is that?

#overlay {
  background: rgba(0,0,0,0.5);
  width: 100%;
  height: 100%;
  position:absolute;
  left: 0;
  top: 0;
  text-align:center;
}

#viewimage {
  position:relative;
  top:30%;
}

Here is how I add the "P":

var $alt = $('<p></p>');
$overlay.append($alt);

FULL CODE:

var $overlay = $('<div id="overlay"></div>')
var $image = $('<img id="viewimage">');
var $alt = $('<p></p>');


$overlay.append($image);
/////////////////////////////////////// HERE I ADD THE <p></p>
$overlay.append($alt);

$("body").append($overlay);
$overlay.hide();
//click
$("#imageGallery li a").click(function(event){
  event.preventDefault();
  var imageURL = $(this).attr("href");
  console.log(imageURL);

  $image.attr('src',imageURL);

  var alttext = $(this).children("img").attr("alt");
  console.log(alttext);
  $overlay.show();
});

$overlay.click(function(){
  $overlay.hide();
});
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" title="no title" charset="utf-8">
    <title>Image Gallery</title>
</head>
<body>
    <h1>Image Gallery</h1>
    <ul id="imageGallery">
        <li><a href="images/refferal_machine.png"><img src="images/refferal_machine.png" width="100" alt="Refferal Machine By Matthew Spiel"></a></li>
        <li><a href="images/space-juice.png"><img src="images/space-juice.png" width="100" alt="Space Juice by Mat Helme"></a></li>
    </ul>

    <script src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/app.js" type="text/javascript" charset="utf-8"></script>

</body>
</html>

If you wrap the image and paragraph in a container element eg. div#contianer and apply the styles you have on the #viewimage element it will work with the p tag

They are in

<div id="overlay"></div>

yeah but this will contain the image and paragraph and position them both rather than only position the image.

1 Answer

Hello, I've just checked with the link you gave. I 'm not sure of what you mean, because when looking at the DOM through the developer tools inside the browser, if I delete the paragraph tags, the position of the image doesn't change at all.

Moreover, If I insepect the id viewimage, the only rule I see applied to this is a margin-top, set to 30%. In other words, I see nohing wrong and I don't see the rules you put in your description applied.

I 've also checked the css through the browser and I don't see those css rules once again.