Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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