Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

HTML & CSS IntroductionChapter 10. CSS: Sizing and Spacing

padding

padding

Utilizing Padding in Web Design Layouts

Padding plays an important role in design layout. Without padding, content is always close to its borders, which makes the design look crowded.

Length and keyword

You can specify the padding property size using one of the length units or keywords. The default padding value is 0. Unlike width, height, and margin, padding has no auto keyword.

Length unit

  • px
  • em
  • rem
  • %
  • vw
  • vh

Keyword

  • initial
  • inherit

Paddings for block elements and inline elements

Like the width and height properties, behaviors of padding can differ by type of element. You can set the padding property for block elements horizontally and vertically; however, when you set padding for inline elements vertically, the design may not look good as the padding set vertically for inline elements can create overlaps with other elements.

Practice 1

Objective:
Test paddings for block elements and inline elements

1. Update the body section of the HTML file

Add the code below to the chapter10.html file.

chapter10.html
<h2>Padding</h2>
<div class="size-object">div element: Original</div>
<div class="size-object" style="padding:20px">div element: Padding 10px</div>
<p class="size-object">p element: Original</p>
<p class="size-object" style="padding:20px">p element: Padding 10px</p>
<a class="size-object">a element: Original</a>
<a class="size-object" style="padding:20px">a element: Padding 10px</a>
<span class="size-object">span element: Original</span>
<span class="size-object" style="padding:20px">span element: Padding 10px</span>
<hr>

2. Check the result with a browser

  • Open chapter10.html with a browser.
  • You can see how each element is displayed. From this, you can understand:
    • The default padding is zero.
    • You can add padding for block elements horizontally and vertically; however, vertical padding for inline elements may overlap with another element.

You can also check the sample result here (Demo Site).

link

Paddings for child elements

Paddings are also applied when an element has child elements.

Practice 2

Objective:
Test paddings with child elements

1. Update the body section of the HTML file

Add the code below to the chapter10.html file.

chapter10.html
<h2>Padding: nesting</h2>
<div class="size-object" style="padding:10px">
  <div class="size-child-object">div child element</div>
  <div class="size-child-object">div child element</div>
</div>
<hr>

2. Add CSS to child elements

Open practice.css and add new code to add color and margin to each HTML element.
We haven't explained the background-color, margin, and border properties yet. For now, just copy and paste the code into your file.

practice.css
.size-child-object{
  background-color: #1E838A;
  color:white;
}

3. Check the result with a browser

  • Open chapter10.html with a browser.
  • You can see that paddings are applied to the block element with child elements.

You can also check the sample result here (Demo Site).

link

Paddings with multiple values

You can set different padding sizes for different sides of an element.

1 value (all sides)

When you specify only one padding value, the value is applied to all sides of the element.

2 values (top and bottom vs. right and left)

When you specify two padding values, the first value is applied to the top and bottom padding while the second value is applied to the right and left padding.

3 values (top vs. right and left vs. bottom)

When you specify three padding values, the first value is applied to the top padding, the second value is applied to the right and left padding, and the last value is applied to the bottom padding.

4 values (clockwise order from the top)

When you specify four padding values, the value is applied from the top in the clockwise order (the order of top, right, bottom, and left).

Practice 3

Objective:
Practice paddings with multiple property values

1. Update the body section of the HTML file

Add the code below to the chapter10.html file.

chapter10.html
<h2>Padding: multiple values</h2>
<div class="size-object" style="width:200px;">
  <div class="size-child-object">Before padding</div>
</div>
<div class="size-object" style="width:200px; padding:10px">
  <div class="size-child-object">Padding 10px</div>
</div>
<div class="size-object" style="width:200px; padding:5px 10px">
  <div class="size-child-object">Padding 5px 10px</div>
</div>
<div class="size-object" style="width:200px; padding:5px 10px 1px">
  <div class="size-child-object">Padding 5px 10px 1px</div>
</div>
</div>
<div class="size-object" style="width:200px; padding:5px 10px 1px 2px">
  <div class="size-child-object">Padding 5px 10px 1px 2px</div>
</div>
<hr>

2. Check the result with a browser

  • Open chapter10.html with a browser
  • You can see that values are applied in different ways depending on how many values you set for the padding property.

You can also check the sample result here (Demo Site).

