📘
NavDoc by Bash School
GithubContact
📘
NavDoc by Bash School
  • 🎓Introduction
  • 🐢Getting Started
  • ⚡Changelog
  • 👨‍🚀Maintainers
  • 🛣️Roadmap
  • Fundamentals
    • The Internet
      • Introduction
      • What is a URL
      • What is a port
      • The DNS protocol
      • The TCP protocol
      • The UDP protocol
      • The Web
      • The HTTP protocol
      • Hyperlinks
      • What is a Web browser
      • What is a Web server
    • HTML
      • Your first HTML page
      • Text tags
      • Attributes
      • Links
      • Images
      • Lists
      • Head Tags
      • Container tags
    • CSS
      • Introduction
      • Colors
      • selectors
      • Cascade
      • Specificity
      • Units
      • Advanced selectors
      • Typography
      • The box model
      • The display property
      • Responsive design
  • JavaScript
    • Basics
      • Introduction
      • Literals , Identifiers, Variables
      • Comments
      • The difference between let, const and var
      • Types
      • Operators and expressions
      • Arithmetic operators
      • The assignment operator
      • Operators precedence
      • Strings
      • Numbers
      • Semicolons, white space and sensitivity
      • Arrays
      • Conditionals
      • Loops
      • Functions
      • Objects
      • Arrays + functions
      • OOPS
      • Asynchronous
      • Scope, hoisting, event loop
      • ES Modules
      • Errors and exceptions
      • Built-in objects
        • The global object
        • Object properties
        • Number
        • String
        • Math
        • JSON
        • Date
        • Intl
        • Set and Map
      • More operators
    • Nodejs
      • Getting Started
      • Installation
      • Hello World in Node
      • Modules
      • Packages
      • File Handling
      • HTTP Request
      • Processing Files
      • HTTP
    • Express.js
      • Getting Started
      • Middleware
      • Serve Static Assets
      • How to Send Files to the Client
      • Sessions
      • Validate Input
      • Sanitizing Data
      • Forms
      • File Uploads
    • React
      • Setting up a React project with Vite
      • React Components
      • Introduction to JSX
      • Using JSX to compose UI
      • The difference between JSX and HTML
      • Embedding JavaScript in JSX
      • Handling user events
      • Managing state
      • Component props
      • Data flow
      • Lifecycle events
      • Managing forms in React
      • Install the React Developer Tools
      • Installing Tailwind CSS in a React app
      • Build a counter in React
    • TypeScript
      • Key Benefits
      • Types of Languages
      • The Need for TypeScript
      • What is TypeScript?
      • The tsc Compiler
      • Basic Types in TypeScript
      • tsconfig
      • Interfaces
      • Types
      • Arrays in TypeScript
      • Enums
      • Exporting and importing
    • MongoDB
      • SQL vs. NoSQL Databases
      • Installing MongoDB
      • MongoDB Databases and Collections
      • Working with Documents
      • MongoDB Operators
      • Sorting, Indexing & Searching
      • Built-in Methods
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Fundamentals
  2. CSS

The box model

Every HTML element on the page is essentially considered a box. The box is a container that includes the element.

The box model sets the sizing of the elements based on a few CSS properties.

From the inside to the outside, we have:

  1. The element itself

  2. Padding between the element and the border

  3. Border

  4. Margin, between the border and the “outside”

The element can be sized by setting its width and height CSS properties:

width: 200px;
height: 50px;

Imagine those properties being set for a selector, like p {}.

The width can be adaptive, meaning you can set a minimum width or maximum width:

min-width: 400px;
max-width: 800px;

Same for height.

Padding can be set using the padding property, and its helpers padding-top, padding-right, padding-bottom, and padding-left:

padding-top: 10px;

padding-bottom: 20px;

padding: 0; /* remove all padding */
padding: 10px; /* set all padding values to 10px */

The padding property allows you to set a value for each side, using this syntax which sets the values of top-right-bottom-left:

padding: 10px 20px 30px 40px;

And there’s a shortcut to set the values of top/bottom and left/right:

padding: 10px 20px; /* top and bottom are 10px, left and right 20px */

Margin can be set in the same way, using margin and its helpers margin-top, margin-right, margin-bottom, and margin-left:

margin-top: 10px;

margin: 20px;

And you can use all the “tricks” we saw above to set top, right, bottom, and left values, or top/bottom and left/right.

The border property can be used to set the border, along with its helpers border-top, border-right, border-bottom, and border-left.

It’s distinct from padding and margin:

border: 1px solid; /* sets a 1px "solid" border */
border: 1px dashed; /* sets a 1px "dashed" border */

You can also use dotted and double as the border style, along with other values.

Then, you can also set the color:

border: 1px solid red; /* sets a 1px "solid" red border */
border: 1px dashed yellow; /* sets a 1px "dashed" yellow border */

You can also just set one border:

border-bottom: 1px solid #000; /* sets a 1px "solid" black bottom border */

And you can also set the properties individually:

border-style: solid;
border-color: #000;
border-width: 1px;

border-bottom-style: dashed;
border-bottom-color: red;
border-bottom-width: 2px;

The best way to visualize the box model is to open the browser DevTools and check how it is displayed. See? From the inside out, you have the elements listed:

Chrome

That was Chrome.

This is how it’s displayed in Firefox:

Firefox

Here you can see how the browser tells me the properties of an element I highlighted. I right-clicked an element, clicked “Inspect” or “Inspect Element”, and went to the “Computed” panel of the DevTools (Chrome) or “Layout” panel of the DevTools (Firefox).

PreviousTypographyNextThe display property

Last updated 1 year ago

Was this helpful?

Chrome
Firefox