This time we will use CSS to draw the avatar of Baymax from Super Marines.
What we do is to use CSS to dynamically change the position of the background image on an element, and add some linear effects.
See the Pen Baymax face - Hero Number 6 by Donovan Hutchinson (@donovanh) on CodePen. The part that shows the face
In order to make the effect cooler, we use radial-gradient on the body to add subtle color gradient changes to make it more like a head.
<div class="baymax"></div>
body { background: radial-gradient(center, #fff, #fff 50%, #aaa); background-size: 100%; background-repeat: no-repeat; height: 100vh; }
The problem is that our element now starts at the midpoint of the container, but not out of the middle.
.baymax { border-bottom: 1.5em solid #000; position: absolute; top: 50%; left: 50%; width: 50%; transform: translate(-50%, -40%); }
In order to offset, we use transform to move the element to the left and up according to 50% of its own width and 40% of its height.
So the position of the mouth is like this
Add eyes
We use before and after pseudo-classes to implement the eyes, which does not require extra HTML , and using CSS entirely is enough.
.baymax::before { background: #000; border-radius: 50%; content: ""; position: absolute; width: 12em; height: 12em; left: -9em; top: -6em; transform: skewX(-4deg); }.baymax::after { background: #000; border-radius: 50%; content: ""; position: absolute; width: 12em; height: 12em; right: -9em; top: -6em; transform: skewX(4deg); }
Low battery
This is a very interesting scene in the movie. (●?●) The battery is out and its upper and lower eyelids are fighting. We can do this using color gradient backgrounds and animations.
We added a linear gradient background, making it twice the height of the container and positioning the top half outside the container.
With the background, we can control the entire action through keyframes animation.
.baymax::before { background: linear-gradient(to bottom, #efefef, #efefef 50%, #000 50%, #000); background-position: 0 -100%; background-size: 200%; ... } .baymax::after { background: linear-gradient(to bottom, #efefef, #efefef 50%, #000 50%, #000); background-position: 0 -100%; background-size: 200%; ... }
The keyframes animation defines a series of frames through percentages. The percentages are related to the animation execution time, so 50% represents half of the animation time.
Here we do the blink animation 50% to 85% of the time.
@keyframes blink { 0%, 50% { background-position: 0 100%; } 85%, 95% { background-position: 0 75%; } 100% { background-position: 0 100%; }}
The next step is to tell the pseudo class to perform this animation. Add the animation attribute.
The above code sets the execution time to 6 seconds and will loop forever.
In addition, in order to make the effect more realistic, we added a 0.1s delay to the after pseudo-class, so it seems that the second eye will be a little slower and look more cute.
.baymax::before { animation: blink 6s infinite; ... } .baymax::after { animation: blink 6s 0.1s infinite; ... }
Browser Compatibility
In this example, I omitted the browser compatibility stuff, -webkit, -moz, etc. Animation needs to consider browser compatibility. Here I recommend you to use a tool like Autoprefixer.
There is a gif version here, you can share it online as you like.
This article is translated by @cssanimation. The entire translation contains my own understanding and meaning. If there is anything that is not translated well or is wrong, please give me some advice. If you want to reprint this translation, please indicate the English source: https://cssanimation.rocks/baymax/