CSS (cascading style sheets) are used to style websites: change the font, colour, spacing, sizes of things, add animations etc.
Use:
- HTML for structure
- CSS for style and animations
- JavaScript for interactivity.
Most of the time, you don’t need JS for a website to look dynamic.
- style attribute:
<h1 style="color: red;">
- inline stylesheet:
<style type="text/css"> h1 { color: blue; } </style>
- external stylesheet:
<link rel="stylesheet" href="style.css" />
p {
color: darkblue;
font-weight: bold;
}Applies style to the HTML elements that have the same tag name — p, button, input etc.
button {
height: 40px;
font-size: 20px;
}Applies style to all elements that have the class.
.large {
width: 200px;
height: 40px;
font-size: 20px;
}<button class="large">Click me</button>
<input class="large" />- element:
p, h2, div, input… - class:
.box, .big-button - elements with pseudo-classes:
a:active, .card:hoveretc. (:hoveris the most used)
Just a few examples to get started.
.something {
background: blue;
color: #fff0f0;
}This is a good reference for colors.
Text alignment: text-align: center; (or left, right, justify)
Font size: font-size: 2em (em: relative size to document font size. px: pixels.)
- Go to fonts.google.com
- Find the font you want and click 'Select this style'
- If the sidebar doesn't open, click the top right icon with squares.
- Between
<link>and@import, choose@import, and copy the code between the<style>tags. You don't need to copy the<style>tag. - Paste the copied line to the top of your CSS file.
Use it as font-family: 'The Font Name';
.one {
width: 600px; /* in pixels */
height: 300px;
max-width: 100%; /* of parent (or screen) */
margin: 20px; /* all sides */
}
.two {
margin: 0 40px; /* 0 on top and bottom, 40px on the sides */
}
.centered {
margin: 0 auto; /* "automatically" as big as possible on the sides */
}
.grid {
/* A simple grid with content arranged in 3 columns. */
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 40px;
}
.card {
border: 4px solid #404040;
border-radius: 3px;
box-shadow: 0 0 8px 0 lightgrey;
}transform: rotate(5deg); /* rotate by 5 degrees */
transform: scale(1.2); /* make 1.2x bigger */There are many more.
Smooth "animations" that change from one style to the other when their properties change. We'll use this with :hover.
Transition should be applied to the original selector (or both if different), not only the :hover state.
.thing {
background: white;
color: blue;
transition: background .5s ease; /* single-property transition only */
transition: all .3s ease; /* all properties */
transition: background .3s ease, color .3s ease; /* only needed properties.
If you transition more than one property, this is a better way than "all". */
}
.thing:hover {
background: blue;
color: white;
}Note: you'll need to restart the server after changes.
- Create and use an external stylesheet called
style.css - Change the body background and the title color.
- Center all the text.
- Import the web font Permanent Marker (from Google Fonts), and apply it to all the text.
- Using the MDN text-shadow docs, apply some text shadow to the title.
- Remove the inline style from the hero image, and use CSS to set max width to 100%. (We'll need this later)
- Wrap all the content inside
bodyin a<main>element. Add a maximum width (something between 600 and 1200px) and center it withmargin. - Wrap the heroes in a
<div>, and add a class to it (e.g.hero-list). - Display the heroes in a 4-column grid.
- Add a class to the hero links (
cardorherofor example) - Add a white background.
- Add a border, border-radius and some box-shadow.
- With the pseudo-class
:hover, apply a transformation to the hero when the mouse is over it. (Hint: usetransform: ...) - Apply a transition between the normal and hover state. Hint: use the changed property name for the transition.
- Learn CSS on MDN
- Why is it called cascading?
There are many pre-coded solutions (think of them as dependencies or external stylesheets) that make styling web apps easier. One of the best and easiest to use is TailwindCSS – at its core, it's a collection of classes that you can use in HTML pages (including Flask templates).