Marcy Lab School Docs
  • Welcome
  • Student Guidelines & Policies
    • Student Handbook
    • AI Policy
    • Academic Calendar
  • Environment Setup
    • Local Environment Setup - Mac
    • Local Environment Setup - Windows
    • GitHub Setup
    • Postgres Setup
  • How-Tos
    • How To Code at Marcy: Code Style Guide
    • How to Do Short Response and Coding Assignments
    • How to Debug
    • How to PEDAC
    • How to Create A GitHub Organization and Scrumboard
    • How to Create Projects with Vite
    • How to Deploy on GitHub Pages
    • How to Deploy on Render
    • How to Test your API with Postman
  • Mod 0 - Command Line Interfaces, Git, and GitHub
    • Overview
    • 1. Command Line Interfaces
    • 2. Git & GitHub
    • 3. Git Pulling & Merging
    • 4. Git Branching & PRs
  • Mod 1 - JavaScript Fundamentals
    • Overview
    • 1. Intro to Programming
    • 2. Errors
    • 3. Node & Node Modules
    • 4. Variables, Functions & String Methods
    • 5. Control Flow, typeof, and Math
    • 6. Loops
    • 7. Arrays
    • 8. Objects
    • 9. Higher Order Functions: Callbacks
    • 10. Higher Order Functions: Array Methods
    • 11. Regex
  • Mod 2 - HTML, CSS & the DOM
    • Overview
    • 1. HTML
    • 2. CSS
    • 3. Accessibility (a11y)
    • 4. The Document Object Model (DOM) API
    • 5. Events
    • 6. Forms
    • 7. The Box Model and Positioning
    • 8. Flexbox
    • 9. Grid & Media Queries
    • 10. ESModules
    • 11. Vite
    • 12. LocalStorage
  • Mod 3 - Async & APIs
    • Overview
    • 1. Promises
    • 2. Fetch
    • 3. Building a Fetching App
    • 4. Async & Await
    • 5. A Generic Fetch Handler
  • Mod 4 - Project Week!
    • Important How Tos and Guides
      • How to Create a GitHub Organization and Scrum Board
      • How To Start a Project with Vite
      • How To Deploy a Project with GitHub Pages
    • Project Week Overview
    • Agile Methodologies
    • Deliverables & Milestones
    • Technical Requirements Checklist
    • Free API List
    • Collaborative GitHub
  • Mod 5 - Object-Oriented Programming
    • Overview
    • 1. Intro to OOP, Encapsulation, Factory Functions, and Closure
    • 2. Classes
    • 3. Private & Static
    • 4. UML Diagrams & Has Many/Belongs To Relationships
    • 5. Challenge: Implementing Has Many/Belongs To
    • 6. Inheritance
    • 7. Polymorphism
    • 8. Review and Practice
    • MDN: Object Prototypes
  • Mod 6 - Data Structures & Algorithms
    • Overview
    • Important How Tos and Guides
      • How to Debug
      • How to PEDAC
    • 1. Nodes & Linked Lists
    • 2. Singly & Doubly Linked Lists
    • 3. Stacks & Queues
    • 4. Recursion
    • 5. Trees
  • Mod 7 - React
    • Overview
    • Important How Tos and Guides
      • How to Create Projects with Vite
      • How to Deploy on GitHub Pages
    • 1. Intro to React
    • 2. Events, State, and Forms
    • 3. Fetching with useEffect
    • 4. React Router
    • 5. Building a Flashcards App
    • 6. React Context
    • 7. Global Context Pattern
  • Mod 8 - Backend
    • Overview
    • Important How Tos and Guides
      • How to Deploy on Render
      • How to Test your API with Postman
      • Postgres Setup
    • 1. Intro to Express
    • 2. Building a Static Web Server with Middleware
    • 3. Securing API Keys and Environment Variables
    • 4. RESTful CRUD API
    • 5. Model-View-Controller Architecture
    • 6. SQL and Databases
    • 7. JOIN (Association) SQL Queries
    • 8. Knex
    • 9. Your First Fullstack App!
    • 10. Migrations & Seeds
    • 11. Schema Design & Normalization
    • 12. Hashing Passwords with Bcrypt
    • 13. React Express Auth Template Overview
  • Mod 9 - Civic Tech Hackathon
    • Overview
    • Rubric
  • Mod 10 - Capstone
    • Overview
