Phaser. Unit 3. Using tilemaps

Introduction

In this unit we are going to use Phaser together with a tilemap editor to build our world. You may find more information at the official page of the Tiled editor and you may also have a look at some interesting demonstrations at the Phaser’s examples page, and also performing some search on the latest examples.

HTML and CSS code

The first thing we should do is linking the Phaser library. We will use the last version at the time this unit has been written:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>

After that we are going to insert some CSS code to remove any margin and padding, and also to remove the scrollbars, so that all the window size is used when the game is started:

html, body {
    margin: 0px;
    padding: 0px;
    overflow: hidden;
    height: 100%;
} 

And finally we are going to use a single file to put all the code inside. This is going to be the basic structure of the “.html” file:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>First game with Phaser 3</title>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>
    <style type="text/css">
        html, body {
            margin: 0px;
            padding: 0px;
            overflow: hidden;
            height: 100%;
        }
    </style>
</head>
<body>
    <script type="text/javascript">
        ...
    </script>
</body>
</html>

Constants

We are going to define some constants at the beginning of the code so that we can easily change the game appearance, difficulty, and some other parameters:

const screenWidth = window.innerWidth;
const screenHeight = window.innerHeight;
const numCoins = 15;
const numHealth = 5;
const numPower = 5;
const numZombies = 10;

Phaser configuration and other variables

Following the constants we are going to create some variables so keep all the information about the game and also to start the initialization of Phaser:

const config = {
    type: Phaser.AUTO,
    width: screenWidth,
    height: screenHeight,
    physics: {
        default: "arcade",
        arcade: {
            debug: false,
            gravity: { y: 0 }
        }
    },
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};

const game = new Phaser.Game(config);
let worldWidth, worldHeight, joystickSize = 40, scale = 1.75, speed = 175;
let belowLayer, worldLayer, aboveLayer, tileset, emptyTiles;
let map, player, cursors, bell, bell2, dead, timeout;
let joyStick = joyStick2 = { up: false, down: false, left: false, right: false };
let score = 0, lives = 5, scoreText = null, gameOver = false;

The assets (images, sprites and sounds)

We will need some images, sprites and sounds. You may use any assets you like, but we are also providing some examples so that you can easily start testing (they can also be downloaded here):

Images

Tileset to be used to build the map
Restart button

Sprites (player and animated objects)

Player’s sprite sheet
Enemy’s sprite sheet
Sprite sheet to animate the coins
Sprite sheet to animate the health objects
Sprite sheet to animate the power object

Sounds and music

When the player gets a coin
When the user collects a health object
When the user gets hurt by a zombie
Background music always playing

Loading all the assets

So that we can use all those images, sprites and music, they have to be loaded previously. That is the purpose of the “preload()” function. We will also include here the initialization of the joystick so that we can use it in another section of this unit.

Proposed exercise: Game initialization

Create a specific folder for your game in your domain to put all the code and assets inside. After that, create an “index.html” inside that folder with the code below, and also create an “assets” folder to put all the images and sounds, which can be downloaded here. After uploading everything to your domain, test the game using the url of that folder in your browser, and you should get a full black screen. Click on it and the background music should start playing.

You can see the result here (you can also look at the source code by pressing Ctrl+U).
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>First game with Phaser 3</title>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>
    <style type="text/css">
        html,
        body {
            margin: 0px;
            padding: 0px;
            overflow: hidden;
            height: 100%;
        }
    </style>
