Cara menggunakan fade in keyframes css

Cara menggunakan fade in keyframes css

As you create web pages, sometimes you will find it necessary to add some effects such as CSS fade out to your text or images. When done correctly, fade-out animation can increase engagement on your website. Animation is one of the ways, websites attract the attention of visitors. Read on to find out how you can use fade-out effects in CSS.

Table of Contents

  • How To Use Fade Out CSS
  • How the Fade Transition Works
  • – How To Fade Out Images in CSS
  • – How To Fade Out Text in CSS
  • – How To Add Fade Out Effect on Hover
  • – How To Fade Out Background
  • – How To Create a Beautiful Fade Out Text Effect
  • How do I fade my page in CSS?
  • How do you make a fade in effect on page load?
  • What is Fade in fadeOut effect?
  • How do you animate on load?

Contents

  • How To Use Fade Out CSS
  • How the Fade Transition Works
    • – How To Fade Out Images in CSS
    • – How To Fade Out Text in CSS
    • – How To Add Fade Out Effect on Hover
    • – How To Fade Out Background
    • – How To Create a Beautiful Fade Out Text Effect
  • Conclusion

How To Use Fade Out CSS

Fade transition is one of the most common and effective forms of animation. By using this subtle transition, the animation will have a lasting impression on your site’s visitors. A fade-out transition lets you set a way of letting text or images gradually disappear on your website.

You can use fade-out animation in several cases, and these include:

  • Fade out image animation
  • Fade out text animation
  • Fade out on hover animation
  • Fade out background

Using fade-out animation on your site can have a powerful impact. Fortunately, CSS makes it easy to implement this effect to improve the general outlook of your website.

How the Fade Transition Works

The fade transition in CSS is a stylistic effect that lets elements such as background, image, or text gradually disappear or appear on a web page. To apply a fade-out effect on an element, you need to use either the animation or transition property in CSS.

Using the transition property, CSS lets you specify the initial and final state, for instance, you can let the contents of a div transition from black to white. However, if you want to make the content transition from black to white to blue, you will need the animation property.

In CSS, transitions require a trigger such as a hover while animations do not. Normally, animations automatically initiate their sequence once the page loads. Nonetheless, you can delay the start of animation through the animation-delay property.

– How To Fade Out Images in CSS

You can use fade-out animation on images. In this case, you can make an image go from visible to transparent. To accomplish this, you need to use the CSS opacity property. Through CSS opacity, you can define how transparent or opaque an element is.

The values for the opacity property vary between zero and one, where one indicates the element is completely visible, whereas zero indicates the element is completely transparent. When you combine this with either the transition or opacity property, you can make an element fade out over a set period.

Example

Suppose you want to add a div element to your web page with an image fade-out effect. Say, you give the div element a “fade-out-img” id name. Here is how to use image fade out CSS to make the image in the div fade out every time the web page loads.

