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

Your thoughts on equal spacing above and below container? Pic included.

Hey guys,

recently when I was designing a site I had a few problems getting spacing right above and below containers. It was especially noticeable at mobile views.

See pic below:

http://s7.postimg.org/7e1y6jikb/space.jpg

A lot of the times you may have equal padding top and bottom of your container, however you may have a h1 at the top which is extending the white space on top.

On the whole, with lots of elements in a container it can be sometimes hard to get equal spacing above and below.

How do you usually prepare/resolve this situation? Or do you not see it as an issue?

I personally like to try and have everything aligned up properly.

Cheers,

Joe

3 Answers

If you identify what spacing is causing the undesired layout, you could use specific selectors to alter the padding or margins.

For example, if your h3 has both a bottom and top margin that's resulting in too much white-space at the top of a container, you could target just the first one and remove the margin.

h3 {
  margin: 1.5rem 2.5%;
}

.container h3:first-child {
  margin: 0 2.5% 1.5rem 2.5%;
}

That's usually how I deal with similar unintended white-space in layouts, but I'm sure there are other techniques as well. Happy coding!

Edit: I've also found that a baseline css file can help when trying to confirm consistent spacing.

Hey Joe, I recommend you to use a reset style sheet, you can find it on google. Or you can also set your margin, padding and box-sizing to border box manually.

* {
   margin: 0;
   padding: 0;
   box-sizing: border-box;
}

That way the elements does not have margins set by default and you can specify them yourself. I normally do that and then add a padding to the container element, if I have an h1 and I want to have the same spacing under it, I set the margin-bottom to the same padding of the container element.

For example:

.container {
   padding: 25px;
}

h1 {
   margin-bottom: 25px;
}

Hi Jesus,

I use normalize.

Either way, I may want margins on my headlines in general. I'm more curious about mobile views where it can more obvious there is uneven spacing above and below.

I sometimes feel myself hacking around to get it right, I feel there must be a better way.

Cheers.

Maybe insead adding margin or paddings to the h1, try adding margins to the paragraph element, that way you have space at the top and the bottom