CSS Image Styling: How to Add, Center, and Resize Images with CSS 

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 of HTMLCSS and JavaScript. Register now through the form below. 

Graduate Story: Ryan

In 2021, Code Institute interviewed many of our students and graduates regarding their time on our Full Stack programme and their career change journey. One such interviewee was Ryan from Shropshire. Tell us about yourself. I am now a developer with Extra Mile Communications in Staffordshire, although I’m actually in Shropshire. I am a career changer. I’m […]

Best Free Coding Bootcamps 2022  

Whenever you look on the internet about coding, you will see articles exaggerating the average time to comprehend and complete coding courses. While practising and mastering coding may take time, learning can be a fun experience. The good news is that there are many free coding bootcamps that can teach you the basics.  The internet […]

Best Way To Make Time To Study

Learning is a continuous process. Learning something new is never too late or too early, and when it comes to programming, the process of learning is simply endless. Every now and then, something new emerges in the tech sector, and to be the perfect programmer, you must be well-versed in the most recent concepts and […]