Styling With CSS
Section overview
In this lesson, we'll style our tip calculator to make it more visually appealing and user-friendly using CSS.
Setting up the CSS file
First, create a new file called styles.css
, create this alongside the index.html
inside your project folder. Add this line in the <head>
section of your HTML, just above the title:
<link rel="stylesheet" href="styles.css" />
<title>Quick Tip</title>
- The
<link>
element defines a relationship between the document and the resource/file we are linking to. - We declare the relationship is a stylesheet using
rel="stylesheet"
. href="styles.css"
points to our CSS file we just created.
Adding class names
A CSS class is a reusable selector that can be applied to one or more HTML elements. It is like adding a label to a single or group of elements so we can apply CSS styling.
We set them using a class
attribute to a HTML element such as <div class="bill">
. Update the HTML to include class names as below:
<main id="container">
<h1>Quick Tip</h1>
<section>
<div class="bill">
<label for="yourBill">Bill</label>
<input type="number" placeholder="Your bill" id="yourBill" />
</div>
<div>
<div class="space-between">
<label for="tipInput">Select tip</label
><span id="tipPercent"></span>
</div>
<input type="range" value="0" id="tipInput" />
</div>
<div class="space-between">
<span>Tip</span>
<span id="tipValue"></span>
</div>
<hr />
<div class="space-between total">
<span>Total</span>
<span id="totalWithTip"></span>
</div>
</section>
<section>
<div>
<div class="space-between">
<label for="splitInput">Split</label>
<span id="splitValue"></span>
</div>
<input type="range" min="1" max="10" value="1" id="splitInput" />
</div>
<div class="space-between">
<span>Bill each</span>
<span id="billEach"></span>
</div>
<div class="space-between">
<span>Tip each</span>
<span id="tipEach"></span>
</div>
</section>
</main>
Base styles
Let's begin with some basic styles to set up our default font size and color. Update the styles.css
file with the following:
html {
font-size: 10px;
color: #4e4d4d;
}
- We set the base font size to
10px
to make it easier to work with rem units (1rem = 10px). - The text color is set to a dark gray (
#4e4d4d
) for better contrast.
Main container styling
Next, let's style the main container. This was the main wrapper for our project:
main {
max-width: 50rem;
margin: 2rem auto;
padding: 3rem 1.6rem 6rem 1.6rem;
background: linear-gradient(30deg, #3dc0c0, #5bceae);
border-radius: 4rem;
box-shadow: 0 2px 2px lightgray;
}
max-width: 50rem
limits the width of the container so the content does not stretch too far.margin: 2rem auto
centers the container horizontally, 2rem of space on the top/bottom. Theauto
value will automatically distribute the remaining space equally between the left/right sides.- The gradient background blends between two values at a 30 degree angle.
border-radius
rounds all four corners.box-shadow
adds subtle depth to the wrapper.
Heading styles
The app title was set in a level-1 heading, apply the following to style the title:
h1 {
text-align: center;
font-size: 3rem;
margin: 0;
padding-bottom: 3rem;
}
text-align: center;
Centers the heading.- Sets a large font size (30px).
margin: 0;
Removes default margin.- Adds padding at the bottom for spacing.
Section styling
Now let's style the sections that contain our inputs and results:
section {
background-color: white;
font-size: 1.8rem;
margin-bottom: 1.6rem;
padding: 0 1.6rem;
border-radius: 1rem;
box-shadow: inset 0 2px 2px lightgray;
}
section > div {
padding: 1.6rem 0;
}
background-color
: White background for contrast.- Comfortable font size (18px).
border-radius
: Rounded corners for all sections.box-shadow
: Inset shadow for depth.- Padding for content spacing.
section > div
: This targets any<div>
that is a direct child of a<section>
element. The padding of 1.6rem on the top and bottom creates vertical space inside each section.
Bill input styling
The bill section has a class="bill"
, use this to style the section:
.bill {
display: flex;
}
.bill label {
flex: 1;
align-self: center;
}
.bill input {
flex: 3;
}
display: flex
: The CSS flexbox is used to layout content in one direction. Either horizontally or vertically.flex
: Label takes up a single space value, and thebill
attempts to take up three times the space if available.align-self: center;
: Vertically centers the label.
Input field styling
Let's style all number inputs:
input[type='number'] {
font-family: inherit;
background: #f4f4f5;
font-size: 1.6rem;
border: 1px solid lightgray;
border-radius: 0.5rem;
padding: 0.8rem 0 0.8rem 1rem;
box-shadow: inset 0 2px 2px lightgray;
}
input[type='number']
: This will select theinput
element with the number type set.
Space between utility class
Let's create a utility class for spacing elements. This is re-used on multiple <div>
elements to apply consistent styling:
.space-between {
display: flex;
justify-content: space-between;
}
display: flex
: Defines this element to be a flex container.justify-content
: Sets this aignment of child items along the main axis. We have not set aflex-direction
property, therefore defaulting torow
(horizontal content).space-between
: When the flex container contains multiple child items, these items have equal space between them, after placing the first and last items on the start/end of the line.
Total amount styling
Our final styling is for the total amount:
.total {
font-size: 2rem;
font-weight: bolder;
}
Final CSS code
Here's the complete CSS code for our tip calculator:
html {
font-size: 10px;
color: #4e4d4d;
}
main {
max-width: 50rem;
margin: 2rem auto;
padding: 3rem 1.6rem 6rem 1.6rem;
background: linear-gradient(30deg, #3dc0c0, #5bceae);
border-radius: 4rem;
box-shadow: 0 2px 2px lightgray;
}
h1 {
text-align: center;
font-size: 3rem;
margin: 0;
padding-bottom: 3rem;
}
section {
background-color: white;
font-size: 1.8rem;
margin-bottom: 1.6rem;
padding: 0 1.6rem;
border-radius: 1rem;
box-shadow: inset 0 2px 2px lightgray;
}
section > div {
padding: 1.6rem 0;
}
.bill {
display: flex;
}
.bill label {
flex: 1;
align-self: center;
}
.bill input {
flex: 3;
}
input[type='number'] {
font-family: inherit;
background: #f4f4f5;
font-size: 1.6rem;
border: 1px solid lightgray;
border-radius: 0.5rem;
padding: 0.8rem 0 0.8rem 1rem;
box-shadow: inset 0 2px 2px lightgray;
}
.space-between {
display: flex;
justify-content: space-between;
}
.total {
font-size: 2rem;
font-weight: bolder;
}