Formatting Images using CSS

Introduction
Images on a website serve various functions, ranging from conveying information to improving aesthetics. Images that are properly formatted have a significant influence on the overall user experience.
Cascading Style Sheets (CSS) play a crucial role in web development because they provide an efficient way to control HTML document presentation and layout.
When it comes to improving a website's visual appeal, images frequently serve as a significant component. CSS has several tools and properties that enable developers to format and style images to achieve a polished and unified design.
CSS also allows developers to modify aspects such as size, alignment, borders, and more. Ensuring that images blend in with the overall look of the web page.
Basics of CSS for Images
Let's start with the fundamentals of using CSS to format images on your web page.
Linking CSS to HTML
To apply CSS styles to your images, you need to link your CSS file to your HTML document. You can do this by including the following line in the <head> section of your HTML file:
<link rel="stylesheet" type="href="styles.css">
Make sure that styles.css is replaced by the actual name of your CSS file.
Selecting Images
CSS works by selecting HTML elements to apply styles. To select an image, the HTML element, which is usually the <img> tag can be used.
Selecting all images on a page can be done with the universal selector or by targeting specific images with classes or IDs.
To select all images on a page, use the following:
img {
/* insert styles here */
}
To select a specific image with class use:
.img-class {
/* insert styles here */
}
To select a specific image with an ID
#img-id {
/* insert styles here */
}
Create an HTML file and paste the code block below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style7.css" />
</head>
<body>
<div class="container">
<h1><b>Light Bulb</b></h1>
<div class="img-container">
<img
class="img-bulb"
src="https://images.unsplash.com/photo-1493612276216-ee3925520721?q=80&w=1000&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8Mnx8cmFuZG9tfGVufDB8fDB8fHww"
alt="random pictures"
/>
</div>
</div>
</body>
</html>
This HTML structure sets up a webpage with a title and an image within containers. The styling can be defined in a linked CSS stylesheet labeled as style7.css.
Result

For this article, the name of the CSS file is styles7.css
* {
margin: 0;
padding: 0;
overflow: hidden;
}
.container {
margin: 0 auto;
width: 80%;
height: 400px;
min-height: 100vh;
background-color: gray;
}
The provided CSS code contains styles to reset default margins and padding and style a container element.
The code is intended to ensure that the container's appearance is consistent across a variety of devices and platforms, while also providing users with an optimal viewing experience.
Changing Image Size
Adjusting the size of images is a commonly performed task in image formatting. The parameters width and height can be utilized for modifying the dimensions of an image.
It is important to note that resizing images can have an impact on their quality and resolution, which can be reduced by using the proper tools and approaches.
.img-bulb {
width: 200px;
height: 200px;
object-fit: cover;
}
The code snippet alters the image's dimensions by changing both the width and height attributes to 200px. This adjustment guarantees that the image fits perfectly within the container.
The object-fit: cover; property resizes the content to fit within the height and width.
Result

In the example above, the cover value is used to adjust the content proportionally to completely cover the provided dimensions while potentially cropping some areas if necessary.
Aligning Images
To achieve optimal image alignment on web pages, a variety of CSS techniques may be employed,employed, depending on the unique positioning and style requirements of the webpage.
The code snippet below creates a class called,.container which has a set of properties.
One of these properties margin: 0 auto;sets the top and bottom margins of the element to 0 and the left and right margins to 1auto. This effectively centers the element horizontally within its container element.
The min-height: 100vh; sets the element's minimum height to 100% of its viewport height. This ensures that the element is at least equal to the viewport height.
Thedisplay: flex; enables the element to be displayed inflexbox mode.
The direction of flexible items inside a flex container can be set using the flex-direction property. When set, the main axis becomes vertical, resulting in a column layout.
.container {
margin: 0 auto;
width: 80%;
height: 400px;
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: center;
background-color: gray;
}
The align-items: center; flex property centers elements horizontally in a column layout along the cross-axis, as shown in the image below column layout along the cross-axis, as shown in the image below.
Thebackground-color: gray; sets the background color of the element to,gray as shown in the image below.
Result

