CSS All Exercises & Assignments

Write CSS to change the text colour and background of a paragraph

Description: Create a paragraph with white text on a dark blue background, with some padding.

Hint:

  • Use color for the text and background-color for the background.
  • Add padding so the text does not touch the edges.

CSS for text colour and background

<!DOCTYPE html>
<html>
<head>
  <style>
    p {
      color: white;
      background-color: darkblue;
      padding: 12px;
    }
  </style>
</head>
<body>
  <p>White text on a dark blue background.</p>
</body>
</html>

Output in the browser:

Output in the browser

White text on a dark blue background.

How it works

  • The selector p applies the rule to every paragraph.
  • color changes the text colour, and background-color changes the area behind it.

Read CSS Colors.

Write CSS to style a heading (centre, uppercase, letter spacing)

Description: Style an h1 heading so that it is centred, in capital letters, with extra space between the letters and a line below it.

Hint:

  • Use text-align, text-transform, letter-spacing and border-bottom.

CSS to style a heading

<style>
  h1 {
    text-align: center;
    text-transform: uppercase;
    letter-spacing: 4px;
    border-bottom: 3px solid #0073aa;
    padding-bottom: 8px;
  }
</style>

<h1>Welcome to my site</h1>

Output in the browser:

Output in the browser

Welcome to my site

How it works

  • text-transform: uppercase shows the text in capitals, though it is written in small letters in HTML.
  • letter-spacing adds space between the letters.

Read CSS Text.

Write CSS to create a box with border, padding and rounded corners

Description: Create a box that has a 2px green border, 20px padding, 10px rounded corners and a light green background.

Hint:

  • Use border, padding, border-radius and background-color.

CSS for a box with rounded corners

<style>
  .box {
    width: 300px;
    padding: 20px;
    border: 2px solid green;
    border-radius: 10px;
    background-color: #e8f5e9;
  }
</style>

<div class="box">This is a box with rounded corners.</div>

Output in the browser:

Output in the browser
This is a box with rounded corners.

How it works

  • padding adds space inside the border.
  • border-radius rounds the corners.

Read CSS Borders and CSS Box Model.

Write CSS to centre a box horizontally using margin auto

Description: Centre a box of width 250px horizontally on the page.

Hint:

  • A block element with a width can be centred with margin: 0 auto.

CSS to centre a box with margin auto

<style>
  .box {
    width: 250px;
    margin: 0 auto;
    padding: 16px;
    background: #ffe0b2;
    text-align: center;
  }
</style>

<div class="box">I am in the centre.</div>

Output in the browser:

Output in the browser
I am in the centre.

How it works

  • margin: 0 auto means zero margin on top and bottom, and automatic margin on left and right.
  • The browser splits the free space equally on both sides. Without a width, the block takes the full width, so there is no free space to share.

Read CSS Margins.

Write CSS to centre a div horizontally and vertically using Flexbox

Description: Centre a small box in the middle of a 200px high container.

Hint:

  • Make the parent a flex container.
  • Use justify-content: center and align-items: center.

CSS to centre a div with Flexbox

<style>
  .parent {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 200px;
    background: #eceff1;
  }
  .child {
    padding: 12px 20px;
    background: #7e57c2;
    color: white;
  }
</style>

<div class="parent">
  <div class="child">Centred</div>
</div>

Output in the browser:

Output in the browser
Centred

How it works

  • justify-content works on the main axis (horizontal by default) and align-items on the cross axis (vertical).
  • The parent needs a height, otherwise there is no free space to centre in.

Read CSS Flexbox.

Write CSS to create a horizontal navigation menu

Description: Create a dark horizontal menu with the links Home, About, Services and Contact, and a different background when the mouse is over a link.

Hint:

  • Remove bullets with list-style: none.
  • Use display: flex on the ul.
  • Style the a tags with padding, and add an a:hover rule.

CSS horizontal navigation menu

<style>
  .menu {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    background: #333;
  }
  .menu a {
    display: block;
    padding: 12px 18px;
    color: white;
    text-decoration: none;
  }
  .menu a:hover {
    background: #0073aa;
  }
