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

Styling Images

Styling Images

Styling Images in Web Design

The <img> element is a unique element from the styling point of view. It is an inline element; however, in some aspects, it behaves differently from the other inline elements.

Inline Element Aspects

Like other inline elements, by default, the width of the <img> element is determined by the length of the content, and no line break is created between inline elements. If you place multiple images, the images are placed in the same line. If the total width of images exceeds the line length, the images that are not placed within the first line go to the next line.

You can also use CSS properties that are applicable to inline elements such as text-align and vertical-align.

Practice 1

Objective:
Check how images are placed along lines

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 using an image file that we prepared in another practice before, but you can use any image for this. To make the differences clear, we also added <div> (block elements) and <span> (inline elements) using the size-object class defined in the previous chapter.

chapter11.html
<h2>Styling Images</h2>
<h3> Images as an Inline Element</h3>
<img src="img/salad.jpg" width="200px">
<img src="img/salad.jpg" width="200px">
<img src="img/salad.jpg" width="200px">
<img src="img/salad.jpg" width="200px">
<img src="img/salad.jpg" width="200px">
<img src="img/salad.jpg" width="200px">
<img src="img/salad.jpg" width="200px">
<img src="img/salad.jpg" width="200px">
<div class="size-object"> Block Element Example</div>
<div class="size-object"> Block Element Example</div>
<span class="size-object"> Inline Element Example</span>
<span class="size-object"> Inline Element Example</span>
<span class="size-object"> Inline Element Example</span>
<span class="size-object"> Inline Element Example</span>
<span class="size-object"> Inline Element Example</span>
<span class="size-object"> Inline Element Example</span>

2. Check the result with a browser

  • Open chapter11.html with a browser.
  • You can confirm that images are behaving as inline elements.

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

link

Practice 2

Objective:
Check how the text-align property works for the <img> 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 using the parent-box class defined in a previous practice.

chapter11.html
<h3> Text Align for Image Element</h3>
<div class="parent-box" style="text-align: left;">
  <img src="img/salad.jpg" height="50px">
</div>
<div class="parent-box" style="text-align: center;">
  <img src="img/salad.jpg" height="50px">
</div>
<div class="parent-box" style="text-align: right;">
  <img src="img/salad.jpg" height="50px">
</div>

2. Check the result with a browser

  • Open chapter11.html with a browser.
  • You can confirm that text-align works for the <img> element.

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

link

Sizing and Spacing

The major differences between the <img> element and other inline elements are sizing and spacing. You can set width and height for the <img> element. Also, you can set padding and margin both horizontally and vertically.

Practice 3

Objective:
Check how sizing and spacing work for the <img> tag

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 practice, we are using two new cases: 1) one image with paddings and margins, and 2) multiple images with paddings and margins. We are also adding <br> tags to avoid object overlaps.

chapter11.html
<h3> Adding Paddings and Margins to Image</h3>
<div style="background-color: #55D0DC; text-align: center; width: 300px; margin: 5px;">
  <img src="img/salad.jpg" style="height:50px; margin:20px; padding: 15px; background-color: #B3EBEF;">
</div>
<div style="background-color: #55D0DC; text-align: center; width: 300px; margin: 5px;">
  <span style="height:50px; margin:20px; padding: 15px; background-color: #B3EBEF;">Inline Element</span>
</div>

<h3> Multiple Images with Paddings and Margins</h3>
<img src="img/salad.jpg" style="height: 50px; margin: 20px; padding: 20px; background-color: #B3EBEF;">
<img src="img/salad.jpg" style="height: 50px; margin: 20px; padding: 20px; background-color: #B3EBEF;">
<img src="img/salad.jpg" style="height: 50px; margin: 20px; padding: 20px; background-color: #B3EBEF;">
<img src="img/salad.jpg" style="height: 50px; margin: 20px; padding: 20px; background-color: #B3EBEF;">
<img src="img/salad.jpg" style="height: 50px; margin: 20px; padding: 20px; background-color: #B3EBEF;">
<img src="img/salad.jpg" style="height: 50px; margin: 20px; padding: 20px; background-color: #B3EBEF;">
<img src="img/salad.jpg" style="height: 50px; margin: 20px; padding: 20px; background-color: #B3EBEF;">
<img src="img/salad.jpg" style="height: 50px; margin: 20px; padding: 20px; background-color: #B3EBEF;">
<br>
<br>
<span style="height: 50px; padding: 20px; margin: 20px; border: 1px dotted #B3EBEF; background-color: #55D0DC;">Inline Element</span>
<span style="height: 50px; padding: 20px; margin: 20px; border: 1px dotted #B3EBEF; background-color: #55D0DC;">Inline Element</span>
<span style="height: 50px; padding: 20px; margin: 20px; border: 1px dotted #B3EBEF; background-color: #55D0DC;">Inline Element</span>
<span style="height: 50px; padding: 20px; margin: 20px; border: 1px dotted #B3EBEF; background-color: #55D0DC;">Inline Element</span>
<span style="height: 50px; padding: 20px; margin: 20px; border: 1px dotted #B3EBEF; background-color: #55D0DC;">Inline Element</span>
<span style="height: 50px; padding: 20px; margin: 20px; border: 1px dotted #B3EBEF; background-color: #55D0DC;">Inline Element</span>
<span style="height: 50px; padding: 20px; margin: 20px; border: 1px dotted #B3EBEF; background-color: #55D0DC;">Inline Element</span>
<span style="height: 50px; padding: 20px; margin: 20px; border: 1px dotted #B3EBEF; background-color: #55D0DC;">Inline Element</span>
<br>
<br>
<br>
<hr>

2. Check the result with a browser

  • Open chapter11.html with a browser.
  • First, you can confirm that you can properly set height, width, padding, and margin both horizontally and vertically, while the <span> element (an inline element) is not properly styled vertically.
  • Second, height, width, padding and margin also properly work for multiple <img> elements while they do not work properly for <span> elements.

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

Idea<img> and background image

You can also display images as background images, which will be explained in the next chapter. The ways to handle images are different. When you use background images, usually block elements are used as you cannot set height for inline elements. Using background images, you can easily overlay text.


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Aligning Flex Items Seamlessly with justify-content

justify-content

Publishing Websites Using Various Platforms

Chapter 19. Publishing Websites

Exploring List Styling Properties in CSS

List Styling Properties

Understanding CSS Length Units: px, em, rem, %, vw, vh

Length

Aligning Flex Items Seamlessly with justify-content

justify-content

Publishing Websites Using Various Platforms

Chapter 19. Publishing Websites

Exploring List Styling Properties in CSS

List Styling Properties

Understanding CSS Length Units: px, em, rem, %, vw, vh

Length

Tags:

Image

Inline Element

Block Element

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;