Table of Contents
First, we give the background two colors, black to show the state of open eyes, and white to represent squinting. The white part needs to be positioned outside the eyes initially, and then animation is used to make the eyes droop.
Share the gif version
Home Web Front-end HTML Tutorial Use CSS to draw a clear picture (●-●)_html/css_WEB-ITnose

Use CSS to draw a clear picture (●-●)_html/css_WEB-ITnose

Jun 24, 2016 am 11:43 AM

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

Style rendering

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>
Copy after login

Next, we position the face. The mouth is just a black line, and we use border to achieve it.

body { background: radial-gradient(center, #fff, #fff 50%, #aaa); background-size: 100%; background-repeat: no-repeat; height: 100vh; }
Copy after login
The first attribute draws a side with a width of 1.5em. Then use absolute positioning to set the position at the midpoint of the container (body). 50% here is related to the size of the container.

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%); }
Copy after login

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.

Each pseudo-class has a black background, a 50% border-radius, and is positioned at the edge of the mouth. Finally, use skew transformation to move the eyes a little closer to the middle, and the result becomes like this.

.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); }
Copy after login

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.

First, we give the background two colors, black to show the state of open eyes, and white to represent squinting. The white part needs to be positioned outside the eyes initially, and then animation is used to make the eyes droop.

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%; ... }
Copy after login

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%; }}
Copy after login

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; ... }
Copy after login

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.

Share the gif version

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/

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

See all articles