The box model Flashcards
What is the Box model?
According to MDN. When laying out a document, the browser’s rendering engine represents each element as a rectangular box according to the standard CSS basic box model. CSS determines the size, position, and properties (color, background, border size, etc.) of these boxes.
Every box is composed of four parts (or areas), defined by their respective edges: the content edge, padding edge, border edge, and margin edge.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What is the Content area?
The content area, bounded by the content edge, contains the “real” content of the element, such as text, an image, or a video player. Its dimensions are the content width (or content-box width) and the content height (or content-box height). It often has a background color or background image.
If the box-sizing property is set to content-box
(default) and if the element is a block element, the content area’s size can be explicitly defined with the width, min-width, max-width, height, min-height, and max-height properties.
Source MDN
What is the Padding area?
The padding area, bounded by the padding edge, extends the content area to include the element’s padding. Its dimensions are the padding-box width and the padding-box height.
The thickness of the padding is determined by the padding-top, padding-right, padding-bottom, padding-left, and shorthand padding properties.
Source MDN
What is the Border area?
The border area, bounded by the border edge, extends the padding area to include the element’s borders. Its dimensions are the border-box width and the border-box height.
The thickness of the borders are determined by the border-width and shorthand border properties. If the box-sizing property is set to border-box
, the border area’s size can be explicitly defined with the width, min-width, max-width, height, min-height, and max-height properties. When there is a background (background-color or background-image) set on a box, it extends to the outer edge of the border (i.e. extends underneath the border in z-ordering). This default behaviour can be altered with the background-clip CSS property.
Source MDN
What is the Margin area?
The margin area, bounded by the margin edge, extends the border area to include an empty area used to separate the element from its neighbors. Its dimensions are the margin-box width and the margin-box height.
The size of the margin area is determined by the margin-top, margin-right, margin-bottom, margin-left, and shorthand margin properties. When margin collapsing occurs, the margin area is not clearly defined since margins are shared between boxes.
Source MDN
What does “box-sizing: content-box;” do?
content-box gives you the default CSS box-sizing behaviour. If you set an element’s width
to 100px
, then the element’s content box will be 100px
wide, and the width
of any border or padding
will be added to the final rendered width
, making the element wider than 100px
.
Source MDN
What does box-sizing: border-box;
do?
border-box
tells the browser to account for any border and padding in the values you specify for an element’s width
and height
. If you set an element’s width to 100px
, that 100 pixels will include any border or padding you added, and the content box will shrink to absorb that extra width. This typically makes it much easier to size elements. box-sizing: border-box
is the default styling that browsers use for the <table>
, <select>
, and <button>
elements, and for <input>
elements whose type is radio, checkbox, reset, button, submit, color, or search.
Source MDN
What is the problem with magic numbers?
For programming in general, magic numbers aren’t desirable. It’s often hard to explain why a magic number works. If you don’t understand where the number comes from, you won’t understand how it will behave under different circumstances.
In other words avoid playing with properties values until the work as you need. Chances are that the values won’t be valid if the screen size changes or if the page loads on a device with a different aspect ratio that the one you are working on.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What is the problem with the following css ruleset?
*, \::before, \::after { box-sizing: border-box; }
If you add third-party components with their own CSS to your page, you may see some broken layouts for those components, especially if their CSS wasn’t written with box-sizing: border-box;
. Correcting this can be problematic.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
How can you apply box-sizing: border-box
to all your site but mitigate issues with added third party components?
\:root { box-sizing: border-box; } *, ::before, ::after { box-sizing: inherit; }
Box sizing isn’t normally an inherited property, but by using the inherit keyword, you can force it to be. With the version shown here, you can convert a third-party component into a content-box when necessary by targeting its top-level container:
.third-party-component { box-sizing: content-box; }
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
Is it recommended to set the element’s height?
No.
Working with element height is different than working with element width. Typically it’s best to avoid setting explicit heights on elements. Normal document flow is designed to work with a constrained width and an unlimited height. Contents fill the width of the viewport and then line wrap as necessary. Because of this, the height of a container is organically determined by its contents, not by the container itself.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What is the normal document flow?
The normal document flow refers to the default layout behaviour of elements on the page. Inline elements flow along with the text of the page, from left to right, line wrapping when they reach the edge of their container. Block-level elements fall on individual lines, with a line break above and below.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.
What is a block element?
A Block-level element occupies the entire horizontal space of its parent element (container), and vertical space equal to the height of its contents, thereby creating a “block”.
Source MDN
What is an inline element?
An Inline-level element only occupy the space bounded by the tags defining the element, and vertical space equal to the height of its contents, instead of breaking the flow of the content as block elements do.
Source MDN
What are the differences between block-level elements and inline-level elements?
There are a couple of key differences between block-level elements and inline elements:
Content model
Generally, block-level elements may contain inline elements and (sometimes) other block-level elements. Inherent in this structural distinction is the idea that block elements create “larger” structures than inline elements.
Default formatting
By default, block-level elements begin on new lines, but inline elements can start anywhere in a line.
source MDN
Examples of HTML block-level elements (no HTML5 semantic block elements)
-
<h1>-\<h6>
: This element is used for including headings of different sizes ranging from 1 to 6. -
<div>
: This is a container tag and is used to make separate divisions of content on the web page. -
<hr>
: This is an empty tag and is used for separating content by horizontal lines. -
<li>
: This tag is used for including list items of an ordered or unordered list. -
<ul>
: This tag is used to make an unordered list. -
<ol>
: This tag is used to make an ordered list. -
<p>
: This tag is used to include paragraphs of content in the webpage. -
<table>
: This tag is used for including the tables in the webpage when there is a need for tabular data.
Source geeksforgeeks
Examples of HTML5 semantic block level elements
-
<header>
: This tag is used for including all the main things of the webpage like navbar, logos, and heading of the webpage. -
<nav>
: This tag helps to navigate through different sections by including different blocks of hyperlinks in the webpage. -
<footer>
: This contains all information about the authorization, contact, and copyright details of the webpage. -
<main>
: The main content of the webpage resides in this tag. -
<section>
: This is used separate different sections in the webpage. -
<article>
: This tag is used to include different independent articles on the webpage. -
<aside>
: This tag is used to mention details of the main content aside.
source geeksforgeeks
Examples of Inline-level elements
-
<a>
: This tag is used for including hyperlinks in the webpage. -
<br>
: This tag is used for mentioning line breaks in the webpage where ever needed. -
<script>
: This tag is used for including external and internal JavaScript codes. -
<input>
: This tag is used for taking input from the users and is mainly used in forms. -
<img>
: This tag is used for including different images in the webpage to add beauty to the webpage. -
<span>
: This is an inline container that takes necessary space only. -
<b>
: This tag is used in places where bold text is needed. -
<label>
: The tag in HTML is used to provide a usability improvement for mouse users i.e, if a user clicks on the text within the < label > element, it toggles the control.
source geeksforgeeks
What is the problem with setting an element’s height?
When you explicitly set an element’s height, you run the risk of its contents overflowing the container. This happens when the content doesn’t fit the specified constraint and renders outside of the parent element.
Source: Keith J. Grant (2018). CSS in Depth. Manning Publications.