2022-01-02
These are just some notes that I quickly drotted down while going through the CSS for JS course
Built-in Declarations and Inheritance
- Browsers provide default styling to HTML elements. These styles are part of the
user-agent stylesheet- Chrome's built in styling can be found in their source code
- Some properties are inherented by all of the children of an element (e.g.
color) whereas other properties are not (e.g.border). See this list of inheritable properties
The Box Model
The box model refers to the layout and spacing algorithm used by browsers to determine how HTML elements are laid out in a page.
Box Sizing
In the box model there are four main attributes that determine the size/amount of space that an HTML element takes up:
- Margin
- Border
- Padding
- Content
By default (in Chrome, other browsers might behave differently) the size of a box is calculated using a
content-boxsystem.- Using the
box-sizingproperty allows us to change the calculation algorithm used:content-box(default value used by Chrome)border-box
- Using the
content-box
The content-box algorithm determines the size of a box by setting the content of the box to the dimension irrespective of any border or padding applied
Example
<style>
section {
width: 500px;
}
.box {
width: 100%;
padding: 20px;
border: 4px solid;
}
</style>
<section>
<div class="box"></div>
</section>
With the content-box calculation, the div element in the html will have a width of 548px and height of 48px as the width of the content in box is set to 100%
border-box
The border-box algorithm determines the size of the box by account for both the padding and border applied to an element, not just the content. With the example provided in context-box section, if a box-sizing: border-box was used, then the entire div would intuitively only have a width of 500px and height of 48px
Flow Layout
There are different layout algorithms ("layout modes") supported by browsers to determine how the layout of an HTML element is calculated.
Commonly used modes are:
- Flow Layout
- Positioned Layout
- Flexible Bos Layout (Flexbox)
- Grid Layout (CSS Grid)
The flow layout is the default layout mode that is typically used by browsers.
- HTML elements will have a
displayvalue ofinline|block|inline-block. Different HTML elements have different defaultdisplayvalues (i.e. by default,<div/>usedblockand<span/>usesinline) - "replaced" elements have their own quirky behaviour
- Replaced elements are elements that embed a "foreign" object like
<img/>,<video/>,<canvas/>,<iframe/>(??)- These elements are treated as
inlinestill (for some reason) so it makes sense to change them toblockwhen styling
- These elements are treated as
- Replaced elements are elements that embed a "foreign" object like
- Depending on the value of
display, the HTML elements follow different sets of rules:inlineelements typically ignore any rules that affect anything to do with vertical spacing (asinlineelements want to be in-line)blockelements try to expand and fill as much horizontal space as possibleinlineelements can line-wrapinline-blocktreats the element as aninlineelement with all of the CSS rules available to ablockelement with the exception that the element can no longer line-wrap.
Width and Height Algorithms
- Default behaviour for
widthis to take up as much space as possible (auto) - The default
heightbehaviour forblockelements is to take up the least amount of space possible - The value of the
widthandheightcan take two types- Measurements (numerical values like 100%, 100px, 1rem)
- Keywords (like auto, fit-content, min-content, max-content)
min-content: Sets thewidthto the smallest possible value that contains all of the elementsmax-content: Sets thewidthto the smallest possible value that contains all of the elements without breaking it upfit-contentSets thewidthto the size of child elements while also ensuring that it fits within the parent container
- Margin collapse is when multiple elements' margin's collapse together to form a single "margin space" (rather than keeping their own distinct margins)