Image Backgrounds
The use of CSS allows for background images, expanding design options for more engaging web content. This feature can be used to improve the visual quality of web pages and applications.
The code below expands on the previous .container class and adds more features, such as gap: 1rem;. This property defines the spacing between child elements of the flex container. In this case, the space between elements is set to,1rem which is a measurement unit equivalent to the root element's font size.
The background-image property enables the setting of a background image to the container. In this case, background image of the container uses a specified imageURL from Unplash and the image URL includes formatting options such as quality and width.
.container {
margin: 0 auto;
width: 80%;
height: 400px;
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: center;
gap: 1rem;
background-image: url("https://images.unsplash.com/photo-1493612276216-ee3925520721?q=80&w=1000&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8Mnx8cmFuZG9tfGVufDB8fDB8fHww");
background-size: cover;
background-position: top;
}
The background-size: cover; specifies that the background image should cover the entire container, maintaining its aspect ratio and potentially cropping parts of the image.
The background-position: top; in this case, aligns the top edge of the background image with the top edge of the container. We can see this in the image below.
Result

Utilizing a combination of CSS techniques to format images on a webpage can result in visually pleasing and well-structured web pages.
By applying a variety of these techniques, a webpage can be optimized to present images in an aesthetically pleasing and user-friendly way. As a result, the webpage becomes more engaging and provides a better user experience.
As you get more familiar with CSS, you can experiment with more advanced image formatting and customization techniques.
Image Styling and Scaling with CSS
The ability to control the size and scaling of images on your webpage is critical for maintaining a consistent and visually appealing layout. This ensures a seamless user experience and maintains the overall appearance of the webpage.
CSS provides several options for adjusting image dimensions and ensuring that they display correctly on a variety of devices and screen sizes.
Setting Image Dimensions
The width and height attributes of CSS allow you to directly set the width and height of images. For example:
.img-bulb {
width: 200px;
height: 300px;
object-fit: cover;
}
This code block sets the image's height to 300px and its width to,200px which will constrain the image to a rectangle area, as seen in the result below.
When specifying these fixed dimensions, the object-fit: cover; property ensures that the image covers the specified dimensions without distorting its aspect ratio.
Result

Maintaining Aspect Ratio
In this context, to maintain the aspect ratio of the image, the width is set at 300px and the height is set at auto.
This height: auto; allows the height of the element to automatically adjusted to maintain the aspect ratio of the image. This guarantees that the image scales proportionally and that the height is adjusted appropriately. For example:
.img-bulb {
width: 300px;
height: auto;
object-fit: cover;
}
Result

The result above shows that the image is constrained to a width of 300 pixels and due to the height: auto; setting, the image's height is adjusted automatically to retain its aspect ratio.
Responsive Image Sizing
To ensure that an image is responsive, one should adjust the value of its width property. The image's height will then be automatically adjusted accordingly.
It is also recommended to use relative units, such as percentages, for better results.
Responsive image sizing allows images to adjust themselves to different screen sizes, making them more suitable for different devices.
The code block below sets the image's width to 100% of its containing element. This means that the image will take up the full width of its container.
.img-bulb {
width: 100%;
height: auto;
}
The height: auto; in this case, allows the height of the element to adjust automatically, maintaining the aspect ratio of the image.
Result

Scaling Images using Transform
The transform property is a powerful tool that enables you to carry out various modifications to the components on your page, such as scaling, rotation, and translation.
The transform property is used to apply a scaling transformation to the element.
In this example, it's specifically used to scale the size of the element in the .img-bulb class.
.img-bulb {
transform: scale(1.9);
}
In this case, transform: scale(1.9); scales the element by 1.9 in both horizontal (x-axis) and vertical (y-axis) directions. This signifies that the element will be increased to 1.9 times its original size, as shown in the image below.
Result

