Make a Random Dress-Up Character
In this tutorial you will build a web page that shows a different character every time you refresh it. You will write everything yourself, one small step at a time. After each step, look at your page in the browser to check that it works.
Green boxes are things to try. Yellow boxes (click on them!) are optional extra information.
Step 0: What you will make
You will make a web page that shows a dress-up character, like this one:
Click the "New character" button to try it!
The special part: every time you refresh the page, the computer picks a new body, new clothes, a new face and a new hairstyle. You will also add a button to get a new character.
You will learn how to:
- use HTML to put pictures on a page,
- use CSS to stack the pictures on top of each other and make the page pretty,
- use JavaScript to choose pieces at random.
The big idea
The character is not one picture. It is made of many see-through pictures (body, shoes, top, eyes, hair...) placed on top of each other, like stickers. To make a new character, we just choose different stickers.
Step 1: Get ready
You need:
-
A folder with the
imagesfolder inside it. Download the images (5-BASICS.zip), then unzip it. - A code editor (for example VS Code).
- A web browser (Chrome, Firefox, Edge...).
Inside images there are lots of pictures, one for each
piece of the character: the body, the hair, the shoes... Open a few of
them to see what they look like.
Notice the names. They all look like basics_body1.png,
basics_eyes3.png, basics_hairA_2.png...
Later we will use these names to pick pieces at random.
Why are the pictures see-through?
The pictures are PNG files with a transparent background. That means we can put them on top of each other, like stickers, and we will see all of them together as one character.
Step 2: Create your page
In the same folder as images (not inside it!), create a new
file called index.html and type this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Dress-Up Studio</title>
</head>
<body>
<h1>Hello!</h1>
</body>
</html>
index.html to open it
in your browser. You should see the word "Hello!".
Whenever you change the file, save it and press F5 in
the browser to see the change.
What does all this mean?
HTML is made of tags like <h1>. Most tags
have an opening part and a closing part with a slash:
<h1>...</h1>.
<head>holds information about the page, such as its title.<body>holds what you actually see on the page.
What is the viewport line?
Phones pretend to have a big screen and then shrink the page to fit,
which makes everything tiny. The line
<meta name="viewport" ...> tells the browser to use
the real width of the device instead. Your page then looks right on a
phone or a tablet, as well as on a computer.
Step 3: Show a picture
Replace the <h1>Hello!</h1> line with a picture
of a body:
<body>
<img src="images/basics_body1.png" alt="body">
</body>
body1 to body2, body3... and see
what happens.
What is src and alt?
src tells the browser where to find the picture.
images/basics_body1.png means "the file
basics_body1.png inside the images folder".
alt is a short description, used if the picture cannot be
shown.
Step 4: Add clothes (and a problem!)
Let's add shoes. We put the pictures inside a <div>,
which is a box that groups things together. Give the box an
id so we can talk about it later:
<body>
<div id="character">
<img src="images/basics_body1.png" alt="body">
<img src="images/basics_shoes1.png" alt="shoes">
</div>
</body>
Step 5: Stack the pictures with CSS
CSS is the language used to style a page. Add a <style>
block inside the <head>, just after the
<title> line:
<style>
#character {
position: relative;
width: 400px;
}
#character img {
position: absolute;
top: 0;
left: 0;
width: 100%;
}
</style>
The first picture (the body) will decide the height of the box. Add this
rule after the two others, inside <style>:
#character img:first-child {
position: relative;
display: block;
}
How does this work?
#charactermeans "the element with the idcharacter" (our box).#character imgmeans "all the pictures inside the box".position: absolutelets a picture sit exactly at a place in the box (here the top-left corner, because oftop: 0andleft: 0). Pictures placed like this overlap each other.position: relativeon the box tells the pictures to measure their position from the box and not from the whole page.width: 100%makes each picture as wide as the box. Since all the pictures have the same size, they fit perfectly.img:first-childis the first picture only. It stays a normal picture, so the box grows to its height.
basics_top1.png, basics_eyes1.png,
basics_mouth1.png and basics_hairA_1.png.
The order matters: the last one is on top.
Step 6: Make the page look nicer
Add these rules inside <style> too:
body {
margin: 0;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 1rem;
background: #f3e9f7;
font-family: sans-serif;
}
Also change the width rule of #character to
width: min(90vw, 500px);.
#f3e9f7 to
another one, for example pink or lightblue.
What do these lines do?
margin: 0removes the empty border around the page.min-height: 100vhmakes the page at least as tall as the window (vh= 1% of the window height).display: flexwithflex-direction: columnputs things in a column, andalign-itemsandjustify-contentcenter them.gapadds space between the things.min(90vw, 500px)means "90% of the window width, but never more than 500 pixels".
Step 7: Let JavaScript add the pictures
We want the computer to choose the pictures, so we will stop writing
them by hand. Delete all the <img> lines inside the
box, so it is empty:
<div id="character"></div>
Now add a <script> just before
</body>. JavaScript is the language that makes pages
do things.
<script>
const layers = ["body1", "shoes1", "top1", "eyes1", "mouth1", "hairA_1"];
const container = document.getElementById("character");
for (const name of layers) {
const img = document.createElement("img");
img.src = "images/basics_" + name + ".png";
img.alt = name;
container.appendChild(img);
}
</script>
What does this code do?
layersis a list of the picture names, from the back to the front.document.getElementById("character")finds our box on the page.for (const name of layers)repeats the lines inside the curly brackets once for each name in the list.- Inside, we create a new picture, tell it which file to use, and put it in the box.
Step 8: Choose at random
Let's write our first function: a small recipe that we can use again and again. This one picks a random item from a list. Put it at the start of your script:
// Return a random item from a list.
function pick(list) {
return list[Math.floor(Math.random() * list.length)];
}
Now try it. Change the first line of the layers list like this:
const layers = [pick(["body1", "body2", "body3"]), "shoes1", "top1", "eyes1", "mouth1", "hairA_1"];
How does pick work?
Math.random()gives a random number between 0 and 1 (like 0.37).- Multiplied by the length of the list (say 3), we get a number between 0 and 3 (like 1.11).
Math.floorrounds it down (to 1).list[1]is the second item of the list. Lists start counting at 0!
Step 9: A second function to avoid typing
Typing "body1", "body2", "body3"... is boring. Most parts
have 5 versions, numbered 1 to 5. Let's write a function that makes the
list [1, 2, 3, 4, 5] for us:
// Return the numbers 1 to n as a list, e.g. range(3) gives [1, 2, 3].
function range(n) {
return Array.from({ length: n }, function (_, i) { return i + 1; });
}
Now we can pick a random number from 1 to 5, and stick it after the name:
const layers = [
"body" + pick(range(5)),
"shoes" + pick(range(5)),
"top" + pick(range(5)),
"eyes" + pick(range(5)),
"eyebrow" + pick(range(5)),
"eyelashes" + pick(range(5)),
"mouth" + pick(range(4)),
"hairA_" + pick(range(5)),
];
range(4)
because there are only 4 mouth pictures.
How does range work?
Array.from builds a list of n items. For
each item, it calls our small function, which receives the position
i (starting at 0) and returns i + 1. So we
get 1, 2, 3... up to n. You can just use it without understanding every detail.
Step 10: More styles of clothes and hair
Look in the images folder again. There are several kinds of
tops, bottoms and hair: top, topB_,
topC_, and hairA_ to hairE_.
Let's choose the kind at random too. Put this just above
const layers:
const bottoms = ["bottom", "bottomB_", "bottomC_"];
const tops = ["top", "topB_", "topC_"];
const hairs = ["hairA_", "hairB_", "hairC_", "hairD_", "hairE_"];
Then update the layers list, with the bottom added too:
const layers = [
"body" + pick(range(5)),
"shoes" + pick(range(5)),
pick(bottoms) + pick(range(5)),
pick(tops) + pick(range(5)),
"eyes" + pick(range(5)),
"eyebrow" + pick(range(5)),
"eyelashes" + pick(range(5)),
"mouth" + pick(range(4)),
pick(hairs) + pick(range(4)),
];
Why range(4) for the hair?
hairB_ only has 4 pictures (1 to 4), while the others have 5.
Using 4 for all hairstyles is the simplest way to be safe. Can you find a
way to use all 5 pictures for the other hairstyles? (Hint: store the
number of pictures next to each name, like
["hairA_", 5].)
Step 11: A button for a new character
Add a button under the box in the HTML:
<div id="character"></div>
<button onclick="location.reload()">New character</button>
Well done!
You made a page that creates a random character using HTML, CSS and JavaScript.
Stuck? See the complete page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Dress-Up Studio</title>
<style>
body {
margin: 0;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 1rem;
background: #f3e9f7;
font-family: sans-serif;
}
#character {
position: relative;
width: min(90vw, 500px);
}
#character img {
position: absolute;
top: 0;
left: 0;
width: 100%;
}
#character img:first-child {
position: relative;
display: block;
}
</style>
</head>
<body>
<div id="character"></div>
<button onclick="location.reload()">New character</button>
<script>
// Return a random item from a list.
function pick(list) {
return list[Math.floor(Math.random() * list.length)];
}
// Return the numbers 1 to n as a list, e.g. range(3) gives [1, 2, 3].
function range(n) {
return Array.from({ length: n }, function (_, i) { return i + 1; });
}
const bottoms = ["bottom", "bottomB_", "bottomC_"];
const tops = ["top", "topB_", "topC_"];
const hairs = ["hairA_", "hairB_", "hairC_", "hairD_", "hairE_"];
// From the back to the front.
const layers = [
"body" + pick(range(5)),
"shoes" + pick(range(5)),
pick(bottoms) + pick(range(5)),
pick(tops) + pick(range(5)),
"eyes" + pick(range(5)),
"eyebrow" + pick(range(5)),
"eyelashes" + pick(range(5)),
"mouth" + pick(range(4)),
pick(hairs) + pick(range(4)),
];
const container = document.getElementById("character");
for (const name of layers) {
const img = document.createElement("img");
img.src = "images/basics_" + name + ".png";
img.alt = name;
container.appendChild(img);
}
</script>
</body>
</html>
Ideas to go further
- Change the background color and the font.
- Add a title above the character.
- Use all 5 hair pictures for the hairstyles that have them.
- Add a second button that picks only a new outfit and keeps the face.