link


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Using float for Text Wrapping Around Images

float: left and right

Styling Components with AI: Buttons, Cards, and More

Styling Components with AI: Buttons, Cards, and More

Fine-tuning List Marker Positioning with CSS

list-style-position

Dark Mode Design: Creating Dark Color Palette in CSS

Dark Mode Design: Creating Dark Color Palette in CSS

Using float for Text Wrapping Around Images

float: left and right

Styling Components with AI: Buttons, Cards, and More

Styling Components with AI: Buttons, Cards, and More

Fine-tuning List Marker Positioning with CSS

list-style-position

Dark Mode Design: Creating Dark Color Palette in CSS

Dark Mode Design: Creating Dark Color Palette in CSS

Tags:

Alignment

Padding

HTML & CSS Introduction
Course Content

Chapter 1. Overview of Website Development

How Websites Work?

Designing, Building and Publishing Websites

Designing Websites

Building Websites – Frontend and Backend Coding

Web App vs. Website

Frontend Coding

Web Framework and CMS

Publishing Websites (Hosting Services)

Chapter 2. Preparing for Website Coding

Two Key Tools to Start Coding Websites

How Browsers Display Web Pages

File Name Rules

Website Directory Structure

Absolute Path vs. Relative Path

Chapter 3. HTML Basics

What Is HTML?

HTML Element

Attribute

HTML Document Structure

Layout Semantics

Heading and Paragraph Tag

Text-Level Semantics

Space, Line-break and Tag in Content

Comments in HTML Document

Chapter 4. HTML: Add Links and Images

Overview of Adding Links and Images

Embed Images – <img>

Image File Format

Add Hyperlinks – <a>

Add Hyperlinks to Images

Add Hyperlinks to Specific Location on Web Page

Link Tag – <link>

Script Tag – <script>

Chapter 5. HTML: Create Lists and Tables

Create Lists

Create Tables

Combine Table Cells

Chapter 6. HTML: Create Forms

Create Forms

Create Text Input Forms and Submit Button

Radio Button and Checkbox

Select Box

Labels

Auto Complete and Disabled

Chapter 7. Bridging HTML and CSS

Block Element vs. Inline Element

Nesting Elements – Parent Elements and Child Elements

Div vs. Span

Global Attribute – Class, ID and Style

Accordion – <Details> and <Summary>

Chapter 8. CSS Basics

What Is CSS?

CSS Syntax

Where To Type CSS?

CSS Basic Selectors

Descendant Selector

Inheritance

Specificity

Reset CSS

Browser Developer Tools for CSS

Comments in CSS Document

Chapter 9. Web Design Basics

Design Element Representation in HTML and CSS

Key Design Points by CSS

Length

Color Code – HEX and RGB

Color Theme

Chapter 10. CSS: Sizing and Spacing

CSS Box Model

width and height

padding

margin

Margin and Padding for Specific Side

margin: auto

box-sizing

Chapter 11. CSS: Styling Text and Images

Text Styling Properties

font-size

color

font-family

Web Font and Google Font

font-weight and font-style

text-decoration

line-height and letter-spacing

text-align

vertical-align

Styling Images

float: left and right

Chapter 12. CSS: Styling Backgrounds

background-color

background-image

background-size

background-repeat

background-position

background-attachment

background (Multiple Properties)

Chapter 13. CSS: Styling Borders and Drawing Lines

border-style

border-color

border-width

border-radius

Border (Multiple Properties)

Borders on Specific Side

Border Radius on Specific Side

Chapter 14. CSS: Layout – Key Concepts and Display Property

Layout Before and After

Layout Key Design Points

Display Property

inline, block and inline-block

display: none

Chapter 15. CSS: Layout – Flex Box

Flex Box – display: flex

flex-direction

Main Axis and Cross Axis

flex-wrap

justify-content

align-items

align-content

align-self

flex-grow

flex-shrink

flex-basis

margin: auto with Flex Box

Inline Flex Box

Nested Flex Box

Chapter 16. CSS: Styling Lists

List Styling Properties

list-style-type

list-style-image

list-style-position

Chapter 17. Creating and Styling Components

Components and Layout

Buttons

Cards

Top Bar

Footer and Bottom Bar

Chapter 18. Completing Website Development

Website Structure Design

Live Server