Alignment and Positioning of Images
It's essential to have a polished and professional look for your web page by accurately aligning and positioning images inside the layout. CSS provides several techniques to achieve this. Let's explore these approaches:
Centering Images Horizontally
To center an image horizontally, you need to use the display: block; property. This property sets the element's display behavior to block, which means that the element will create a block-level box that will start on a new line and use up the entire width available.
The margin: 0 auto; sets the top and bottom margins to 0 and the left and right margins to 1auto.
.container {
margin: 0 auto;
width: 80%;
height: 400px;
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: center;
background-color: gray;
}
.img-bulb {
display: block;
margin: 0 auto;
}
Result

This technique is commonly used to center a block-level element horizontally inside its container, as shown in the image above.
By using the auto the margin on both the left and right edges, any leftover space is distributed equally, effectively centering the element.
Centering Images Vertically
Achieving a well-designed web page requires mastering the art of centering images vertically.
.container {
margin: 0 auto;
width: 80%;
height: 400px;
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: center;
background-color: gray;
}
.img-bulb {
position: relative;
top: 50%;
transform: translateY(-30%);
}
The code block above centers the image vertically with the use of these properties:
The position: relative; property creates a positional context for the element. Elements with the position: relative; attributes are positioned relative to their regular place in the document flow.
The top: 50%; sets the element's top offset to 50% of its containing element's height. However, this alone would cause the element's top to be positioned in the center of its container.
The transform: translateY(-30%); uses the transform property to translate the element vertically. It moves the element by 30% of its height. The negative percentage is used to change the position relative to the element's dimensions.
The combination of top: 50%; and transform: translateY(-30%); is commonly used to vertically center an element within its container. The top: 50%; moves the element's top to the vertical center of its container, as seen in the image below and then transform: translateY(-30%); raises it back up by 30% of its original height.
Result

Floating Images
When you use CSS to create floating images, it is possible to achieve seamless integration into your webpage. This can help to improve the overall aesthetic appeal and produce a more engaging user experience.
The float: right; property is used to move an element to the right within its containing element.
.img-bulb {
float: right;
}
Result

When an element is floated to the right, it is positioned on the right side of its parent element. The content that follows flows around the element to the left, resulting in a wrapping effect.
Styling Images
By using CSS, you can successfully improve the images by providing a variety of styles that improve their visual appeal and seamlessly integrate them into the overall design of your website.
CSS gives you the tools needed to create amazing visuals that capture your audience, whether it's basic color changes or complex designs.
Here are a few methods for using CSS to style images:
Borders
The border property is a typical method for providing a visual outline of elements on a webpage. The utilization of this feature can contribute to an overall improved user experience.
.img-bulb {
border: 3px solid #333;
}
The code block above shows that the border: 3px solid #333; creates a border width of 3 pixels , a border style of solid and a border color of,#333, which is a dark shade of gray. This is shown in the image below.
Result

Rounded Corners
The use of thisborder-radius property to achieve rounded images is a well-established method. This styling technique is used to achieve a visually appealing and polished look.
By adding this feature, rounded corners can be applied to any image, enhancing its aesthetic appeal.
The higher the value of the squareborder-radius, the more pronounced the rounding of the corners will be.
.img-bulb {
border-radius: 50px;
}
Result

