CSS Boxes and Borders

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 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 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.