</head>
<body>
    <div id="game-container"></div>
    <script type="text/javascript">
        const screenWidth = window.innerWidth;
        const screenHeight = window.innerHeight;
        const numCoins = 15;
        const numHealth = 5;
        const numPower = 5;
        const numZombies = 10;

        const config = {
            type: Phaser.AUTO,
            width: screenWidth,
            height: screenHeight,
            physics: {
                default: "arcade",
                arcade: {
                    debug: false,
                    gravity: { y: 0 }
                }
            },
            scene: {
                preload: preload,
                create: create,
                update: update
            }
        };

        const game = new Phaser.Game(config);
        let worldWidth, worldHeight, joystickSize = 40, scale = 1.75, speed = 175;
        let belowLayer, worldLayer, aboveLayer, tileset, emptyTiles;
        let map, player, cursors, bell, bell2, dead, timeout;
        let joyStick = joyStick2 = { up: false, down: false, left: false, right: false };
        let score = 0, lives = 5, scoreText = null, gameOver = false;

        function preload() {
            this.load.image("restart", "assets/restart.png");
            this.load.image("tiles", "assets/tileset.png");
            this.load.tilemapTiledJSON("map", "assets/town.json");
            this.load.spritesheet("player", "assets/lidia.png", { frameWidth: 64, frameHeight: 64 });
            this.load.spritesheet("coin", "assets/coin.png", { frameWidth: 32, frameHeight: 32 });
            this.load.spritesheet("health", "assets/health.png", { frameWidth: 32, frameHeight: 32 });
            this.load.spritesheet("power", "assets/power.png", { frameWidth: 64, frameHeight: 64 });
            this.load.spritesheet("zombie", "assets/zombie.png", { frameWidth: 48, frameHeight: 64 });

            this.load.audio("bell", "assets/ding.mp3");
            this.load.audio("bell2", "assets/ding2.mp3");
            this.load.audio("dead", "assets/dead.mp3");
            this.load.audio("music", "assets/music.mp3");

            this.load.plugin('rexvirtualjoystickplugin', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/rexvirtualjoystickplugin.min.js', true);
        }

        function initSounds() {
            bell = this.sound.add('bell', { volume: 0.2 });
            bell2 = this.sound.add('bell2', { volume: 0.2 });
            dead = this.sound.add('dead', { volume: 0.7 });
            this.sound.add('music', { volume: 0.4 }).play({ loop: -1 });
        }

        function create() {
            initSounds.call(this);
        }

        function update(time, delta) {
        }
    </script>
</body>
</html>

Proposed exercise: Using your own music

Download any other music file you like to use it as a background music and change the JavaScript code inside the “preload()” function to set the file name accordingly. Also in the “initSounds()” function you will find that you may change the volume of the music (volume:0.4). Try changing that value from 0.1 to 1 and you will notice that the volume of the music will also change.

You may find in the following sites some free images and sounds which you can download and use for your own game:

The map

Once we have initialized Phaser and after loading all the assets, we can go ahead and display the map on the screen.

Proposed exercise: Displaying the map

Add the function “createWorld()” (provided below) to your code, and modify the calling function “create()” so that it is called when the game is started (also shown below). Finally test everything in your browser from your domain.

About the music, do not forget to click on the screen after loading the game (the music will not start playing unless the browser gets the focus). Also, you can see the result here (you can look at the source code by pressing Ctrl+U).
  1. This is the new function you have to insert in your code to create the world:
function createWorld() {
    map = this.make.tilemap({ key: "map" });

    scale *= Math.max(screenWidth/map.widthInPixels, screenHeight/map.heightInPixels);

    worldWidth = map.widthInPixels * scale;
    worldHeight = map.heightInPixels * scale;
    speed *= scale;
    joystickSize *= scale;

    // Parameters are the name you gave the tileset in Tiled and then the key of the tileset image in
    // Phaser's cache (i.e. the name you used in preload)
    tileset = map.addTilesetImage("tileset", "tiles");

    // Parameters: layer name (or index) from Tiled, tileset, x, y
    belowLayer = map.createLayer("Below Player", tileset, 0, 0).setScale(scale).setDepth(1);
    worldLayer = map.createLayer("World", tileset, 0, 0).setScale(scale).setDepth(2);
    aboveLayer = map.createLayer("Above Player", tileset, 0, 0).setScale(scale).setDepth(3);

    worldLayer.setCollisionByProperty({ collides: true });

    // Find empty tiles where new zombies, coins or health can be created
    emptyTiles = worldLayer.filterTiles(tile => (tile.index === -1 || !tile.collides));
}
  1. Do not forget to update the “create()” function to create the world. This is the only line you should add to that function:
function create() {
    ...
    createWorld.call(this);            
}

Proposed exercise: Scaling the map

Change the value of the “scale” variable (at the top of your code) and refresh the map in your browser to check that it is scaled accordingly. You can set values such as “scale = 0.5” and you will notice that the map just fits half the screen. Set also higher values and check the results again.

The animations (sprites)

We will use sprites to create the player and the animated objects. For example, let’s have a look at the player:

Player’s sprite sheet

We may appreciate that we have 36 pictures divided into 4 different groups. This way, we may easily create all four animations:

  • Up: Pictures from 0 to 8
  • Left: Pictures from 9 to 17
  • Down: Pictures from 18 to 26
  • Right: Pictures from 27 to 35

Controlling the player’s animations

So that this game can be used in both desktop and mobile devices, we have to initialize both the cursor keys and the joystick. Once this is done, we must change the player’s velocity and the current animation when the user presses the keys or moves the joystick. In the next exercise we will first define the animations inside the “createAnimations()” function, and after that we will go ahead with the “createPlayer()” function, where we will display the player. We will finally include in the “update()” function some conditions to adjust the player’s speed and current animation.

Proposed exercise: Adding the player

Add the code below to your game and check both the player animations and movements. After that, look for another sprite and change the code accordingly to use your own sprite. Finally, change the values of the constant “speed” (at the top of your code) and check the results. You will also notice that the camera will follow you on each player’s movement.

You can see the result here (you can look at the source code by pressing Ctrl+U). Also, as listed above, you may find many free assets in OpenGameArt and choose other sprites you like.
  1. These are the new functions you have to insert in your code to create the animations and initialize everything related to the player:
function createAnimations() {
    const anims = this.anims;
    // Player
    anims.create({
        key: "left", frameRate: 10, repeat: -1,
        frames: this.anims.generateFrameNumbers('player', { start: 9, end: 17 }),
    });
    anims.create({
        key: "right", frameRate: 10, repeat: -1,
        frames: this.anims.generateFrameNumbers('player', { start: 27, end: 35 }),
    });
    anims.create({
        key: "up", frameRate: 10, repeat: -1,
        frames: this.anims.generateFrameNumbers('player', { start: 0, end: 8 }),
    });
    anims.create({
        key: "down", frameRate: 10, repeat: -1,
        frames: this.anims.generateFrameNumbers('player', { start: 18, end: 26 }),
    });
    anims.create({
        key: "waiting", frameRate: 3, repeat: -1,
        frames: this.anims.generateFrameNumbers('player', { start: 19, end: 21 }),
    });
    // Coin
    anims.create({
        key: "flying", frameRate: 10, repeat: -1,
        frames: this.anims.generateFrameNumbers('coin', { start: 0, end: 7 }),
    });
    // Health
    anims.create({
        key: "floating", frameRate: 5, repeat: -1,
        frames: this.anims.generateFrameNumbers('health', { start: 0, end: 3 }),
    });
    // Power
    anims.create({
        key: "growing", frameRate: 5, repeat: -1,
        frames: this.anims.generateFrameNumbers('power', { start: 0, end: 3 }),
    });
    // Zombie
    anims.create({
        key: "moving", frameRate: 5, repeat: -1,
        frames: this.anims.generateFrameNumbers('zombie', { start: 0, end: 11 }),
    });
}

function createPlayer() {
    // Object layers in Tiled let you embed extra info into a map - like a spawn point or custom
    // collision shapes. In the tmx file, there's an object layer with a point named "Spawn Point"
    const spawnPoint = map.findObject("Objects", obj => obj.name === "Spawn Point");

    // Create a sprite with physics enabled via the physics system. The image used for the sprite has
    // a bit of whitespace, so I'm using setSize & setOffset to control the size of the player's body
    player = this.physics.add
        .sprite(spawnPoint.x * scale, spawnPoint.y * scale, "player")
        .setSize(16, 32).setOffset(24, 32).setScale(scale)
        .setCollideWorldBounds(true).setDepth(2);

    // Watch the player and worldLayer for collisions, for the duration of the scene
    this.physics.add.collider(player, worldLayer);

    const camera = this.cameras.main;
    const world = this.physics.world;
    camera.startFollow(player);
    camera.setBounds(0, 0, worldWidth, worldHeight);
    world.setBounds(0, 0, worldWidth, worldHeight);

    cursors = this.input.keyboard.createCursorKeys();

    // Do not show the joysticks on desktop devices
    if (!this.sys.game.device.os.desktop) {
        joyStick = this.plugins.get('rexvirtualjoystickplugin').add(this, {
            x: screenWidth - joystickSize * 2,
            y: screenHeight - joystickSize * 2,
            radius: joystickSize,
            base: this.add.circle(0, 0, joystickSize, 0x888888).setAlpha(0.5).setDepth(4),
            thumb: this.add.circle(0, 0, joystickSize, 0xcccccc).setAlpha(0.5).setDepth(4)
        }).on('update', update, this);

        joyStick2 = this.plugins.get('rexvirtualjoystickplugin').add(this, {
            x: joystickSize * 2,
            y: screenHeight - joystickSize * 2,
            radius: joystickSize,
            base: this.add.circle(0, 0, joystickSize, 0x888888).setAlpha(0.5).setDepth(4),
            thumb: this.add.circle(0, 0, joystickSize, 0xcccccc).setAlpha(0.5).setDepth(4)
        }).on('update', update, this);
    }          
}
  1. Also you have to insert some code inside the “update()” function to respond to the cursor keys and the joystick:
function update(time, delta) {
    if (gameOver) return;

    let vX = 0, vY = 0;

    // Horizontal movement
    if (cursors.left.isDown || joyStick.left || joyStick2.left) vX = -speed;
    else if (cursors.right.isDown || joyStick.right || joyStick2.right) vX = speed;

    // Vertical movement
    if (cursors.up.isDown || joyStick.up || joyStick2.up) vY = -speed;
    else if (cursors.down.isDown || joyStick.down || joyStick2.down) vY = speed;

    // Set and normalize the velocity so that player can't move faster along a diagonal
    player.setVelocity(vX, vY).body.velocity.normalize().scale(speed);

    // Choose the right player's animation
    if (vX < 0) player.anims.play('left', true);
    else if (vX > 0) player.anims.play('right', true);
    else if (vY < 0) player.anims.play('up', true);
    else if (vY > 0) player.anims.play('down', true);
    else player.anims.play('waiting', true);            
}
  1. And finally do not forget to update the “create()” function to create the animations and the player. You just have to insert a couple of new lines:
function create() {
    ...
    createAnimations.call(this); 
    createPlayer.call(this);
}

Proposed exercises: Modifying the map

Download the tile map editor from this link and install it on your computer. Go to the assets folder and open the file “town.tmx” which contains the map of the example explained in this unit. Select the layer “World” (on the right corner, at the top) and change anything you like (by adding or removing some objects to or from the map). When you finish, export the map to JSON format using the option “File” -> “Export as…”, and overwrite the file “town.json”, located inside the “assets” folder. Finally check the results using your browser and move the player to go to each part of the map. You will notice again that the camera will follow you on each movement.

Do not forget to fully refresh the contents of the browser by pressing Ctrl+F5, since the map and some other contents are usually cached by the browser. Also move the player and check that the map has been updated with the changes you have made.

Score, coins, power, health, and enemies

We will create many different objects at once using the same images. We will just change the size of the objects by scaling them, and we will use a loop to print as many objects as we like.

Proposed exercise: Adding the score

Using the global variables, we may easily show both the score and number of remaining lives. Add the code below to your game and check the results. After that, change the value of the variable “lives” (at the top of your code) to check how easily you can customize that text.

You can see the result here (you can look at the source code by pressing Ctrl+U).
function showScore() {
    if (!scoreText) {
        scoreText = this.add.text(screenWidth/2, 16, '', { fontSize: (32 * scale) + 'px', fill: '#FFF' });
        scoreText.setShadow(3, 3, 'rgba(0,0,0,1)', 3).setOrigin(0.5, 0).setScrollFactor(0).setDepth(4);
    }
    scoreText.setText('Score:' + score + ' / Lives:' + lives);
}
function create() {
    ...
    showScore.call(this);
}

Proposed exercises: Adding the coins

Add the code below to your file and check the results. After that, change the value of the constant “numCoins” (at the top of your code) to check how easily you can customize your game. Refresh the page several times and you will notice that the generated coins have random sizes and they appear at random positions. Finally, look for another sprite sheet (it may be a coin or any other object you like) and change the code accordingly to use your own animated object.

You can see the result here (you can look at the source code by pressing Ctrl+U). Also, as listed above, you may find many free assets in OpenGameArt and choose the images you like.
function newObject(type, animation, context) {
    let tile = Phaser.Utils.Array.GetRandom(emptyTiles);
    return context.physics.add.sprite(tile.pixelX * scale, tile.pixelY * scale, type).anims.play(animation, true).setDepth(2);
}

function createCoin() {
    const v = Phaser.Math.Between(speed / 10, speed / 5);
    let coin = newObject('coin', 'flying', this).setSize(16, 32).setScale(1.5 * scale).setBounce(1).setVelocity(v, 0);
    coin.body.setAllowGravity(false).setCollideWorldBounds(true);
    this.physics.add.collider(coin, worldLayer);
    this.physics.add.overlap(player, coin, collectCoin, null, this);
} 

function collectCoin(player, coin) {
    bell.play();
    coin.destroy();
    createCoin.call(this);

    score += 10;
    showScore();
}
function create() {
    ...
    for (i = 0; i < numCoins; i++) setTimeout(() => createCoin.call(this), Phaser.Math.Between(0, 5000));
}

Proposed exercises: Adding the health

Add the code below to your game and check the results. After that, change the value of the constants “numHealth” (at the top of your code) to check how easily you can customize your game. Finally, look for another sprite and change the code accordingly to use your own object to provide the player with extra lives.

You may click here to see how the game looks like. As listed above, you may find many free assets in OpenGameArt.
function createHealth() {
    let health = newObject('health', 'floating', this).setSize(16, 32).setScale(1.2 * scale);
    health.body.setAllowGravity(false);
    this.physics.add.overlap(player, health, collectHealth, null, this);
}

function collectHealth(player, health) {
    bell2.play();
    health.destroy();
    createHealth.call(this);

    lives++;
    showScore();
}
function create() {
    ...
    for (i = 0; i < numHealth; i++) setTimeout(() => createHealth.call(this), Phaser.Math.Between(0, 5000));
}

Proposed exercises: Adding the power

Add the code below to your game and check the results. After that, change the value of the constants “numPower” (at the top of your code) to check how easily you can customize your game. Finally, look for another sprite and change the code accordingly to use your own power object.

You may click here to see how the game looks like. As listed above, you may find many free assets in OpenGameArt.
function createPower() {
    let power = newObject('power', 'growing', this).setSize(32, 64).setScale(scale/2);
    power.body.setAllowGravity(false);
    this.physics.add.overlap(player, power, collectPower, null, this);
}

function protect(color) {
    player.setTint(color);
    if (timeout) clearTimeout(timeout);
    timeout = setTimeout(() => { timeout = false; player.clearTint() }, 5000);
}

function collectPower(player, power) {
    bell2.play();
    power.destroy();
    createPower.call(this);

    protect(0xFFFF00);
}
function create() {
    ...
    for (i = 0; i < numPower; i++) setTimeout(() => createPower.call(this), Phaser.Math.Between(0, 5000)); 
}

Proposed exercises: Adding the enemies

Add the code below to your game and check the results. After that, change the value of the constants “numZombies” (at the top of your code) to check how easily you can customize your game. Finally, look for another sprite and change the code accordingly to display your own enemies.

You may click here to see how the game looks like. As listed above, you may find many free assets in OpenGameArt.
function createZombie() {
    const v = Phaser.Math.Between(-speed / 4, -speed / 3);
    let zombie = newObject('zombie', 'moving', this).setSize(16, 32).setOffset(16, 32).setScale(1.2 * scale).setBounce(1).setVelocity(0, v);
    zombie.body.setAllowGravity(false).setCollideWorldBounds(true);
    this.physics.add.collider(zombie, worldLayer);
    this.physics.add.overlap(player, zombie, hitZombie, null, this);
}

function hitZombie(player, zombie) {
    // If the player is already hurt, it cannot be hurt again for a while
    if (player.tintTopLeft == 0xFF0000) return;

    if (timeout) {
        score += 15;
        showScore();
    }
    else {
        protect(0xFF0000);
        lives--;
    }

    dead.play();
    showScore();

    if (lives == 0) {
        this.physics.pause();
        gameOver = true;
        this.add.image(screenWidth / 2, screenHeight / 2, 'restart')
            .setScale(4).setScrollFactor(0).setDepth(4)
            .setInteractive().on('pointerdown', () => location.reload());
    }
    else {
        zombie.destroy();
        createZombie.call(this);
    }
} 
function create() {
    ...
    for (i = 0; i < numZombies; i++) setTimeout(() => createZombie.call(this), Phaser.Math.Between(0, 5000)); 
}

Enjoy the game!

You may enjoy playing this wonderful game online here.

Phaser. Unit 2. A bigger world

Introduction

In this unit we are going to expand our world with a whole different scene. We will use the game developed in the previous unit so that by modifying or adding some code, we will have double the entertainment than before 🙂

The new assets

These are the new assets that we will use to build the next scene (you can download all the assets here):

An arrow to jump to the next scene
Flying stars that will increase the score
Dead trees
Flying dragons trying to kill the player
Sound to be played after collecting a star

Expanding the world

The first thing we are going to do is creating a wider sky and platform, using new colors for the new scene.

Proposed exercise: Expanding sky and platform

First check that you have a working game on your domain. After that, follow the instructions below, and upload the new code and the new assets to your domain (you can get all the assets from this link). Finally, check the results in your browser.

Now you should have an arrow on the right corner, at the top of the screen, so that you can easily jump to the next scene. Your new game should look now like this one.
  1. Insert at the top of your code a new constant with the new size of the world (double than before):
const maxWorldWidth = worldWidth * 2;
  1. Modify the function “createWorld()” so that both the camera and world bounds use this new constant, and also insert the new sky and the new platform:
function createWorld() {
    this.cameras.main.setBounds(0, 0, maxWorldWidth, screenHeight);
    this.physics.world.setBounds(0, 0, maxWorldWidth, screenHeight);

    ...

    // New sky
    this.add.rectangle(worldWidth, 0, worldWidth, screenHeight, 0xff6600).setOrigin(0);

    // New platform
    platform2 = this.add.rectangle(worldWidth, screenHeight, worldWidth, platformHeight, 0x006600).setOrigin(0, 1);
    this.physics.add.existing(platform2);
    platform2.body.setCollideWorldBounds(true);
}
  1. Activate the collision between the player and the new platform. This can be done with a single line inside the “createPlayer()” function:
function createPlayer() {
    ...
    this.physics.add.collider(player, platform2);
}
  1. Load the new assets (the button to jump to the next scene, the dead tree, the stars to be collected, the sound to be played when collecting the stars, and the sprite sheet of the flying dragon) inside the “preload()” function:
function preload() {
    ...
    this.load.image('arrow', 'assets/arrow.png');
    this.load.image('tree2', 'assets/tree2.png');
    this.load.image('star', 'assets/star.png');
    this.load.audio('bell2', 'assets/ding2.mp3');
    this.load.spritesheet('dragon', 'assets/dragon.png', { frameWidth: 144, frameHeight: 128 });
}
  1. Insert a new function to display the arrow to jump to the next scene:
function showArrow() {
    this.add.image(screenWidth, 0, 'arrow').setOrigin(1, 0).setScale(2).setScrollFactor(0).setInteractive().on('pointerdown', () => player.x = (player.x + worldWidth) % maxWorldWidth);
}
  1. And finally you must execute that code, just by adding a new line to the “create()” function:
function create() {
    ...            
    showArrow.call(this);
}

Proposed exercise: A big tree between the two scenes

To achieve a better transition between one scene to the other, we will put a dead tree in the middle. You just need to insert one line of code at the end of the “createWorld()” function:

You can see the expected result here.
function createWorld() {
    ...
    this.add.image(worldWidth, 0, 'tree2').setOrigin(0.5, 0).setDisplaySize(screenWidth/5, screenHeight);
}

Proposed exercise: Changing the new scene’s colors

Choose any colors you like for the new scene, and change the colors of both the sky and the platform. You only have to change a couple of values inside the function “createWorld()”.

You can use a color picker to get the values of the new colors in hexadecimal notation.

More trees, more fires, more clouds…

Now we have to show the fires and the clouds in the new scene. And to change even more the appearance of the new scene, we will not change only the colors but also the background images, by using new trees.

Proposed exercise: More trees

Update the constant with the number of trees to be displayed, and also, add a new line to the “createTree()” function so that lots of new trees are shown inside the new scene.

You can see the expected result here.
  1. Update the constant to show as many trees as you like:
const numTrees = 100;
  1. Create new trees in the new scene (you will notice that these trees are shown at ‘x+worldWidth’ position, and also the ‘scale’ is adjusted for the new image):
function createTree() {
    ...
    this.add.image(x + worldWidth, y, 'tree2').setOrigin(1).setScale(scale * 2);
}

Proposed exercise: More fires

Update the constant with the number of fires to be displayed, and also, update the “createFire()” function to use the constant ‘maxWorldWidth’ as the limit to generate the fires.

You can see the expected result here.
  1. Update the constant to show as many fires as you like:
const numFires = 50;
  1. Update the following line to use ‘maxWorldWidth’ constant instead:
function createFire() {
    const x = Phaser.Math.Between(screenWidth/2, maxWorldWidth);
    ...
}

Proposed exercise: More clouds

Update the constant with the number of clouds to be displayed, and also, update the “createClouds()” function to use the constant “maxWorldWidth” as the limit to generate the clouds.

You can see the expected result here.
  1. Update the constant to show as many clouds as you like:
const numClouds = 100;
  1. Update the following line to use “maxWorldWidth” constant instead:
function createCloud() {
    const x = Phaser.Math.Between(0, maxWorldWidth);
    ...
}

The new moving objects (stars and dragons)

Now we will add to the new scene a couple of moving objects: stars and dragons. After collecting a star, a new sound will be played, and as a new feature, we will be protected for several seconds so that in case we hit a killing object, we remain unaffected. About the dragons, we may get the same behaviour as if we hit the bombs or the fires.

Proposed exercise: Adding the stars

Add or modify all the required code that will be in charge of creating the stars in the new scene and that will also handle the events related to the new objects (playing sound, increasing lives, etc.). You will have to follow the steps below.

You can see the expected result here.
  1. Create the new variables (the number of stars to be displayed, and the timeout to be protected when you are hit by the bombs, the fires, or the dragons):
const numStars = 50;
var timeout = false;
  1. Initialize the sound to be played when collecting the stars, just by adding a single line inside the existing “initSounds()” function. You might also need to adjust the volume by using the right value from 0.1 to 1:
function initSounds() {
    bell2 = this.sound.add('bell2', { volume: 0.2 });
    ...
}
  1. Add the new functions to your code. The “collectStar()” function will print each star, the “protect()” function will activate a player’s protection to avoid being killed for a while, and the “collectStar()” function will contain the code be executed each time you collect a star:
function createStar() {
    const x = Phaser.Math.Between(worldWidth, worldWidth * 2);
    const vX = Phaser.Math.Between(-velocityX, velocityX);
    const vY = Phaser.Math.Between(velocityY/2, velocityY);
    let star = this.physics.add.image(x, 0, 'star').setScale(0.5).setBounce(1).setCollideWorldBounds(true).setVelocity(vX, vY);
    star.body.setAllowGravity(false);
    this.physics.add.collider(star, platform);
    this.physics.add.collider(star, platform2);
    this.physics.add.collider(player, star, collectStar, null, this);
}

function protect(color) {
    player.setTint(color);
    if (timeout) clearTimeout(timeout);
    timeout = setTimeout(() => { timeout=false; player.clearTint() }, 3000);
}

function collectStar(player, star) {
    bell2.play();
    star.destroy();
    createStar.call(this);

    protect(0xFFFF00);

    score += 10;
    if (score % 100 == 0) lives++;
    showScore();
}
  1. And finally insert the loop to create all the stars at once inside the “create()” function:
funtion create() {
    ...
    for (i = 0; i < numStars; i++) createStar.call(this);
}

Proposed exercise: Adding the dragons

Add or modify all the required code that will be in charge of creating the dragons in the new scene and that will also handle the events related to them (playing sound, decreasing lives, etc.). You will have to follow the steps below.

You can see the expected result here.
  1. Insert at the top of your code a new constat to set the number of dragons you would like to print:
const numDragons = 25;
  1. Add the “flying” animation to the function “createAnimations()”:
function createAnimations() {
    ...
    // Dragon
    this.anims.create({
        key: 'flying',
        frames: this.anims.generateFrameNumbers('dragon', { start: 0, end: 11 }),
        frameRate: 5,
        repeat: -1
    });
}
  1. Add the new functions “createDragon()” and “hitDragon()” to your code, to display each dragon and to perform any action we want to be done after being hit:
function createDragon() {
    const x = Phaser.Math.Between(worldWidth, worldWidth * 2);
    const y = Phaser.Math.Between(0, screenHeight - platformHeight);
    const v = Phaser.Math.Between(velocityY/2, velocityY);
    let dragon = this.physics.add.sprite(x, y, 'dragon').setOrigin(1).setSize(72, 64).setScale(2).anims.play('flying', true).setBounce(1).setVelocity(0, v);
    dragon.body.setAllowGravity(false).setCollideWorldBounds(true);
    this.physics.add.collider(dragon, platform2);
    this.physics.add.collider(player, dragon, hitDragon, null, this);
}

function hitDragon(player, dragon) {
    dead.play();
    lives--;
    showScore();

    protect(0xFF0000);

    if (lives == 0) {
        this.physics.pause();
        gameOver = true;
        this.add.image(screenWidth/2, screenHeight/2, 'restart').setScale(5).setScrollFactor(0).setInteractive().on('pointerdown', () => location.reload());
    }
    else {
        dragon.destroy();
    }
}
  1. And finally, add a new loop to the “create()” function to print all the dragons at once:
function create() {
    ...
    for (i = 0; i < numDragons; i++) createDragon.call(this);
}

Enjoy the game!

You may enjoy playing this wonderful game online here.

Phaser. Unit 1. Your first game

Introduction

Phaser is one of the best frameworks to develop Desktop and mobile HTML games. You will be able to create 2D games and make them available to everyone with a simple domain with less than 10MB storage capacity. Also, it is fast, free, and opensource!

You may find more information at the Phaser official page and you may also have a look at some interesting demonstrations at the examples page.

HTML and CSS code

The first thing we should do is linking the Phaser library. We will use the last version at the time this unit has been written:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>

After that we are going to insert some CSS code to remove any margin and padding, and also to remove the scrollbars, so that all the window size is used when the game is started:

html, body {
    margin: 0px;
    padding: 0px;
    overflow: hidden;
    height: 100%;
} 

And finally we are going to use a single file to put all the code inside. This is going to be the basic structure of the “.html” file:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>First game with Phaser 3</title>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>
    <style type="text/css">
        html, body {
            margin: 0px;
            padding: 0px;
            overflow: hidden;
            height: 100%;
        }
    </style>
</head>
<body>
    <script type="text/javascript">
        ...
    </script>
</body>
</html>

Constants

We are going to define some constants at the beginning of the code so that we can easily change the game appearance, difficulty, and some other parameters:

// The game will use the whole window
const screenWidth = window.innerWidth;
const screenHeight = window.innerHeight;

// The world width will be 10 times the size of the screen
const worldWidth = screenWidth*10;

// Height of platform and joystick size (at the bottom)
const platformHeight = screenHeight/6;
const joystickSize = platformHeight/3;

// Both x and y velocity (depending on the screen size) 
const velocityX = screenWidth/4;
const velocityY = screenHeight/2;

// Number of clouds, tries, fires and coins
const numClouds = 50;
const numTrees = 100;
const numFires = 25;
const numCoins = 50;

Phaser configuration and other variables

Following the constants we are going to create some variables so keep all the information about the game and also to start the initialization of Phaser:

var config = {
    type: Phaser.AUTO,
    width: screenWidth,
    height: screenHeight,
    physics: {
        default: 'arcade',
        arcade: {
            gravity: { y: velocityY }
        }
    },
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};

var platform, player, bell, dead;
var cursors, joyStick;
var score = 0, lives = 5, scoreText = null;
var gameOver = false;

// Init Phaser
var game = new Phaser.Game(config);

The assets (images, sprites and sounds)

We will need some images, sprites and sounds. You may use any assets you like, but we are also providing some examples so that you can easily start testing (they can also be downloaded here):

Images

Cloud

Tree
Bomb
Restart button

Sprites (player and animated objects)

Player
Fire
Coin

Sounds and music

When the player gets a coin
When the user gets hurt by a bomb or a fire
Background music always playing

Loading all the assets

So that we can use all those images, sprites and music, they have to be loaded previously. That is the purpose of the “preload()” function. We will also include here the initialization of the joystick so that we can use it in another section of this unit.

Proposed exercise: Game initialization

Create a specific folder for your game in your domain to put all the code and assets inside. After that, create an “index.html” inside that folder with the code below, and also create an “assets” folder to put all the images and sounds, which can be downloaded here. After uploading everything to your domain, test the game using the url of that folder in your browser, and you should get a full black screen. Click on it and the background music should start playing.

You can see the result here (you can also look at the source code by pressing Ctrl+U).
<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <title>First game with Phaser 3</title>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>
    <style type="text/css">
        html, body {
            margin: 0px;
            padding: 0px;
            overflow: hidden;
            height: 100%;
        }
    </style>
</head>

<body>
    <script type="text/javascript">
        const screenWidth = window.innerWidth;
        const screenHeight = window.innerHeight;

        const worldWidth = screenWidth * 10;

        const platformHeight = screenHeight / 6;
        const joystickSize = platformHeight / 3;

        const velocityX = screenWidth / 4;
        const velocityY = screenHeight / 2;

        const numTrees = 100;
        const numClouds = 50;
        const numFires = 25;
        const numCoins = 50;

        var config = {
            type: Phaser.AUTO,
            width: screenWidth,
            height: screenHeight,
            physics: {
                default: 'arcade',
                arcade: {
                    gravity: { y: velocityY }
                }
            },
            scene: {
                preload: preload,
                create: create,
                update: update
            }
        };

        var platform, player, bell, dead;
        var cursors, joyStick;
        var score = 0, lives = 5, scoreText = null;
        var gameOver = false;

        var game = new Phaser.Game(config);

        function preload() {
            // Load joyStick
            this.load.plugin('rexvirtualjoystickplugin', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/rexvirtualjoystickplugin.min.js', true);
       
            // Load images
            this.load.image('cloud', 'assets/cloud.png');
            this.load.image('tree', 'assets/tree.png');
            this.load.image('bomb', 'assets/bomb.png');
            this.load.image('restart', 'assets/restart.png');
            this.load.spritesheet('dude', 'assets/dude.png', { frameWidth: 32, frameHeight: 48 });
            this.load.spritesheet('fire', 'assets/fire.png', { frameWidth: 32, frameHeight: 32 });
            this.load.spritesheet('coin', 'assets/coin.png', { frameWidth: 32, frameHeight: 32 });

            // Load sounds and music
            this.load.audio('bell', 'assets/ding.mp3');
            this.load.audio('dead', 'assets/dead.mp3');
            this.load.audio('music', 'assets/music.mp3');
        }

        function initSounds() {
            bell = this.sound.add('bell');
            dead = this.sound.add('dead');
            this.sound.add('music', {volume:0.5}).play({ loop: -1 });
        }

        function create() {
            initSounds.call(this);
        }

        function update() {
        }
    </script>
</body>
</html>

Proposed exercise: Using your own music

Download any other music file you like to use it as a background music and change the JavaScript code inside the “preload()” function to set the file name accordingly. Also in the “initSounds()” function you will find that you may change the volume of the music (volume:0.5). Try changing that value from 0.1 to 1 and you will notice that the volume of the music will also change.

You may find in the following sites some free images and sounds which you can download and use for your own game:

Sky and platform

Once we have initialized Phaser and after loading all the assets, we can go ahead and paint something on the screen. We are going the create a couple of rectangles filled with any color we like, and this way we will create the sky and the platform.

We will use the following colors (you may find more information about colors in hexadecimal notation here):

  • Sky (light blue): 0x87CEEB
  • Platform (brown): 0xB76743

Proposed exercise: Adding sky and platform

Add the function “createWorld()” (provided below) to your code, and modify the calling function “create()” so that it is called when the game is started (also shown below). Finally test everything in your browser from your domain. You may also change the colors of both the platform and the sky.

You may easily get the hexadecimal code of any color you like with a simple color picker. About the music, do not forget to click on the screen after loading the game (the music will not start playing unless the browser gets the focus). Also, you can see the result here (you can look at the source code by pressing Ctrl+U).
  1. This is the new function you have to insert in your code to create the world:
function createWorld() {
    this.cameras.main.setBounds(0, 0, worldWidth, screenHeight);
    this.physics.world.setBounds(0, 0, worldWidth, screenHeight);

    // Sky
    sky = this.add.rectangle(0, 0, worldWidth, screenHeight, 0x87CEEB).setOrigin(0);

    // Platform
    platform = this.add.rectangle(0, screenHeight, worldWidth, platformHeight, 0xB76743).setOrigin(1);
    this.physics.add.existing(platform);
    platform.body.setCollideWorldBounds(true);
}
  1. Do not forget to update the “create()” function to create the world (sky, platform, camera, and world boundaries). This is the only line you should add to that function:
function create() {
    ...
    createWorld.call(this);            
}

Objects in background (trees)

We will first create the trees so that they remain in the background. We may create all of them using the same image. We will just change the size of the trees by scaling them randomly, and we will also choose a random position for each of them. Finally we will use a loop to print as many trees as we like.

Proposed exercises: Adding the trees

Add the code below to your file and check the results. After that, change the value of the constant “numTrees” (at the top of your code) and also the value of the “scale” variable to check how easily you can customize your game. Refresh the page several times and you will notice that the generated trees have random sizes and they also appear in random positions. Finally, look for another image (a tree or any other object) and change the code accordingly to use your own image.

You can see the result here (you can look at the source code by pressing Ctrl+U). Also, as listed above, you may find many free assets in OpenGameArt.
  1. This is the new function you have to insert in your code to create each tree:
function createTree() {
    const x = Phaser.Math.Between(0, worldWidth);
    const y = screenHeight-platformHeight;
    const scale = Phaser.Math.FloatBetween(0.5, 2);
    this.add.image(x, y, 'tree').setOrigin(1).setScale(scale);
}
  1. Do not forget to update the “create()” function to create all the trees. You only need one single line to create all trees at once using a loop:
function create() {
    ...
    for(i=0; i<numTrees; i++) createTree.call(this);          
}

The animations (sprites)

We will use sprites to create the player and the animated objects. For example, let’s have a look at the player:

Sprite with three player animations

We may appreciate that we have 9 pictures divided into 3 different groups. This way, we may easily create all three animations:

  • Left: Pictures from 0 to 3
  • Turn: Picture number 4
  • Right: Pictures from 5 to 8

Controlling the player’s animations

So that this game can be used in both desktop and mobile devices, we have to initialize both the cursor keys and the joystick. Once this is done, we must change the player’s velocity and the current animation when the user presses the keys or moves the joystick. In the next exercise we will first define the animations inside the “createAnimations()” function, and after that we will go ahead with the “createPlayer()” function, where we will display the player. We will finally include in the “update()” function some conditions to adjust the player’s speed and current animation.

Proposed exercise: Adding the player

Add the code below to your game and check both the player animations and movements. After that, look for another sprite and change the code accordingly to use your own sprite. Finally, change the values of the constants “velocityX” and “velocityY” (at the top of your code) and check the results.

You can see the result here (you can look at the source code by pressing Ctrl+U). Also, as listed above, you may find many free assets in OpenGameArt and choose other sprites you like.
  1. These are the new functions you have to insert in your code to create the animations and initialize everything related to the player:
function createAnimations() {
    // Player
    this.anims.create({
        key: 'left',
        frames: this.anims.generateFrameNumbers('dude', { start: 0, end: 3 }),
        frameRate: 10,
        repeat: -1
    });
    this.anims.create({
        key: 'turn',
        frames: [{ key: 'dude', frame: 4 }]
    });
    this.anims.create({
        key: 'right',
        frames: this.anims.generateFrameNumbers('dude', { start: 5, end: 8 }),
        frameRate: 10,
        repeat: -1
    });
    // Fire
    this.anims.create({
        key: 'burning',
        frames: this.anims.generateFrameNumbers('fire', { start: 0, end: 4 }),
        frameRate: 10,
        repeat: -1
    });
    // Coin
    this.anims.create({
        key: 'rotate',
        frames: this.anims.generateFrameNumbers('coin', { start: 0, end: 7 }),
        frameRate: 10,
        repeat: -1
    });            
}

function createPlayer() {
    player = this.physics.add.sprite(0, screenHeight-platformHeight, 'dude').setOrigin(1).setScale(3).setBounce(0.2).setCollideWorldBounds(true);
    this.physics.add.collider(player, platform);
    this.cameras.main.startFollow(player, true, 0.05, 0.05);
    // Cursor keys and joystick
    cursors = this.input.keyboard.createCursorKeys();
    joyStick = this.plugins.get('rexvirtualjoystickplugin').add(this, {
        x: screenWidth / 2,
        y: screenHeight - joystickSize*1.5,
        radius: joystickSize
    }).on('update', update, this);
}
  1. Also you have to insert some code inside the “update()” function to respond to the cursor keys and the joystick:
function update() {
    if (gameOver) return;

    if (cursors.left.isDown || joyStick.left) {
        player.setVelocityX(-velocityX).anims.play('left', true);
    }
    else if (cursors.right.isDown || joyStick.right) {
        player.setVelocityX(velocityX).anims.play('right', true);
    }
    else {
        player.setVelocityX(0).anims.play('turn');
    }

    if ((cursors.up.isDown || joyStick.up) && player.body.touching.down) {
        player.setVelocityY(-velocityY);
    }
}
  1. And finally do not forget to update the “create()” function to create the animations and the player. You just have to insert a couple of new lines:
function create() {
    ...
    createAnimations.call(this); 
    createPlayer.call(this);
}

The clouds, the score, the fires and the coins

We will create many different objects (clouds, fires and coins) at once using the same images. We will just change the size of the objects by scaling them randomly, and we will use a loop to print as many objects as we like choosing random positions.

Proposed exercises: Adding the clouds

Add the code below to your file and check the results. After that, change the value of the constant “numClouds” (at the top of your code) and also the value of the “scale” variable to check how easily you can customize your game. Refresh the page several times and you will notice that the generated clouds and also the trees have random sizes and they appear in random positions. Finally, look for another image (a cloud or any other object) and change the code accordingly to use your own image.

You can see the result here (you can look at the source code by pressing Ctrl+U). Also, as listed above, you may find many free assets in OpenGameArt and choose the images you like.
function createCloud() {
    const x = Phaser.Math.Between(0, worldWidth);
    const y = Phaser.Math.Between(0, screenHeight-platformHeight*2);
    const scale = Phaser.Math.FloatBetween(0.5, 1.5);
    this.add.image(x, y, 'cloud').setScale(scale);
}
function create() {
    ...
    for(i=0; i<numClouds; i++) createCloud.call(this);
}

Proposed exercise: Adding the score

Using the global variables, we may easily show both the score and number of remaining lives. Add the code below to your game and check the results. After that, change the value of the constant “numLives” (at the top of your code) and also the value of the “scale” variable to check how easily you can customize that text.

You can see the result here (you can look at the source code by pressing Ctrl+U).
function showScore() {
    if (!scoreText) scoreText = this.add.text(16, 16, '', { fontSize:(screenWidth/20)+'px', fill:'#000' }).setScrollFactor(0);
    scoreText.setText('Score:' + score + ' / Lives:' + lives);
}
function create() {
    ...
    showScore.call(this);
}

Proposed exercises: Adding the fires and the coins

Add the code below to your game and check the results. After that, change the value of the constants “numFires” and “numCoins” (at the top of your code) and also the value of the “scale” variables to check how easily you can customize your game. Finally, look for another sprites (fires, coins, or any other objects) and change the code accordingly to use your own sprites.

You may click here to see how the game looks like. As listed above, you may find many free assets in OpenGameArt.
function createFire() {
    const x = Phaser.Math.Between(screenWidth/2, worldWidth);
    const y = Phaser.Math.Between(screenHeight-platformHeight, screenHeight);
    let fire = this.physics.add.sprite(x, y, 'fire').setOrigin(1).setScale(3).setImmovable(true).anims.play('burning', true)
    fire.body.setAllowGravity(false);
    this.physics.add.collider(player, fire, hitBombOrFire, null, this);   
}

function createCoin() {     
    const x = Phaser.Math.Between(screenWidth/2, worldWidth);
    const bounce = Phaser.Math.FloatBetween(0.1, 0.5);
    let coin = this.physics.add.sprite(x, 0, 'coin').setOrigin(1).setScale(3).setBounce(bounce).anims.play('rotate');
    coin.body.setOffset(0, -10);
    this.physics.add.collider(coin, platform);
    this.physics.add.overlap(player, coin, collectCoin, null, this);
}

function createBomb() {
    const x = Phaser.Math.Between(0, worldWidth);
    const v = Phaser.Math.Between(-velocityX, velocityX);
    let bomb = this.physics.add.image(x, 0, 'bomb').setScale(2).setBounce(1).setCollideWorldBounds(true).setVelocity(v, velocityY);
    bomb.body.setAllowGravity(false);
    this.physics.add.collider(bomb, platform);     
    this.physics.add.collider(player, bomb, hitBombOrFire, null, this);   
}

function collectCoin(player, coin) { 
    bell.play();
    coin.destroy();
    createCoin.call(this);
    createBomb.call(this);    

    score += 10;
    if (score % 100 == 0) lives++;
    showScore();
}

function hitBombOrFire(player, thing) {
    dead.play();
    lives--;
    showScore();
    player.setTint(0xff0000).anims.play('turn');

    if (lives == 0) {
        this.physics.pause();
        gameOver = true;
        this.add.image(screenWidth/2, screenHeight/2, 'restart').setScale(5).setScrollFactor(0).setInteractive().on('pointerdown', ()=>location.reload());                
    }
    else {
        thing.destroy();
        setTimeout(()=>player.clearTint(), 3000);
    }
}
function create() {
    ...
    for(i=0; i<numCoins; i++) createFire.call(this);
    for(i=0; i<numCoins; i++) createCoin.call(this);
}

Enjoy the game!

You may enjoy playing this wonderful game online here.

CSS. Unit 6. Flexbox.

Why flexbox?

Flexbox is a one-dimensional layout method for laying out items in rows or columns. Items flex to fill additional space and shrink to fit into smaller spaces. This unit explains all the fundamentals.

For a long time, the only reliable cross browser-compatible tools available for creating CSS layouts were things like floats and positioning. These are fine, and they work, but in some ways they are also rather limiting and frustrating.

The following simple layout requirements are either difficult or impossible to achieve with such tools, in any kind of convenient, flexible way:

  • Vertically centering a block of content inside its parent.
  • Making all the children of a container take up an equal amount of the available width/height, regardless of how much width/height is available.
  • Making all columns in a multiple-column layout adopt the same height even if they contain a different amount of content.

As you’ll see in subsequent sections, flexbox makes a lot of layout tasks much easier. Let’s dig in!

Introducing a simple example

In this unit we are going to get you to work through a series of exercises to help you understand how flexbox works. To get started, we will use some simple code like this one:

<header>
  <h1>Valencian Community</h1>
</header>

<section>
  <article>
    <h2>Alicante</h2>
    <p>The campus of the University of Alicante lies in San Vicente del Raspeig, bordering the city of Alicante to the north. More than 25,000 students attend the University.</p>
  </article>

  <article>
    <h2>Valencia</h2>
    <p>Valencia is the capital of the autonomous community of Valencia and the third-largest city in Spain after Madrid and Barcelona, surpassing 800,000 inhabitants in the municipality.</p>
  </article>

  <article>
    <h2>Castellón</h2>
    <p>The city is notorious for its music festivals, among which we find: the Tanned Tin music festival for alternative music, the Benicàssim's International Festival, the Arenal Sound and the Rototom Sunsplash Festival, known for its reggae music.</p>
  </article>
</section>
html {
  font-family: sans-serif;
}

body {
   margin: 0;
}

header {
  background: purple;
  height: 75px;
}

h1 {
  text-align: center;
  color: white;
  line-height: 75px;
  margin: 0;
}

article {
  padding: 1%;
  margin: 1%;
  background: aqua;
}

Valencian Community

Alicante

The campus of the University of Alicante lies in San Vicente del Raspeig, bordering the city of Alicante to the north. More than 25,000 students attend the University.

Valencia

Valencia is the capital of the autonomous community of Valencia and the third-largest city in Spain after Madrid and Barcelona, surpassing 800,000 inhabitants in the municipality.

Castellón

The city is notorious for its music festivals, among which we find: the Tanned Tin music festival for alternative music, the Benicàssim’s International Festival, the Arenal Sound and the Rototom Sunsplash Festival, known for its reggae music.

You’ll see that we have a <header> element with a top level heading inside it, and a <section> element containing three <article>s. We are going to use these to create a fairly standard three column layout.

Proposed exercise: Simple boxes

Using the code of the previous example, create a new webpage with a similar content. You can use any other colors and styles you like.

Specifying what elements to lay out as flexible boxes

To start with, we need to select which elements are to be laid out as flexible boxes. To do this, we set a special value of display on the parent element of the elements you want to affect. In this case we want to lay out the <article> elements, so we set this on the <section>:

section {
  display: flex;
}

This causes the <section> element to become a flex container, and its children to become flex items. The result of this should be something like so:

Valencian Community

Alicante

The campus of the University of Alicante lies in San Vicente del Raspeig, bordering the city of Alicante to the north. More than 25,000 students attend the University.

Valencia

Valencia is the capital of the autonomous community of Valencia and the third-largest city in Spain after Madrid and Barcelona, surpassing 800,000 inhabitants in the municipality.

Castellón

The city is notorious for its music festivals, among which we find: the Tanned Tin music festival for alternative music, the Benicàssim’s International Festival, the Arenal Sound and the Rototom Sunsplash Festival, known for its reggae music.

So, this single declaration gives us everything we need — incredible, right? We have our multiple column layout with equal-sized columns, and the columns are all the same height. This is because the default values given to flex items (the children of the flex container) are set up to solve common problems such as this.

To be clear, let’s reiterate what is happening here. The element we’ve given a   display value of flex to is acting like a block-level element in terms of how it interacts with the rest of the page, but its children are being laid out as flex items. The next section will explain in more detail what this means. Note also that you can use a display value of inline-flex if you wish to lay out an element’s children as flex items, but have that element behave like an inline element.

Proposed exercise: Flexible boxes

Add some CSS code to the previous exercise so that all the contents inside the <section> element are displayed as flexible boxes (display:flex) which are shown from left to right, all of them having the same height.

As explained, you only have to add three lines of code and everything is done! You will have flexible boxes:
section {
  display: flex;
}

The flex model

When elements are laid out as flex items, they are laid out along two axes:

  • The main axis is the axis running in the direction the flex items are being laid out in (e.g. as rows across the page, or columns down the page.) The start and end of this axis are called the main start and main end.
  • The cross axis is the axis running perpendicular to the direction the flex items are being laid out in. The start and end of this axis are called the cross start and cross end.
  • The parent element that has display: flex set on it (the <section> in our example) is called the flex container.
  • The items being laid out as flexible boxes inside the flex container are called flex items (the <article> elements in our example).

Bear this terminology in mind as you go through subsequent sections. You can always refer back to it if you get confused about any of the terms being used.

Columns or rows?

Flexbox provides a property called flex-direction that specifies what direction the main axis runs in (what direction the flexbox children are laid out in). By default this is set to row, which causes them to be laid out in a row in the direction your browser’s default language works in (usually from left to right), but you can also use the following declaration inside your <section> rule:

flex-direction: column;

This would put the items back in a column layout, much like they were before we added any CSS.

Wrapping

One issue that arises when you have a fixed amount of width or height in your layout is that eventually your flexbox children will overflow their container, breaking the layout. And also if you have many boxes, the final result will be presented with some quite narrow columns. Have a look at our example:

Valencian Community

Alicante

The campus of the University of Alicante lies in San Vicente del Raspeig, bordering the city of Alicante to the north. More than 25,000 students attend the University.

Valencia

Valencia is the capital of the autonomous community of Valencia and the third-largest city in Spain after Madrid and Barcelona, surpassing 800,000 inhabitants in the municipality.

Castellón

The city is notorious for its music festivals, among which we find: the Tanned Tin music festival for alternative music, the Benicàssim’s International Festival, the Arenal Sound and the Rototom Sunsplash Festival, known for its reggae music.

Alcoy

Alcoy is an industrial and university city, region and municipality located in the province of Alicante. The Serpis river crosses the municipal boundary.

Benidorm

It has been a tourist destination since its port was extended and the first hotels were built. Today it is known for its beaches and skyscrapers and receives as many tourists from abroad as from Spain.

Here we see that the children are indeed breaking out of their container for small screens of filling the window with narrow columns. One way in which you can fix this is to add the following declaration to your <section> rule:

section {
  ...
  flex-wrap: wrap;
}

Also, add the following declaration to your <article> rule:

article {
  ...
  flex: 48%;
}

You’ll see that the layout looks much better with this included:

Valencian Community

Alicante

The campus of the University of Alicante lies in San Vicente del Raspeig, bordering the city of Alicante to the north. More than 25,000 students attend the University.

Valencia

Valencia is the capital of the autonomous community of Valencia and the third-largest city in Spain after Madrid and Barcelona, surpassing 800,000 inhabitants in the municipality.

Castellón

The city is notorious for its music festivals, among which we find: the Tanned Tin music festival for alternative music, the Benicàssim’s International Festival, the Arenal Sound and the Rototom Sunsplash Festival, known for its reggae music.

Alcoy

Alcoy is an industrial and university city, region and municipality located in the province of Alicante. The Serpis river crosses the municipal boundary.

Benidorm

It has been a tourist destination since its port was extended and the first hotels were built. Today it is known for its beaches and skyscrapers and receives as many tourists from abroad as from Spain.

We now have multiple rows — as many flexbox children are fitted onto each row as makes sense, and any overflow is moved down to the next line. The flex:48% declaration set on the articles means that each will be at least half the window size (48% wide, since we have 1% margins on both left and right). You might also notice that the last few children on the last row are each made wider so that the entire row is still filled.

But there’s more we can do here. For example, you can try changing your flex-direction property value to row-reverse. Now you’ll see that you still have your multiple row layout, but it starts from the opposite corner of the browser window and flows in reverse.

Proposed exercise: Wrapped content

Using the code of the last example above, add three more towns you like (to show at least 9 towns in total), and also insert a picture for each one. You may also change the colors of the boxes and text as you like, or you can even add more styles to your code (box and text shadows, etc.).

For example:

Valencian Community

Alicante

The campus of the University of Alicante lies in San Vicente del Raspeig, bordering the city of Alicante to the north. More than 25,000 students attend the University.

Valencia

Valencia is the capital of the autonomous community of Valencia and the third-largest city in Spain after Madrid and Barcelona, surpassing 800,000 inhabitants in the municipality.

Castellón

The city is notorious for its music festivals, among which we find: the Tanned Tin music festival for alternative music, the Benicàssim’s International Festival, the Arenal Sound and the Rototom Sunsplash Festival, known for its reggae music.

Benidorm

It has been a tourist destination since its port was extended and the first hotels were built. Today it is known for its beaches and skyscrapers and receives as many tourists from abroad as from Spain.

CSS. Unit 5. The box model.

Introduction

Everything in CSS has a box around it, and understanding these boxes is key to being able to create layouts with CSS, or to align items with other items. In this lesson, we will take a proper look at the CSS Box Model so that you can build more complex layout tasks with an understanding of how it works and the terminology that relates to it.

What is the CSS box model?

The full CSS box model applies to block boxes. This model defines how the different parts of a box (margin, border, padding, and content) work together to create a box that you can see on the page.

Parts of a box

When making up a block box in CSS we will have to keep in mind the following parts:

  • Content box: The area where your content is displayed, which can be sized using properties like width and height.
  • Padding box: The padding sits around the content as white space; its size can be controlled using padding and related properties.
  • Border box: The border box wraps the content and any padding. Its size and style can be controlled using border and related properties.
  • Margin box: The margin is the outermost layer, wrapping the content, padding and border as whitespace between this box and other elements. Its size can be controlled using margin and related properties.

The below diagram shows these layers:

In the standard box model, if you give a box a width and a height attribute, this defines the width and height of the content box. Any padding and border is then added to that width and height to get the total size taken up by the box. This is shown in the image below.

If we assume that the box has the following CSS defining widthheightmarginborder, and padding:

.box {
  width: 350px;
  height: 150px;
  margin: 10px;
  padding: 25px;
  border: 5px solid black;
}

The space taken up by our box using the standard box model will actually be 410px (350 + 25 + 25 + 5 + 5), and the height 210px (150 + 25 + 25 + 5 + 5), as the padding and border are added to the width used for the content box:

The margin is not counted towards the actual size of the box. Sure, it affects the total space that the box will take up on the page, but only the space outside the box. The box’s area stops at the border (it does not extend into the margin).

Proposed exercise: Playing with box models

In the below example, you can see a couple of boxes with a CSS class which gives some values to width, height, marginborder, and padding. Using that code, create a web page with five different boxes, each one with different styles. You have to change also the color of the text (you can choose the colors your like). And finally add some shadows to the text using different styles and colors.

You can have a look at the different colors here: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value

CSS code:

.box1 {
  border: 5px solid red;
  background-color: lightgray;
  padding: 10px;
  margin: 25px;
  width: 250px;
  height: 200px;
  ...
}

.box2 {
  border: 7px solid green;
  background-color: gray;
  padding: 5px;
  margin: 0px;
  width: 300px;
  height: 200px;
  color: white;
  ...
}

...

HTML code:

<div class="box1">This is a 250x200px box with a 5px red border, and lightgray background, 10px padding and 25px margin.</div>
<div class="box2">This is a 300x200px box with a 7px green border, gray background, white text color, 5px padding and 0px margin.</div>
...

And the result:

This is a 250x200px box with a 5px red border, and lightgray background, and 10px padding and 25px margin.
This is a 300x200px box with a 7px green border, gray background, white text color, and 5px padding and 0px margin.

Margins, padding and borders

You’ve already seen the marginpadding, and border properties at work in the example above. The properties used in that example are shorthands and allow us to set all four sides of the box at once. These shorthands also have equivalent longhand properties, which allow control over the different sides of the box individually.

Let’s explore these properties in more detail.

Margin

The margin is an invisible space around your box. It pushes other elements away from the box. Margins can have positive or negative values. Setting a negative margin on one side of your box can cause it to overlap other things on the page.

We can control all margins of an element at once using the margin property, or each side individually using the equivalent longhand properties:

Proposed exercise: Custom margins

Create a web page with the example below, and change the margin values to see how the boxes are pushed around due to the margin creating or removing space (if it is a negative margin) between this element and the containing element. Finally, add three more boxes with any style you like inside the same container.

.container {
  border: 5px solid red; 
}

.box1 {
  border: 5px solid green;
  margin-top: 10px;
  margin-left: 10px;
  width: 50%;
}

.box2 {
  border: 5px solid blue;
  margin-top: 10px;
  margin-left: 50px;
  width: 50%;
}

.box3 {
  border: 5px solid orange;
  margin-top: 10px;
  margin-left: 100px;
  width: 50%;
}
...
<div class="container">
  <div class="box1">This is a box with 50% width and 10px from top and left inside a container</div>
  <div class="box2">This is a box with 50% width and 10px from top and 50px left inside a container</div>
  <div class="box3">This is a box with 50% width and 10px from top and 100px left inside a container</div>
  ...
</div>
This is a box with 50% width and 10px from top and left inside a container
This is a box with 50% width and 10px from top and 50px left inside a container
This is a box with 50% width and 10px from top and 100px left inside a container

Padding

The padding sits between the border and the content area. Unlike margins you cannot have negative amounts of padding, so the value must be 0 or a positive value. Any background applied to your element will display behind the padding, and it is typically used to push the content away from the border.

We can control the padding on each side of an element individually using the padding property, or each side individually using the equivalent longhand properties:

Proposed exercise: Custom padding

If you change the values for padding on the classes .box in the example below you can see that this changes where the text begins in relation to the box. Padding can be changed on any element, and will make space between its border and whatever is inside the element. Create a web page with the code below and add three more boxes using different paddings and styles.

.box1 {
  border: 5px solid red;
  margin: 10px;
  ...
}

.box2 {
  border: 5px solid blue;
  margin: 10px;
  padding: 25px;
  ...
}

.box3 {
  border: 5px solid green;
  margin: 10px;
  padding: 50px;
  ...
}
...
<div class="box1">
  This is a box without padding
</div>
<div class="box2">
  This is a box with 25 px padding
</div>
<div class="box3">
  This is a box with 50px padding
</div>
...
This is a box without padding
This is a box with 25 px padding
This is a box with 50px padding

Borders

The border is drawn between the margin and the padding of a box. If you are using the standard box model, the size of the border is added to the width and height of the box.

For styling borders, there are a large number of properties (there are four borders, and each border has a style, width and color that we might want to manipulate).

You can set the width, style, or color of all four borders at once using the border property.

To set the properties of each side individually, you can use:

To set the width, style, or color of all sides, use the following:

To set the width, style, or color of a single side, you can use one of the most granular longhand properties:

Proposed exercise: Custom borders

In the example below we have used various shorthands and longhands to style the borders. Create a new web page with that code and have a play around with the different properties to check that you understand how they work. Finally add three more boxes and set the border styles as you like.

You can have a look at the MDN pages (linked above) to get information about the different styles of border you can choose from. Also you can have a look at the different colors here: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value
.box1 {
  border-top: 5px dotted green;
  border-right: 5px dashed orange;
  border-bottom: 5px dotted red;
  border-left: 5px dashed blue;
  padding: 15px;
  margin-top: 15px;
  margin-bottom: 15px;
  text-align: center;
  ...
}

.box2 {
  border: 5px solid black;
  border-left-width: 10px;
  border-right-width: 10px;
  border-top-color: aqua;
  border-bottom-color: pink;
  padding: 15px;
  text-align: center;
  ...
}

.box3 {
  border: 2px solid black;
  padding: 10px;
  ...
}

...
<div class="box1">This is a box with different types of borders</div>
<div class="box2">This is another box with different types of borders</div>
<div class="box1">
  <div class="box2">This is a box with different types of borders inside another box</div>
</div>
<div class="box3">
  <div class="box3">
    <div class="box3">
      <div class="box3">
        <div class="box3">
          <div class="box3">
            These are boxes inside boxes
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
...
This is a box with different types of borders
This is another box with different types of borders
This is a box with different types of borders inside another box
These are boxes inside boxes

CSS. Unit 4. Styling lists.

Introduction

Lists behave like any other text for the most part, but there are some CSS properties specific to lists that you need to know about, and some best practices to consider. This unit explains all.

A simple list example

To begin with, let’s look at a simple example with lists. Throughout this unit, we’ll look at unordered, ordered, and description lists (all have styling features that are similar, and some that are particular to their type of list). The HTML for our example looks like so:

<h2>Shopping (unordered list)</h2>

<ul>
  <li>Hummus</li>
  <li>Pita</li>
  <li>Green salad</li>
  <li>Halloumi</li>
</ul>

<h2>Recipe (ordered list)</h2>

<ol>
  <li>Toast pita, leave to cool, then slice down the edge.</li>
  <li>Fry the halloumi in a shallow, non-stick pan, until browned on both sides.</li>
  <li>Wash and chop the salad.</li>
  <li>Fill pita with salad, hummus, and fried halloumi.</li>
</ol>

<h2>Ingredients (description list)</h2>

<dl>
  <dt>Hummus</dt>
  <dd>A thick dip/sauce generally made from chick peas blended with tahini, lemon juice, salt, garlic, and other ingredients.</dd>
  <dt>Pita</dt>
  <dd>A soft, slightly leavened flatbread.</dd>
  <dt>Halloumi</dt>
  <dd>A semi-hard, unripened, brined cheese with a higher-than-usual melting point, usually made from goat/sheep milk.</dd>
  <dt>Green salad</dt>
  <dd>That green healthy stuff that many of us just use to garnish kebabs.</dd>
</dl>

The list elements probably will have the following styling defaults:

  • The <ul> and <ol> elements have a top and bottom margin of 16px (1em)  and a padding-left of 40px (2.5em).
  • The list items (<li> elements) have no set defaults for spacing.
  • The <dl> element has a top and bottom margin of 16px (1em), but no padding set.
  • The <dd> elements have margin-left of 40px (2.5em).
  • The <p> elements we’ve included for reference have a top and bottom margin of 16px (1em), the same as the different list types.

Proposed exercise: Lists with default styling

Create a web page with the list of the previous example, and check the result in your browser. It should be displayed with the default styling (no CSS must be used right now).

You should get something like this:

Shopping (unordered list)

  • Hummus
  • Pita
  • Green salad
  • Halloumi

Recipe (ordered list)

  1. Toast pita, leave to cool, then slice down the edge.
  2. Fry the halloumi in a shallow, non-stick pan, until browned on both sides.
  3. Wash and chop the salad.
  4. Fill pita with salad, hummus, and fried halloumi.

Ingredients (description list)

Hummus
A thick dip/sauce generally made from chick peas blended with tahini, lemon juice, salt, garlic, and other ingredients.
Pita
A soft, slightly leavened flatbread.
Halloumi
A semi-hard, unripened, brined cheese with a higher-than-usual melting point, usually made from goat/sheep milk.
Green salad
That green healthy stuff that many of us just use to garnish kebabs.

Handling list styling

When styling lists, you need to adjust their styles so they keep the same vertical spacing as their surrounding elements (such as paragraphs and images; sometimes called vertical rhythm), and the same horizontal spacing as each other.

Some CSS for the text styling and spacing could be as follows:

html {
  font-family: Helvetica, Arial, sans-serif;
  font-size: 10px;
}

h2 {
  font-size: 2rem;
  text-shadow: 1px 1px 5px orange;
}

ul, ol, dl {
  font-size: 1.5rem;
}

li {
  line-height: 1.5;
}

dd, dt {
  line-height: 1.5;
}

dt {
  font-weight: bold;
  padding-left: 2rem;
}
  • The first rule sets a sitewide font and a baseline font size of 10px. These are inherited by everything on the page.
  • Rules 2 and 3 set relative font sizes for the headings, and different list types (the children of the list elements inherit these). This means that each list will have the same font size and top and bottom spacing, helping to keep the vertical rhythm consistent.
  • Rule 4 sets the same line-height on list items, so that each individual list item will have the same spacing between lines. This will also help to keep the vertical rhythm consistent.
  • Rules 5 and 6 apply to the description list. We set the same line-height on the description list terms and descriptions as we did with the list items. Again, consistency is good! We also make the description terms have bold font, so they visually stand out easier.

Proposed exercise: Lists with your own styles

Create a web page with the lists and styles of the previous example, and change or add any new styles you like. Now you have to create a CSS file and link it from the HTML code. Finally check the result in your browser and validate your code.

Your result may look now something like this:

Shopping (unordered list)

  • Hummus
  • Pita
  • Green salad
  • Halloumi

Recipe (ordered list)

  1. Toast pita, leave to cool, then slice down the edge.
  2. Fry the halloumi in a shallow, non-stick pan, until browned on both sides.
  3. Wash and chop the salad.
  4. Fill pita with salad, hummus, and fried halloumi.

Ingredients (description list)

Hummus
A thick dip/sauce generally made from chick peas blended with tahini, lemon juice, salt, garlic, and other ingredients.
Pita
A soft, slightly leavened flatbread.
Halloumi
A semi-hard, unripened, brined cheese with a higher-than-usual melting point, usually made from goat/sheep milk.
Green salad
That green healthy stuff that many of us just use to garnish kebabs.

List-specific styles

Now that we’ve looked at general spacing techniques for lists, let’s explore some list-specific properties. As seen in a previous unit, there are three properties you should know about to start with, which can be set on both <ul> or <ol> elements:

  • list-style-type: Sets the type of bullets to use for the list, for example, square or circle bullets for an unordered list, or numbers, letters or roman numerals for an ordered list.
  • list-style-position: Sets whether the bullets appear inside the list items, or outside them before the start of each item.
  • list-style-image: Allows you to use a custom image for the bullet, rather than a simple square or circle.

Bullet and number styles

As mentioned above, the list-style-type property allows you to set what type of bullet to use for the bullet points. In our example, we can set the ordered list to use uppercase roman numerals, with:

ol {
  list-style-type: upper-roman;
}
  1. Toast pita, leave to cool, then slice down the edge.
  2. Fry the halloumi in a shallow, non-stick pan, until browned on both sides.
  3. Wash and chop the salad.
  4. Fill pita with salad, hummus, and fried halloumi.

Bullet position

The list-style-position property sets whether the bullets appear inside the list items, or outside them before the start of each item. The default value is outside, which causes the bullets to sit outside the list items, as seen above.

If you set the value to inside, the bullets will sit inside the lines:

ol {
  list-style-type: upper-roman;
  list-style-position: inside;
}
  1. Toast pita, leave to cool, then slice down the edge.
  2. Fry the halloumi in a shallow, non-stick pan, until browned on both sides.
  3. Wash and chop the salad.
  4. Fill pita with salad, hummus, and fried halloumi.

Using a custom bullet image

The list-style-image property allows you to use a custom image for your bullet. The syntax is pretty simple:

ul {
  list-style-image: url("https://mdn.github.io/learning-area/css/styling-text/styling-lists/star.svg");
}

However, this property is a bit limited in terms of controlling the position, size, etc. of the bullets. You are better off using the background family of properties, which you may also find in the Backgrounds and borders article.

In our example, we have styled the unordered list like so:

ul {
  padding-left: 2rem;
  list-style-type: none;
}

ul li {
  padding-left: 2rem;
  background-image: url("https://mdn.github.io/learning-area/css/styling-text/styling-lists/star.svg");
  background-position: 0 0;
  background-size: 1.6rem 1.6rem;
  background-repeat: no-repeat;
}
  • Hummus
  • Pita
  • Green salad
  • Halloumi

Here we’ve done the following:

  • Set the padding-left of the <ul> down from the default 40px to 20px, then set the same amount on the list items. This is so that overall the list items are still lined up with the order list items and the description list descriptions, but the list items have some padding for the background images to sit inside. If we didn’t do this, the background images would overlap with the list item text, which would look messy.
  • Set the list-style-type to none, so that no bullet appears by default. We’re going to use background properties to handle the bullets instead.
  • Inserted a bullet onto each unordered list item. The relevant properties are as follows:
    • background-image: This references the path to the image file you want to use as the bullet.
    • background-position: This defines where in the background of the selected element the image will appear. In this case we are saying 0 0, which means the bullet will appear in the very top left of each list item.
    • background-size: This sets the size of the background image. We ideally want the bullets to be the same size as the list items (or very slightly smaller or larger). We are using a size of 1.6rem (16px), which fits very nicely with the 20px padding we’ve allowed for the bullet to sit inside (16px plus 4px of space between the bullet and the list item text works well).
    • background-repeat: By default, background images repeat until they fill up the available background space. We only want one copy of the image inserted in each case, so we set this to a value of no-repeat.

Proposed exercise: Putting it all together

Using the code of the previous exercise, change the defaults bullets to use an image, and also change the default numbering, and any other styles you like. Finally check the result in your browser and do not forget to validate your code.

Your result may look now something like this:

Shopping (unordered list)

  • Hummus
  • Pita
  • Green salad
  • Halloumi

Recipe (ordered list)

  1. Toast pita, leave to cool, then slice down the edge.
  2. Fry the halloumi in a shallow, non-stick pan, until browned on both sides.
  3. Wash and chop the salad.
  4. Fill pita with salad, hummus, and fried halloumi.

Ingredients (description list)

Hummus
A thick dip/sauce generally made from chick peas blended with tahini, lemon juice, salt, garlic, and other ingredients.
Pita
A soft, slightly leavened flatbread.
Halloumi
A semi-hard, unripened, brined cheese with a higher-than-usual melting point, usually made from goat/sheep milk.
Green salad
That green healthy stuff that many of us just use to garnish kebabs.

List-style shorthand

It is also worth to know that we may set the three properties mentioned in the section above using a single shorthand property, list-style. For example, the following CSS:

ul {
  list-style-type: disc;
  list-style-image: url(example.png);
  list-style-position: inside;
}

Could be replaced by this:

ul {
  list-style: disc url(example.png) inside;
}

The values can be listed in any order, and you can use one, two or all three (the default values used for the properties that are not included are discnone, and outside). If both a type and an image are specified, the type is used as a fallback if the image can’t be loaded for some reason.

Quiz

Test your skills with this quiz about styling lists.

CSS. Unit 3. Styling links.

Introduction

When styling links, it is important to understand how to make use of pseudo-classes to style link states effectively, and how to style links for use in common varied interface features such as navigation menus and tabs. We’ll look at all these topics in this unit.

Let’s look at some links

The first thing to understand is the concept of link states (different states that links can exist in, which can be styled using different pseudo-classes):

  • Link: A link which has a destination (i.e. not just a named anchor), styled using the :link pseudo class.
  • Visited: A link when it has already been visited (exists in the browser’s history), styled using the :visited pseudo class.
  • Hover: A link when it is being hovered over by a user’s mouse pointer, styled using the :hover pseudo class.
  • Focus: A link when it has been focused (for example moved to by a keyboard user using the Tab key or similar, or programmatically focused using HTMLElement.focus()). This is styled using the :focus pseudo class.
  • Active: A link when it is being activated (e.g. clicked on), styled using the :active pseudo class.

Default styles

The following example illustrates what a link will behave like by default (the CSS is simply enlarging and centering the text to make it stand out more):

p {
  font-size: 2rem;
  text-align: center;
}
<p><a href="https://fernandoruizrico.com" target="_blank">A simple link</a></p>

A simple link

You’ll notice a few things as you explore the default styles:

  • Links are underlined.
  • Unvisited links are blue.
  • Visited links are purple.
  • Hovering a link makes the mouse pointer change to a little hand icon.
  • Focused links have an outline around them. You should be able to focus on the links with the keyboard by pressing the tab key (On Mac, you’ll need to use option + tab, or enable the Full Keyboard Access: All controls option by pressing Ctrl + F7.)
  • Active links are red. You can try this feature by holding down the mouse button on the link as you click it.

Proposed exercise: Larger and centered links

Create a web page with at least 5 paragraphs, each one with a different link to any sites you like. You also have to style your page so that the links are double the size of the rest of the text, and they are aligned in the center of the window, as done in the example above.

You are not just limited to the above properties to style your links, you are free to use any properties you like.

Styling some links

Interestingly enough, the default styles used for the links are nearly the same as they were back in the early days of browsers in the mid-1990s. This is because users know and have come to expect this behaviour (if links were styled differently, it would confuse a lot of people). This doesn’t mean that you shouldn’t style links at all, just that you should not stray too far from the expected behaviour. You should at least:

  • Use underlining for links, but not for other things. If you don’t want to underline links, at least highlight them in some other way.
  • Make them react in some way when hovered/focused, and in a slightly different way when activated.

The default styles can be turned off/changed using the following CSS properties:

  • color for the text color.
  • cursor for the mouse pointer style, although you shouldn’t turn this off unless you’ve got a very good reason.
  • outline for the text outline (an outline is similar to a border, the only difference being that border takes up space in the box and an outline doesn’t; it just sits over the top of the background). The outline is a useful accessibility aid, so think carefully before turning it off; you should at least double up the styles given to the link hover state on the focus state too.

Now that we’ve looked at the default states in some detail, let’s look at a typical set of link styles. To start off with, we’ll write out our empty rulesets:

a:link {
}

a:visited {
}

a:focus {
}

a:hover {
}

a:active {
}

This order is important because the link styles build on one another, for example the styles in the first rule will apply to all the subsequent ones, and when a link is being activated, it is usually also being hovered over. If you put these in the wrong order, and you’re changing the same properties in each ruleset, things won’t work as you expect. To remember the order, you could try using a mnemonic like LoVFears HAte.

Now let’s add some more information to get this styled properly:

p {
  font-size: 2rem;
  text-align: center;
}

a:link {
  color: green;
}

a:visited {
  color: olive;
}

a:focus {
  background: orange;
}

a:hover {   
  background: yellow;
  text-decoration: underline red wavy;
}

a:active {
  background: red;
}

We’ll also provide some sample HTML to apply the CSS to:

<p>There are several browsers available, such as:</p>
<p><a href="https://www.mozilla.org/en-US/firefox/new/" target="_blank">Mozilla Firefox</a></p>
<p><a href="https://www.google.com/chrome/" target="_blank">Google Chrome</a></p>
<p><a href="https://www.apple.com/safari/" target="_blank">Apple Safari</a></p>

Putting the two together gives us this result:

There are several browsers available, such as:

Mozilla Firefox

Google Chrome

Apple Safari

Proposed exercise: Styling colors and text decoration

Create a new web page with at least 5 links pointing to your preferred websites. You must change the default colors and decorations so that each possible state (link, visited, focus, hover, active) has other color than the default one, and also another decoration, as done in the example above, where the “underline red wavy” text decoration is used.

As you have seen, you can change the color of the text using the color property, and the background color using the background property. Also remember you can have a look at the different colors here: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value. About the text decoration, remember it can be easily changed with the text-decoration property.

Including icons on links

Some developers include icons on links to provide more of an indicator as to what kind of content the link points to. Let’s look at a really simple example that adds an icon to external links (links that lead to other sites). Such an icon usually looks like a little arrow pointing out of a box (for this example, we’ll use this great example from icons8.com).

For this example, we will use the same HTML as before:

<p>There are several browsers available, such as:</p>
<p><a href="https://www.mozilla.org/en-US/firefox/new/" target="_blank">Mozilla Firefox</a></p>
<p><a href="https://www.google.com/chrome/" target="_blank">Google Chrome</a></p>
<p><a href="https://www.apple.com/safari/" target="_blank">Apple Safari</a></p>

And we will adjust the CSS code:

p {
  font-size: 2rem;
  text-align: center;
}

a {
  background: url('https://mdn.mozillademos.org/files/12982/external-link-52.png') no-repeat 100% 0;
  background-size: 2rem;
  padding-right: 2.5rem;
}

Putting the two together gives us this result:

There are several browsers available, such as:

Mozilla Firefox

Google Chrome

Apple Safari

So what’s going on here? We’ll skip the CSS related to the paragraphs, as it’s just the same information you’ve looked at before. The last rule however is interesting : here we are inserting a custom background image on external links using background shorthand instead of the individual properties. We set the path to the image we want to insert, specify no-repeat so we only get one copy inserted, and then specify the position as 100% of the way over to the right of the text content, and 0 pixels from the top.

We also use background-size to specify the size we want the background image to be shown at. So that you get a good result, it is useful to have a larger icon and then resize it like this as needed for responsive web design purposes.

Finally, we set some padding-right on the links to make space for the background image to appear in, so we aren’t overlapping it with the text.

Proposed exercise: Links with icons

Create a new web page with at least 5 links pointing to your preferred websites (you can use the code you wrote before) and include an icon at the end of each one. Also change some other styles as the text color or text decoration.

Styling links as buttons

The tools you’ve explored so far in this article can also be used in other ways. For example, states like hover can be used to style many different elements, not just links — you might want to style the hover state of paragraphs, list items, or other things.

In addition, links are quite commonly styled to look and behave like buttons in certain circumstances — a website navigation menu is usually marked up as a list containing links, and this can be easily styled to look like a set of control buttons or tabs that provide the user with access to other parts of the site. Let’s explore how.

First, some HTML:

<ul class="navbar">
  <li><a href="#">Home</a></li>
  <li><a href="#">News</a></li>
  <li><a href="#">Contact</a></li>
  <li><a href="#">About</a></li>
</ul>

And now our CSS:

/* General class for navigation bar */
.navbar {
  font-size: 1.25rem;
  font-family: sans-serif;
  text-align: center;
  background: yellow;
}

/* Select only the list items (li) inside the navigation bar */
ul.navbar li {
  display: inline;
}

/* Select only the links (a) inside the navigation bar */
ul.navbar a {
  text-decoration: none;
  display: inline-block;
  padding: 1rem;
}

/* Select only the links inside the navigation bar when mouse over them (a:hover) */
ul.navbar a:hover {     
  background: orange;
}

/* Select only the links inside the navigation bar when mouse pressing (a:active) */
ul.navbar a:active {
  color: white;
  background: red;
}

This gives us the following result:

Let’s explain what’s going on here, focusing on the most interesting parts:

  • Creating a new class for the list:
    • We have created the navbar class so that only the items inside that class have the style of a navigation bar.
    • ul.navbar: By putting this before any selector, the CSS properties between braces will apply only to the items inside an unordered list with class navbar.
  • The styles related to the <ul> element:
    • We change the font-size to enlarge the text a little bit.
    • We change the font-family so that it is different from the rest of the text.
    • We change the text-align so that the links are centered .
    • We change the background to yellow color.
    • <li> elements will normally behave like block elements (they sit on their own lines). In this case, we want to create a horizontal list of links, so we set the display property to inline, which causes the list items to sit on the same line one after the other (they now behave like inline elements).
  • The styles related to the <a> elements:
    • We start by turning off the default text-decoration (we don’t want those spoiling our look).
    • Next, we set the display to inline-block (this will allow to size them, as we will explain in another unit).
    • We set the padding to 1rem to give the buttons some space around the text (we will also explain this in another unit).
    • We also change the color of the items when the mouse is over them, and when they are being pressed.

Proposed exercise: Navigation bar

Create a web page with a navigation bar of your own style. You can change any properties you like (colors, text decoration, font size, font family, etc.). After that, just below insert some other links you like. You can also try to use some other CSS rules to style these links, as shown below:

There are several browsers available, such as:

Mozilla Firefox

Google Chrome

Apple Safari

Quiz

Test your skills with this quiz about styling links.

CSS. Unit 2. Text and font styling.

Introduction

Here we’ll go through all the basic fundamentals of text/font styling in detail, including setting font weight, family and style, font shorthand, text alignment and other effects, and line and letter spacing.

What is involved in styling text in CSS?

The CSS properties used to style text generally fall into two categories, which we’ll look at separately in this unit:

  • Font styles: Properties that affect the font that is applied to the text, affecting what font is applied, how big it is, whether it is bold, italic, etc.
  • Text layout styles: Properties that affect the spacing and other layout features of the text, allowing manipulation of, for example, the space between lines and letters, and how the text is aligned within the content box.

Bear in mind that the text inside an element is all affected as one single entity. You can’t select and style subsections of text unless you wrap them in an appropriate element (such as a <span> or <strong>, for example).

Font color

Let’s move straight on to look at properties for styling fonts. In this section we’ll apply some different CSS properties to the same HTML sample.

The color property sets the color of the foreground content of the selected elements (which is usually the text, but can also include a couple of other things, such as an underline or overline placed on text using the text-decoration property):

p {
  color: red;
}

Proposed exercise

Create a new web page made of 2 files: one HTML file with the code below, and another one with some CSS code to change the <h1> and <p> elements to any color you like. Do not forget to link the CSS file from the HTML file, and validate your code.

<h1>Tommy the cat</h1>

<p>Well I remember it as though it were a meal ago...</p>

<p>Said Tommy the Cat as he reeled back to clear whatever foreign matter may have nestled its way into his mighty throat. Many a fat alley rat had met its demise while staring point blank down the cavernous barrel of this awesome prowling machine. Truly a wonder of nature this urban predator — Tommy the cat had many a story to tell. But it was a rare occasion such as this that he did.</p>

Font families

To set a different font on your text, you use the font-family property — this allows you to specify a font (or list of fonts) for the browser to apply to the selected elements. The browser will only apply a font if it is available on the machine the website is being accessed on; if not, it will just use a browser default font. A simple example looks like so:

p {
  font-family: arial;
}

This would make all paragraphs on a page adopt the arial font, which is found on any computer.

Default fonts

CSS defines five generic names for fonts:  serifsans-serifmonospacecursive and fantasy. Those are very generic and the exact font face used when using those generic names is up to each browser and can vary for each operating system they are running on. It represents a worst case scenario where the browser will try to do its best to provide at least a font that looks appropriate. serifsans-serif and monospace are quite predictable and should provide something reasonable. On the other hand, cursive and fantasy are less predictable and we recommend using them very carefully, testing as you go.

The five names are defined as follows (you will find below some examples about how they look like):

  • serif: Fonts that have serifs (the flourishes and other small details you see at the ends of the strokes in some typefaces).
  • sans-serif: Fonts that don’t have serifs.
  • monospace: Fonts where every character has the same width, typically used in code listings.
  • cursive: Fonts that are intended to emulate handwriting, with flowing, connected strokes.
  • fantasy: Fonts that are intended to be decorative.
  • This is serif
  • This is sans-serif
  • This is monospace
  • This is cursive
  • This is fantasy

Proposed exercise: Default fonts

Using the code of the previous exercise, copy and paste the paragraphs several times (5 in total) and define some classes to apply all default fonts (serif, sans-serif, monospace, cursive, fantasy). You must set a different font and color each time. Your source code and the result should look something like this:

HTML code:

<h1>Tommy the cat</h1>

<p class="serif">Well I remember it as though it were a meal ago...</p>
<p class="serif">Said Tommy the Cat as he reeled back to clear whatever foreign matter may have nestled its way into his mighty throat. Many a fat alley rat had met its demise while staring point blank down the cavernous barrel of this awesome prowling machine. Truly a wonder of nature this urban predator — Tommy the cat had many a story to tell. But it was a rare occasion such as this that he did.</p>

...

<p class="fantasy">Well I remember it as though it were a meal ago...</p>
<p class="fantasy">Said Tommy the Cat as he reeled back to clear whatever foreign matter may have nestled its way into his mighty throat. Many a fat alley rat had met its demise while staring point blank down the cavernous barrel of this awesome prowling machine. Truly a wonder of nature this urban predator — Tommy the cat had many a story to tell. But it was a rare occasion such as this that he did.</p>

CSS code:

.serif {
    font-family: serif;
    color: blue;
}

...

.fantasy {
    font-family: fantasy;
    color: green;
}

And the result:

Tommy the cat

Well I remember it as though it were a meal ago…

Said Tommy the Cat as he reeled back to clear whatever foreign matter may have nestled its way into his mighty throat. Many a fat alley rat had met its demise while staring point blank down the cavernous barrel of this awesome prowling machine. Truly a wonder of nature this urban predator — Tommy the cat had many a story to tell. But it was a rare occasion such as this that he did.

Well I remember it as though it were a meal ago…

Said Tommy the Cat as he reeled back to clear whatever foreign matter may have nestled its way into his mighty throat. Many a fat alley rat had met its demise while staring point blank down the cavernous barrel of this awesome prowling machine. Truly a wonder of nature this urban predator — Tommy the cat had many a story to tell. But it was a rare occasion such as this that he did.

Web safe fonts

Speaking of font availability, there are only a certain number of fonts that are generally available across all systems and can therefore be used without much worry. These are the so-called web safe fonts.

Most of the time, as web developers we want to have more specific control over the fonts used to display our text content. The problem is to find a way to know which font is available on the computer used to see our web pages. There is no way to know this in every case, but the web safe fonts are known to be available on nearly all instances of the most used operating systems (Windows, macOS, the most common Linux distributions, Android, and iOS).

The list of actual web safe fonts will change as operating systems evolve, but it’s reasonable to consider the following fonts web safe, at least for now (many of them have been popularized thanks to the Microsoft Core fonts for the Web initiative in the late 90s and early 2000s):

  • Arial (sans-serif)
  • Courier New (monospace)
  • Georgia (serif)
  • Times New Roman (serif)
  • Trebuchet MS (sans-serif)
  • Verdana (sans-serif)

Among various resources, the cssfontstack.com website maintains a list of web safe fonts available on Windows and macOS operating systems, which can help you make your decision about what you consider safe for your usage.

Proposed exercise: Main safe fonts

Create a new web page with a header (<h1>) and six other paragraphs (<p>) with any content you like, and set a different font for each of them (Arial, Courier New, Georgia, Times New Roman, Trebuchet MS, Verdana). You can also change the color of the text too.

Font names that have more than one word (like Trebuchet MS) need to be surrounded by quotes, for example:
font-family: "Trebuchet MS";

Font stacks

Since you can’t guarantee the availability of the fonts you want to use on your webpages (even a web font could fail for some reason), you can supply a font stack so that the browser has multiple fonts it can choose from. This simply involves a font-family value consisting of multiple font names separated by commas, e.g.

p {
  font-family: "Trebuchet MS", Verdana, sans-serif;
}

In such a case, the browser starts at the beginning of the list and looks to see if that font is available on the machine. If it is, it applies that font to the selected elements. If not, it moves on to the next font, and so on.

It is a good idea to provide a suitable generic font name at the end of the stack so that if none of the listed fonts are available, the browser can at least provide something approximately suitable. To emphasise this point, paragraphs are given the browser’s default serif font if no other option is available — which is usually Times New Roman — this is no good for a sans-serif font!

Proposed exercise: More safe fonts and font stacks

Create a new web page with a header and five other paragraphs with any content you like, and set a different font for each of them, but this time you cannot use any font used previously: you must select some other fonts from cssfontstack.com. Moreover, in this exercise you have to use font stacks to ensure at least one font is always available, and you can also change the color too. Finally, check your web page using several browsers from several devices and operating systems, to make sure that all fonts are shown.

If you are changing the color and the font family using font stacks, your CSS and HTML code should look like this:
.font-book {
  color: blue;
  font-family: "Book Antiqua", Arial, sans-serif;
}
<p class="font-book"> ... </p>

Using an online font service

Online font services generally store and serve fonts for you, so you don’t have to worry about the font availability, and generally you just need to insert a simple line or two of code into your site to make everything work. Examples include Adobe Fonts and Cloud.typography. Most of these services are subscription-based, with the notable exception of Google Fonts, a useful free service, especially for rapid testing work and writing demos.

Most of these services are easy to use, so we won’t cover them in great detail. Let’s have a quick look at Google fonts, so you can get the idea. These are the steps that you have to follow to use one or several of the fonts they provide:

  1. Go to Google Fonts.
  2. You may use the filters to display the kinds of fonts you like.
  3. To select a font family, click on it, and after that press the “+ Select this style” option on the right hand side.
  4. When you’ve chosen the font families, press the “View your selected families” icon at the top right corner of the page.
  5. In the resulting screen, you first need to copy the line of HTML code shown and paste it into the head of your HTML file. Put it above the existing <link> element, so that the font is imported before you try to use it in your CSS.
  6. You then need to copy the CSS declarations listed into your CSS as appropriate, to apply the custom fonts to your HTML.

Proposed exercise: Google fonts

Create a web page with ten paragraphs, each one using a different Google font.

For example, to use the Google fonts “Nerko One”, “Permanent Marker” and “Rock Salt”, your HTML code should look like this:
<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Web font example</title>
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Nerko+One&family=Permanent+Marker&family=Rock+Salt&display=swap">
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <h1>Web font example</h1>
    <p class="nerko">This is a text with Nerko One font</p>
    <p class="marker">This is a text with Permanent Marker font</p>
    <p class="rock">This is a text with Rock Salt</p>
  </body>
</html>

The CSS code inside your own file styles.css:

.nerko {
  font-family: 'Nerko One', cursive;
}
.marker {
  font-family: 'Permanent Marker', cursive;
}
.rock {
  font-family: 'Rock Salt', cursive;
}

And the result:

This is a text with Nerko One font

This is a text with Permanent Market font

This is a text with Rock Salt font

Font size

Font size (set with the font-size property) can take values measured in several units (such as percentages), however the most common units you’ll use to size text are:

  • px (pixels): The number of pixels high you want the text to be (this is an absolute unit).
  • em: 1 em is equal to the font size set on the parent element of the current element we are styling (more specifically, the width of a capital letter M contained inside the parent element.) This can become tricky to work out if you have a lot of nested elements with different font sizes set, but t is quite natural once you get used to it, and you can use em to size everything, not just text. You can have an entire website sized using em, which makes maintenance easy.
  • rem: These work just like em, except that 1 rem is equal to the font size set on the root element of the document (i.e. <html>), not the parent element. This makes doing the maths to work out your font sizes much easier, although if you want to support really old browsers, you might struggle (rem is not supported in Internet Explorer 8 and below).

Absolute size (pixels)

The easiest way to change the size of your text is setting a specific number of pixels. However, this might not be the best solution, since in case you want to increase (or decrease) the size of all text in your website, you should change each value manually (one time per each CSS rule). So, we are going ahead with a first exercise using pixels, but after that we will use a more convenient way in another exercise using relative values.

Proposed exercise: Absolute sizing with “px”

Create a new web page with at least 15 paragraphs of some text you like, and set the font size for each paragraph a bit larger each time. You must use pixels to set each font size.

Your source code and result should look like this (you may choose any class names, colors and sizes you like):
.ten {
  color: black;
  font-size: 10px;
}

.eleven {
  color: blue;
  font-size: 11px;
}

.twelve {
  color: green;
  font-size: 12px;
}

.thirteen {
  color: brown;
  font-size: 13px;
}

...
<p class="ten">This is a text with 10 px font size</p>
<p class="eleven">This is a text with 11 px font size</p>
<p class="twelve">This is a text with 12 px font size</p>
<p class="thirteen">This is a text with 13 px font size</p>
...

This is a text with 10 px font size

This is a text with 11 px font size

This is a text with 12 px font size

This is a text with 13 px font size

This is a text with 14 px font size

Relative size (“em” and “rem”)

The font-size of an element is inherited from that element’s parent element. This all starts with the root element of the entire document  (<html>) the font-size of which is set to 16 px as standard across browsers. Any paragraph (or another element that doesn’t have a different size set by the browser) inside the root element will have a final size of 16 px. Other elements may have different default sizes, for example an <h1> element has a size of 2 em set by default, so it will have a final size of 32 px.

Things become more tricky when you start altering the font size of nested elements. For example, if you had an <article> element in your page, and set its font-size to 1.5 em (which would compute to 24 px final size), and then wanted the paragraphs inside the <article> elements to have a computed font size of 20 px, what em value would you use?

<!-- document base font-size is 16px -->
<article> <!-- If my font-size is 1.5em -->
  <p>My paragraph</p> <!-- How do I compute to 20px font-size? -->
</article>

You would need to set its em value to 20/24, or 0.83333333 em. The maths can be complicated, so you need to be careful about how you style things. It is best to use rem where you can, to keep things simple, and avoid setting the font-size of container elements where possible.

When sizing your text, it is usually a good idea to set the base font-size of the document to 10px, so that then the maths is a lot easier to work out (required (r)em values are then the pixel font size divided by 10, not 16). After doing that, you can easily size the different types of text in your document to what you want. Also it is a good idea to list all your font-size rulesets in a designated area in your stylesheet, so they are easy to find.

Proposed exercise: Relative sizing with “rem”

Create a new web page with at least 15 paragraphs of some text you like, and set the font size for each paragraph a bit larger each time. This time, you must use relative sizing to set each font size (for example, with “rem” units). When you finish writing your code, check the results using your browser, and set different values for the main size (inside the <html> element) and check that the size of all the paragraphs changes accordingly.

Your source code and result should look like this (you may choose any class names, colors and sizes you like):
html {
  color: black;
  font-size: 10px;
}

.eleven {
  color: blue;
  font-size: 1.1rem;
}

.twelve {
  color: green;
  font-size: 1.2rem;
}

.thirteen {
  color: olive;
  font-size: 1.3rem;
}

...
<p>This is a text with 1 rem font size</p>
<p class="eleven">This is a text with 1.1 rem font size</p>
<p class="twelve">This is a text with 1.2 rem font size</p>
<p class="thirteen">This is a text with 1.3 rem font size</p>
...

This is a text with 1 rem font size

This is a text with 1.1 rem font size

This is a text with 1.2 rem font size

This is a text with 1.3 rem font size

This is a text with 1.4 rem font size

Font style, font weight, text transform, and text decoration

CSS provides four common properties to alter the visual weight/emphasis of text:

  • font-style: Used to turn italic text on and off. Possible values are as follows (you’ll rarely use this, unless you want to turn some italic styling off for some reason):
    • normal: Sets the text to the normal font (turns existing italics off)
    • italic: Sets the text to use the italic version of the font if available; if not available, it will simulate italics with oblique instead.
    • oblique: Sets the text to use a simulated version of an italic font, created by slanting the normal version.
  • font-weight: Sets how bold the text is. This has many values available in case you have many font variants available (such as -light-normal-bold-extrabold-black, etc.), but realistically you’ll rarely use any of them except for normal and bold:
    • normalbold: Normal and bold font weight
    • lighterbolder: Sets the current element’s boldness to be one step lighter or heavier than its parent element’s boldness.
    • 100900: Numeric boldness values that provide finer grained control than the above keywords, if needed. 
  • text-transform: Allows you to set your font to be transformed. Values include:
    • none: Prevents any transformation.
    • uppercase: Transforms ALL TEXT TO CAPITALS.
    • lowercase: Transforms all text to lower case.
    • capitalize: Transforms all words to Have The First Letter Capitalized.
    • full-width: Transforms all glyphs to be written inside a fixed-width square, similar to a monospace font, allowing aligning of e.g. Latin characters along with Asian language glyphs (like Chinese, Japanese, Korean).
  • text-decoration: Sets/unsets text decorations on fonts (you’ll mainly use this to unset the default underline on links when styling them.) Available values are:
    • none: Unsets any text decorations already present.
    • underline: Underlines the text.
    • overline: Gives the text an overline.
    • line-through: Puts a strikethrough over the text.

You may use some combinations of the options above. For example:

p {
  font-weight: bold;
  text-transform: uppercase;
  text-decoration: line-through;
}

This an uppercase text with line through text decoration

You should also note that text-decoration can accept multiple values at once, if you want to add multiple decorations simultaneously, for example text-decoration: underline overline. Also note that text-decoration is a shorthand property for text-decoration-linetext-decoration-style, and text-decoration-color. You can use combinations of these property values to create interesting effects, for example:

p {
  font-weight: bold;
  text-transform: uppercase;
  text-decoration: line-through red wavy;
}

This a bold uppercase text with a text decoration of a red wavy line through

Proposed exercise: Style, weight, transform and decoration

Create a website with at least 15 paragraphs to show most of the values that can be used with font-style , font-weight , font-transform and font-decoration properties.

This is an example of the result you should get (you may choose your own combinations):

This is a normal text

This is an italic text

This is a bold text

This is an uppercase and italic text

This is a bold and uppercase text

This is a capitalized text with a weight of 500

This is a capitalized text with a weight of 600

This is a capitalized text with a weight of 700

This is a capitalized text with a weight of 800

This is a capitalized text with a weight of 900

This is an uppercase bold text with an orange line through decoration

This is an uppercase bold text with a blue line through decoration

This is a capitalized bold text with a wavy red line through

This is an uppercase bold text underlined and also with red line-through

Text drop shadows

You can apply drop shadows to your text using the text-shadow property. This takes up to four values, as shown in the example below:

text-shadow: 4px 4px 5px red;

The four properties are as follows:

  1. The horizontal offset of the shadow from the original text. This can take most available CSS length and size units, but you’ll most commonly use px. Positive values move the shadow right, and negative values left. This value has to be included.
  2. The vertical offset of the shadow from the original text; behaves basically just like the horizontal offset, except that it moves the shadow up/down, not left/right. This value has to be included.
  3. The blur radius. A higher value means the shadow is dispersed more widely. If this value is not included, it defaults to 0, which means no blur. This can take most available CSS length and size units.
  4. The base color of the shadow, which can take any CSS color unit. If not included, it defaults to currentColor, i.e. the shadow’s color is taken from the element’s color property.

You can also apply multiple shadows to the same text by including multiple shadow values separated by commas, for example:

text-shadow: 1px 1px 1px red,
             2px 2px 1px red;

Proposed exercise: Simple shadows

Create a website with at least 15 paragraphs with shadows, each one with different offsets and colors. You can also change any other properties you like, such as the font-family. You should use Google Fonts to ensure that everything is displayed in the right way for all the operating systems and browsers.

This is an example of the result you should get (you may choose your own combinations):

This is a white text with Permanent Marker font and 2 px offset black shadow with 5 px blur

This is a text with Permanent Marker font and 2 px offset yellow shadow without blur

This is a text with Permanent Marker font and 3 px offset yellow shadow with 5 px blur

This is a text with Nerko One font and 2 px offset orange shadow without blur

This is a text with Nerko One font and 2 px offset orange shadow with 5 px blur

This is a bold text with Rock Salt font and 2 px offset red shadow without blur

This is a bold text with Rock Salt font and 2 px offset red shadow with 5 px blur

Proposed exercise: Multiple shadows

Create a website with at least 10 paragraphs each one with multiple shadows and also different offsets and colors. You can also change any other properties you like, such as the font-family and font-size. You should use Google Fonts to ensure that everything is displayed in the right way for all the operating systems and browsers.

This is an example of the result you should get (you may choose your own combinations):

This is a bold white text with Sedgwick Ave font and 1 px offset red shadow and 3 px offset black shadow with 5 px blur

This is a bold text with Sedgwick Ave font and 2 px offset yellow shadow and also a 3 px offset blue shadow without blur

This is a bold text with Sedgwick Ave font and 3 px offset yellow shadow and also a 6 px offset blue shadow with 5 px blur

This is a text with Lakki Reddy font and 1 px offset orange shadow and also a 2 px offset red shadow without blur

This is a text with Lakki Reddy font and 3 px offset orange shadow and also a 6 px offset red shadow with 5 px blur

This is an orange bold text with Gloria Hallelujah font and 2 px offset red shadow and also a 4 px offset blue shadow without blur

This is an orange bold text with Gloria Hallelujah font and 3 px offset red shadow and also a 6 px blue shadow with 5 px blur

Text-alignment

The text-align property is used to control how text is aligned within its containing content box. The available values are as follows, and work in pretty much the same way as they do in a regular word processor application:

  • left: Left-justifies the text.
  • right: Right-justifies the text.
  • center: Centers the text.
  • justify: Makes the text spread out, varying the gaps in between the words so that all lines of text are the same width. You need to use this carefully, since sometimes it can look terrible, especially when applied to a paragraph with lots of long words in it. If you are going to use this, you should also think about using something else along with it, such as hyphens, to break some of the longer words across lines.

For example, you can center some text just by using the following CSS code:

text-align: center;

Proposed exercise: Alignment types

Create a web page with 4 headers and 4 paragraphs, each one with a different alignment type.

You can use any text you like, but keeping in mind that you must insert several lines to properly see how the alignment is done:

This is left alignment

Well I remember it as though it were a meal ago… Said Tommy the Cat as he reeled back to clear whatever foreign matter may have nestled its way into his mighty throat. Many a fat alley rat had met its demise while staring point blank down the cavernous barrel of this awesome prowling machine. Truly a wonder of nature this urban predator. Tommy the cat had many a story to tell. But it was a rare occasion such as this that he did.

This is right alignment

Well I remember it as though it were a meal ago… Said Tommy the Cat as he reeled back to clear whatever foreign matter may have nestled its way into his mighty throat. Many a fat alley rat had met its demise while staring point blank down the cavernous barrel of this awesome prowling machine. Truly a wonder of nature this urban predator. Tommy the cat had many a story to tell. But it was a rare occasion such as this that he did.

This is center alignment

Well I remember it as though it were a meal ago… Said Tommy the Cat as he reeled back to clear whatever foreign matter may have nestled its way into his mighty throat. Many a fat alley rat had met its demise while staring point blank down the cavernous barrel of this awesome prowling machine. Truly a wonder of nature this urban predator. Tommy the cat had many a story to tell. But it was a rare occasion such as this that he did.

This is justify alignment

Well I remember it as though it were a meal ago… Said Tommy the Cat as he reeled back to clear whatever foreign matter may have nestled its way into his mighty throat. Many a fat alley rat had met its demise while staring point blank down the cavernous barrel of this awesome prowling machine. Truly a wonder of nature this urban predator. Tommy the cat had many a story to tell. But it was a rare occasion such as this that he did.

Line height

The line-height property sets the height of each line of text. This can take most length and size units, but can also take a unitless value, which acts as a multiplier and is generally considered the best option (the font-size is multiplied to get the line-height). Body text generally looks nicer and is easier to read when the lines are spaced apart; the recommended line height is around 1.5 – 2 (double spaced.) So to set our lines of text to 1.6 times the height of the font, you’d use this:

line-height: 1.6;

Proposed exercise: Line heights

Create a web page with at least 3 headers and 3 paragraphs, each one with a different line height.

This text has 0.8 line height:

Well I remember it as though it were a meal ago… Said Tommy the Cat as he reeled back to clear whatever foreign matter may have nestled its way into his mighty throat. Many a fat alley rat had met its demise while staring point blank down the cavernous barrel of this awesome prowling machine. Truly a wonder of nature this urban predator. Tommy the cat had many a story to tell. But it was a rare occasion such as this that he did.

This text has 1.2 line height:

Well I remember it as though it were a meal ago… Said Tommy the Cat as he reeled back to clear whatever foreign matter may have nestled its way into his mighty throat. Many a fat alley rat had met its demise while staring point blank down the cavernous barrel of this awesome prowling machine. Truly a wonder of nature this urban predator. Tommy the cat had many a story to tell. But it was a rare occasion such as this that he did.

This text has 1.6 line height:

Well I remember it as though it were a meal ago… Said Tommy the Cat as he reeled back to clear whatever foreign matter may have nestled its way into his mighty throat. Many a fat alley rat had met its demise while staring point blank down the cavernous barrel of this awesome prowling machine. Truly a wonder of nature this urban predator. Tommy the cat had many a story to tell. But it was a rare occasion such as this that he did.

Letter and word spacing

The letter-spacing and word-spacing properties allow you to set the spacing between letters and words in your text. You won’t use these very often, but might find a use for them to get a certain look, or to improve the legibility of a particularly dense font. They can take most length and size units.

So as an example, we could apply some word-spacing and letter-spacing like this:

word-spacing: 4px;
letter-spacing: 4px;

Proposed exercise: Spacing

Create a web page with at least 2 headers and 2 paragraphs, each one with different word and letter spacing (you may choose any values you like).

This text has 10 px word spacing:

Well I remember it as though it were a meal ago… Said Tommy the Cat as he reeled back to clear whatever foreign matter may have nestled its way into his mighty throat. Many a fat alley rat had met its demise while staring point blank down the cavernous barrel of this awesome prowling machine. Truly a wonder of nature this urban predator. Tommy the cat had many a story to tell. But it was a rare occasion such as this that he did.

This text has 5 px letter spacing:

Well I remember it as though it were a meal ago… Said Tommy the Cat as he reeled back to clear whatever foreign matter may have nestled its way into his mighty throat. Many a fat alley rat had met its demise while staring point blank down the cavernous barrel of this awesome prowling machine. Truly a wonder of nature this urban predator. Tommy the cat had many a story to tell. But it was a rare occasion such as this that he did.

Font shorthand

Many font properties can also be set through the shorthand property font. These are written in the following order:  font-stylefont-variantfont-weightfont-stretchfont-sizeline-height, and font-family. Among all those properties, only font-size and font-family are required when using the font shorthand property. A forward slash has to be put in between the font-size and line-height properties.

A full example would look like this:

font: italic normal bold normal 3em/1.5 Helvetica, Arial, sans-serif;

Proposed exercise: Font properties in one line

Create a new web page with at least five paragraphs and change the font properties using one single CSS line (font shorthand) for each one.

This is a big bold italic text with Special Elite font family.

This is a bigger bold italic text with Bonbon font family.

Other properties worth looking at

The above properties give you an idea of how to start styling text on a webpage, but there are many more properties you could use. We just wanted to cover the most important ones here. Once you’ve become used to using the above, you should also explore the following:

Font styles:

Text layout styles:

  • text-indent: Specify how much horizontal space should be left before the beginning of the first line of the text content.
  • text-overflow: Define how overflowed content that is not displayed is signaled to users.
  • white-space: Define how whitespace and associated line breaks inside the element are handled.
  • word-break: Specify whether to break lines within words.
  • direction: Define the text direction (This depends on the language and usually it’s better to let HTML handle that part as it is tied to the text content.)
  • hyphens: Switch on and off hyphenation for supported languages.
  • line-break: Relax or strengthen line breaking for Asian languages.
  • text-align-last: Define how the last line of a block or a line, right before a forced line break, is aligned.
  • text-orientation: Define the orientation of the text in a line.
  • overflow-wrap: Specify whether or not the browser may break lines within words in order to prevent overflow.
  • writing-mode: Define whether lines of text are laid out horizontally or vertically and the direction in which subsequent lines flow.

Quiz

Test your skills with this quiz about text formatting.

CSS. Unit 1. First steps.

Introduction

In the HTML units we covered what HTML is, and how it is used to mark up documents. These documents will be readable in a web browser. Headings will look larger than regular text, paragraphs break onto a new line and have space between them. Links are colored and underlined to distinguish them from the rest of the text. What you are seeing is the browser’s default styles (very basic styles that the browser applies to HTML to make sure it will be basically readable even if no explicit styling is specified by the author of the page). However, the web would be a boring place if all websites looked like that. Using CSS you can control exactly how HTML elements look in the browser, presenting your markup using whatever design you like.

What is CSS for?

As we have mentioned before, CSS is a language for specifying how documents are presented to users (how they are styled, laid out, etc).

document is usually a text file structured using a markup language (HTML is the most common markup language).

Presenting a document to a user means converting it into a form usable by your audience. Browsers, like FirefoxChrome, or Edge , are designed to present documents visually, for example, on a computer screen, projector or printer.

CSS can be used for very basic document text styling (for example changing the color and size of headings and links). It can also be used to create layout (for example turning a single column of text into a layout with a main content area and a sidebar for related information). It can even be used for effects such as animation.

CSS syntax

CSS is a rule-based language: you define rules specifying groups of styles that should be applied to particular elements or groups of elements on your web page. For example “I want the main heading on my page to be shown as large blue text.”

The following code shows a very simple CSS rule that would achieve the styling described above:

h1 {
    color: blue;
}

The rule opens with a selector . This selects the HTML element that we are going to style. In this case we are styling level one headings (<h1>).

We then have a set of curly braces { }. Inside those will be one or more declarations, which take the form of property and value pairs. Each pair specifies a property of the element(s) we are selecting, then a value that we’d like to give the property.

Before the colon, we have the property, and after the colon, the value. CSS properties have different allowable values, depending on which property is being specified. In our example, we have the color property, which can take various color values. We may also have some other properties inside the braces, such as the font-size property to change the size of the text (this property can take various size units as a value).

A CSS stylesheet may also contain many such rules, written one after the other:

h1 {
    color: blue;
}

p {
    color: green;
}

You will find that you can quickly learn some values, whereas others you will need to look up. The individual property pages on MDN (CSS reference) give you a quick way to look up properties and their values when you forget, or want to know what else you can use as a value.

Adding CSS to our document

The very first thing we need to do is to tell the HTML document that we have some CSS rules we want to use. There are three different ways to apply CSS to an HTML document that you’ll commonly come across, however, for now, we will look at the most usual and useful way of doing so: linking CSS from the head of your document.

For example, to link and use the file styles.css, you just have to add the following line somewhere inside the <head> section of the HTML document (the .css extension shows that it is a CSS file):

<link rel="stylesheet" href="styles.css">

This <link> element tells the browser that we have a stylesheet, using the rel attribute, and the location of that stylesheet as the value of the href attribute. You can test that the CSS works by adding some rules to styles.css. For example, using your code editor you may add the following code to your CSS file:

h1 {
  color: blue;
}

If you save your HTML and CSS files and reload the page in a web browser, the <h1> headers should now be blue. If that happens, congratulations (you have successfully applied some CSS to an HTML document). If that doesn’t happen, carefully check that you’ve typed everything correctly.

Proposed exercise: Linking files and adding styles

Our starting point in this unit is an HTML document. Copy the code below and save it into a new HTML file to work on your own computer. After that, create a new CSS file to set all <h1> titles to red color and save it as styles.css in the same folder as the HTML document. Finally check the results using your browser and do not forget to validate the code.

Remember that the most important part in this exercise is that you link your CSS file by putting a single line inside your <head> section; after that, inside styles.css you will only need to insert three lines:
  1. HTML file:
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Getting started with CSS</title>

    <!-- We will use the styles which are inside 'styles.css' -->
    <link rel="stylesheet" href="styles.css">
</head>
<body>   
    <h1>I am a level one heading</h1>

    <p>This is a paragraph of text.</p> 
    <p>This is a paragraph and also a link to <a href="https://google.com">Google</a>.</p>

    <ul>
        <li>Item one</li>
        <li>Item two</li>
        <li>Item three</li>
    </ul>
</body>
</html>
  1. CSS file (styles.css):
h1 {
  ...
}

Proposed exercise: Styling headers

Using the HTML and CSS files of the previous exercise, add some <h2> headers and change their colors. You can try several colors and choose anyone you like (red, green, blue, gray, purple, olive, navy, etc). As usual, check the results using your browser and do not forget to validate the code.

You may find the keywords to set many colors at https://developer.mozilla.org/en-US/docs/Web/CSS/color_value.

Styling HTML elements

By making our heading blue or red, we have already demonstrated that we can target and style an HTML element. We do this by targeting an element selector (this is a selector that directly matches an HTML element name). To target all paragraphs in the document you would use the selector p. For example, to turn all paragraphs green you would use:

p {
  color: green;
}

You can target multiple selectors at once, by separating the selectors with a comma. For example, if you want all paragraphs and all list items to be green, your rule will look like this:

p, li {
    color: green;
}

Proposed exercise: Change color of several elements

Using the HTML and CSS files of the previous exercise, change the color of the paragraphs and items in the list. You can try several colors and choose anyone you like (red, green, blue, gray, purple, olive, navy, etc). As usual, check the results using your browser and do not forget to validate the code.

Remember you may find the keywords to set many colors at https://developer.mozilla.org/en-US/docs/Web/CSS/color_value.

Styling things based on state

Another type of styling we shall take a look at in this unit is the ability to style things based on their state. A straightforward example of this is when styling links. When we style a link we need to target the <a> (anchor) element. This has different states depending on whether it is unvisited, visited, being hovered over, focused via the keyboard, or in the process of being clicked (activated). You can use CSS to target these different states. For example, the CSS below styles unvisited links pink and visited links green:

a:link {
  color: pink;
}

a:visited {
  color: green;
}

You can also change the way the link looks when the user hovers over it, for example removing the underline, which is achieved by the next rule:

a:hover {
  text-decoration: none;
}

In the above example, we have removed the underline on our link on hover. You could remove the underline from all states of a link. It is worth remembering however that in a real site, you want to ensure that visitors know that a link is a link. Leaving the underline in place, can be an important clue for people to realize that some text inside a paragraph can be clicked on (this is the behavior they are used to). As with everything in CSS, there is the potential to make the document less accessible with your changes. We will aim to highlight potential pitfalls in appropriate places.

Proposed exercise: Styling links

Using the HTML and CSS files of the previous exercise, insert several paragraphs with some other links to your preferred pages, and change the color of all the links. You must also use different colours when the link has been visited, and when the mouse is over the link. You can try several colors and choose anyone you like (red, green, blue, gray, purple, olive, navy, etc). As usual, check the results using your browser and do not forget to validate the code.

Remember you may find the keywords to set many colors at https://developer.mozilla.org/en-US/docs/Web/CSS/color_value.

Changing the default behavior of elements

When we look at a well-marked up HTML document, even something as simple as our example, we can see how the browser is making the HTML readable by adding some default styling. Headings are large and bold and our list has bullets. As we said before, this happens because browsers have internal stylesheets containing default styles, which they apply to all pages by default; without them all of the text would run together in a clump and we would have to style everything from scratch. All modern browsers display HTML content by default in pretty much the same way.

However, you will often want something other than the choice the browser has made. This can be done by simply choosing the HTML element that you want to change, and using a CSS rule to change the way it looks.  A good example is our <ul>, an unordered list. It has list bullets, and if I decide I don’t want those bullets I can remove them like so:

ul {
  list-style-type: none;
}

The list-style-type property is a good property to look at on MDN to see which values are supported. Take a look at the page for list-style-type and you will find an interactive example at the top of the page to try some different values in, then all allowable values are detailed further down the page. Looking at that page you will discover that in addition to removing the list bullets you can change them.

Proposed exercise: Changing list bullets

Using the HTML and CSS files of the previous exercise, try to set the list-style-type to square. As usual, check the results using your browser and do not forget to validate the code.

You should get something like this:
  • Item one
  • Item two
  • Item three

Proposed exercise: Changing list numbers

Using the HTML and CSS files of the previous exercise, add an ordered list (<ol>) and set the list-style-type to upper-roman. As usual, check the results using your browser and do not forget to validate the code.

You should get something like this:
  • Item one
  • Item two
  • Item three
  1. Item one
  2. Item two
  3. Item three

Adding classes

So far we have styled elements based on their HTML element names. This works as long as you want all of the elements of that type in your document to look the same. Most of the time that isn’t the case and so you will need to find a way to select a subset of the elements without changing the others. The most common way to do this is to add a class to your HTML element and target that class.

In your HTML document, you can add a class attribute to any element. For example, your list may look like this when using the class attribute:

<ul class="square-red-list">
  <li>Item one</li>
  <li>Item two</li>
  <li>Item three</li>
</ul>
<ul class="circle-blue-list">
  <li>Item one</li>
  <li>Item two</li>
  <li>Item three</li>
</ul>

After that, in the CSS you can target the classes of square-red-list and circle-blue-list by creating specific selectors that start with a full stop character. For example:

.square-red-list {
  list-style-type: square;
  color: red;
}
.circle-blue-list {
  list-style-type: circle;
  color: blue;
}

To see what the result is, you only need to save and refresh.

Proposed exercise: Different styles for each list

Using the HTML and CSS files of the previous exercise, add several lists to get something similar to the result below. As usual, check the results using your browser and do not forget to validate the code:

Regarding the unordered lists (<ul>) you have to create 4 different classes, each one using a different value for the list-style-type property (none, disc, circle, square). For the ordered lists (<ol>) you have to create another classes, each one also using a different value for the list-style-type property (upper-roman, lower-greek, lower-alpha, upper-alpha).

Unordered lists

  • Item one
  • Item two
  • Item three
  • Item one
  • Item two
  • Item three
  • Item one
  • Item two
  • Item three
  • Item one
  • Item two
  • Item three

Ordered lists

  1. Item one
  2. Item two
  3. Item three
  1. Item one
  2. Item two
  3. Item three
  1. Item one
  2. Item two
  3. Item three
  1. Item one
  2. Item two
  3. Item three

Proposed exercise: Using CSS to style headers, paragraphs and general text

Change the color of any elements you like from your previous HTML exercises about headers, paragraphs and text formatting in units 1 and 2 (https://fernandoruizrico.com/html-unit-1/ and https://fernandoruizrico.com/html-unit-2/). Do not forget to validate your code again.

Remember you may find the keywords to set many colors at https://developer.mozilla.org/en-US/docs/Web/CSS/color_value.

Proposed exercise: Using CSS to style hyperlinks

Change the color of any elements you like from your previous HTML exercises about links in unit 3 (https://fernandoruizrico.com/html-unit-3/). Do not forget to validate your code again.

Proposed exercise: Using CSS to style lists

Change the color, bullets and numbering of any lists you like from your previous HTML exercises about lists in unit 4 (https://fernandoruizrico.com/html-unit-4/).

Quiz

Test your skills with this quiz about basic concepts, and this one about colors and simple selectors.

HTML. Unit 10. Advanced forms.

How to structure a web form

With the basics out of the way, we’ll now look in more detail at the elements used to provide structure and meaning to the different parts of a form. The flexibility of forms makes them one of the most complex structures in HTML; you can build any kind of basic form using dedicated form elements and attributes. Using correct structure when building an HTML form will help you to ensure that the form is both usable and accessible.

The <form> element

As explained previously, the <form> element formally defines a form and attributes that determine the form’s behaviour. Each time you want to create an HTML form, you must start it by using this element, nesting all the contents inside. Now let’s move forward and cover the structural elements you’ll find nested in a form.

The <fieldset> and <legend> elements

The <fieldset> element is a convenient way to create groups of widgets that share the same purpose, for styling and semantic purposes. You can label a <fieldset> by including a <legend> element just below the opening <fieldset> tag. The text content of the <legend> formally describes the purpose of the <fieldset> it is included inside.

For maximum usability/accessibility, you are advised to surround each list of related items in a <fieldset>, with a <legend> providing an overall description of the list.  Each individual pair of <label>/<input> elements should be contained in its own list item (or similar). The associated <label> is generally placed immediately after the radio button or checkbox, with the instructions for the group of radio button or checkboxes generally being the content of the <legend>. See the examples in the previous unit for structural examples.

Many assistive technologies will use the <legend> element as if it is a part of the label of each control inside the corresponding <fieldset> element. For example, some screen readers such as Jaws and NVDA will speak the legend’s content before speaking the label of each control.

Here is a little example:

<form>
  <fieldset>
    <legend>Fruit juice size</legend>
    <p>
      <label><input type="radio" name="size" value="small" />Small</label>
    </p>
    <p>
      <label><input type="radio" name="size" value="medium" />Medium</label>
    </p>
    <p>
      <label><input type="radio" name="size" value="large" />Large</label>
    </p>
  </fieldset>
</form>
Fruit juice size

When reading the above form, a screen reader will speak “Fruit juice size small” for the first widget, “Fruit juice size medium” for the second, and “Fruit juice size large” for the third.

The use case in this example is one of the most important. Each time you have a set of radio buttons, you should nest them inside a <fieldset> element. There are other use cases, and in general the <fieldset> element can also be used to section a form. Ideally, long forms should be spread across multiple pages, but if a form is getting long and must be on a single page, putting the different related sections inside different fieldsets improves usability.

Proposed exercise: Drink and hamburger

Using the code of the previous example, create a web page to choose the size of both a drink and a hamburger, each one inside a different <fieldset> with the corresponding <legend>. Also, as done before, the user should be able to choose among three different sizes: small, medium and large:

Drink

Hamburger

A form with sections

Beyond the structures specific to web forms, it’s good to remember that form markup is just HTML. This means that you can use all the power of HTML to structure a web form. As you can see in the examples, it’s common practice to wrap a label and its widget with a <p> element within. Lists are also recommended for structuring multiple checkboxes or radio buttons.

In addition to the <fieldset> element, it’s also common practice to use HTML titles (e.g. <h1>, <h2>) and sectioning (e.g. <section>) to structure complex forms. Above all, it is up to you to find a comfortable coding style that results in accessible, usable forms. Each separate section of functionality should be contained in a separate <section> element, with <fieldset> elements to contain radio buttons.

Let’s put these ideas into practice and build a slightly more involved form — a payment form. This form will contain more control types than the previous example. Read the descriptions carefully as you follow the below instructions, and start to form an appreciation of which wrapper elements we are using to structure the form, and why.

  1. First, we will create the form by adding the outer <form> element:
<form>
    ...
</form>
  1. Inside the <form> tags, we will add a heading and paragraph to inform users how required fields are marked:
<form>
  <h1>Payment form</h1>
  <p>Required fields are followed by <strong>*</strong>.</p>
  ...
</form>
  1. We’ll also add a simple <button> of type submit, at the bottom of the form, for submitting the form data:
<form>
  <h1>Payment form</h1>
  <p>Required fields are followed by <strong>*</strong>.</p>
  ...
  <p><button type="submit">Validate the payment</button></p>
</form>
  1. Next we’ll add a larger section of code into the form, below our previous entry. Here you’ll see that we are wrapping the contact information fields inside a distinct <section> element. Moreover, we have a set of three radio buttons, each of which we are putting in a new line. We also have two standard text <input> and their associated <label> elements, each contained inside a <p>, and a password input for entering a password:
<form>
  <h1>Payment form</h1>
  <p>Required fields are followed by <strong>*</strong>.</p>

  <section><fieldset>
    <legend><h2>Contact information</h2></legend>

    <fieldset>
      <legend>Title</legend>
      <label><input type="radio" name="title" value="mr" />Mr</label><br />
      <label><input type="radio" name="title" value="mrs" />Mrs</label><br />
      <label><input type="radio" name="title" value="miss" />Miss</label><br />
    </fieldset>

    <p><label>Name: <input type="text" name="name" required /> *</label></p>
    <p><label>E-mail: <input type="email" name="email" required /> *</label></p>
    <p><label>Password: <input type="password" name="password" required /> *</label></p>
  </fieldset></section>

  ...

  <p><button type="submit">Validate the payment</button></p>
</form>
  1. The second <section> of our form is the payment information. We have three distinct controls along with their labels, each contained inside a <p>. The first is a drop-down menu (<select>) for selecting credit card type. The second is an <input> element of type tel, for entering a credit card number; while we could have used the number type, we don’t want the number’s spinner UI. The last one is an <input> element of type date, for entering the expiration date of the card; this one will come up with a date picker widget in supporting browsers, and fall back to a normal text input in non-supporting browsers.
<form>
  <h1>Payment form</h1>
  <p>Required fields are followed by <strong>*</strong>.</p>

  <section><fieldset>
    <legend><h2>Contact information</h2></legend>
    <fieldset>
      <legend>Title</legend>
      <label><input type="radio" name="title" value="mr" />Mr</label><br />
      <label><input type="radio" name="title" value="mrs" />Mrs</label><br />
      <label><input type="radio" name="title" value="miss" />Miss</label> <br />
    </fieldset>
    <p><label>Name: <input type="text" name="name" required /> *</label></p>
    <p><label>E-mail: <input type="email" name="email" required /> *</label></p>
    <p><label>Password: <input type="password" name="password" required /> *</label></p>
  </fieldset></section>

  <section><fieldset>
    <legend><h2>Payment information</h2></legend>

    <p><label>Card type:
      <select name="card_type">
        <option value="visa">Visa</option>
        <option value="mc">Mastercard</option>
        <option value="amex">American Express</option>
      </select>
    </label></p>

    <p><label>Card number: <input type="tel" name="card_number" required /> *
    </label></p>

    <p><label>Expiration date: <input type="date" name="expiration" required /> *
    </label></p>
  </fieldset></section>

  <p><button type="submit">Validate the payment</button></p>
</form>

Proposed exercise: Payment form

Using the code of the example above, create a more sophisticated payment form. Inside the “Contact information” section, you have to add a group of radio buttons so that the user can select its status (either “Student”, “Teacher”, or “Other”), and a new text field to enter the phone number. And inside the “Payment information” section you have to add a new selection box so that the user can select the preferred payment type (either “Credit card” or “Paypal”) and a new email field to enter the Paypal account:

PAYMENT FORM (Required fields are followed by *)

Contact information
Status


Title






Payment information






A real example: search engine forms

Searching for text

Let’s now create a simple form which will provide all necessary data (a simple text) to be passed to some of the most known search engines:

<form action="https://google.com/search" method="GET">
  <label>Google: <input type="text" name="q" required /></label>
  <button type="submit">Search</button>
</form>
...
<form action="https://duckduckgo.com/" method="GET">
  <label>DuckDuckGo: <input type="text" name="q" required /></label>
  <button type="submit">Search</button>
</form>
...
<form action="https://bing.com/search" method="GET">
  <label>Bing: <input type="text" name="q" required /></label>
  <button type="submit">Search</button>
</form>

You will notice that when you press the submit button, the query (q parameter) is included in the url, and this way the search engine will know what to search. For example, if we are searching the word “dogs” on Google, the resulting url when submitting the form will be this one: https://www.google.es/search?q=dog.

Proposed exercise: Text search

Using the example of the form above to search information on Google, DuckDuckGo and Bing, develop a web page similar to the one below to search information on several search engines (at least five).

The only difference from one form to another is the value of the action attribute (“https://google.com/search”, “https://duckduckgo.com/”, “https://bing.com/search”, “https://www.ecosia.org/search”, “https://search.givewater.com/serp”, etc.). This address can be guessed by having a look at the url when you are using each particular search engine.

TEXT SEARCH

Searching for images

Now we will change the code a little bit so that the results provided by the search engines are images instead of text. In some cases we only need to change the action attribute, but sometimes we have to add some additional fields:

<form action="https://google.com/search" method="GET">
  <label>Gooogle: <input type="text" name="q" required /></label>
  <input type="hidden" name="tbm" value="isch" />
  <button type="submit">Search</button>
</form>
...
<form action="https://duckduckgo.com/" method="GET">
  <label>DuckDuckGo: <input type="text" name="q" required /></label>
  <input type="hidden" name="iax" value="images" />
  <input type="hidden" name="ia" value="images" />
  <button type="submit">Search</button>
</form>
...
<form action="https://search.givewater.com/serp" method="GET">
  <label>giveWater: <input type="text" name="q" required /></label>
  <input type="hidden" name="qc" value="images" />
  <button type="submit">Search</button>
</form>

You will notice that when you press the submit button, those hidden fields (which are not entered by the user) are included automatically in the url so that the search engine knows that has to show images instead of text. This way, in this example we are passing two parameters: q (the search string) and tbm (to search for images). For example, if we are searching for images about dogs on Google, the resulting url when submitting the form will be this one: https://www.google.es/search?q=dog&tbm=isch.

Proposed exercise: Image search

Using the code of the previous exercise, develop a new web page to search for images on several search engines (at least five).

To search for images using Bing and Ecosia, you only have to use the right value for the action attribute (“https://bing.com/images/search”, “https://www.ecosia.org/images”). You only have to use the hidden fields for Google (tbm), DuckDuckGo (iax, ia) and giveWater (qc), as done in the example above. Both the addresses and the hidden fields can be guessed by having a look at the url when you are using each particular search engine.

IMAGE SEARCH

Choosing between text and image search

Now let’s concentrate on Google’s search engine and let’s go one step forward to add a checkbox to give the user the option to choose between searching for text or images:

<form action="https://google.com/search">
  <label>Google: <input type="text" name="q" required /></label>
  <label>Search for images <input type="checkbox" name="tbm" value="isch" /></label>
  <button>Search</button>
</form>

Proposed exercise: Text or images

Develop a web page to search either text or images on Google and giveWater search engines. You have to provide the user with a checkbox so that can easily change from one type to another:

TEXT SEARCH

Filtering the results

Finally let’s concentrate again on Google’s search engine to add several controls so that the user can filter the results when searching for images. We will also add a reset button to set the default values:

<form action="https://google.com/search" method="GET">
  <p>Search: <input type="text" name="q" class="big" required /></p>

  <fieldset>
    <legend>Size</legend>
    <select name="tbs"> 
      <option selected disabled>Any size</option>
      <option value="isz:l">Large</option>
      <option value="isz:m">Medium</option>
      <option value="isz:i">Icon</option>
    </select>
  </fieldset>
  <fieldset>
    <legend>Color</legend>        
    <select name="tbs"> 
      <option selected disabled>Any color</option>
      <option value="ic:color">Color</option>
      <option value="ic:gray">Black and white</option>
      <option value="ic:trans">Transparent</option>
   </select>
  </fieldset> 
  <fieldset>       
    <legend>Type</legend>        
    <select name="tbs"> 
      <option selected disabled>Any type</option>
      <option value="itp:clipart">Clip art</option>
      <option value="itp:lineart">Line drawing</option>
      <option value="itp:animated">GIF</option>
    </select>
  </fieldset>  
  <fieldset>     
    <legend>Date</legend>        
    <select name="tbs"> 
      <option selected disabled>Any date</option>
      <option value="qdr:d">Past 24 hours</option>
      <option value="qdr:w">Past week</option>
    </select>
  </fieldset> 
  <fieldset>       
    <legend>License</legend>        
    <select name="tbs"> 
      <option selected disabled>Any license</option>
      <option value="il:cl">Creative commons</option>
      <option value="il:ol">Commercial and other</option>
    </select>
  </fieldset>

  <input type="hidden" name="tbm" value="isch" />

  <button type="reset">Reset</button>
  <button type="submit">Search</button>
</form>

As you will see, we have added many options to set different values for a parameter called “tbs” (we have guessed this parameter and all its possible values by looking at the url when searching for any information on Google). This way, in this example we are passing three parameters: q (the search string), tbm (to search for images) and tbs (to filter the results). For example, if we are searching for GIF images about dogs, the resulting url when submitting the form will be this one: https://www.google.es/search?q=dog&tbm=isch&tbs=itp:animated.

Also you will notice that inside each <select> element we are using a default option: <option selected disabled>...</option> so that by default, none of the available options are selected and the results are not filtered.

Proposed exercise: Filtering images with dropdown boxes

Using the code of the previous example, develop a web page to search for images on Google and filter the results using several dropdown boxes:

Search:

Size
Color
Type
Date
License

Proposed exercise: Filtering images with radio buttons

Create a new web page to search for images on Google and filter the results using radio buttons:

Search:

Size Large
Medium
Icon
Color Color
Black and white
Transparent
Type Clip art
Line drawing
GIF
Date Past 24 hours
Past week
License Creative commons
Commercial and other

Quiz

Test your skills with this quiz about HTML forms and some other concepts related to this unit.