Theborder-radius: 50px; property setsborder-radius of the element create rounded corners, as we can see in the image above.
In this case, the value is set to, which means that each corner of the element will have a circular curve with a radius of 50 pixels.
Shadows
An image gains elevation when it appears to cast a shadow, which adds depth to the page.
To achieve this feature, the box-shadow property is applied.
The box-shadow property allows the image to cast a shadow, lending a three-dimensional quality to the page, which in turn enhances the visual appeal of the image.
.img-bulb {
box-shadow: 3px 3px 5px #370caf;
}
In the code above, the box-shadow: 3px 3px 5px #370caf; property applies a box-shadow to the element.
The values inside the box-shadow properly represent the horizontal offset (3px), vertical offset (3px), blur radius (5px), and color (#370caf)- a shade of purple.
Result

This creates a subtle shadow effect to give the image a lifted appearance from the background. Values can be adjusted according to design preferences.
Opacity
One built-in function for applying a filter to an image and adjusting its transparency is called opacity. The opacity of an image can be controlled using the opacity property.
This opacity property is a simple and effective way to control the transparency of elements and create layered visual effects that enhance user interface design.
The opacity: 0.3; property in the code below controls the transparency of an element and in this case, it is set to 0.3, which means the element will be 30% transparent.
.img-bulb {
opacity: 0.3;
}
This will cause the element's content to appear slightly transparent, as shown in the image below.
Result

Filters Effects
You can modify the appearance of images through the use of the filter property, which enables visual effects and transformations such as color, brightness, contrast, blur, and other modifications.
Let us break down these filter: sepia(50%) blur(2px) brightness(80%); effects individually:
The sepia(50%) applies a sepia tone to the element, giving it a brownish color. The 50% value represents the intensity of the sepia effect.
The blur(2px) adds a blur effect to the element. In this case, the blur radius is set to 2 pixels, so the element's content will be slightly blurred.
The brightness(80%) adjusts the element's brightness. The 80% value indicates that the element's brightness has been lowered, causing it to appear darker.
.img-bulb {
filter: sepia(50%) blur(2px) brightness(80%);
}
Combining these filter effects results in a visual effect where the element has a sepia tone, mild blur, and reduced brightness, as shown in the image below.
Result

Image Overlays
Image overlays are a typical method for converting text or images over one another. Image overlays can be a solid color, a gradient, a color block with transparency, or a blur.
Overlays are an excellent way to enhance an image and create an appealing website.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Image Overlay</title>
<style>
body {
margin: auto;
width: 80%;
height: 400px;
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: gray;
}
.container {
position: relative;
width: 300px;
}
.image {
width: 300px;
height: auto;
display: block;
}
.overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(2, 46, 31, 0.5);
display: flex;
justify-content: center;
align-items: center;
color: rgba(238, 231, 231, 0.904);
}
</style>
</head>
<body>
<div class="container">
<img
class="image"
src="https://images.unsplash.com/photo-1493612276216-ee3925520721?q=80&w=1000&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8Mnx8cmFuZG9tfGVufDB8fDB8fHww"
alt="random pictures"
/>
<div class="overlay"><b>Light Bulb</b></div>
</div>
</body>
</html>
Let's break down the properties in the .overlay class:
The position: absolute; sets the element's position relative to its nearest ancestor because it has a relative location; it is positioned relative to the.container element in this instance.
The top: 0; left: 0;: sets the overlay's top-left corner to match the top-left corner of the.container element.
The width: 100%; height: 100%; sets the width and height of the overlay to fill the complete dimensions of its contained element(.container), ensuring the entire image is covered.
The background: rgba(2, 46, 31, 0.5); specifies the overlay's background color. It employs the RGBA color model, with the first three values representing the red, green, and blue color channels and the fourth value(0.5) representing the alpha channel, which controls transparency. In this case, it produces a semi-transparent green background, as can be seen in the image below.
Thisdisplay: flex; enables theflexbox layout of the overlay.
The justify-content: center; and align-items: center; these flexbox properties horizontally and vertically center the content (text) within the overlay.
The color: rgba(238, 231, 231, 0.904); sets the text color using the RGB color model. The text is a light shade of gray with some transparency.
Result

These styles provide a full-width and full-height overlay with a semi-transparent green background and centered text in a light gray color.
Creating Image Hover Effects
The hover the effect causes an image to respond when the mouse hovers over it and it is one of the best methods for quickly and easily building a polished website.
Your website can benefit from interactive elements and eye-catching imagehover effects. The following CSS methods can be used to create image hover effects:
Zoom in on Hover
When an image is hovered over, it can be dynamically enlarged, resulting in a visually engaging effect.
By utilizing this technique, developers can create a more interactive and engaging experience for their audience. As such, it is an essential tool for modern-day digital designers and content creators.
In this code block below, the opacity: 1; sets the element's opacity to 1 when hovered over. This signifies that the element becomes completely opaque.
The transform: scale(1.8); on the other hand, applies a scaling change that increases the element's size by a factor of 1.8 when the element is hovered over. This gives the element a larger appearance.
.img-bulb:hover {
opacity: 1;
transform: scale(1.8);
transition: transform 0.7s;
}
The transition: transform 0.7s; sets a smooth transition effect for the transform property over 0.7 seconds.
Result

