Sure! Below is a complete HTML, CSS, and JavaScript code that creates a stunning black cat graphic entirely with CSS. The cat has a face with ears, eyes that blink, a nose, and a mouth. Additionally, there are separate buttons to control different parts of the cat, allowing you to toggle the visibility or animate various features.
You can copy and paste this code into an .html file and open it in your browser to see the effect.
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS Black Cat</title>
<style>
body {
background-color: #2c3e50;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
}
.cat-container {
position: relative;
width: 300px;
height: 300px;
}
/* Cat Head */
.cat-head {
position: relative;
width: 100%;
height: 100%;
background: black;
border-radius: 50% 50% 45% 45%;
box-shadow: inset -10px -10px 0 rgba(0,0,0,0.2);
}
/* Ears */
.ear {
position: absolute;
top: -30px;
width: 60px;
height: 60px;
background: black;
border: 10px solid #34495e;
border-bottom: none;
border-radius: 0 0 50% 50%;
transform: rotate(45deg);
transition: transform 0.3s;
}
.ear.left {
left: 50px;
transform: rotate(-20deg);
}
.ear.right {
right: 50px;
transform: rotate(20deg);
}
/* Eyes */
.eye {
position: absolute;
top: 80px;
width: 50px;
height: 30px;
background: white;
border-radius: 50%;
overflow: hidden;
}
.eye.left {
left: 70px;
}
.eye.right {
right: 70px;
}
.pupil {
position: absolute;
top: 5px;
left: 15px;
width: 20px;
height: 20px;
background: #2c3e50;
border-radius: 50%;
transition: top 0.3s;
}
/* Blinking Animation */
@keyframes blink {
0%, 20%, 100% {
height: 30px;
}
10% {
height: 0px;
}
}
.eye.blinking {
animation: blink 2s infinite;
}
/* Nose */
.nose {
position: absolute;
top: 140px;
left: 50%;
transform: translateX(-50%);
width: 20px;
height: 20px;
background: pink;
border-radius: 50%;
box-shadow: 0 0 10px rgba(255,192,203,0.5);
transition: background 0.3s;
}
/* Mouth */
.mouth {
position: absolute;
top: 160px;
left: 50%;
transform: translateX(-50%);
width: 40px;
height: 20px;
border-bottom: 3px solid pink;
border-radius: 0 0 20px 20px;
}
/* Whiskers */
.whiskers {
position: absolute;
top: 150px;
width: 100%;
display: flex;
justify-content: space-between;
padding: 0 20px;
}
.whisker {
width: 50px;
height: 2px;
background: white;
transform: rotate(0deg);
}
/* Buttons */
.controls {
margin-top: 20px;
display: flex;
gap: 10px;
flex-wrap: wrap;
justify-content: center;
}
.controls button {
padding: 10px 15px;
border: none;
background-color: #34495e;
color: white;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
.controls button:hover {
background-color: #2c3e50;
}
/* Optional: Add some interactivity animations */
.ear.active.left {
transform: rotate(-40deg);
}
.ear.active.right {
transform: rotate(40deg);
}
</style>
</head>
<body>
<div class="cat-container">
<div class="cat-head">
<div class="ear left"></div>
<div class="ear right"></div>
<div class="eye left">
<div class="pupil"></div>
</div>
<div class="eye right">
<div class="pupil"></div>
</div>
<div class="nose"></div>
<div class="mouth"></div>
<div class="whiskers">
<div class="whisker"></div>
<div class="whisker"></div>
<div class="whisker"></div>
<div class="whisker"></div>
<div class="whisker"></div>
</div>
</div>
</div>
<div class="controls">
<button id="toggleBlink">Toggle Blink</button>
<button id="toggleEars">Toggle Ears</button>
<button id="toggleNose">Toggle Nose</button>
<button id="toggleMouth">Toggle Mouth</button>
<button id="toggleWhiskers">Toggle Whiskers</button>
</div>
<script>
const blinkButton = document.getElementById('toggleBlink');
const earsButton = document.getElementById('toggleEars');
const noseButton = document.getElementById('toggleNose');
const mouthButton = document.getElementById('toggleMouth');
const whiskersButton = document.getElementById('toggleWhiskers');
const eyes = document.querySelectorAll('.eye');
const ears = document.querySelectorAll('.ear');
const nose = document.querySelector('.nose');
const mouth = document.querySelector('.mouth');
const whiskers = document.querySelector('.whiskers');
blinkButton.addEventListener('click', () => {
eyes.forEach(eye => {
eye.classList.toggle('blinking');
});
});
earsButton.addEventListener('click', () => {
ears.forEach(ear => {
ear.classList.toggle('active');
});
});
noseButton.addEventListener('click', () => {
if (nose.style.display === 'none') {
nose.style.display = 'block';
} else {
nose.style.display = 'none';
}
});
mouthButton.addEventListener('click', () => {
if (mouth.style.display === 'none') {
mouth.style.display = 'block';
} else {
mouth.style.display = 'none';
}
});
whiskersButton.addEventListener('click', () => {
if (whiskers.style.display === 'none') {
whiskers.style.display = 'flex';
} else {
whiskers.style.display = 'none';
}
});
</script>
</body>
</html>
Explanation of the Code:
HTML Structure:
The .cat-container holds the entire cat graphic.
The .cat-head represents the cat’s head.
.ear.left and .ear.right are the cat’s ears.
.eye.left and .eye.right are the eyes, each containing a .pupil.
.nose and .mouth represent the cat’s nose and mouth, respectively.
.whiskers contains multiple .whisker elements for the whiskers.
The .controls div contains buttons to control different parts of the cat.
CSS Styling:
The body has a dark background to make the black cat stand out.
The cat’s head is a black circle with some inner shadow for depth.
Ears are styled using rotated squares with borders to create a triangular effect.
Eyes are white with dark pupils. The .blinking class triggers a CSS animation to simulate blinking.
The nose is a pink circle with a subtle shadow.
The mouth is a simple line with a curved border to represent the cat’s mouth.
Whiskers are horizontal lines positioned on either side of the face.
Buttons are styled for a consistent look and have hover effects for interactivity.
JavaScript Interactivity:
Each button toggles a specific part of the cat:
Toggle Blink: Adds or removes the .blinking class to make the eyes blink.
Toggle Ears: Adds or removes the .active class to animate the ears.
Toggle Nose: Shows or hides the nose.
Toggle Mouth: Shows or hides the mouth.
Toggle Whiskers: Shows or hides the whiskers.
Animations and Transitions:
The blinking effect is achieved using a keyframes animation that temporarily reduces the height of the eyes.
Ears rotate when the .active class is toggled, creating a movement effect.
Transitions are used for smooth changes when toggling visibility or applying animations.
Customization:
Colors: You can change the colors by modifying the CSS properties. For example, changing the background of .cat-head to a different shade.
Size: Adjust the width and height of .cat-container and its child elements to resize the cat.
Additional Features: You can add more buttons to control other parts or add more animations for enhanced interactivity.
Feel free to enhance the design further by adding more details or refining the existing styles. Enjoy your interactive CSS black cat!