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 12. CSS: Styling Backgrounds

background-position

background-position

Positioning Background Images with CSS

The background-position property is used for setting the position of the background image.

Position with %

You can specify position using %. Specify x-axis position first followed by y-axis position. The default position is 0% 0%.

background-position with %

Position with keywords

You can also specify position using keywords:

  • To specify x-axis position use left, center or right
  • To specify y-axis position use top, center or bottom
background-position with keyword

Practice

Objective:
Test different position settings using % and keywords

1. Update the body section of the HTML file

Add the code below at the end of the body section in the chapter12.html file. In this code, We are using the classes already defined in the previous practices. Create two sets of code – one is with % and the other is with keywords.

In the code below, we are wrapping each set of frames using the <div> element with the display and flex-wrap properties to structure the results. We'll explain these properties in detail later. For now, just copy and paste the code.

chapter12.html
<h2>Background Image Position</h2>
<div style="display: flex; flex-wrap: wrap;">
  <div class="bg-img-frame bg-image-3" style="background-size: 25%; background-position: 0% 0%;">Position 0% 0%</div>
  <div class="bg-img-frame bg-image-3" style="background-size: 25%; background-position: 50% 0%;">Position 50% 0%</div>
  <div class="bg-img-frame bg-image-3" style="background-size: 25%; background-position: 100% 0%;">Position 100% 0%</div>
</div>

<div style="display: flex; flex-wrap: wrap;">
  <div class="bg-img-frame bg-image-3" style="background-size: 25%; background-position: 0% 50%;">Position 0% 50%</div>
  <div class="bg-img-frame bg-image-3" style="background-size: 25%; background-position: 50% 50%;">Position 50% 50%</div>
  <div class="bg-img-frame bg-image-3" style="background-size: 25%; background-position: 100% 50%;">Position 100% 50%</div>
</div>

<div style="display: flex; flex-wrap: wrap;">
  <div class="bg-img-frame bg-image-3" style="background-size: 25%; background-position: 0% 100%;">Position 0% 100%</div>
  <div class="bg-img-frame bg-image-3" style="background-size: 25%; background-position: 50% 100%;">Position 50% 100%</div>
  <div class="bg-img-frame bg-image-3" style="background-size: 25%; background-position: 100% 100%;">Position 100% 100%</div>
</div>

<h2>Background Image Position (Keyword)</h2>
<div style="display: flex; flex-wrap: wrap;">
  <div class="bg-img-frame bg-image-3" style="background-size: 25%; background-position: left top;">Position left top</div>
  <div class="bg-img-frame bg-image-3" style="background-size: 25%; background-position: center top;">Position center top</div>
  <div class="bg-img-frame bg-image-3" style="background-size: 25%; background-position: right top;">Position right top</div>
</div>

<div style="display: flex; flex-wrap: wrap;">
  <div class="bg-img-frame bg-image-3" style="background-size: 25%; background-position: left center;">Position left center</div>
  <div class="bg-img-frame bg-image-3" style="background-size: 25%; background-position: center center;">Position center center</div>
  <div class="bg-img-frame bg-image-3" style="background-size: 25%; background-position: right center;">Position right center</div>
</div>

<div style="display: flex; flex-wrap: wrap;">
  <div class="bg-img-frame bg-image-3" style="background-size: 25%; background-position: left bottom;">Position left bottom</div>
  <div class="bg-img-frame bg-image-3" style="background-size: 25%; background-position: center bottom;">Position center bottom</div>
  <div class="bg-img-frame bg-image-3" style="background-size: 25%; background-position: right bottom;">Position right bottom</div>
</div>
<hr>

2. Check the result with a browser

  • Open chapter12.html with a browser.
  • You will see the results that are the same as the examples in the main section.

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

link


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Essential Steps for Publishing Websites

Key Steps to Publish Websites

Aligning Flex Items Effortlessly with align-items

align-items

Exploring Browser Developer Tools for CSS

Browser Developer Tools for CSS

Essential Steps for Publishing Websites

Key Steps to Publish Websites

Aligning Flex Items Effortlessly with align-items

align-items

Exploring Browser Developer Tools for CSS

Browser Developer Tools for CSS

Tags:

Image

Alignment

Background

Background Image

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;