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

John Coolidge
John Coolidge
12,614 Points

<img> and <p> in a <section>

I have a multiple section elements in my html that include both an img element and a p element as shown below:

<section class="mask">
  <img src="mask.jpg" alt="Paintball Mask" class="img-mask">
  <p>text here</p>
</section>

When I style the text in CSS and add a background color or a margin to the class "img-mask", the background color and margin only affect the text of the section element and not the img element.

The CSS is shown below:

.mask {
  margin: 10px;
  background-color: coral;
}

I want a background that encompasses both image and text and then I want that section to be positioned relative to other elements on the page. I'm not sure where I'm going wrong here. Any help would be great! Thanks. :)

Here is a photo of the result. With Dev Tools open in Chrome you can see that the margin is applied to the text (the orange area) even though it is applied to the section containing the image and text in the HTML.

photo of margin on text, not section

1 Answer

Strange..I just tried it in my workspace and it worked correctly for me. Can you post a picture of the result you are getting?

John Coolidge
John Coolidge
12,614 Points

I have edited the OP to include a photo link to imgur.

Oh I see. It seems like you are floating the elements inside the section. If this is the case, you will need to add a div that clears just before the closing of the section tag. Like this;

<section class="mask">

<img src="mask.jpg" alt="Paintball Mask" class="img-mask">

<p>text here</p>

<div class="clear"></div>

</section>
.clear {
clear: both;
}
John Coolidge
John Coolidge
12,614 Points

Thanks! That did the trick. :)

Glad to help.

Jorge