This GIF demonstrates a smooth and gradual scaling transition when the user hovers over the element for a set time.
Image Fade-In:
Afade in transition is a style effect that gradually shows or hides an image on a page. To achieve this effect, you can use the opacity attribute.
Thefade-in transition is a popular web design method for adding visual appeal and creating a more engaging user experience.
In the code below, opacity: 0; sets the initial opacity of elements with the class .img-bulb to 0, making them completely transparent.
The transition: opacity 0.3s; creates a seamless transition effect for the opacity property over 0.3 seconds. This implies that any changes to theopacity property will be animated over the specified time.
The img:hover selects allimg elements when hovered over.
The opacity: 1; sets the opacity of the hoveredimg elements to 1, making them fully opaque.
When an element with the class.img-bulb is hovered over, its opacity is set to 0, and it gradually becomes visible with a seamless transition. The transition duration is set at 0.3 seconds. This produces a fade-in effect for components in the.img-bulb class when the user interacts with them.
.img-bulb {
opacity: 0;
transition: opacity 0.3s;
}
img:hover {
opacity: 1;
}
Result

This GIF demonstrates a gradual and smooth fade-in effect when hovering over the element.
Image Flip Effect
The transform property in CSS can be used to produce an image flip effect by employing the rotateY transformation.
This technique is excellent for developing dynamic and visually engaging web page content.
The rotateY transformation allows web developers to modify the image along the y-axis and create the illusion of a flipped image.
This code below creates a 3D rotation effect for images within an element with the class .img-container when hovered over.
Let's break down these properties individually:
The perspective: 1000px; determines the perspective for child elements of the.img-container. This feature specifies the distance, in pixels, of the 3D space from the user. Its current setting is 1000 pixels.
The transition: transform 0.7s; applies a smooth transition effect to the transform property for 0.7 seconds. It is important to note that this rule also applies to any child img elements within the .img-container.
Ittransform-style: preserve-3d; specifies that the transformations applied to the child components of the .img-container should be maintained in a 3D space.
The transform: rotateY(180deg); is used to rotate an image around the Y-axis by 180 degrees, creating a card-flip effect when the container is hovered.
.img-container {
perspective: 1000px;
}
.img-container img {
transition: transform 0.7s;
transform-style: preserve-3d;
}
.img-container:hover img {
transform: rotateY(180deg);
}
Hovering over the .img container triggers a 3D flip effect where the image rotates along the Y-axis, resulting in a smooth transition. The container'sperspective property affects the depth and perspective of the 3D transformation.
Result

Conclusion
CSS is a powerful tool that enables you to format images on your website in different ways. With CSS, you have complete control over your visual effects, allowing you to resize images, add borders, create flexible designs, and apply filters.
By experimenting with these approaches, you can enhance the appearance of your web pages. It's important to find a balance between aesthetics and usability to achieve effective image formatting.
Finally, combining CSS skills with a creative eye for design will allow developers to create attractive image layouts that captivate viewers and contribute to the overall success of a web project.
Image reference: Unsplash
About the author
A professional technical writer with over two years of experience encompasses an ability and proficiency in producing technical documents such as manuals, guides, and handbooks for software systems and technical products. I have extensive professional experience in various industries, which include IT, finance, and telecommunication. As a technical writer, I can simplify technical content to a point where one can easily understand it, especially for the development groups and the users. I possess good knowledge and experience in the most typical tools and technologies for documentation and creating the best quality technical documents.
Comments
Join the discussionNo comments yet. Be the first to comment.