</style>

<ul class="menu">
  <li><a href="#">Home</a></li>
  <li><a href="#">About</a></li>
  <li><a href="#">Services</a></li>
  <li><a href="#">Contact</a></li>
</ul>

Output in the browser:

Output in the browser

How it works

  • A menu is a list of links. display: flex on the ul puts the items in one row.
  • display: block with padding on the links makes the whole box clickable, not only the text.
  • On your page, the background of a link turns blue when you hover over it (the hover colour does not show in this box).

Read CSS Lists and CSS Links.

Write CSS to style a table with zebra stripes

Description: Style a table of names and marks with collapsed borders, a blue header and grey even rows.

Hint:

  • Use border-collapse: collapse.
  • Use tr:nth-child(even) for the stripes.

CSS for a striped table

<style>
  table { border-collapse: collapse; width: 100%; }
  th, td { border: 1px solid #ccc; padding: 8px; text-align: left; }
  th { background: #0073aa; color: white; }
  tr:nth-child(even) { background: #f2f2f2; }
</style>

<table>
  <tr><th>Name</th><th>Marks</th></tr>
  <tr><td>Robin</td><td>85</td></tr>
  <tr><td>Maria</td><td>92</td></tr>
  <tr><td>John</td><td>78</td></tr>
  <tr><td>Asha</td><td>88</td></tr>
</table>

Output in the browser:

Output in the browser
Name Marks
Robin 85
Maria 92
John 78
Asha 88

How it works

  • border-collapse: collapse joins neighbour borders.
  • tr:nth-child(even) selects the 2nd, 4th, 6th row and so on. The header row is the first row, so it is odd.

Read CSS Tables.

Write CSS to create a three column layout using Flexbox

Description: Create three columns: a left sidebar, a main content area (twice as wide) and a right sidebar.

Hint:

  • Make the container display: flex.
  • Use flex: 1 for the sides and flex: 2 for the middle.

CSS three column layout

<style>
  .row { display: flex; gap: 10px; }
  .left, .right { flex: 1; background: #ffe0b2; padding: 12px; }
  .main { flex: 2; background: #c8e6c9; padding: 12px; }
</style>

<div class="row">
  <div class="left">Left</div>
  <div class="main">Main content</div>
  <div class="right">Right</div>
</div>

Output in the browser:

Output in the browser
Left
Main content
Right

How it works

  • The free space is shared in the ratio 1 : 2 : 1, so the middle column gets half of the width.
  • gap adds space between the columns.

Read CSS Flexbox.

Write CSS to create a responsive card grid using CSS Grid

Description: Create a grid of four cards that fits as many columns as the screen allows, and stacks on a phone.

Hint:

  • Use display: grid with grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)).

CSS responsive card grid

<style>
  .cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 12px;
  }
  .card {
    border: 1px solid #ddd;
    border-radius: 6px;
    padding: 14px;
  }
</style>

<div class="cards">
  <div class="card">Card 1</div>
  <div class="card">Card 2</div>
  <div class="card">Card 3</div>
  <div class="card">Card 4</div>
</div>

Output in the browser:

Output in the browser
Card 1
Card 2
Card 3
Card 4

How it works

  • minmax(150px, 1fr) says each column is at least 150px wide, and can grow to share the free space.
  • auto-fit makes as many columns as fit. On a narrow phone the cards come in one column.

Read CSS Grid.

Write CSS to create a circle and a pill-shaped button

Description: Create a red circle of 100px and a green button with fully rounded sides.

Hint:

  • A circle needs equal width and height with border-radius: 50%.
  • Use a big value like 999px for a pill shape.

CSS circle and pill button

<style>
  .circle {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    background: #e53935;
  }
  .pill {
    display: inline-block;
    padding: 10px 28px;
    border-radius: 999px;
    background: #43a047;
    color: white;
  }
</style>

<div class="circle"></div>
<span class="pill">Pill button</span>

Output in the browser:

Output in the browser

Pill button

How it works

  • 50% rounds each corner by half of the side, so a square becomes a circle.
  • A very big radius on a wide box rounds the short sides fully, which gives a pill.

Read Rounded Corners in CSS.

Write CSS to create a card with box shadow and gradient background

Description: Create a card with a purple to blue gradient background, white text, rounded corners and a soft shadow.

Hint:

  • Use background-image: linear-gradient(...).
  • Use box-shadow with a transparent black colour.

CSS card with shadow and gradient

<style>
  .card {
    padding: 24px;
    color: white;
    border-radius: 10px;
    background-image: linear-gradient(135deg, #7b1fa2, #1976d2);
    box-shadow: 4px 6px 14px rgba(0, 0, 0, 0.3);
  }
</style>

<div class="card">
  <h3>Gradient card</h3>
  <p>With a soft shadow.</p>
</div>

Output in the browser:

Output in the browser

Gradient card

With a soft shadow.

How it works

  • 135deg sets a diagonal direction for the gradient.
  • The shadow uses rgba with 0.3 opacity, so it looks soft.

Read CSS Gradients and CSS Shadows.

Write CSS to make an image responsive and round

Description: Make one image that shrinks on small screens, and another image shown as a 100px circle without stretching.

Hint:

  • Use max-width: 100%; height: auto; for the first image.
  • Use border-radius: 50% and object-fit: cover for the avatar.

CSS responsive and round image

<style>
  .photo { max-width: 100%; height: auto; }
  .avatar {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    object-fit: cover;
  }
</style>

<img class="photo" src="mountain.jpg" alt="Mountain">
<img class="avatar" src="mountain.jpg" alt="Profile picture">

Output in the browser:

Output in the browser

How it works

  • max-width: 100% lets the image become smaller than its real size, but never bigger than its box.
  • object-fit: cover crops the picture to fill the circle, so it does not look squeezed.

Read CSS Images.

Write CSS to style a login form

Description: Style a login form with a username, a password and a Log in button, inside a bordered box of 300px.

Hint:

  • Use width: 100% and box-sizing: border-box on the inputs.
  • Use input:focus to show an outline when the field is selected.

CSS login form

<style>
  .login { width: 300px; padding: 20px; border: 1px solid #ccc; border-radius: 6px; }
  .login input {
    width: 100%;
    padding: 8px;
    margin: 4px 0 12px;
    box-sizing: border-box;
    border: 1px solid #aaa;
    border-radius: 4px;
  }
  .login input:focus { outline: 2px solid #0073aa; }
  .login button {
    width: 100%;
    padding: 10px;
    background: #0073aa;
    color: white;
    border: 0;
    border-radius: 4px;
  }
</style>

<form class="login">
  <label>Username</label>
  <input type="text">
  <label>Password</label>
  <input type="password">
  <button type="submit">Log in</button>
</form>

Output in the browser:

Output in the browser

How it works

  • box-sizing: border-box makes width: 100% include the padding and border, so the inputs do not stick out of the box.
  • input:focus styles the field that has the cursor. Do not remove the focus outline without giving another one.

Related: HTML Forms and HTML login form template.

Write CSS to create a pulsing animation

Description: Create a circle that grows and shrinks again and again.

Hint:

  • Define the steps with @keyframes.
  • Attach it with animation: name duration timing infinite.

CSS pulsing animation

<style>
  @keyframes pulse {
    0%   { transform: scale(1); }
    50%  { transform: scale(1.3); }
    100% { transform: scale(1); }
  }
  .dot {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: #e91e63;
    animation: pulse 1.2s ease-in-out infinite;
  }
</style>

<div class="dot"></div>

Output in the browser:

Output in the browser

How it works

  • The result is a pink circle. On your page it grows to 1.3 times its size at 50% of the time and then returns to normal. This does not move in this box.
  • infinite repeats the animation forever.
  • transform is smooth and cheap for the browser to animate.

Read CSS Transitions and Animations.