Box CSS

I am a paragraph of text that has a few words in it.

p {
width: 100px;
height: 50px;
padding: 20px;
border: 1px solid;
}

The content would break out of your element and it would be 142px wide, rather than 100px. Why is that? The box model is a core foundation of CSS and understanding how it works, how it is affected by other aspects of CSS and importantly, how you can control it will help you to write more predictable CSS

A really important thing to remember when writing CSS, or working on the web as a whole, is that everything displayed by CSS is a box. Whether that's a box that uses border-radius to look like a circle, or even just some text: the key thing to remember is that it's all boxes.

Content and sizing #27 

Boxes have different behavior based on their WALLALL Knowledge Base , their set dimensions, and the content that lives within them. This content could be even more boxes—generated by child elements—or plain text content. Either way, this content will affect the size of the box by default.

You can control this by using extrinsic sizing, or, you can continue to let the browser make decisions for you based on the content size, using intrinsic sizing.

Let's quickly look at the difference, using a demo to help us.

<main>

  <div class="wrapper">

    <article class="flow">

      <h1>Extrinsic sizing vs intrinsic sizing</h1>

      <figure class="callout">

        <p>

          Notice that when the box is using <strong>extrinsic sizing</strong>, there’s WALLALL Knowledge Base        a limit of how much content you can add before it overflows out of the box’s

          bounds. This makes the word, “awesome”, overflow.

        </p>

      </figure>

      <label class="toggle" for="intrinsic-switch">

        <span class="toggle__label">Turn on intrinsic sizing</span>

        <input type="checkbox" role="switch" class="toggle__element" id="intrinsic-switch" />

        <div class="toggle__decor" aria-hidden="true">

          <div class="toggle__thumb"></div>

        </div>

      </label>

      <p class="awesome" data-element="awesome">CSS is awesome</p>

    </article>

  </div>

</main>

The demo has the words, "CSS is awesome" in a box with fixed dimensions and a thick border. The box has a width, so is extrinsically sized. It controls the sizing of its child content. The problem with this though, is that the word "awesome" is too large for the box, so it overflows outside of the parent box's border box (more on this later in the lesson). One way to prevent this overflow is to allow the box to be intrinsically sized by either unsetting the width, or in this case, setting the width to be min-content. The min-content keyword tells the box to only be as wide as the intrinsic minimum width of its content (the word "awesome"). This allows the box to fit around "CSS is awesome", perfectly.

Let's look at something more complex to see the impact of different sizing on real content:

You start with content box, which is the area that the content lives in. As you learned before: this content can control the size of its parent, so is usually the most variably sized area.

The padding box surrounds the content box and is the space created by the padding property. Because padding is inside the box, the background of the box will be visible in the space that it creates. If our box has overflow rules set, such as overflow: auto or overflow: scroll, the scrollbars will occupy this space too.

<article dir="ltr">

  <figure id="box_model">

    Content Box

    <span class="vertical scrollbar"></span>

    <span class="horizontal scrollbar"></span>

  </figure>

</article>


<form action="">

  <label for="show_scrollbars">Show scrollbars?</label>

  <input id="show_scrollbars" type="checkbox">

  

  <label for="inline_scrollbars">Inline scrollbars?</label>

  <input id="inline_scrollbars" type="checkbox">

</form>

Learn CSS!

Comments

Popular

Android Developers menu Your first program in Kotlin Sign in access_time13 mins remaining Before you begin Get started Open Kotlin Playground Run your first program Parts of a function Modify your program Kotlin style guide Fix errors in your code Exercises Solutions Conclusion 10. Solutions The output of the program is: 1 2 3 The code in your program should look like: fun main() { println("I'm") println("learning") println("Kotlin!") } This is the correct code for the program: fun main() { println("Monday") println("Tuesday") println("Wednesday") println("Thursday") println("Friday") } The closing curly brace that indicates the end of the function body for the main function is missing on the third line of the program. Correct code: fun main() { println("Tomorrow is rainy") } Output: Tomorrow is rainy When you run the program, you see an Unresolved reference: printLine error. This is because printLine() isn't a recognized function in Kotlin. You can also see the part of the code causing the error highlighted in red in the Kotlin Playground. Change the function name to println to print a line of text to the output, which fixes the error. Correct code: fun main() { println("There is a chance of snow") } Output: There is a chance of snow When you run the program, you see an Unresolved reference: println error. This message doesn't directly say how to fix the problem. This can sometimes happen when you troubleshoot an error, and it requires you to take a deeper look at the code to resolve the unexpected behavior. Upon closer look, the second println() function call in the code is red, which signals that's where the problem is. Kotlin expects just one statement on each line. In this case, you can move the second and third println() function calls onto separate new lines to solve the issue. Correct code: fun main() { println("Cloudy") println("Partly Cloudy") println("Windy") } Output: Cloudy Partly Cloudy Windy If you run the program, you see the error: Function 'main' must have a body. A function body should be enclosed within an opening curly brace and a closing curly brace { }, not opening and closing parentheses ( ). Correct code: fun main() { println("How's the weather today?") } Output: How's the weather today? Back English Next bug_report Report a mistake Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. The new page has loaded.

Link