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
jdh
12,334 PointsCentering or aligning divs
I have a container div that has another div with three other divs. This structure is supposed to hold three images all on the same vertical alignment. How can I get these three images so that they are spaced evenly and not have the alignment mess up when changing viewport sizes?
I originally i had the css of the right image to float: right and left image to float: left but the center image gets moved around when changing the window size.
Thanks for the help
3 Answers
Brian Studwell
9,964 PointsHey Jake,
If you float those left and right images, but not the center image, the center image will still be in normal document flow, meaning it will get pushed around independently from the other two. Try floating all three divs.
When you say they're on the same vertical alignment, does that mean they're all stacked in a column? Or that they're all on the same baseline, in a row? If they're meant to appear in a row float all three divs will bump them up next to each other nicely.
Also, how are you defining the dimensions of the three divs? If you want them to change size in different viewports while holding their layout you'll need to give them responsive units of measurement (%, ems or vh/vw).
Finally, using a flexbox might be a nice option for you, particularly if your'e trying to stack the divs in a column. Check out the flexy goodness.
Brian
Arthur Verschaeve
20,816 PointsFor something really simple as centering a div, you don't need flexbox i think. You can do it with a margin 'trick'. Check out this very quick demo:
http://codepen.io/arthur_versch/pen/eFjvH
This way, your div can have a percentage width too.
If you set the display value to 'table-cell', you can use the vertical-align property too.
jdh
12,334 Pointsthis is weird because i tried the margin "trick" and it didnt work.
Adam Bennett
7,663 PointsIt sounds like you are trying to get all of the images to line up from left to right on the same line. So I was wondering, why don't you get rid of the 3 containing divs and just have the 3 images within the secondary div? The reason that you have to use float is because a div tag is a block level element so it takes up a whole line by itself, whereas an IMG tag is an inline-block element and they will line up on the same line. Then you can just do text-align: center; on the containing div and they will all move to the center. Unless I'm reading your post wrong.
Adam Bennett
7,663 PointsOh sorry, and make sure that your containing div has a width of 100% so it stretches across the screen, or your the div will only be the size of the images.
jdh
12,334 Pointsjdh
12,334 PointsThanks, Brian.
Great suggestion. I'm just fooling around w a flexbox layout right now but will probably end up using this.