<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Paul Thompson</title>
    <description>The latest articles on DEV Community by Paul Thompson (@paulnthompson84).</description>
    <link>https://dev.to/paulnthompson84</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F88956%2F9e65cc79-a82b-4f59-8d4b-f58779ce3939.jpeg</url>
      <title>DEV Community: Paul Thompson</title>
      <link>https://dev.to/paulnthompson84</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/paulnthompson84"/>
    <language>en</language>
    <item>
      <title>Default flexbox - what does display flex do?</title>
      <dc:creator>Paul Thompson</dc:creator>
      <pubDate>Sat, 03 Sep 2022 20:16:07 +0000</pubDate>
      <link>https://dev.to/paulnthompson84/default-flexbox-what-does-display-flex-do-4nia</link>
      <guid>https://dev.to/paulnthompson84/default-flexbox-what-does-display-flex-do-4nia</guid>
      <description>&lt;p&gt;&lt;em&gt;An exploration into the initial/default values of flexbox and their effects.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  What is &lt;code&gt;display: flex&lt;/code&gt;?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.container&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;display: flex&lt;/code&gt; is a &lt;a href="https://www.w3.org/TR/css-display-3/" rel="noopener noreferrer"&gt;CSS display&lt;/a&gt; declaration, which enables &lt;strong&gt;flexbox&lt;/strong&gt; (the &lt;a href="https://www.w3.org/TR/css-flexbox-1/" rel="noopener noreferrer"&gt;Flexible Box Layout Module&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;With flexbox enabled:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The selected element becomes a &lt;a href="https://www.w3.org/TR/css-flexbox-1/#flex-container" rel="noopener noreferrer"&gt;flex container&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Its child contents become &lt;a href="https://www.w3.org/TR/css-flexbox-1/#flex-items" rel="noopener noreferrer"&gt;flex items&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;It will &lt;strong&gt;algorithmically layout its contents&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To the uninitiated, this can be a little surprising and confusing at first.  So let's get a better understanding of flexbox and its default settings.&lt;/p&gt;



&lt;h2&gt;
  
  
  Flexbox overview
&lt;/h2&gt;

&lt;p&gt;Flexbox is a layout system, which assumes the responsibility of arranging, aligning and flexibly sizing flex items within its flex containers. &lt;/p&gt;

&lt;p&gt;It's a line-based system, meaning it will arrange and align its contents along a single-line, which can be wrapped.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fktrda396ln2zw2pah7us.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fktrda396ln2zw2pah7us.jpg" alt="Illustration of a single direction wrapping line - with boxes arranged on the line"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This single breakable line can appear horizontally like in rows, or vertically like in columns, but it cannot be both at the same time. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flexbox is a one dimensional layout system&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Flexbox is also writing mode aware. &lt;/p&gt;

&lt;p&gt;This means it will automatically arrange its flex items in the same orientations and directions as text flows in the document.&lt;/p&gt;



&lt;h2&gt;
  
  
  Writing mode
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://www.w3.org/TR/css-writing-modes-3/" rel="noopener noreferrer"&gt;writing mode&lt;/a&gt; details how text/content flows within its documents or components. Its purpose, is to internationalize writing on the web.&lt;/p&gt;

&lt;p&gt;It determines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;inline direction:&lt;/strong&gt;&lt;br&gt;
The direction a line of text flows in&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;block flow direction:&lt;/strong&gt;&lt;br&gt;
The direction new lines of text are added&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;inline base direction:&lt;/strong&gt;&lt;br&gt;
The primary direction content (text and other elements) flows within the line&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt; &lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;em&gt;Browser default writing mode&lt;/em&gt; - &lt;code&gt;horizontal-tb&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;By default, the browser will arrange lines of text in &lt;strong&gt;&lt;code&gt;horizontal&lt;/code&gt;&lt;/strong&gt; rows.&lt;br&gt;
New lines of text are added vertically,  in a top-to-bottom direction (&lt;strong&gt;&lt;code&gt;tb&lt;/code&gt;&lt;/strong&gt;) . &lt;/p&gt;

&lt;p&gt;Within each line, text flows from left-to-right (&lt;strong&gt;&lt;code&gt;ltr&lt;/code&gt;&lt;/strong&gt;) &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftlowco17h7r9zs1v9an3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftlowco17h7r9zs1v9an3.jpg" alt="Illustration of writing mode: horizontal-tb - text runs in horizontal lines from left to right. New lines of text are added vertically, in a top-to-bottom direction"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;h2&gt;
  
  
  Layout axes and default directions
&lt;/h2&gt;

&lt;p&gt;Layout in flexbox consists of two axes, which determine how content is arranged and aligned.&lt;/p&gt;

&lt;p&gt;By default, the document's writing mode will determine how these axes are organised:&lt;br&gt;
 &lt;/p&gt;
&lt;h3&gt;
  
  
  Main axis
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The main axis is the &lt;strong&gt;primary axis of flexbox&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Flex items are arranged in a single direction along this axis&lt;/li&gt;
&lt;li&gt;Its default orientation will follow the &lt;strong&gt;inline direction&lt;/strong&gt; of the document's writing‑mode  (&lt;strong&gt;&lt;code&gt;horizontal&lt;/code&gt;&lt;/strong&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Cross axis
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The cross axis runs perpendicular (&lt;em&gt;or 90 degrees&lt;/em&gt;) to the main axis (it "crosses" the main axis)&lt;/li&gt;
&lt;li&gt;This axis is used to align content in relation to the main axis&lt;/li&gt;
&lt;li&gt;Its default orientation and direction will follow the &lt;strong&gt;block flow direction&lt;/strong&gt; of the document's writing‑mode (&lt;strong&gt;&lt;code&gt;tb&lt;/code&gt;&lt;/strong&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Flex items will flow in the same directions as the document's flow of text (left-to-right, top-to-bottom).&lt;/p&gt;
&lt;h3&gt;
  
  
  Default axes and writing mode
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fze0rg22qtimuhxto9ydr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fze0rg22qtimuhxto9ydr.jpg" alt="Illustration of default flexbox, writing mode and inline and block axes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;br&gt;
The orientation and direction of flexbox axes can be overridden. The main axis can appear horizontal or vertical, regardless of the inline axis direction.&lt;/p&gt;



&lt;h2&gt;
  
  
  A deep dive into &lt;code&gt;display: flex&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;When we create a flex container,  we set two display types or contexts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;an &lt;strong&gt;Outer display type&lt;/strong&gt; &lt;br&gt;
&lt;em&gt;for the flex container&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;an &lt;strong&gt;Inner display type&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;for its flex-items&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


 
&lt;h3&gt;
  
  
  Outer display type
&lt;/h3&gt;

&lt;p&gt;When declaring &lt;strong&gt;&lt;code&gt;flex&lt;/code&gt;&lt;/strong&gt;,  the outer display type of the flex container is set to  &lt;strong&gt;&lt;code&gt;block&lt;/code&gt;&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;For layout, this means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the selected element's width expands to fit its parent container or to its own set width&lt;/li&gt;
&lt;li&gt;its vertical height is equal to that of its contents&lt;/li&gt;
&lt;li&gt; &lt;code&gt;overflow&lt;/code&gt; applies to the container&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt; &lt;br&gt;
&lt;strong&gt;Note:&lt;/strong&gt; &lt;br&gt;
Setting &lt;code&gt;display: inline-flex&lt;/code&gt; will create a flex container, which acts like an inline-element in normal document flow &lt;br&gt;
&lt;em&gt;e.g. a flex container inline with text&lt;/em&gt;&lt;/p&gt;



&lt;h3&gt;
  
  
  Inner display type
&lt;/h3&gt;

&lt;p&gt;Within the flex container, its child contents become 'flex items'.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuyoot1m7pefl6fy3gtzb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuyoot1m7pefl6fy3gtzb.jpg" alt="Illustration of HTML, depicting a flex container (ul element) and its contents (li elements). Its direct contents are flex items"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Child contents such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;HTML elements&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;e.g. &lt;code&gt;div&lt;/code&gt;, &lt;code&gt;li&lt;/code&gt;, &lt;code&gt;img&lt;/code&gt;, &lt;code&gt;span&lt;/code&gt;,&lt;code&gt;input&lt;/code&gt;,&lt;code&gt;table&lt;/code&gt; etc&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Pseudo elements&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;i.e. &lt;code&gt;::before&lt;/code&gt;, &lt;code&gt;::after&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Non-empty text nodes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;i.e. a continuous string of text, other than whitespace, without a containing element, such as  &lt;code&gt;"lorem ipsum"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;String content will be wrapped in an anonymous flex-item wrapper and &lt;strong&gt;⚠ cannot be directly styled with CSS&lt;/strong&gt; due to being anonymous.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3em2tfhhzbvtme932gyw.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3em2tfhhzbvtme932gyw.jpg" alt="Illustration of types of flex item"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, not all child items become flex-items:-&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Absolutely-positioned child elements&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Elements that are absolutely positioned with CSS (e.g. &lt;code&gt;position: absolute&lt;/code&gt;) are not flex items, as they are taken out of document flow&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;br&gt;
Flex items can also be set as flex containers.&lt;br&gt;
The item will act as both:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a flex item to its parent container &lt;/li&gt;
&lt;li&gt;a flex container to its contents&lt;/li&gt;
&lt;/ul&gt;



&lt;h4&gt;
  
  
  Flex formatting context
&lt;/h4&gt;

&lt;p&gt;Flex items participate in a &lt;a href="https://www.w3.org/TR/css-flexbox-1/#flex-containers" rel="noopener noreferrer"&gt;flex formatting context&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;This participation is regardless of the flex item's initial display type. The flex items will instead act "&lt;em&gt;like blocks&lt;/em&gt;".&lt;/p&gt;

&lt;p&gt;However, there are differences in block and flex formatting contexts.  &lt;/p&gt;

&lt;p&gt;Within a flex container:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;float&lt;/strong&gt; and &lt;strong&gt;clear&lt;/strong&gt; do not apply to flex items&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;vertical-align&lt;/strong&gt; has no effect on flex items&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;margin collapsing&lt;/strong&gt; is not a feature of flex formatting contexts&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;first formatting pseudo elements&lt;/strong&gt; do not apply&lt;br&gt;
(&lt;code&gt;::first-line&lt;/code&gt;, &lt;code&gt;::first-letter&lt;/code&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;h2&gt;
  
  
  Default flexbox layout
&lt;/h2&gt;

&lt;p&gt;Declaring &lt;strong&gt;&lt;code&gt;flex&lt;/code&gt;&lt;/strong&gt; on a parent element, will typically result in one of the following layouts:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;content in a row with free space&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67i3y3eukdilwafo3cbj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67i3y3eukdilwafo3cbj.jpg" alt="Illustration of a flexbox container with three flex items. The flex items are smaller than their container and so free space is available"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;content in a row with no free space&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0t5foltgbjgxghppcmag.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0t5foltgbjgxghppcmag.jpg" alt="Illustration of a flexbox container with three flex items. The flex items have shrunk to fit their container."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;content in a row overflowing its container&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnmcttnhruksrknd15vs9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnmcttnhruksrknd15vs9.jpg" alt="Illustration of a flexbox container with three flex items. The flex items despite shrinking overflow their container due to excessive content/size."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In all scenarios, we see:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;a single row&lt;/strong&gt; of flex items&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;equal height&lt;/strong&gt; flex items within this row&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is because flexbox was optimized for user interfaces and common interface designs call for a row of equal height elements/columns.  &lt;/p&gt;

&lt;p&gt;In the past, this default layout was difficult to produce and developers often resorted to workarounds and hacks, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Floated faux columns&lt;/li&gt;
&lt;li&gt;CSS table styling&lt;/li&gt;
&lt;li&gt;Set column heights&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thankfully, we can now achieve this with a single style declaration:&lt;br&gt;
&lt;strong&gt;&lt;code&gt;display: flex&lt;/code&gt;&lt;/strong&gt;  &lt;/p&gt;



&lt;h3&gt;
  
  
  Default flexbox properties and values
&lt;/h3&gt;

&lt;p&gt;When a flex container is created with &lt;strong&gt;&lt;code&gt;display: flex;&lt;/code&gt;&lt;/strong&gt; the following initial browser styles will take effect:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For the flex container:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flex-direction: row;
flex-wrap: nowrap;
justify-content: flex-start;
align-items: stretch;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;For its flex items:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flex-grow: 0;
flex-shrink: 1;
flex-basis: auto;
order: 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h3&gt;
  
  
  Flexbox terminology
&lt;/h3&gt;

&lt;p&gt;To help describe the behaviour of flexbox, let's take a detour and introduce some more terminology:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Main-size&lt;/strong&gt;  - The size of the flex item/container along the main axis/dimension.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cross-size&lt;/strong&gt; - The size of the flex item/container along the cross axis/dimension.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flex base size&lt;/strong&gt; (&lt;em&gt;or base size&lt;/em&gt;) - The content box size of the flex item, before it resizes (grows or shrinks).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hypothetical main size&lt;/strong&gt; - The flex base size clamped by  min/max values  (such as &lt;code&gt;min-width&lt;/code&gt;,&lt;code&gt;max-width&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inner size&lt;/strong&gt; - The content box size of an element.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Outer size&lt;/strong&gt; - The margin box size of an element. &lt;br&gt;
The margin box includes the content, padding, borders and margins of an element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo5eu4l9iumjlskpskufh.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo5eu4l9iumjlskpskufh.jpg" alt="Illustration of a flexbox container and its axes and sizing labels."&gt;&lt;/a&gt;&lt;/p&gt;



&lt;h3&gt;
  
  
  Flex container - default arrangement
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;What properties and values define how flex items are arranged in the container.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;
&lt;h4&gt;
  
  
  Main axis orientation
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;flex-direction: row;&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The flex-direction property sets the orientation of the main-axis.&lt;/p&gt;

&lt;p&gt;In default layout, the main-axis will match the orientation of the writing mode's inline axis, which is horizontal.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flex items are arranged horizontally in a row&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If the writing mode was vertical (&lt;em&gt;such as in Chinese or Japanese&lt;/em&gt;)&lt;br&gt;
the &lt;code&gt;flex-direction: row&lt;/code&gt; would run vertically, following the inline axis.&lt;/p&gt;



&lt;h4&gt;
  
  
  Cross axis orientation
&lt;/h4&gt;

&lt;p&gt;The cross-axis runs perpendicular to the main axis.&lt;/p&gt;

&lt;p&gt;In default layout, the &lt;strong&gt;cross axis runs vertically from top to bottom&lt;/strong&gt; in the block flow direction.&lt;/p&gt;



&lt;h4&gt;
  
  
  Main axis direction
&lt;/h4&gt;

&lt;p&gt;By default, the main axis direction matches the inline base direction of the document.&lt;/p&gt;

&lt;p&gt;The main-start and main-end directions are equivalent to the inline-start and inline-end of the inline axis.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frpl0pcocxjzbg7b4tp57.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frpl0pcocxjzbg7b4tp57.jpg" alt="Illustration of  how flexbox main axis aligns with its inline axis"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Flex items are &lt;strong&gt;arranged from left-to-right&lt;/strong&gt; (&lt;strong&gt;&lt;code&gt;ltr&lt;/code&gt;&lt;/strong&gt;).&lt;/p&gt;



&lt;h4&gt;
  
  
  Single flex line
&lt;/h4&gt;

&lt;p&gt;Initially flex items are laid out in a single line along the main axis of the flex container.&lt;/p&gt;

&lt;p&gt;This line of flexbox content is called a flex line.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frcber5je3qf03vs99s7u.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frcber5je3qf03vs99s7u.jpg" alt="Illustration of a flex line which is a line of flex items in a single row within a flex container"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Flex items &lt;strong&gt;do not automatically wrap&lt;/strong&gt; onto new flex lines.&lt;/p&gt;

&lt;p&gt;This is due to the default setting: &lt;br&gt;
&lt;strong&gt;&lt;code&gt;flex-wrap: nowrap;&lt;/code&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;If there is not enough space within the flex container, the contents will overflow.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffwtl6lpmutihseo3hovk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffwtl6lpmutihseo3hovk.jpg" alt="Illustration of flex items overflowing their flex container"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;h3&gt;
  
  
  Flex container - default alignment
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;What properties and values define how flex items are aligned in the container.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;
&lt;h4&gt;
  
  
  Main axis alignment
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;justify-content: flex-start;&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Initially, flex items will align to the main-start of the main axis.&lt;/p&gt;

&lt;p&gt;In default layout, the main-start is equivalent to the inline-start of the inline axis.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faa5sjugr635178wbtw8l.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faa5sjugr635178wbtw8l.jpg" alt="Illustration justify-content: flex start - by default in LTR languages, flex items are aligned to the left-side of the flex container"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flex items are aligned as a group to the left side of the container&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;


  
&lt;h4&gt;
  
  
  Cross axis alignment
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;align-items: stretch;&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;By default, flex items  will "&lt;em&gt;stretch&lt;/em&gt;" to fill the flex container's cross size, aligning to its cross-start and its cross-end.&lt;/p&gt;

&lt;p&gt;The flex items will stretch vertically from top-to-bottom, following the block flow direction of the writing mode.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flex items with the same cross size will appear equal height.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The cross size of a flex container is determined by either of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The flex-item with the largest cross size &lt;/li&gt;
&lt;li&gt;The set cross size of the flex container&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cross size determined by flex item&lt;/strong&gt; &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsamkf49yp7diyez8wo60.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsamkf49yp7diyez8wo60.jpg" alt="Illustration of flex container height set by content"&gt;&lt;/a&gt;&lt;br&gt;
By default, the flex container will expand to fit its largest flex item. If the flex container contains other flex items, the smaller flex items will be stretched to fit the flex container. &lt;/p&gt;

&lt;p&gt;This results in all flex items having equal cross sizes/heights.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cross size determined by container&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftatt70qpqaj7ogh0h7sr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftatt70qpqaj7ogh0h7sr.jpg" alt="Illustration of a flex container with a definite/set height"&gt;&lt;/a&gt;&lt;br&gt;
If the flex container has a set cross size (&lt;em&gt;such as a set&lt;/em&gt; &lt;code&gt;height&lt;/code&gt;), each smaller flex item will be stretched to fit the flex container's cross size. &lt;/p&gt;

&lt;p&gt;This results in all flex items having equal cross sizes/heights.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overflow (cross axis)&lt;/strong&gt;&lt;br&gt;
If there is a shortage of free space in the cross dimension, flex items may overflow their flex container.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;How do flex items stretch?&lt;/strong&gt;&lt;br&gt;
When a flex item is “&lt;em&gt;stretched&lt;/em&gt;”, its outer margin edge will become flush with the cross-start and cross-end of the flex container's content box.  This results in a larger content box cross size for the flex item.&lt;/p&gt;

&lt;p&gt;If the margin box of the flex item has no value, then its margin edge will be flush against whichever of the flex item's box model has a value. Falling back in the following order:  &lt;strong&gt;margin&lt;/strong&gt; &amp;gt; &lt;strong&gt;border&lt;/strong&gt; &amp;gt; &lt;strong&gt;padding&lt;/strong&gt; &amp;gt; &lt;strong&gt;content&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Exceptions to flex item stretch&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Height&lt;/strong&gt;&lt;br&gt;
If a flex item has a set &lt;code&gt;height&lt;/code&gt;,  then the item will not be be stretched. The item will be sized in the cross axis to this height and will be aligned to the cross-start of the flex container. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Min/max-height&lt;/strong&gt;&lt;br&gt;
If a &lt;code&gt;min&lt;/code&gt;/&lt;code&gt;max-height&lt;/code&gt; value is set, the flex item will be stretched, however, its cross size will be limited by these values. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Auto margins&lt;/strong&gt;&lt;br&gt;
 e.g. &lt;code&gt;margin-top: auto&lt;/code&gt; &lt;br&gt;
If a flex item has an &lt;code&gt;auto&lt;/code&gt; margin in the cross axis, then this margin will take the positive free space for itself, preventing the item stretching. The margin will push the flex item's box content away from the cross edge.&lt;/p&gt;

&lt;p&gt;If the flex item has auto margins set on both cross sides and free space is available, this will center the flex item within the cross axis. The free space is equally shared by the margins.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsetbkizfozhiwnj302yq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsetbkizfozhiwnj302yq.jpg" alt="Illustration of how items stretch within a flex container"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;h3&gt;
  
  
  Flex item - default ordering
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;order: 0;&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;By default, flex items will display in their source document order following the main axis direction from left-to-right.&lt;/p&gt;

&lt;p&gt;This default order can be overridden by specifying an integer value for the flex item. Items with an order greater than zero will display after items with a smaller order value.&lt;/p&gt;



&lt;h3&gt;
  
  
  Flex item - default sizing
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;What properties and values define how flex items are sized within the container.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;p&gt;The default flex item sizing properties, relate to three sizing questions:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;flex-grow&lt;/code&gt;&lt;/strong&gt; - &lt;em&gt;can the flex item grow, if there is space available?&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;&lt;code&gt;flex-shrink&lt;/code&gt;&lt;/strong&gt; - &lt;em&gt;can the flex item shrink, if there is a shortage of space?&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;&lt;code&gt;flex-basis&lt;/code&gt;&lt;/strong&gt; - &lt;em&gt;what is the flex item's initial size, before any resizing occurs?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The application of these properties are dependent upon:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the content size of the flex items&lt;/li&gt;
&lt;li&gt;the available free space within the flex container&lt;/li&gt;
&lt;/ul&gt;



&lt;h4&gt;
  
  
  Flex item growth
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;flex-grow: 0;&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;By default, &lt;strong&gt;flex items do not grow&lt;/strong&gt; beyond their initial content-size.&lt;/p&gt;

&lt;p&gt;The flex items have a growth factor/value of &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If there is positive free space available within the flex container, the flex items are inflexible.&lt;/p&gt;



&lt;h4&gt;
  
  
  Flex item shrinkage
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;flex-shrink: 1;&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;flex items are allowed to shrink&lt;/strong&gt; by default. &lt;/p&gt;

&lt;p&gt;This is because each flex item has a shrink-factor of &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Shrinking happens when the summed flex items are larger than the flex container, resulting in negative free space (&lt;em&gt;a shortage of space&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8flkor0itg495uim61yl.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8flkor0itg495uim61yl.jpg" alt="Illustration of negative free space within a flex container. Its summed flex item contents exceeds the width of the flex container "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Flexbox will distribute this negative free space when applying the flex-shrink factor to its flex items.&lt;/p&gt;

&lt;p&gt;As each flex item has the same flex-shrink value, you  would think each flex item would shrink by the same amount, but this is not the case.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The flex shrink factor determines the items shrink rate&lt;/strong&gt; and should not to be confused with the shrink amount.&lt;/p&gt;

&lt;p&gt;The amount each item shrinks depends upon its base size (content) and its shrink factor. &lt;/p&gt;

&lt;p&gt;As seen in &lt;a href="https://www.w3.org/TR/css-flexbox-1/#valdef-flex-flex-shrink" rel="noopener noreferrer"&gt;Section 7.1&lt;/a&gt; of the specification:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: The flex shrink factor is multiplied by the flex base size when distributing negative space. This distributes negative space in proportion to how much the item is able to shrink, so that e.g. a small item won’t shrink to zero before a larger item has been noticeably reduced.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The larger flex items will proportionally shrink more than their smaller siblings. This means that when large items are present, the smaller items are less likely to shrink to a tiny unreasonable size. &lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fubmf1dci3f848rqnysey.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fubmf1dci3f848rqnysey.jpg" alt="Illustration of how the application of flex shrink 1 varies, when flex items vary in width/content. Flex items don't shrink by the same amount"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Furthermore, default flex items will not shrink below their smallest content size (&lt;code&gt;min-content&lt;/code&gt;).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;strong&gt;&lt;code&gt;min-content&lt;/code&gt;&lt;/strong&gt; sizing keyword represents the intrinsic minimum width of the content. For text content  this means that the content will take all soft-wrapping opportunities, becoming as small as the longest word.&lt;br&gt;
-- &lt;cite&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/min-content" rel="noopener noreferrer"&gt;mdn web docs&lt;/a&gt;&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0py09nm87uaakicsgl9s.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0py09nm87uaakicsgl9s.jpg" alt="Illustration of min-content within a flex container. Each flex items contains text of various lengths. Each flex items width is defined by its longest word."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For replaced elements, such as &lt;code&gt;img&lt;/code&gt;, &lt;code&gt;video&lt;/code&gt; or  &lt;code&gt;input&lt;/code&gt;:&lt;br&gt;
the &lt;code&gt;min-content&lt;/code&gt; size is the natural/intrinsic size of the element or the browser set default size (&lt;em&gt;unless overridden by CSS&lt;/em&gt;). &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overflow (main axis)&lt;/strong&gt;&lt;br&gt;
If there is a shortage of free space in the main dimension, flex items may overflow the flex container.&lt;/p&gt;



&lt;h4&gt;
  
  
  Flex item initial size
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;flex-basis: auto;&lt;/code&gt;&lt;/strong&gt;  &lt;/p&gt;

&lt;p&gt;&lt;code&gt;flex-basis&lt;/code&gt; relates to the flex item's &lt;strong&gt;initial main size&lt;/strong&gt;, before any growth or shrinking is applied.&lt;/p&gt;

&lt;p&gt;The default  &lt;code&gt;auto&lt;/code&gt; value, is equal to  the &lt;code&gt;max-content&lt;/code&gt; size of the flex item. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;strong&gt;&lt;code&gt;max-content&lt;/code&gt;&lt;/strong&gt; sizing keyword represents the intrinsic maximum width or height of the content. For text content this means that the content will not wrap at all even if it causes overflows.&lt;br&gt;
-- &lt;cite&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/max-content" rel="noopener noreferrer"&gt;mdn web docs&lt;/a&gt;&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn857ssve81bf9hodubf0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn857ssve81bf9hodubf0.jpg" alt="Illustration of max-content in a flexbox container. Each flex item's width is defined by a single line of text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If free space is available within the flex container, the flex item will appear as big its contents and will not grow beyond this.&lt;/p&gt;



&lt;h3&gt;
  
  
  Default layout - Resolving flexible lengths
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;How and when should flex items grow or shrink?&lt;/em&gt;&lt;br&gt;
 &lt;br&gt;
&lt;strong&gt;Please note:&lt;/strong&gt; &lt;br&gt;
&lt;em&gt;The following is an oversimplification of the flexbox sizing algorithm and concerns default layout only.&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;
&lt;h4&gt;
  
  
  1. Determine the used flex factor
&lt;/h4&gt;

&lt;p&gt;When sizing flex items, flexbox determines whether the flex container has positive or negative free space. &lt;/p&gt;

&lt;p&gt;Flexbox does this by summing the flex items outer hypothetical main sizes and compares this sum against the inner main size of the flex container. &lt;/p&gt;

&lt;p&gt;If the sum is less than the flex container's, then there is positive free space, otherwise, there is negative free space.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;if there is &lt;strong&gt;positive free space&lt;/strong&gt;&lt;br&gt;
then the &lt;strong&gt;flex grow&lt;/strong&gt; factor is used&lt;br&gt;
 &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;if there is &lt;strong&gt;negative free space&lt;/strong&gt; &lt;br&gt;
then the &lt;strong&gt;flex shrink&lt;/strong&gt; factor is used&lt;br&gt;
 &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  2. Size inflexible items
&lt;/h4&gt;

&lt;p&gt;Before flexbox applies flex grow or shrink, it first determines if any flex items are inflexible.&lt;/p&gt;

&lt;p&gt;When there is positive free space, the &lt;code&gt;flex-grow&lt;/code&gt; factor applies. However, flex items are inflexible in this case, as their flex grow factor is zero.&lt;/p&gt;

&lt;p&gt;Flex items in this scenario are set to their target &lt;strong&gt;hypothetical main size&lt;/strong&gt;, this is their &lt;code&gt;max-content&lt;/code&gt; size clamped by min/max sizing.&lt;br&gt;
 &lt;/p&gt;
&lt;h4&gt;
  
  
  3. Calculate the initial free space
&lt;/h4&gt;

&lt;p&gt;In this step, flexbox will calculate the &lt;strong&gt;amount&lt;/strong&gt; of free space available within the flex container. This free space can either be positive or negative. &lt;/p&gt;

&lt;p&gt;Both frozen and unfrozen flex items are included in this calculation.&lt;br&gt;
 &lt;/p&gt;
&lt;h4&gt;
  
  
  4. Loop and distribute free space
&lt;/h4&gt;

&lt;p&gt;In default layout, if the algorithm has got to this step, it can mean one of two things regarding the space in the flex container:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;if there is &lt;strong&gt;no free space&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;do nothing&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;if there is &lt;strong&gt;negative free space&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;apply the flex shrink factor&lt;/em&gt;&lt;br&gt;
 &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Applying the free shrink factor&lt;/strong&gt;&lt;br&gt;
Flexbox will distribute negative free space by calculating a target size for each flex item. The target size will take into account its content-size, shrink factor and shrinkable siblings, like so:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For each unfrozen flex item&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Calculate its &lt;strong&gt;scaled flex shrink factor&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;flex-shrink factor 1 multiplied by its inner base size&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Calculate its &lt;strong&gt;shrink ratio&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;its scaled shrink factor / sum of the scaled flex shrink factors of all unfrozen items on the line&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reduce the flex item by &lt;strong&gt;a fraction of the absolute negative free space proportional to its shrink ratio&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;item base size - (abs. negative free space multiplied by its shrink ratio)&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
  &lt;strong&gt;worked example of default flex shrink&lt;/strong&gt;
  &lt;p&gt;In this example, we'll focus on the first flex item and the  the application of  &lt;strong&gt;&lt;code&gt;flex-shrink: 1;&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fubmf1dci3f848rqnysey.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fubmf1dci3f848rqnysey.jpg" alt="Illustration of how the application of flex shrink 1 varies, when flex items vary in width/content. Flex items don't shrink by the same amount"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The flex container has &lt;strong&gt;negative free space&lt;/strong&gt;:  &lt;strong&gt;&lt;code&gt;-400px&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;600px - (100px + 200px + 300px + 400px) = -400px&lt;/code&gt;&lt;br&gt;
&lt;em&gt;This is the flex container's main size minus the total main size of the flex items.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When expressed as an absolute number, the negative free space is: &lt;strong&gt;&lt;code&gt;400px&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The first flex item in the row has a base size of:   &lt;strong&gt;&lt;code&gt;100px&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Its &lt;strong&gt;scaled flex factor&lt;/strong&gt; is: &lt;strong&gt;&lt;code&gt;100&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;100 * 1 = 100&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Its &lt;strong&gt;shrink ratio&lt;/strong&gt; is:  &lt;strong&gt;&lt;code&gt;0.1&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;100 / (100 * 1, 200 * 1, 300 * 1, 400 * 1) = 0.1&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Its &lt;strong&gt;target flex item&lt;/strong&gt; size is: &lt;strong&gt;&lt;code&gt;60px&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;100px - (400 * 0.1) = 60px&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;    Its &lt;strong&gt;shrink amount&lt;/strong&gt; is: &lt;strong&gt;&lt;code&gt;-40px&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
    &lt;code&gt;-400 * 0.1 = -40px&lt;/code&gt;&lt;/p&gt;






&lt;/p&gt;

&lt;p&gt;Alternatively, the flexbox algorithm will exit this loop, when there is no remaining free space left to distribute.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Once the loop is complete, the used main size is set to the target main size, its final size.&lt;/strong&gt;&lt;/p&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Often CSS can be tricky when we don't fully understand how it works.&lt;/p&gt;

&lt;p&gt;Assuming that the web page is written in English, we can assume that &lt;strong&gt;&lt;code&gt;display: flex&lt;/code&gt;&lt;/strong&gt; will most likely create:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a &lt;strong&gt;block&lt;/strong&gt; styled flex container&lt;/li&gt;
&lt;li&gt;a &lt;strong&gt;single row&lt;/strong&gt; of flex items &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;left-to-right ordered&lt;/strong&gt; flex items&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;equal height&lt;/strong&gt; flex items&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;max-content&lt;/code&gt;&lt;/strong&gt; sized flex items &lt;em&gt;unless shrunk&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;min-content&lt;/code&gt;&lt;/strong&gt; sized flex items &lt;em&gt;when shrunk&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;content overflow&lt;/strong&gt; if the flex container is smaller than the final size of its flex items &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If it doesn't, we can check for possible causes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;a default flexbox value may have been overridden&lt;br&gt;
(e.g. &lt;code&gt;flex: 1&lt;/code&gt;, &lt;code&gt;flex-direction: column&lt;/code&gt;)  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;additional layout values may have been set&lt;br&gt;
(e.g. &lt;code&gt;height&lt;/code&gt;, &lt;code&gt;width&lt;/code&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the document may have a different writing mode&lt;br&gt;
(e.g. &lt;code&gt;veritical-rl&lt;/code&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Its also worth remembering that not all flex items are flexible, and that flex item sizing is based upon the flex item's content, unless overridden. &lt;/p&gt;





&lt;h3&gt;
  
  
  Related reading
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://css-tricks.com/snippets/css/a-guide-to-flexbox/" rel="noopener noreferrer"&gt;CSS Tricks -  A Complete Guide to Flexbox&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://book.mixu.net/css/4-flexbox.html" rel="noopener noreferrer"&gt;Learn CSS Layout - the pedantic way - 4. Flexbox&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.samanthaming.com/flexbox30/24-flex-shrink-calculation/" rel="noopener noreferrer"&gt;Samantha Ming - flex-shrink calculation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.madebymike.com.au/writing/understanding-flexbox/" rel="noopener noreferrer"&gt;MadeByMike-  Understanding flexbox&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.smashingmagazine.com/2018/09/flexbox-sizing-flexible-box/" rel="noopener noreferrer"&gt;Smashing Magazine - Flexbox: How Big Is That Flexible Box?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.smashingmagazine.com/2018/08/flexbox-display-flex-container/" rel="noopener noreferrer"&gt;Smashing magazine - What Happens When You Create A Flexbox Flex Container?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3.org/TR/css-flexbox-1/" rel="noopener noreferrer"&gt; CSS Flexible Box Layout Module Level 1 specification&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3.org/TR/css-writing-modes-3/" rel="noopener noreferrer"&gt;CSS Writing Modes Level 3 specification&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>What is the DOM?</title>
      <dc:creator>Paul Thompson</dc:creator>
      <pubDate>Mon, 16 May 2022 21:46:14 +0000</pubDate>
      <link>https://dev.to/paulnthompson84/what-is-the-dom-58k9</link>
      <guid>https://dev.to/paulnthompson84/what-is-the-dom-58k9</guid>
      <description>&lt;p&gt;The DOM stands for &lt;strong&gt;Document Object Model&lt;/strong&gt;, which is an interface that represents the contents of a HTML or XML document.&lt;/p&gt;

&lt;p&gt;This interface is a collection of objects, actions, attributes and relationships, which is collectively is known as an Object Model. &lt;/p&gt;

&lt;p&gt;The DOM provides developers with a language-independent application programming interface to access and manipulate the document.&lt;/p&gt;

&lt;p&gt;Documents as a result can be &lt;em&gt;as dynamic as web app or as static as a read-only page&lt;/em&gt; &lt;/p&gt;




&lt;p&gt;&lt;em&gt;For the rest of this article, I will be discussing the DOM in relation to HTML documents (web pages)  which are accessed in web browsers, such as Chrome, Safari, Edge or Firefox.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;  &lt;/p&gt;

&lt;h2&gt;
  
  
  How is the DOM created?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The browser first needs to process the document, in order to use it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before a web browser can display or use a HTML document, it needs to create the DOM. It does this by downloading and processing the document, creating the resulting object model in its memory.&lt;/p&gt;

&lt;p&gt;Various processes need to take place, in order to create the DOM:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bytes&lt;/strong&gt; &amp;gt; &lt;strong&gt;Conversion&lt;/strong&gt; &amp;gt; &lt;strong&gt;Tokenizing&lt;/strong&gt; &amp;gt; &lt;strong&gt;Lexing&lt;/strong&gt; &amp;gt; &lt;strong&gt;DOM construction&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Bytes&lt;/strong&gt;:- the browser reads the document in a stream of bytes, via packets sent across the internet or retrieved from local disk. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Conversion&lt;/strong&gt;:- these bytes are then converted into characters based upon the encoding specified in the document (usually &lt;code&gt;UTF-8&lt;/code&gt;). &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tokenizing&lt;/strong&gt;:- the characters are then pattern matched into tokens as defined by the HTML specification, such as &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt;,&lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lexing&lt;/strong&gt;:- the tokens are then converted into "objects", which define their properties and rules. &lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;DOM construction&lt;/strong&gt;:- finally the relationships of these objects are built into a tree-like structure (&lt;em&gt;a parse tree&lt;/em&gt;) as defined by the HTML document. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The entire document is represented by a &lt;code&gt;document&lt;/code&gt; node.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; element is the root element , which represents the top-most part of the HTML document.&lt;/li&gt;
&lt;li&gt;All other nodes are descendants of the root element, within the DOM. &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;For example: the &lt;code&gt;head&lt;/code&gt; and &lt;code&gt;body&lt;/code&gt; element nodes are descendants of the root &lt;code&gt;html&lt;/code&gt; node.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frt9ngba77xxywly4qrfp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frt9ngba77xxywly4qrfp.png" alt="Diagram of DOM hierarchy:- document, html, head, body tags"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;More than one child node creates a sibling relationship.&lt;br&gt;
 e.g. &lt;code&gt;li&lt;/code&gt;'s are siblings within the &lt;code&gt;ul&lt;/code&gt; tag.&lt;br&gt;
  &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5c0hw9usrs72e5r6m3hu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5c0hw9usrs72e5r6m3hu.png" alt="Diagram of DOM siblings:- li elements within ul tag"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every time the browser processes HTML, this DOM creation process runs. &lt;/p&gt;
&lt;h4&gt;
  
  
  Browsers are tolerant of HTML errors
&lt;/h4&gt;

&lt;p&gt;During the DOM creation phase, the browser will address various issues or errors it encounters with the source HTML.  This error handling results in a resilient foundation for our web page/app content. &lt;/p&gt;

&lt;p&gt;  &lt;/p&gt;
&lt;h2&gt;
  
  
  Features of the DOM
&lt;/h2&gt;

&lt;p&gt;The document object model covers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Interfaces and objects&lt;/strong&gt; used to manipulate the document&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;meaning&lt;/strong&gt; of these interfaces and objects, including attributes and behaviour&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;relationships&lt;/strong&gt; and &lt;strong&gt;collaborations&lt;/strong&gt; among the interfaces and objects
  &lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Logical structure
&lt;/h3&gt;

&lt;p&gt;The DOM closely represents the document's structure via nodes and links, whereby:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTML elements, attributes, text etc are represented by &lt;strong&gt;nodes&lt;/strong&gt; (&lt;em&gt;a basic unit in a data structure&lt;/em&gt;)

&lt;ul&gt;
&lt;li&gt;(Each node contains objects)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Links&lt;/strong&gt; or pointers are used between nodes, which represent the nodes hierarchical relationships to each other&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Tree structure
&lt;/h4&gt;

&lt;p&gt;The logical structure is generally thought of as a tree-like structure, so you may encounter terms such as '&lt;em&gt;document tree&lt;/em&gt;' and '&lt;em&gt;DOM tree&lt;/em&gt;'  when describing the document's structure or hierarchy.  This tree-like structure, is not a DOM specification or implementation requirement, however, it is a generally understood mental model to have for HTML documents. &lt;/p&gt;
&lt;h3&gt;
  
  
  Properties and methods
&lt;/h3&gt;

&lt;p&gt;The object model provides properties and methods, which allow developers to find, access and interact with the document's contents&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Properties&lt;/strong&gt; are &lt;u&gt;values&lt;/u&gt; of objects (&lt;em&gt;typically HTML elements&lt;/em&gt;) which you can return or change, such as &lt;code&gt;classList&lt;/code&gt;, &lt;code&gt;innerHTML&lt;/code&gt;, &lt;code&gt;innerText&lt;/code&gt;  etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Methods&lt;/strong&gt; are &lt;u&gt;actions&lt;/u&gt; related to DOM objects, such as &lt;code&gt;querySelector()&lt;/code&gt;, &lt;code&gt;createElement()&lt;/code&gt;, &lt;code&gt;addEventListener()&lt;/code&gt; etc&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Properties and methods vary depending upon the node type in the DOM.&lt;/p&gt;
&lt;h3&gt;
  
  
  Representative types/objects
&lt;/h3&gt;

&lt;p&gt;The object model can represent the HTML document in various ways.  How a document part is represented, will  determine how it can be interacted with (i.e. what properties and methods it has).&lt;/p&gt;
&lt;h4&gt;
  
  
  Node type
&lt;/h4&gt;

&lt;p&gt;Every node in the DOM has a node type.&lt;br&gt;
There are 12 node types in total but some node types have been deprecated. The integer (number) value represents the type of the node.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Notable&lt;/strong&gt; node types include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;ELEMENT_NODE (1)&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Which represent HTML tags such as &lt;code&gt;div&lt;/code&gt;,&lt;code&gt;body&lt;/code&gt;,&lt;code&gt;p&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;TEXT_NODE (3)&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The text/string value between HTML tags&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;COMMENT_NODE (8)&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The comment value in the HTML&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;DOCUMENT_NODE (9)&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;This represents the entire document in the tree structure&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Attribute values&lt;/strong&gt;&lt;br&gt;
Values such as &lt;code&gt;id&lt;/code&gt;,&lt;code&gt;class&lt;/code&gt;  &lt;code&gt;href&lt;/code&gt; are technically not part of the DOM tree.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;since they are not actually child nodes of the element they describe, the DOM does not consider them part of the document tree.&lt;br&gt;
...&lt;br&gt;
The DOM takes the view that attributes are properties of elements rather than having a separate identity from the elements they are associated with&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Source: &lt;a href="https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#ID-637646024" rel="noopener noreferrer"&gt;Document Object Model (DOM) Level 3 Core Specification - Interface  _Attr&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Attribute objects inherit the node interface and have a node type:   &lt;strong&gt;ATTRIBUTE_NODE (2)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;  &lt;/p&gt;
&lt;h4&gt;
  
  
  Collections
&lt;/h4&gt;

&lt;p&gt;Depending upon DOM selection method used and the node(s) selected, the returned value could be a collection of elements/nodes rather than a single element or node.&lt;/p&gt;
&lt;h5&gt;
  
  
  Nodelists
&lt;/h5&gt;

&lt;p&gt;Nodelists are collections of nodes, which are array-like in nature. Nodelists may be looped over using &lt;code&gt;forEach&lt;/code&gt; when interacting with the DOM. &lt;br&gt;
Nodelists can contain text and attributes nodes. &lt;/p&gt;
&lt;h5&gt;
  
  
  HTMLCollection
&lt;/h5&gt;

&lt;p&gt;HTMLCollections are collections of elements (tags) unlike Nodelists, which can contain all node types. Like NodeLists, HTMLcollections are array-like.&lt;/p&gt;

&lt;p&gt;HTMLCollection is a historical interface returned by the older DOM selection methods&lt;br&gt;
&lt;code&gt;getElementsByTagName()&lt;/code&gt;&lt;br&gt;
&lt;code&gt;getElementsByClassName()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Collection values can be either live or static:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Live values update when the DOM updates&lt;/li&gt;
&lt;li&gt;Static values are obtained only when DOM selection is executed, they are unaffected by future updates to the DOM&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;br&gt;
Both &lt;code&gt;NodeLists&lt;/code&gt; and &lt;code&gt;HTMLcollections&lt;/code&gt; can be converted to JavaScript arrays,  granting developers greater flexibility in data manipulation (with methods unavailable in these array-like data structures). &lt;/p&gt;

&lt;p&gt;  &lt;/p&gt;
&lt;h2&gt;
  
  
  DOM interface / Web APIs
&lt;/h2&gt;

&lt;p&gt;Although the DOM is cross-platform and language independent, it's typically manipulated with JavaScript in the browser. &lt;/p&gt;

&lt;p&gt;When working with the DOM in JavaScript, expressions are imperatively structured. &lt;br&gt;
This means we are telling the browser &lt;strong&gt;exactly&lt;/strong&gt; what to do, such as:  &lt;em&gt;select a DOM node and perform an action on that selection.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This can be done directly on the DOM selection:&lt;br&gt;
e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;heading&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or by using JavaScript variables to more effectively structure the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pageTitle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;heading&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;pageTitle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;blue&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;pageTitle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textTransform&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Uppercase&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The DOM API provides various properties and methods to manipulate the document, which can be grouped by type/function, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Selection&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Traversal&lt;/strong&gt; (&lt;em&gt;navigating between nodes&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Creating or updating content&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deleting or  removing content&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Styling content&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Node/collection methods&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Events&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Declarative DOM manipulation
&lt;/h3&gt;

&lt;p&gt;For many developers, direct DOM manipulation is too verbose (e.g. overly long method names) and too expensive  for modern web app implementations. &lt;/p&gt;

&lt;p&gt;Developers found their implementations would grow complexity (numerous DOM event listeners) with performance downsides of frequent DOM updates.&lt;/p&gt;

&lt;p&gt;Developers as a result, will now often use a JavaScript framework or library instead, which takes care of this complexity for them.  These tools provide the developer with a declarative interface, where they can tell the browser/program logic "&lt;em&gt;what to do&lt;/em&gt;" instead of "&lt;em&gt;how to do it&lt;/em&gt;". &lt;/p&gt;

&lt;p&gt;Examples include: &lt;em&gt;React, Vue, Svelte etc&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In these cases, the framework/library does the DOM manipulation for the developer, usually with an intermediary step (such as a Virtual DOM or compilation).&lt;/p&gt;




&lt;p&gt;  &lt;/p&gt;

&lt;h2&gt;
  
  
  What ISN'T the DOM?
&lt;/h2&gt;

&lt;p&gt;When working with the DOM, it's worth knowing what isn't the DOM, particularly when using browser dev tools. &lt;/p&gt;

&lt;h3&gt;
  
  
  The DOM is not your HTML markup
&lt;/h3&gt;

&lt;p&gt;The DOM is not your source  &lt;code&gt;.html&lt;/code&gt; file,&lt;br&gt;
it is not viewable when you &lt;code&gt;View page source&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Although the DOM is a representation of the document, it is not an exact duplication in the browser. The browser will fix various issues with the underlying mark up (HTML). &lt;/p&gt;

&lt;p&gt;Such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inserting omitted &lt;code&gt;tbody&lt;/code&gt; tags into &lt;code&gt;table&lt;/code&gt; structures&lt;/li&gt;
&lt;li&gt;closing unclosed tags &lt;/li&gt;
&lt;li&gt;moving tags incorrectly nested in other elements&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The DOM is not static
&lt;/h3&gt;

&lt;p&gt;Since the DOM is not your markup, it can be continually amended and updated with JavaScript, once the JavaScript has been loaded and run in the browser (&lt;em&gt;the client&lt;/em&gt;) .   &lt;/p&gt;

&lt;p&gt;DOM manipulation can happen as part of the script's program design,  it may also happen as part of scripted responses to user input or events, which can occur at any time. &lt;/p&gt;

&lt;h4&gt;
  
  
  Client-side JavaScript can be executed from various sources:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Within the HTML document&lt;/strong&gt; (via &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tags) &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Linked externally&lt;/strong&gt; via markup.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dynamically loaded&lt;/strong&gt; with JavaScript &lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Browser extension&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: &lt;em&gt;browser extensions often dynamically append elements to the DOM. Bare this in mind, when troubleshooting web pages/applications.&lt;/em&gt; &lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The DOM is not CSS content
&lt;/h3&gt;

&lt;p&gt;When inspecting elements in browser devtools, you may also see CSS pseudo-elements (&lt;code&gt;::before&lt;/code&gt;, &lt;code&gt;::after&lt;/code&gt;)  near DOM elements. CSS pseudo-elements aren't part of the DOM, you can't manipulate them using DOM APIs and JavaScript. &lt;/p&gt;

&lt;h3&gt;
  
  
  The DOM is not JavaScript
&lt;/h3&gt;

&lt;p&gt;Although you may write DOM APIs in JavaScript, DOM APIs aren't JavaScript nor are its objects the same as JavaScript objects. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;querySelector&lt;/code&gt; != &lt;code&gt;JavaScript&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The DOM is after all, an interface for JavaScript to interact with the source document. &lt;/p&gt;

&lt;p&gt;  &lt;/p&gt;

&lt;h2&gt;
  
  
  Related models/concepts
&lt;/h2&gt;

&lt;h3&gt;
  
  
  BOM
&lt;/h3&gt;

&lt;p&gt;BOM stands for Browser Object Model and is a related object model used for interacting with documents inside web browsers. &lt;/p&gt;

&lt;p&gt;The BOM is an unofficial standard but generally consists of the following objects:&lt;br&gt;
&lt;code&gt;Navigator&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Window&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Location&lt;/code&gt; &lt;br&gt;
&lt;code&gt;History&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Frame&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;document&lt;/code&gt; object is child object of the &lt;code&gt;window&lt;/code&gt; object, which is a global object for the browser. &lt;/p&gt;

&lt;p&gt;The BOM like the DOM, has it's own properties and methods.  For example, the &lt;code&gt;DOMContentLoaded&lt;/code&gt; window event, fires when the initial HTML has been loaded and parsed, regardless of stylesheet, image and subframe loading state.&lt;/p&gt;

&lt;h3&gt;
  
  
  CSSOM and Render Tree
&lt;/h3&gt;

&lt;p&gt;Beside the DOM, the browser also creates an accompanying CSS Object Model, (the CSSOM) taking into account all the styles applied to the document, from various sources (&lt;em&gt;external, embedded, inline, user-agent, etc&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;The browser then combines the DOM and CSSOM in a render tree, using only the nodes needed to render the web page. &lt;/p&gt;

&lt;p&gt;The browser will use the render tree to paint/render the document to the device's screen. &lt;/p&gt;

&lt;p&gt;The DOM and CSSOM, are independent models and form the critical rendering path.&lt;/p&gt;

&lt;h3&gt;
  
  
  Accessibility tree
&lt;/h3&gt;

&lt;p&gt;Browsers will create the accessibility tree &lt;em&gt;based upon the DOM&lt;/em&gt;.  This tree exposes the accessible objects for HTML elements to assistive technologies and its users.&lt;/p&gt;

&lt;p&gt;For accessible web pages, it is important to author HTML in a semantic/meaningful way, wherever possible adhering to web standards.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>dom</category>
      <category>html</category>
    </item>
  </channel>
</rss>
