Welcome to EasyCodingWithAI!

Before you dive into coding with AI, take a moment to consider some valuable insights.

Our articles cover the pros and cons of using AI in development, the importance of having a development environment, and how AI empowers hobbyists and small businesses to create and maintain their own websites, without the need of hiring professional developers.

Richard Robins

Guide : Experimenting with CSS Art and Generative Design with AI

Posted by Richard Robins on December 1, 2024 - Last modified on December 1, 2024.

The intersection of design and code can yield visually stunning results, especially when experimenting with CSS art and generative design.

By using CSS for drawing and animations, you can create intricate, interactive, and aesthetically pleasing visuals—all without relying on images.

AI tools like ChatGPT can guide you through the process, help troubleshoot issues, and suggest design improvements, making it an ideal partner for both beginner and advanced designers.

This guide will explore how AI can assist in creating CSS art and generative designs with JavaScript, enabling developers and designers to push the boundaries of web design.


Step 1: Understanding CSS Art and Generative Design

CSS art refers to using HTML and CSS to create images and visual effects purely through code. By leveraging CSS properties like border-radius, box-shadow, linear-gradient, and transform, you can craft complex shapes and animations.

Generative design involves using algorithms to create designs that can evolve or adapt based on specific parameters. It’s an approach that combines creativity and mathematics, often used for producing unique patterns, textures, and forms.

With AI tools, you can experiment with both concepts and generate innovative visuals more efficiently.


Step 2: Generating Simple CSS Art

AI can assist you in generating basic CSS shapes and art. By prompting AI to create a visual, you can focus on experimenting with layout, color schemes, and animation effects.

Example Prompt:

“Generate a simple CSS-based flower shape using border-radius and box-shadow.”

AI-Generated Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Flower</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
margin: 0;
}

.flower {
position: relative;
width: 100px;
height: 100px;
transform: rotate(45deg);
}

.petal {
position: absolute;
width: 50px;
height: 80px;
background-color: #FF6347;
border-radius: 50%;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}

.petal:nth-child(1) {
top: 0;
left: 25px;
transform: rotate(0deg);
}

.petal:nth-child(2) {
top: 25px;
left: 0;
transform: rotate(90deg);
}

.petal:nth-child(3) {
top: 25px;
left: 50px;
transform: rotate(180deg);
}

.petal:nth-child(4) {
top: 50px;
left: 25px;
transform: rotate(270deg);
}
</style>
</head>
<body>
<div class="flower">
<div class="petal"></div>
<div class="petal"></div>
<div class="petal"></div>
<div class="petal"></div>
</div>
</body>
</html>

This code creates a simple flower shape using CSS properties like border-radius for rounded petals and box-shadow for adding subtle depth.


Step 3: Adding Animation and Interactivity

AI can assist with creating dynamic and interactive elements, such as animations, hover effects, and transitions. Using CSS animations and JavaScript, you can bring your designs to life.

Example Prompt for Animation:

“Add a subtle pulse animation to the flower petals using @keyframes.”

AI-Generated Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Flower with Animation</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
margin: 0;
}

.flower {
position: relative;
width: 100px;
height: 100px;
transform: rotate(45deg);
animation: pulse 1.5s infinite;
}

.petal {
position: absolute;
width: 50px;
height: 80px;
background-color: #FF6347;
border-radius: 50%;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
animation: pulsePetal 1.5s infinite;
}

.petal:nth-child(1) {
top: 0;
left: 25px;
transform: rotate(0deg);
}

.petal:nth-child(2) {
top: 25px;
left: 0;
transform: rotate(90deg);
}

.petal:nth-child(3) {
top: 25px;
left: 50px;
transform: rotate(180deg);
}

.petal:nth-child(4) {
top: 50px;
left: 25px;
transform: rotate(270deg);
}

@keyframes pulse {
0% { transform: rotate(45deg); }
50% { transform: rotate(46deg); }
100% { transform: rotate(45deg); }
}

@keyframes pulsePetal {
0% { transform: scale(1); }
50% { transform: scale(1.1); }
100% { transform: scale(1); }
}
</style>
</head>
<body>
<div class="flower">
<div class="petal"></div>
<div class="petal"></div>
<div class="petal"></div>
<div class="petal"></div>
</div>
</body>
</html>

In this version, the petals pulse with a subtle animation. The @keyframes rule animates the rotation of the flower and the scaling of the petals to create a breathing effect.


Step 4: Generative Design with JavaScript

AI can also assist you in incorporating generative design principles into your project by helping you create dynamic, algorithmically-generated visuals. Using randomization or mathematical functions, you can generate unique patterns, shapes, or layouts.

Example Prompt for Generative Design:

“Create a generative design that randomly positions circles in the background using JavaScript.”

AI-Generated Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Generative Design with Circles</title>
<style>
body {
margin: 0;
background-color: #1e1e1e;
overflow: hidden;
}
.circle {
position: absolute;
border-radius: 50%;
background-color: rgba(255, 99, 71, 0.7);
}
</style>
</head>
<body>
<script>
const numCircles = 100;

for (let i = 0; i < numCircles; i++) {
let circle = document.createElement('div');
let size = Math.random() * 50 + 20; // Random size between 20px and 70px
let x = Math.random() * window.innerWidth;
let y = Math.random() * window.innerHeight;
let opacity = Math.random() * 0.5 + 0.5; // Random opacity between 0.5 and 1

circle.style.width = `${size}px`;
circle.style.height = `${size}px`;
circle.style.top = `${y}px`;
circle.style.left = `${x}px`;
circle.style.opacity = opacity;

document.body.appendChild(circle);
}
</script>
</body>
</html>

This JavaScript snippet creates 100 random circles of varying sizes, positions, and opacities. The randomization creates a unique design each time the page is loaded.


Step 5: Refining and Iterating with AI Feedback

As you develop your CSS art or generative design, you might need to refine your code to improve performance, interactivity, or visual appeal. AI can help suggest optimizations for better responsiveness, smoother animations, or cleaner code. You can also ask for suggestions on color palettes, design principles, or how to structure your HTML/CSS for scalability.

Example Prompt for Refining Code:

“My circles are not appearing smoothly; can you suggest optimizations for smoother transitions?”

AI-Generated Suggestion:

To ensure smoother animations, you can try using requestAnimationFrame() for JavaScript animations, or you can optimize your CSS transitions by using transform and opacity properties, as they are handled by the GPU for better performance. Also, try limiting the number of DOM elements rendered at once for smoother animations.


Conclusion

With AI as your guide, you can easily dive into the world of CSS art and generative design.

From simple shapes to intricate animations and dynamic, algorithmic patterns, AI can help you generate code, troubleshoot issues, and refine your designs. Experimenting with these techniques not only enhances your design skills but also deepens your understanding of how code and creativity intersect in modern web development.


Richard Robins

Richard Robins

Richard is passionate about sharing how AI resources such as ChatGPT and Microsoft Copilot can be used to create addons and write code, saving small website owners time and money, freeing them to focus on making their site a success.


Disclaimer

The coding tips and guides provided on this website are intended for informational and educational purposes only. While we strive to offer accurate and helpful content, these tips are meant as a starting point for your own coding projects and should not be considered professional advice.

We do not guarantee the effectiveness, security, or safety of any code or techniques discussed on this site. Implementing these tips is done at your own risk, and we encourage you to thoroughly test and evaluate any code before deploying it on your own website or application.

By using this site, you acknowledge that we are not responsible for any issues, damages, or losses that may arise from your use of the information provided herein.