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 11. CSS: Styling Text and Images

text-align

text-align

Aligning Text Horizontally with text-align in CSS

The text-align property is used to set the horizontal position of the text in the element or relative to the parent element. It is also used to set it for the <img> element. As the <img> element is an inline element, you can adjust its position using this property.

Keywords for text-align

There are four main keywords for property values.

  • left: This is the default setting. With or without specifying it, the text is positioned at the left edge of the element or its parent element.
  • center: The text is positioned in the center of the element or its parent element.
  • right: The text is positioned on the right edge of the element or its parent element.
  • justify: This is only applicable when the text has multi-lines. With this setting, the text is still positioned on the left edge of the element but each word is spread from the left end to the right end, except the last line. It looks like a newspaper.

Padding and text-align

If there is padding in the element, the left and right edges are defined after padding like in the illustration below.

text-align within the element

Nesting Structure (text-align from the parent)

The text-align property is also used in the nesting structure. You can control the position of the child element from the parent element like in the illustration below.

text-align relative to the parent element (nesting structure)

There are two remarks to make this work:

  1. The child element has to be an inline element (if it is a block element, you need to use a different approach such as margin: auto or the align-items property with the display: flex property. We'll explain them later in this course.
  2. Set the CSS property for the parent element.

As the <img> element is an inline element, you can also use this technique for the <img> element.

Inheritance

The text-align property is inherited. For example, when you set text-align: right for the grandparent element, its grandchild element (if it's an inline element) is positioned at the right edge of the parent element like in the illustration below.

text align inheritance

Practice 1

Objective:
Test how text-align works within the same element

1. Update the body section of the HTML file

Add the code below at the end of the <body> section in the chapter11.html file. In this code, we are testing two types of text:

  • Short text (fits within one line)
  • Multi-line text

We are using the outline class set from the previous practice to show the result clearly.

chapter11.html
<h2>Text Align</h2>
<h3>Text Align within the Element</h3>
<p style="border:1px dotted #26BCCE;">Text Align: Original</p>
<p style="border:1px dotted #26BCCE">
  "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
</p>
<p class="outline" style="text-align: left; ">Text Align: Left</p>
<p class="outline" style="text-align: left; ">
  "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
</p>
<p class="outline" style="text-align: right; ">Text Align: Right</p>
<p class="outline" style="text-align: right; ">
  "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
</p>
<p class="outline" style="text-align: center; ">Text Align: Center</p>
<p class="outline" style="text-align: center; ">
  "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
</p>
<p class="outline" style="text-align: justify; ">Text Align: Justify</p>
<p class="outline" style="text-align: justify; ">
  "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
</p>

2. Check the result with a browser

  • Open chapter11.html with a browser.

  • You can see how the text-align property works.

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

link

Practice 2

Objective:
Test how text-align works in a nesting structure

1. Update the body section of the HTML file

Add the code below at the end of the <body> section in the chapter11.html file. In this code, we are adding the parent-box and child-box classes to contrast the parent element and child element.

chapter11.html
<h3> Text Align Relative to the Parent Element</h3>
<div class="parent-box">
  <a class="child-box">original position</a>
</div>
<div class="parent-box" style="text-align: left;">
  <a class="child-box">text-align:left</a>
</div>
<div class="parent-box" style="text-align: center;">
  <a class="child-box" style="text-align: right;">text-align:center</a>
</div>
<div class="parent-box" style="text-align: right;">
  <a class="child-box" style="text-align: right;">text-align:right</a>
</div>

2. Update the custom CSS file

Open the practice.css file and add new code for adding styles to the parent-box and child-box classes.

practice.css
.parent-box{
  background-color: #55D0DC;
  margin:20px;
  width:300px;
  border:5px dotted #26BCCE;
  padding:30px;
  box-sizing: border-box;
}

.child-box{
  background-color: #E0F7F9;
  border-style: solid;
  border-color:#B3EBEF;
  border-width:5px;
}

3. Check the result with a browser

  • Open chapter11.html with a browser.
  • You can see how the text-align property works under the nesting structure.

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

link

Practice 3

Objective:
Check how text-align inheritance works

1. Update the body section of the HTML file

Add the code below at the end of the <body> section in the chapter11.html file.

chapter11.html
<h3> Text Align Inheritance</h3>
<div class="parent-box" style="width:300px; text-align: right;">
  <div class="child-box">
    <span>ABC</span>
  </div>
</div>
<hr>

2. Check the result with a browser

  • Open chapter11.html with a browser.
  • You can see that the text-align property set at the ground parent level works for the grandchild level.

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

link


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Blend Modes Explained: Creating Blend Mode CSS Code with AI

Blend Modes Explained: Creating Blend Mode CSS Code with AI

Chapter 2. Figma Basic Tools

Chapter 2. Figma Basic Tools

Chapter 4. Wireframing with Figma

Chapter 4. Wireframing with Figma

Blend Modes Explained: Creating Blend Mode CSS Code with AI

Blend Modes Explained: Creating Blend Mode CSS Code with AI

Chapter 2. Figma Basic Tools

Chapter 2. Figma Basic Tools

Chapter 4. Wireframing with Figma

Chapter 4. Wireframing with Figma

Tags:

Alignment

Text Styling

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;