Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Simple Floating Examples</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="style.css">
        <link href="https://fonts.googleapis.com/css?family=Righteous" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css?family=Roboto+Condensed" rel="stylesheet">
    </head>
    <body>
        <main id="float-examples">
            <h1>Simple Floating Examples</h1>
            <p>For additional examples, <a href="https://codepen.io/codifiedconcepts/pen/MVxgvw" target="_blank">check out my other pen.</a></p>
            <p class="sectionbreak"><em>The first three look the same, but see how different approaches are used to achieve the same result. See the comments in the HTML & CSS.</em></p>

            <ul>
                <li><em>Where does the margin go if you remove overflow from article?</em></li>
                <li><em>What happens when you remove margin: 0 from p? Does the browser apply any default styles, like margin?</em></li>
                <li><em>I'm applying the padding to the element inside the div, trying applying it to the div (just .short) instead. What happens?</em></li>
            </ul>

            <article id="floatsample-1">    <!-- I'm nesting boxes together here -->
                <aside class="column">
                    <div class="short warmcolor-1 boxnumber-inline">
                        <p class="boxnumber">1</p>
                        <p>We (1 and 3) are nested together.</p>
                    </div>
                    <div class="short coolcolor-2">
                        <p><span class="boxnumber">3</span><br>We (1 and 3) are nested together.</p>
                    </div>
                </aside>

                <aside class="column">
                    <div class="short coolcolor-1">
                        <p><span class="boxnumber">2</span><br>We (2 and 4) are nested together.</p>
                    </div>
                    <div class="short warmcolor-2 boxnumber-inline">
                        <p class="boxnumber">4</p>
                        <p>We (2 and 4) are nested together.</p>
                    </div>
                </aside>
            </article>

            <ul>
                <li><em>What happens if you remove the width on the article (#floatsample-2)?</em></li>
                <li><em>What happens if you have #floatsample-2 div float right?</em></li>

            </ul>
            <article id="floatsample-2">    <!-- this time I'm not nesting them together, but I've given the overall parent element a fixed width -->
                <div class="column short warmcolor-1 boxnumber-inline">
                    <p class="boxnumber">1</p>
                    <p>We are feeling contrained.</p>
                </div>
                <div class="column short coolcolor-1">
                    <p><span class="boxnumber">2</span><br>We are feeling constrained.</p>
                </div>
                <div class="column short coolcolor-2">
                    <p><span class="boxnumber">3</span><br>We are feeling constrained.</p>
                </div>
                <div class="column short warmcolor-2 boxnumber-inline">
                    <p class="boxnumber">4</p>
                    <p>We are feeling constrained.</p>
                </div>
            </article>

            <ul>
                <li><em>What happens if you remove the clear on #block3?</em></li>
            </ul>
            <article id="floatsample-3">    <!-- this time I'm not nesting or giving the container div a fixed width, I'm using clear -->
                <div class="column short warmcolor-1 boxnumber-inline">
                    <p class="boxnumber">1</p>
                    <p>Box 3 is staying clear.</p>
                </div>
                <div class="column short coolcolor-1">
                    <p><span class="boxnumber">2</span><br>Box 3 is staying clear.</p>
                </div>
                <div class="column short coolcolor-2" id="block3">
                    <p><span class="boxnumber">3</span><br>Box 3 is staying clear.</p>
                </div>
                <div class="column short warmcolor-2 boxnumber-inline">
                    <p class="boxnumber">4</p>
                    <p>Box 3 is staying clear.</p>
                </div>
            </article>

            <p class="sectionbreak"><em>But what about when I have a tall box next to a smaller box? See the comments in the HTML & CSS.</em></p>
            <ul>
                <li><em>Try applying clear to #floatsample-4 #block4. What happens?</em></li>
                <li><em>What happens if you float #floatsample-4 div instead of the asides (#floatsample-4 .column)?</em></li>
                <li><em>What happens if you apply clear to #floatsample-4 #block2?</em></li>
            </ul>
            <article id="floatsample-4">
                <aside class="column">
                    <div class="short warmcolor-1">
                        <p><span class="boxnumber">1</span><br>How do I stack up?</p>
                    </div>
                    <div class="short coolcolor-1" id="block2">
                        <p><span class="boxnumber">2</span><br>How do I stack up?</p>
                    </div>
                </aside>
                <aside class="column">
                    <div class="tall coolcolor-2">
                        <p><span class="boxnumber">3</span><br>How do I stack up?</p>
                    </div>
                </aside>
                <div class="warmcolor-2" id="block4">
                    <p><span class="boxnumber">4</span><br>I'm not floating, so what am I up to?</p>
                </div>
            </article>
        </main>
    </body>
</html>
              
            
!

CSS

              
                /*------------------  General Styles  ------------------*/
body {
    margin: 2.5%;
    background-color: #E4DFDA;
    font-family: 'Roboto Condensed', Arial, sans-serif;
    font-size: 16px;
}

main {
    text-align: center;
}

article {
    overflow: auto;  /* notice where does the margin go if you remove this; with floating elements inside, this collapses */
    clear: both;
    margin: 48px 0;
    text-align: center;
}

h1 {
    font-family: 'Righteous', cursive;
    font-size: 32px;
}

ul {
    list-style-position: inside;
}
ul li {
    padding-bottom: 10px;
}

p {
    margin: 0;  /* the browser likes to apply styles to some elements, like margin to p tags; we'll show you how to find these soon, but for now I'm overriding any browser styles by using margin 0 */
    padding: 18px 0;
}

.sectionbreak {
    margin-top: 80px;
    padding-top: 80px;
    border-top: 10px dotted #aaaaaa;
}

/*------------------ My Styles  ------------------*/
.tall {
    width: 250px;
    height: 500px;
}

.short {
    width: 250px;
    height: 250px;
}

.short p {
    padding: 60px 25px;  /* notice I am applying the padding to the element inside the div, trying applying it to the div (just .short) instead and see what happens */
}
.tall p {
    padding: 200px 25px;
}

.boxnumber {
    font-family: 'Righteous', cursive;
    font-size: 48px;
}

.boxnumber-inline p {
    padding: 0;  /* if I don't want to use the break tag after my box number (like with the 1's and 4's), I need to adjust the padding by overriding padding coming from ".short p" above and applying it somewhere else */
}

.boxnumber-inline .boxnumber {
    padding: 60px 0 0;
}

/*------------------  Floating  ------------------*/
#floatsample-1 .column {
    float: left;
}

