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

Paul Murphy
Paul Murphy
6,162 Points

h2 overspill & displaying sections in center of a container

http://codepen.io/paul-masteel/pen/iEGJD

Please see link.

Firstly, where it says "Clad Material in the h2 I want the background color of the h2 to only fill the width of the words.

Secondly, I want to able to to display the content in the center of the page.

Any suggestions on how this can be achieved?

5 Answers

Tom Bedford
Tom Bedford
15,645 Points

Hi Paul

The background on the headers is spanning the whole width as they are display: block; by default. You could use display: inline-block; to keep the width the same as the text.

To center the content you are doing the right thing with margin: 0 auto; however you also need to give your container a width e.g. width:90%;

See this pen for the new styles applied.

You also had an extraneous } at the start of your CSS which I commented out.

Aaron Graham
Aaron Graham
18,033 Points

Just to get some clarification:

On your first problem, do you mean the green color surrounding your h1 text, or are you planning to set a background color for the h2 as well? In either case, something like this:

.container-products section h2 {
  display: inline;
}

.container-products section h2:before {
  content: "";
  display: block;
  cear: both;
}

might get you in the ballpark. Just note that if it is the H1 you want, you will obviously need to change the h2 selectors to h1. Also, the "dispaly; inline;" can be added to your current ".container-products section h2" statement; no need to make another, I just posted it this way for clarity.

On your second problem, have a look at this. I'm just guessing, but I believe you are looking for functionality similar to that. Hope that helps.

Edit: use Tom Bedford's answer. It's cleaner.

Paul Murphy
Paul Murphy
6,162 Points

Tom Bedford Aaron Graham Just one further question. If you look at the code again the <li> where it says "Inc Carbon, Stainless, Nickel Alloys & Copper Alloys" how do I target that specific list item and reduce the width so that it looks better visually.

Aaron Graham
Aaron Graham
18,033 Points

You certainly have some options for selecting that one list item, but have you considered just setting a width for your .container-products sections? It would probably keep your layout more uniform.

Paul Murphy
Paul Murphy
6,162 Points

Have taken your advice. Much appreciated!