CSS & Images: How to Add, Centre & Resize - Code Institute Global (2024)

HTML, CSS, and Javascript are three essential computer languages in web development that influence the website’s aesthetics. This is general knowledge, and almost everyone knows about it.

If HTML is about giving the website’s framework, then CSS is the language that concentrates on adding features to the underlying framework, allowing the website to become more visually appealing.

This is why, through CSS, we are going to look at the different ways we can add, style, and align images on a website.

How to Add an Image in CSS?

Before we look into these topics, we have to get the facts straight.

First, the job of CSS is to provide an enhancing design to the structure of the website. So we’ll be adding the image through HTML and not CSS.

So to add images, we use the <img> inline element. Through this HTML element, we will only be able to add the image, source, and link. Then with the help of CSS, we will mention the proportions and other needed styles of the image.

Before jumping into examples, let’s look at some properties which influence the shape, size, and other altercations of images:

  • First, the border property is used to specify the image’s border.
  • Next, the height property is used to specify the image’s height.
  • Finally, the width property is used to specify the image’s width.

Example:

In HTML:

img src="https://tinyurl.com/dkxknyfv" alt="A black, brown, and white dog">

In CSS:

img {display: block;max-width: 100%;}

In the above example, we have used HTML to add images to the website and use CSS to set display options on how the website should appear.

How to Centre and Image in CSS?

Images can be easily centred with the help of left-margin and right-margin properties. Here is an example that vividly explains how we can centre images on a web page using CSS.

Example:

In HTML:

<img src= "https://tinyurl.com/dkxknyfv"alt="centred-image"style="width:50%">

In CSS:

img {display: block;margin-left: auto;margin-right: auto;}

How to Resize an Image in CSS?

The issue with HTML is that when images are added, it usually takes up more pixels of the webpage. Therefore, for a responsive web design, the images on a website should be able to resize on their own depending on the screen size. This, fortunately, can be achieved through the following properties:

  • The max-width and max-height Properties

The max-width and max-height properties will automatically resize the images depending on the screen size displayed on the site.

Example:

In HTML:

<div class="dog"><img src="https://tinyurl.com/dkxknyfv" /></div>

In CSS:

img {max-width: 100%;max-height: 100%;}.dog {height: 200px;width: 200px;}
  • The object-fit Property

The object-fit property is another property used to resize the website’s image automatically. The object-fit property will resize the image using the contain value according to the screen requirements.

Example:

In HTML:

<div class="dog"><img src="https://tinyurl.com/dkxknyfv" /></div>

In CSS:

img {height: 100%;width: 100%;object-fit: contain;}.dog {height:300px;width: 300px;}

How to Align an Image in CSS?

Whether it is left alignment or right alignment, or even centre alignment, it is possible, and images can be aligned within a matter of seconds in CSS through the text-align and float properties:

  • Using the text-align Property

Using the text-align property can easily align images on a website. With the text-align property and values such as left, centre, and right, one can appropriately align images on a website.

Example:

In HTML:

<div id ="center"><img src="https://tinyurl.com/dkxknyfv" style="width:50%"></div>

In CSS:

#center {text-align: center;}
  • Using float property

The float property is yet another property used in aligning images on a website.

Example:

In HTML:

<div class="row"><div class="column"><img src="https://tinyurl.com/dkxknyfv" alt="Snow" style="width:100%"></div>

In CSS:

.column {float: right;width: 33.33%;padding: 5px;}

How to Add a Background Image in CSS?

A visually enhanced website sets beautiful minimalistic images as background images. Fortunately, CSS has the background-image property to set beautiful images as background images for websites.

Example:

In CSS:

body {background-image: url("https://tinyurl.com/dkxknyfv");background-color: #cccccc;}

Conclusion

Adding, altering, and aligning images to change a plain-looking website into a visually appealing one takes only a few minutes with CSS. We hope you learned something new about front-end development to improve the appearance of websites.

Learn CSS basics for free

If you want to learn some of the basics of CSS for free, try our 5 Day Coding Challenge. After an hour a day over five days, you will learn the basics ofHTML,CSSandJavaScript. Register now through the form below.

CSS & Images: How to Add, Centre & Resize - Code Institute Global (2024)
Top Articles
TheMealDB.com
Parent Involvement and Family Engagement
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Cvs Sport Physicals
Mercedes W204 Belt Diagram
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Selly Medaline
Latest Posts
Article information

Author: Nathanial Hackett

Last Updated:

Views: 6127

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Nathanial Hackett

Birthday: 1997-10-09

Address: Apt. 935 264 Abshire Canyon, South Nerissachester, NM 01800

Phone: +9752624861224

Job: Forward Technology Assistant

Hobby: Listening to music, Shopping, Vacation, Baton twirling, Flower arranging, Blacksmithing, Do it yourself

Introduction: My name is Nathanial Hackett, I am a lovely, curious, smiling, lively, thoughtful, courageous, lively person who loves writing and wants to share my knowledge and understanding with you.