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

CSS

Stacking an img above a div

I have an img I'd like to stack on top of a div, as if it were resting right above it. I don't mean z-indexing. Just the bottom of my img touching the top of the div. Positions absolute and relative aren't working out to well. Fixed seemed decent but not quite what I was looking for. Unsure how to go about this responsively. Any ideas?

Here's the HTML:

<div class='la-tours'>
  <img src='images/hollywood.svg' alt='An 8-bit image of the Hollywood sign'>
  <div><span>book</span></div>
</div>

Here's the CSS:

.la-tours img {
  margin: 0 auto;
  width: 25%;
}
.la-tours div {
  border: 1px solid #181745;
  margin: 0 auto;
  position: absolute;
  top: 30%; right: 0;
  left: 0;
  padding: 1em 2em;
  width: 60%;
}

2 Answers

All you really need to do is add display:block to your image class :)

Working example here: http://codepen.io/lilianab/pen/QExdWr

If you want the bottom of the image to touch the top of the div below, just remove the absolute positioning: http://codepen.io/lilianab/pen/QExdwr

Hope that helps!

Two things, your class name is tours but you are targeting .la-tours.

Also, if all you want is one on top of the other, the default display: block should work.

If you want to use absolute positioning, you would need to do something like this...

.tours {
  position: relative;
  width: 500px;
  height: 500px;
}

.tours img {
  position: absolute;
  width: 200px;
  height: 200px;
  top: 0;
  left: 150px;
}

.tours div {
  position: absolute;
  width: 400px;
  top: 210px;
  left: 50px;
}

Not sure why i'm being down graded when giving the actual correct answer. I mentioned that display block would fix it and showed an actual example of how you would use Relative and absolute positioning which is also correct.

My mentioning of using the wrong selector was taken into account and the original question was modified with the now correct selector being used.