Powered by GitBook
On this page
  • Key Terms
  • Web Design: Everything is a Box
  • Box Model
  • Sizing is Based on the Content Box
  • Box Model Defaults and a CSS Reset
  • Display and Position
  • Display
  • Position
  • Test your Skills!
  1. Mod 2 - HTML, CSS & the DOM

7. The Box Model and Positioning

Previous6. FormsNext8. Flexbox

Last updated 6 months ago

Follow along with code examples !

Table of Contents

Key Terms

  • Box Model — How elements in an HTML document are modeled in the browser and how their dimensions are calculated based on the provided CSS properties. It consists of content, padding, border, and margin.

  • The content — the space that the content of an element occupies (the text of a <p> tag, the image of an img tag, etc...)

    • Content can be modified with the width and height properties.

  • The padding — the space around the content but inside the border. This is the "background" of the content.

    • Padding can be modified with the padding property.

  • The border — the space around the padding.

    • Border can be modified with the border and border-radius properties.

  • The margin — the space around the border and "between" adjacent elements.

    • Margin can be modified with the margin property.

    • Note: Margin is always transparent and the margins of adjacent elements can overlap.

  • The box-sizing property determines how the total width and height of an element is calculated.

    • The default box-sizing: content-box setting has the width and height properties affect the content box, with padding and border dimensions added.

    • The box-sizing:border-box setting makes the width and height properties affect the border box, with the content box automatically shrinking to fit inside the padding box.

  • A CSS Reset is a set of rules intended to remove default CSS styles applied by the user agent stylesheet.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
  • The display property changes how elements are arranged relative to each other:

    • display: block elements will stack on top of each other, regardless of their width.

    • display: inline elements will sit next to each other (if there is space available) and are not affected by the width or height properties

    • display: inline-block elements will sit next to each other (if there is space available) and ARE affected by the width and height properties

    • display: none elements are removed from the flow of the document.

Web Design: Everything is a Box

You are a new web developer and you want to learn how to make pretty websites, right?

The first rule to understanding web design is that in an HTML website, everything is a box.

This becomes immediately clear when you add this CSS rule to a page:

/* All elements have a 1px solid red border */
* {
  border: 1px solid red;
}

Knowing that everything is a box, we must start by learning how to control those boxes.

Box Model

The box model defines the size and spacing of any element and it consists of 4 parts:

  • The content — the space that the content of an element occupies (the text of a <p> tag, the image of an img tag, etc...)

    • Content can be modified with the width and height properties.

  • The padding — the space around the content but inside the border. This is the "background" of the content.

    • Padding can be modified with the padding property.

  • The border — the space around the padding.

    • Border can be modified with the border and border-radius properties.

  • The margin — the space around the border and "between" adjacent elements.

    • Margin can be modified with the margin property.

    • Note: Margin is always transparent and the margins of adjacent elements can overlap.

For example, consider the following styles applied to p elements:

p {
  /* Adding a background helps us see the different parts of the box model */
  background: gold;

  /* Box Model Properties */
  width: 100px;
  height: 50px;
  padding: 5px;
  border: 4px solid purple;
  margin: 10px;
}

Resulting in the output below:

Tip: View your styles using the developer tools! Clicking on an element in the Elements tab will pull up the CSS applied to that element in the Styles sub-tab.

Sizing is Based on the Content Box

An important detail to note is that the width and height properties affect the content box. As a result, by default, the total space that an element occupies is not the same as its width and height.

Question: What is the total width and height of the paragraph elements in the example above (excluding the margins)?

The total width excluding the margins is 100 + 10 + 8 or 118 (the sum of the content, the padding, and the border).

The total height excluding the margins is 50 + 10 + 8 or 68 (the sum of the content, the padding, and the border).

Now, Imagine you had a screen that was 1000px wide and you needed to fit 5 elements perfectly inside that space. However, each element includes 10px of padding and a 2px border.

