CSS Top Exercises
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-radiusandbackground-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:
How it works
paddingadds space inside the border.border-radiusrounds the corners.
Read CSS Borders and CSS Box Model.
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: centerandalign-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:
How it works
justify-contentworks on the main axis (horizontal by default) andalign-itemson 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: flexon theul. - Style the
atags with padding, and add ana:hoverrule.
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:
How it works
- A menu is a list of links.
display: flexon theulputs the items in one row. display: blockwith 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).
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: gridwithgrid-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:
How it works
minmax(150px, 1fr)says each column is at least 150px wide, and can grow to share the free space.auto-fitmakes as many columns as fit. On a narrow phone the cards come in one column.
Read CSS Grid.
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%andbox-sizing: border-boxon the inputs. - Use
input:focusto 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:
How it works
box-sizing: border-boxmakeswidth: 100%include the padding and border, so the inputs do not stick out of the box.input:focusstyles the field that has the cursor. Do not remove the focus outline without giving another one.
Related: HTML Forms and HTML login form template.