/*TRY APPLYING FLOAT RIGHT TO SEE WHAT HAPPENS*/
#floatsample-2 div,
#floatsample-3 div{
    float: left;
}

/*TRY COMMENTING OUT THIS WIDTH TO SEE WHAT HAPPENS*/
#floatsample-2 {
    width: 500px;
}

/*TRY COMMENTING OUT THIS CLEAR TO SEE WHAT HAPPENS*/
#floatsample-3 #block3 {
    clear: left;
}

/*TRY APPLYING CLEAR (CURRENTLY COMMENTED OUT) TO SEE WHAT HAPPENS*/
#floatsample-4 #block4 {
    /*clear: left;*/
}

/*TRY FLOATING #floatsample-4 div INSTEAD OF #floatsample-4 .column TO SEE WHAT HAPPENS*/
#floatsample-4 .column {
    float: left;
}

/*TRY APPLYING CLEAR (CURRENTLY COMMENTED OUT) TO SEE WHAT HAPPENS*/
#floatsample-4 #block2 {
    /*clear: left;*/
}

/*------------------  Backgrounds and Breaks  ------------------*/
.warmcolor-1 {
    background: deeppink;
}
.warmcolor-2 {
    background: orange;
}
.coolcolor-1 {
    background: green;
}
.coolcolor-2 {
    background: blue;
}

#floatsample-4 .warmcolor-1 {
    background: orangered;
}
#floatsample-4 .warmcolor-2 {
    background: lightpink;
}
#floatsample-4 .coolcolor-1 {
    background: mediumpurple;
}
#floatsample-4 .coolcolor-2 {
    background: skyblue;
}
              
            
!

JS

              
                
              
            
!
999px

Console