CSS blur
CSS (Cascading Style Sheets) is a powerful language used to format the layout and design of web pages. One of the many properties CSS offers is the ability to create a blur effect on elements, adding a sense of depth and focus to the webpage. In this article, we will explore the CSS blur property in detail and provide code examples to demonstrate its functionality.
What is CSS blur?
The CSS blur property allows developers to apply a blur effect to an element and its contents. The blur effect softens the edges of an element, making it appear out of focus. This effect can be used to enhance the visual appeal of a webpage or draw attention to a specific area of the content.
CSS blur syntax and usage
The CSS blur property can be applied to both text and images. It accepts a value that specifies the amount of blur to be applied. The value can be defined in pixels, percentage, or the unit rem
.
Here is the general syntax for the blur property:
selector {
blur: value;
}
Let’s dive into some examples to grasp a better understanding of the CSS blur property.
Example 1: Blur a Text Element
<!DOCTYPE html>
<html>
<head>
<style>
.blur-text {
blur: 10px;
}
</style>
</head>
<body>
<h1 class="blur-text">Hello, World!</h1>
</body>
</html>
The code above applies a blur effect of 10 pixels to the text inside the “Hello, World!” <h1>
element. The result will be a blurred headline.
Example 2: Blur an Image
<!DOCTYPE html>
<html>
<head>
<style>
.blur-image {
blur: 5px;
}
</style>
</head>
<body>
<img src="image.jpg" class="blur-image" alt="Blurred Image" />
</body>
</html>
In this example, the blur-image
class is used to apply a 5-pixel blur effect to an image with the filename “image.jpg”. The result will be a blurred image on the webpage.
Example 3: Applying a Blur to an Element on Hover
<!DOCTYPE html>
<html>
<head>
<style>
.blur-on-hover {
transition: blur 0.5s;
}
.blur-on-hover:hover {
blur: 8px;
}
</style>
</head>
<body>
<p class="blur-on-hover">Hover over this text to see the blur effect!</p>
</body>
</html>
In this example, the .blur-on-hoverclass applies a transition effect with a duration of 0.5 seconds to smoothly transition the blur effect. When the text is hovered over, the
:hover` pseudo-class triggers the blur of 8 pixels to create the desired effect.
Example 4: Blur Multiple Elements
<!DOCTYPE html>
<html>
<head>
<style>
.blur-element {
blur: 5px;
}
</style>
</head>
<body>
<h1 class="blur-element">Heading 1</h1>
<p class="blur-element">Paragraph 1</p>
<img src="image1.jpg" class="blur-element" alt="Blurred Image 1" />
<img src="image2.jpg" class="blur-element" alt="Blurred Image 2" />
</body>
</html>
In this example, the .blur-element
class is applied to multiple elements, including headings, paragraphs, and images. All these elements will have a 5-pixel blur effect applied to them.
Example 5: Nesting Blur Effects
<!DOCTYPE html>
<html>
<head>
<style>
.blur-element {
blur: 5px;
}
.blur-container {
blur: 0; /* Reset the blur effect of the container */
}
</style>
</head>
<body>
<div class="blur-container blur-element">
<h1>Heading in a Blurred Container</h1>
<p>Text inside a container with a blurred background.</p>
</div>
</body>
</html>
In this example, the .blur-element
class is applied to both the container div and its nested elements. By setting the blur value of the container to 0, the nested elements will have a relative blur effect applied to them.
Example 6: Blur an Element’s Background Image
<!DOCTYPE html>
<html>
<head>
<style>
.blur-background {
background-image: url("image.jpg");
background-position: center;
background-size: cover;
height: 400px;
width: 100%;
blur: 5px;
}
</style>
</head>
<body>
<div class="blur-background">
<h1>Blurred Background</h1>
<p>This element has a blurred background image.</p>
</div>
</body>
</html>
In this example, the .blur-background
class applies a background image to a div element while also using the blur property to create a 5-pixel blur effect on the background image. The result is a div with a blurred background image.
Example 7: Blur Only the Background of an Element
<!DOCTYPE html>
<html>
<head>
<style>
.blur-only-background {
background-color: rgba(0, 0, 0, 0.5);
position: relative;
z-index: 0;
}
.blur-only-background::before {
content: "";
background-image: url("image.jpg");
background-position: center;
background-size: cover;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: -1;
blur: 5px;
}
</style>
</head>
<body>
<div class="blur-only-background">
<h1>Blurred Background Only</h1>
<p>This element has a blurred background only.</p>
</div>
</body>
</html>
In this example, the .blur-only-background
class is applied to a div element to create a semi-transparent background color. Using the ::before
pseudo-element and a negative z-index, a blurred background image is placed underneath the div. This technique allows only the background image to have a blur effect.
Example 8: Creating Multiple Blur Layers
<!DOCTYPE html>
<html>
<head>
<style>
.blur-layer {
width: 400px;
height: 300px;
position: relative;
}
.blur-layer::before,
.blur-layer::after {
content: "";
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
backdrop-filter: blur(10px);
}
.blur-layer::before {
background-color: rgba(0, 0, 0, 0.5);
z-index: 1;
}
.blur-layer::after {
background-color: rgba(255, 255, 255, 0.1);
z-index: 2;
}
.blur-layer-content {
position: relative;
z-index: 3;
color: white;
}
</style>
</head>
<body>
<div class="blur-layer">
<div class="blur-layer-content">
<h1>Multiple Blur Layers</h1>
<p>This element has two blur layers.</p>
</div>
</div>
</body>
</html>
In this example, the .blur-layer
class is used to create a container div with two blur layers. The first blur layer is created using the ::before
pseudo-element with a background color and a z-index of 1. The second blur layer is created using the ::after
pseudo-element with a different background color and a higher z-index of 2to place it above the first layer. The backdrop-filter
property is used to apply the blur effect to the layers. The text content is placed within a separate div with a higher z-index of 3 to ensure it is not affected by the blur.
Example 9: Transitioning Blur Effect
<!DOCTYPE html>
<html>
<head>
<style>
.blur-transition {
transition: blur 0.5s;
}
.blur-transition:hover {
blur: 8px;
}
</style>
</head>
<body>
<div class="blur-transition">
<h1>Blur with Transition</h1>
<p>Hover over this element to see the blur effect with a transition.</p>
</div>
</body>
</html>
In this example, the .blur-transition
class is used to create a div element that transitions the blur effect when hovered over. The transition
property defines the duration of the transition, and the :hover
pseudo-class triggers the toggle of the blur effect on hover.
Example 10: Applying Blur to a Background Video
<!DOCTYPE html>
<html>
<head>
<style>
.blur-video {
position: relative;
width: 100%;
height: 0;
padding-bottom: 56.25%; /* 16:9 aspect ratio */
overflow: hidden;
}
.blur-video::before {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-image: url("video-thumbnail.jpg");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
filter: blur(10px);
}
.blur-video video {
position: absolute;
height: 100%;
width: 100%;
left: 0;
top: 0;
object-fit: cover;
filter: blur(0); /* Reset the blur effect on the video */
}
</style>
</head>
<body>
<div class="blur-video">
<video src="video.mp4" autoplay muted loop></video>
</div>
</body>
</html>
In this example, the .blur-video
class is applied to a div element that acts as a container for a background video. The padding-bottom
property is used to maintain the aspect ratio of 16:9 for the video container. The ::before
pseudo-element is used to display a blurred video thumbnail as the background. The actual video is placed inside the container using the <video>
element and is set to cover the container. By resetting the blur effect on the video using filter: blur(0);
, we ensure that the video itself is not blurred.
CSS blur conclusion
The CSS blur property allows developers to create visually appealing effects on webpages by applying a blur effect to elements and their contents. We explored various examples demonstrating the usage of the blur property on text, images, backgrounds, and more. Take your time to experiment with these examples and discover innovative ways to handle blur effects in your web design projects. CSS provides immense possibilities to elevate the aesthetic appeal and user experience of your webpages, and the blur property is just one of the many tools at your disposal.