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 4. HTML: Add Links and Images

Embed Images – <img>

Embed Images – <img>

How to Embed Images Using the <img> Tag

The <img> tag is used to embed images in HTML documents.

Note: Images can be used for the background of web pages but background setting is done by CSS (not by HTML). The way to add background images will be explained later in the CSS section of this course.

Key attributes

The <img> tag has four key attributes:

  1. src (source): Sets the file path of an image file.
  2. alt (alternative information): Adds alternative information when the image file is not properly rendered.
  3. width: Sets the width of the image. You can use px, %, em, rem, or vw to define a size (also known as length) or set it to auto.
  4. height: Sets the height of the image. You can use px, %, em, rem, or vh to define a size (also known as length) or set it to auto.

File Path and Document Structure

There are many ways to store image files as website resources.

The most basic approach is creating the img directory under the project directory. In this case, the file path to the image file should be written as a relative path (refer to Relative Path vs. Absolute Path ).

You can also store image files in external storage, such as AWS S3. In this case, the file path to the image file should be an absolute URL.

Image Size

Each image has an original aspect ratio. If you set both width and height, the image's aspect ratio can be distorted. If you want to maintain the ratio, you should specify only width or height while using auto as the other attribute value. As the default value of height and width is auto anyway, you can also skip the setting.

If you don't specify both width and height, the image size will follow the original pixel size. It can be too large or too small.

IdeaTips: Preparing Image Files

Preparing image files for your website is time-consuming work. To save time, you can use image download services. Before HTML coding, it may be good to download some free images and save them under your project directory so that you can better focus on your HTML coding practice. Here are some websites that provide free images.

Unsplash

This website provides free high-resolution photos.

Unsplash

Freepik

This website covers a large variety of image files including photos and vector images.

Freepik

Practice

Objective:
Embed images in an HTML document

1. Create a new HTML file for this chapter

To create a new file for this chapter, utilize the file for chapter 3. Copy chapter3.html and change the file name to chapter4.html. And, change titles in the head and body sections.

2. Prepare image files

  • Go to the Unsplash website (https://unsplash.com/).
  • Search and download images. We use one photo of salad and one photo of soup in our example.
  • Create the img directory under the project directory.
  • Save the photos under the img directory. We use the following file names in our example.
    • salad.jpg
    • soup.jpg

3. Insert the <img> tags before the menu text: i.e., Salad and Soup

Open the chapter4.html file with VS Code.

In the <body> element (line12 and 18):

  • Add <img> tags after the <h2> start tag.
  • Add src attribute (file path for each image).
  • Set width = "50px".

The lines of code will be like the ones below. The lines highlighted are the updated part.

chapter4.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>4. HTML: Add Links and Images</title>
</head>
<body>
<h1>Chapter 4. HTML: Add Links and Images</h1>

<h1>Today's Menu</h1>
<h2><img src="img/salad.jpg" width="50px">Salad</h2>
<ul>
  <li>Greek Salad</li>
  <li>Avocado Salad</li>
  <li>Tomato Salad</li>
</ul>
<h2><img src="img/soup.jpg" width="50px">Soup</h2>
<ul>
  <li>Minestrone</li>
  <li>Clam Chowder</li>
  <li>Corn Soup</li>
</ul>

</body>
</html>

VS Code is helpful for coding. VS Code has input support functionality to find image files like the one below.

Selecting an image file in VS Code

After editing the file, make sure that you save it (⌘ + S for Mac, Ctrl + S for Windows).

4. Check the result with a browser

Open the chapter4.html file with a browser. You can see that the two images are embedded on the web page. At this stage, the web design does not look nice as CSS is not added to the page yet.

How embedded images are displayed in a web browser

link


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Customizing Buttons with HTML & CSS

Buttons

How Do Browsers Display Webpages

How Browsers Display Web Pages

Understanding HTML Attributes - What are HTML Attributes

Attribute

Understanding Block vs. Inline Elements for Effective Web Design

Block Element vs. Inline Element

Exploring Line Breaks in HTML & CSS

Line Break – <br>

Customizing Buttons with HTML & CSS

Buttons

How Do Browsers Display Webpages

How Browsers Display Web Pages

Understanding HTML Attributes - What are HTML Attributes

Attribute

Understanding Block vs. Inline Elements for Effective Web Design

Block Element vs. Inline Element

Exploring Line Breaks in HTML & CSS

Line Break – <br>

Tags:

Relative Path

Image

Aspect Ratio

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;