Question: What would the width property need to be?

The answer is 176px. We take 1000px and divide it by 5 to get 200px for each element. Then, we need to also subtract the padding and border from both sides (200px minus 20px of padding minus 4px of border).

We can greatly simplify this by changing the box-sizing of an element:

p {
  background: gold;

  box-sizing: border-box;
  /* Now, width and height are the total width and height */
  width: 100px;
  height: 50px;
  padding: 5px;
  border: 4px solid purple;
  margin: 10px;

}

The box-sizing: border-box style makes it so that the width and height properties include the padding and border.

As a result, the size of the content box shrinks so that the total width and height of the "border box" matches the width and height properties.

Learn more here: https://css-tricks.com/box-sizing/

Question: What would the width property need to be with box-sizing: border-box?

If we set box-sizing: border-box the answer is 200px!

Box Model Defaults and a CSS Reset

Consider the website below which can be found in the 1-box-model-basics folder.

You can see the HTML structure contains some common HTML elements like h1, h2, p, div, ul, and li. To help see the where each element's box begins and ends, we've added background colors to each element and a border to all elements:

In the Styles panel of our developer tools, the browser has applied some default styles via the user agent stylesheet. For example, the h1 element has these styles:

/* User Agent Stylesheet */
h1 {
    display: block;
    font-size: 2em;
    margin-block-start: 0.67em;
    margin-block-end: 0.67em;
    margin-inline-start: 0px;
    margin-inline-end: 0px;
    font-weight: bold;
    unicode-bidi: isolate;
}

As you can see if you poke around in the example, most elements have some default margin and padding styles which can conflict with any styling that we want to apply.

To address this, it is common to define a CSS Reset rule that eliminates those default styles:

* {
  /* Remove the default margin and padding applied to many elements, giving us full control over spacing */
  margin: 0px;
  padding: 0px;

  /* Makes `width` apply to the border box (padding and content are included) */
  box-sizing: border-box;
}

ul {
  list-style: none;
}

In addition to resetting the margin and padding for all elements to 0px, we change the box-sizing property to border-box.

I also like to remove bullets from ul elements.

Display and Position

Display

  • Notice how the div and p tags each get a new line, but the a and img tags are in the same line?

  • That's because their display is different:

Display Type
New Line?
Accepts Height / Width?
When To Use
Default Elements

display: block;

Yes!

Yes!

When you want things on a new line.

Most things!

display: inline

No

No (but accepts horizontal padding/margin)

For inserting an element within an existing line of text content.

span, b, i, a

display: inline-block

No

Yes!

Buttons in a navigation bar!

None

display: none

N/A

N/A

To completely remove an element from view.

None

See what happens when you assign each type of display to div an a and an img tag!

Position

We can also directly control positioning of our elements on the page through "position." This is a very powerful, but very finicky tool.

  • position: static

    • Elements render in normal document flow and no offset values can move statically positioned elements

  • position: relative

    • Elements are positioned relative to their normal position

    • Offset properties like top, right, bottom, left, etc. can move elements from their original spot

    • Other content flows around the original position

  • position: absolute

    • Elements removed from normal document flow, and other elements will be able to fill that space

    • Positioned relative to nearest positioned ancestor.

      • This is important, as if the parent isn't absolute or relative then the child will be positioned relative to the entire body

      • I've included a parent div for the two rectangles exactly for this reason, so experiment with adding/removing relative to i

  • position: fixed

    • Behaves like absolute positioning, except its positioned to the viewport

  • position: sticky

    • this will behave like relative until you scroll past it, then it will stick on the top bottom or sides

    • Finicky, be careful about parents blocking it

Test your Skills!

Use this to play around with it.

Head over to !

interactive documentation on MDN
MDN to test your box model skills
here
Key Terms
Web Design: Everything is a Box
Box Model
Sizing is Based on the Content Box
Box Model Defaults and a CSS Reset
Display and Position
Display
Position
Test your Skills!
The box model has 4 parts: the content, the padding, the border, and the margin
The index.html file in the 1-box-model-basics folder