Home (Landing) Page Development

Main (List) Page Development

Content (Detail) Page Development

Chapter 19. Publishing Websites

Key Steps to Publish Websites

Domain and DNS Server

Favicon

GitHub Pages

Chapter 20. Supplemental Topics

Horizontal Rule – <hr>

Line Break – <br>

Reserved Characters and HTML Entities

Non-breaking Space – &nbsp;

Chapter 1. Overview of Website Development

How Websites Work?

Designing, Building and Publishing Websites

Designing Websites

Building Websites – Frontend and Backend Coding

Web App vs. Website

Frontend Coding

Web Framework and CMS

Publishing Websites (Hosting Services)

Chapter 2. Preparing for Website Coding

Two Key Tools to Start Coding Websites

How Browsers Display Web Pages

File Name Rules

Website Directory Structure

Absolute Path vs. Relative Path

Chapter 3. HTML Basics

What Is HTML?

HTML Element

Attribute

HTML Document Structure

Layout Semantics

Heading and Paragraph Tag

Text-Level Semantics

Space, Line-break and Tag in Content

Comments in HTML Document

Chapter 4. HTML: Add Links and Images

Overview of Adding Links and Images

Embed Images – <img>

Image File Format

Add Hyperlinks – <a>

Add Hyperlinks to Images

Add Hyperlinks to Specific Location on Web Page

Link Tag – <link>

Script Tag – <script>

Chapter 5. HTML: Create Lists and Tables

Create Lists

Create Tables

Combine Table Cells

Chapter 6. HTML: Create Forms

Create Forms

Create Text Input Forms and Submit Button

Radio Button and Checkbox

Select Box

Labels

Auto Complete and Disabled

Chapter 7. Bridging HTML and CSS

Block Element vs. Inline Element

Nesting Elements – Parent Elements and Child Elements

Div vs. Span

Global Attribute – Class, ID and Style

Accordion – <Details> and <Summary>

Chapter 8. CSS Basics

What Is CSS?

CSS Syntax

Where To Type CSS?

CSS Basic Selectors

Descendant Selector

Inheritance

Specificity

Reset CSS

Browser Developer Tools for CSS

Comments in CSS Document

Chapter 9. Web Design Basics

Design Element Representation in HTML and CSS

Key Design Points by CSS

Length

Color Code – HEX and RGB

Color Theme

Chapter 10. CSS: Sizing and Spacing

CSS Box Model

width and height

padding

margin

Margin and Padding for Specific Side

margin: auto

box-sizing

Chapter 11. CSS: Styling Text and Images

Text Styling Properties

font-size

color

font-family

Web Font and Google Font

font-weight and font-style

text-decoration

line-height and letter-spacing

text-align

vertical-align

Styling Images

float: left and right

Chapter 12. CSS: Styling Backgrounds

background-color

background-image

background-size

background-repeat

background-position

background-attachment

background (Multiple Properties)

Chapter 13. CSS: Styling Borders and Drawing Lines

border-style

border-color

border-width

border-radius

Border (Multiple Properties)

Borders on Specific Side

Border Radius on Specific Side

Chapter 14. CSS: Layout – Key Concepts and Display Property

Layout Before and After

Layout Key Design Points

Display Property

inline, block and inline-block

display: none

Chapter 15. CSS: Layout – Flex Box

Flex Box – display: flex

flex-direction

Main Axis and Cross Axis

flex-wrap

justify-content

align-items

align-content

align-self

flex-grow

flex-shrink

flex-basis

margin: auto with Flex Box

Inline Flex Box

Nested Flex Box

Chapter 16. CSS: Styling Lists

List Styling Properties

list-style-type

list-style-image

list-style-position

Chapter 17. Creating and Styling Components

Components and Layout

Buttons

Cards

Top Bar

Footer and Bottom Bar

Chapter 18. Completing Website Development

Website Structure Design

Live Server

Home (Landing) Page Development

Main (List) Page Development

Content (Detail) Page Development

Chapter 19. Publishing Websites

Key Steps to Publish Websites

Domain and DNS Server

Favicon

GitHub Pages

Chapter 20. Supplemental Topics

Horizontal Rule – <hr>

Line Break – <br>

Reserved Characters and HTML Entities

Non-breaking Space – &nbsp;