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-size

background-size

Controlling Background Image Size in CSS

The background-size is used for setting the size of the background image. Using the property, you can avoid the problems explained in the previous topic.

Default value

The default value of the background-size property is auto which returns the original size of the image. You can check the size of the image in the file information. Below is an example of a user interface in Mac OS. You can check the image pixel size using Preview. When you double-click on the image file, you can see a preview of the image. There is an information icon on top. By pressing the icon, you can see the image's width and height. In this case, the image size is 6720x4480 pixels. 6720 is the width of the image and 4480 is the height of the image.

Checking image size on Mac

Lengths (sizes)

You can use several lengths. If you want to specify absolute sizes, you can use px. If you want to specify sizes relative to the parent element, you can use %. If you want to set a certain ratio of the browser window size you can use vw or vh. If you want to align the image size with font sizes, you can use rem or em.

When you specify both width and height, you need to specify width first. You can specify width or height only as well.

  • When you want to specify width only, you need just one value.
  • If you want to specify height only and keep the same aspect ratio, type auto before the height value.

Here are some examples:

Width and Height

background-size: 400px 300px;

Width only (height is automatically defined while keeping the same aspect ratio)

background-size: 400px;

Height only (width is automatically defined while keeping the same aspect ratio)

background-size: auto 400px;

 

IdeaAspect Ratio

Aspect Ratio is often used to define display screen size or image size. For example, 4:3 or 16:9 are frequently used aspect ratios. The first number defines width and the second number defines height.

Aspect ratio

 

Cover and Contain

For the background-size property, the keywords of cover and contain are often used.

cover

The cover keyword adjusts the background image size to cover the entire element that the background-size property is set for. When the element has both width and height values and its aspect ratio is different from the image's aspect ratio, a part of the image won't be displayed.

For example, if the image is vertically longer, the image width will be aligned with the width of the element while the bottom part of the image can be cut like in the example below. In this case, the result becomes the same as the result when you set 100% for the width of the background size.

background-size property: cover case 1

If the image is horizontally longer, the image height will be aligned with the height of the element while the right part of the image can be cut like in the example below. In this case, the result becomes the same as the result when you set 100% for the height of the background size.

background-size property: cover case 2

 

contain

The contain keyword adjusts the background image size to fully display the image. When the element has both width and height values and its aspect ratio is different from the image's aspect ratio, a blank space can be created, or repeated images can be shown.

Here are two examples when background-repeat: no-repeat is set for the element. (We'll explain the background-repeat property in the next topic.)

For example, if the image is vertically longer, the image height will be aligned with the height of the element while a blank space will appear on the right side like in the example below. In this case, the result becomes the same as the result when you set 100% for the height of the background size.

background-size property: contain case 1

If the image is horizontally longer, the image width will be aligned with the width of the element while a blank space will appear at the bottom part like in the example below. In this case, the result becomes the same as the result when you set 100% for the width of the background size.

background-size property: contain case 2

 

Practice 1

Objective:
Set a background image for the body element and adjust the image size

 

1. Prepare an image file

For this practice, we are using a photo from Unsplash. Find a monochrome color photo as it is used for the background. Save the file under the img directory under the project directory.

2. Update the CSS file

Open the practice.css file and add new code that allows to add the background-image and background-size properties to the body element. You can use any image you prepared. Set the background-size at 100vw, so that the image fits the width of the browser window.

practice.css
body{
  background-color: white;
  color: #196060;
  font-family: roboto, sans-serif;
  background-image: url(../img/monotone_bg.jpg);
  background-size: 100vw;
}

3. Check the result with a browser

  • Open chapter12.html with a browser.
  • You can confirm that the background image is set properly.
background size: 100vw

4. Comment out the properties

As we don't want to keep the background image for the following practices, comment out the background-image and background-size properties.

body{
  background-color: white;
  color: #196060;
  font-family: roboto, sans-serif;
/* background-image: url(../img/monotone_bg.jpg); */
/* background-size: 100vw; */
}

 

Practice 2

Objective:
Set a background image for the elements with width and height

 

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 add the bg-img-frame class to define the size of the image frame. We also set the bg-image-1 and bg-image-2 classes for two different background images.

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 without Size Setting</h2>
<div style="display: flex; flex-wrap: wrap;">
  <div class="bg-img-frame bg-image-1"><span style="vertical-align: bottom;"></span></div>
  <div class="bg-img-frame bg-image-2"><span style="vertical-align: bottom;"></span></div>
</div>
<hr>
<h2>Background Image and Size</h2>
<div style="display: flex; flex-wrap: wrap;">
  <div class="bg-img-frame bg-image-1" style="background-size: cover;">Cover</div>
  <div class="bg-img-frame bg-image-1" style="background-size: contain;">Contain</div>
  <div class="bg-img-frame bg-image-1" style="background-size: 100%;">Width 100%</div>
  <div class="bg-img-frame bg-image-1" style="background-size: auto 100%;">Height 100%</div>
  <div class="bg-img-frame bg-image-1" style="background-size: 50%;">Width 50%</div>
</div>

<div style="display: flex; flex-wrap: wrap;">
  <div class="bg-img-frame bg-image-2" style="background-size: cover;">Cover</div>
  <div class="bg-img-frame bg-image-2" style="background-size: contain;">Contain</div>
  <div class="bg-img-frame bg-image-2" style="background-size: 100%;">Width 100%</div>
  <div class="bg-img-frame bg-image-2" style="background-size: auto 100%;">Height 100%</div>
  <div class="bg-img-frame bg-image-2" style="background-size: auto 50%;">Height 50%</div>
</div>
<hr>

2. Prepare image files

We are using two images – one is a vertically long image and the other is a horizontally long image. Save the two image files under the img directory of the project.

3. Update the CSS file

Open the practice.css file and add the new code to add styles to the image frame (250x250 and other properties) and file paths of image files. We haven't explained some properties yet. For now, just copy and paste the code into your file.

practice.css
.bg-img-frame{
  width: 250px;
  height: 250px;
  margin: 10px;
  padding: 10px;
  border:2px solid seagreen;
  background-repeat: no-repeat;
  box-sizing: border-box;
  font-weight: bold;
}

.bg-image-1{
  background-image:url(../img/icecream_400x600.png);
}

.bg-image-2{
  background-image:url(../img/sandwich_600x400.png);
}

4. Check the result with a browser

  • Open chapter12.html with a browser.
  • You can see how different image size settings work.

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

link


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Advanced CSS Selectors

Advanced CSS Selectors

Translate() Function in CSS: Repositioning HTML Elements

Translate() Function in CSS: Repositioning HTML Elements

Using display: none to Hide Elements in CSS

display: none

CSS Scroll-Snap

CSS Scroll-Snap

Advanced CSS Selectors

Advanced CSS Selectors

Translate() Function in CSS: Repositioning HTML Elements

Translate() Function in CSS: Repositioning HTML Elements

Using display: none to Hide Elements in CSS

display: none

CSS Scroll-Snap

CSS Scroll-Snap

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;