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

Multiple DIVs within DIV formatting

Hi,

I am trying to format DIVs a certain way. If anyone could help that would be great.

main ->center horizontally and vertically based on its parent

topic -> have them be listed horizontally (and centered!) within main

subject -> have them listed vertically within topic

basically:

<DIV id="parent">
<DIV id="main">
    <DIV class="topic">
        <DIV class="subject">
        </DIV>
        <DIV class="subject">
        </DIV>
        <DIV class="subject">
        </DIV>
    </DIV>
    <DIV class="topic">
        <DIV class="subject">
        </DIV>
        <DIV class="subject">
        </DIV>
        <DIV class="subject">
        </DIV>
    </DIV>
    <DIV class="topic">
        <DIV class="subject">
        </DIV>
        <DIV class="subject">
        </DIV>
        <DIV class="subject">
        </DIV>
    </DIV>
</DIV>
</DIV>

3 Answers

Took me a while but I came up with a solution.

Here it is in case someone else will have the same question.

http://jsfiddle.net/9DBCv/51/

#main
{
    width: 100%;
    height: 100%;
    background: pink;
    padding: 10px 30px;
    display: flex;
    justify-content: center; /* align horizontal */

}

.topic
{
    width: 12em;
    height: 100%;
    background: green;
    display:inline-block;
    margin: 0 5px;
    position: relative;
}

.subject, .subjectc{
  height: 3em;
  min-height:100px;
  width:100%;
  background-color:blue;
  vertical-align: middle;
}

.subject + .subjectc {
    margin-top:1em;
}

.subjectc + .subject {
    margin-top:1em;
}

.subject:first-child{
    margin-top:20px;
}

I've also added a margin-top element for the first child in case there is a menu bar (Just delete this part if you don't need it):

.subject:first-child{
    margin-top:20px;
}

This is how far I got:

http://jsfiddle.net/9DBCv/42/

I need the blue boxes to be in the horizontal middle of the green..

Cheers.

To position the blue boxes in the horiz middle of the green: remove height:1000px of main selector. change subject margin-top to margin: 20px 0;