#fade-out-img
{
Width: 100%;
height: 10%;
background-image: url(https://images.unsplash.com/photo-1640622658353-c6cecbe91488?ixlib=rb-1.2.1&ixid=MnwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80);
background-repeat: no-repeat;
background-size: cover;
background-position: center right;
background-color: orange;
font-size: 2em;
text-align: center;
position: absolute;
animation: fadeOut 8s;
-webkit-animation: fadeOut 8s;
-moz-animation: fadeOut 8s;
-o-animation: fadeOut 8s;
-ms-animation: fadeOut 8s;
}
@keyframes fadeOut {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@-webkit-keyframes fadeOut {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@-moz-keyframes fadeOut {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@-o-keyframes fadeOut {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@-ms-keyframes fadeOut {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}

In the example above, you will notice the use of vendor-prefix properties like -ms, -o, -webkit, and -moz. The properties ensure your fade-out animation code works across different browsers. Using the -webkit vendor prefix you ensure the fade out animation CSS works across Safari and Chrome browsers.

– How To Fade Out Text in CSS

Using the same properties as in the previous example, you can make text fade out.

Example

Now consider you want to fade out text in a div whose id name is “fade-out”. You can accomplish this like so in CSS:

#fade-out
{
Width: 100%;
height: 10%;
color: orange;
font-size: 2em;
text-align: center;
position: absolute;
animation: fadeOut 8s;
-webkit-animation: fadeOut 8s;
-moz-animation: fadeOut 8s;
-o-animation: fadeOut 8s;
-ms-animation: fadeOut 8s;
}
@keyframes fadeOut {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@-webkit-keyframes fadeOut {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@-moz-keyframes fadeOut {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@-o-keyframes fadeOut {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
@-ms-keyframes fadeOut {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}

– How To Add Fade Out Effect on Hover

Having in mind, you can add a fade-out animation to an element on hover. The animation can be applied to text or images.

Example

Suppose you want to make an image fade out from being 100% visible to 50% visible in 3 seconds when a cursor hovers above it. Say the image is in a div tag whose id name is “fade-out” the example below is how you can accomplish this in CSS:

#fade-out
{
Width: 100%;
height: 100%;
background-image: url(https://images.unsplash.com/photo-1601524909162-ae8725290836?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTN8fGVsZWN0cm9uaWNzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=700&q=60);
background-position: bottom left;
background-size: cover;
position: absolute;
opacity: 100%;
}
#fade-out:hover
{
opacity: 50%;
transition: 3s;
}

– How To Fade Out Background

With CSS, you can make the background color or image of your webpage fade out using the animation property. You can use the body element to style and fade out the background.

Example

Consider adding a fade to the background color of the body tag from orange to grey every 4 seconds for an infinite number of times. You can accomplish this like so:

body
{
background: orange;
animation: fadebackground 4s infinite;
}
@keyframes fadebackground
{
from {background-color: orange;}
to {background-color: grey;}
}

– How To Create a Beautiful Fade Out Text Effect

With a little creativity, you can create a unique fade-out animation on text. You can use the letter-spacing property to create a CSS slide animation which makes the text seem to fade out by spacing letters.

Example – Adding Fade Out Effect to Space Text

Suppose you want to create a web page with a background image and text. You may also want to fade out the text on hover and make it expand. Here is how you can use CSS to accomplish this:

body
{
background: url(https://images.unsplash.com/photo-1588508065123-287b28e013da?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1374&q=80);
background-size: 100%;
background-repeat: no-repeat;
}
h2
{
bottom: 50%;
left: 50%;
position: absolute;
transform: translate(-50%, -50%);
padding: 0;
margin: 0;
font-size: 50px;
font-weight: bold;
font-family: arial, serif;
color: orange;
text-shadow: 2px 2px white;
transition-duration: 2s;
-webkit-background-clip: text;
}
h2:hover
{
opacity: 0.5;
letter-spacing: 4px;
}

Example – Making Text Transparent on Hover

Suppose you now want to make the text transparent such that it uses the background image of the h2 element to fill it. In other words, you will be cropping the image into the text. The example below shows how you can let CSS animate fade out to make the text color change from orange to the background image.

body
{
background-image: url(https://images.unsplash.com/photo-1588508065123-287b28e013da?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1374&q=80);
background-size: 100%;
background-repeat: no-repeat;
}
body h2
{
background: url(https://images.unsplash.com/photo-1588508065123-287b28e013da?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1374&q=80);
background-size: fill;
background-repeat: no-repeat;
position: absolute;
bottom: 50%;
left: 50%;
transform: translate(-50%, -50%);
margin: 0;
padding: 0;
font-size: 70px;
font-weight: bold;
font-family: Arial, serif;
text-shadow: 2px 2px;
transition-duration: 2s;
background-clip: text;
-webkit-background-clip: text;
text-align: center;
color: orange;
}
h2:hover
{
color: transparent;
letter-spacing: 4px;
}

In both examples, you will notice the use of the body tag to specify the margin, background size, and background image. To position the text, the examples use left, position, and bottom properties. With the transform property, CSS lets you define 2-dimensional or 3-dimensional transformation.

Also, you will notice the use of padding and margin properties on the text, as well, choosing the font and font size, these examples use the font-family and font-size properties. Thanks to the transition-duration property, CSS lets you determine how long the text animation will take.

Lastly, the background-clip property lets you paint the text using the background using the “text” value. The examples apply the :hover pseudo-class in CSS to style text on hover and set the letter-spacing property.

Conclusion

Fade transition is one of the popular forms of animation in CSS. It allows you to fade out images or text by gradually making them disappear. Here is a quick recap of what you covered in this guide:

  • CSS provides an easy way of implementing fade-out animation
  • To accomplish you can use either animation or transition property
  • Usually, fade-out transitions require a trigger like a hover event
  • Animation on the other hand normally starts automatically when a web page loads
  • Combining the opacity property with the animation or transition property lets you define the fade animation

With this knowledge, you can now use fade-out animation to improve engagement and tell a meaningful story on your web page.
  • Author
  • Recent Posts

Position Is Everything: Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL.

How do I fade my page in CSS?

In the CSS, use the @keyframes rule paired with fadeIn. At 0%, set the opacity to 0. At 100%, set the opacity to 1. This creates the fade-in effect.

How do you make a fade in effect on page load?

Use animation and transition property to create a fade-in effect on page load using CSS. Method 1: Using CSS animation property: A CSS animation is defined with 2 keyframes. One with the opacity set to 0, the other with the opacity set to 1.

What is Fade in fadeOut effect?

The Fade In/Fade Out behavior lets you dissolve into and out of any object by ramping the opacity of the object from 0 percent to 100 percent at the start, and then back to 0 percent at the end.

How do you animate on load?

Add CSS.

Add @keyframes with the name "slideInLeft" and use 0% and 100% as a starting point and ending point of the animation, respectively. ... .

Use the animation property on the <header> and specify its background and padding..

Style the <body> element by setting the margin to 0 and specifying the font-family..