Cool Stylin’ Sheets¶
Let’s add some Cascading Style Sheets (CSS) to customize our page better.
Selectors: The ba-CSS-ics¶
Insert the following code in the <head>
right before the closing tag (i.e. </head>
):
html
is theCSS
selector, basically saying, “get anything in thehtml
tags!background-color
is the key, and thenaqua
is the color we are setting it to.
What happened to the page?
Answer
It became aqua
!
Special selectors¶
The *
selector is a special selector that selects all elements on the page. This is useful for resetting all margins and paddings to ensure consistency across browsers. Add the following code to the <style>
tag:
font-family
is the key, and thenArial, sans-serif
is the font we are setting it to.
Combining selectors¶
You can combine selectors to target specific elements. Add the following code to the <style>
tag:
This code sets the height of the html
and body
elements to 100vh
, which is 100% of the viewport height. This ensures that the content fits the screen height.
Adding linked CSS¶
CSSelecting content and changing them is cool, but what if you want to change the style of multiple pages? You would have to copy and paste the <style>
tag into each page!
So this way of using CSS, called inline CSS (because it is in the HTML), can make your HTML file long and cumbersome.
Standard practice is to have a separate file for CSS and bring that whole file in as a linked source.
Click the New Folder button:
Type in styles to name the folder styles
:
Highlight the styles
folder by clicking on it:
Then click on the New file button:
Name the file style.css:
Double click to open the new file. Then copy and paste the following CSS:
/* The * selects everything and acts as a global reset to ensure consistency across browsers */
* {
font-family: Arial, sans-serif;
margin: 0;
padding: 0; /*(4)! */
box-sizing: border-box; /*(1)! */
}
/* Sets the overall background and ensures the content fits the screen height */
html{
background-color: aqua; /*(3)! */
}
html, body {
height: 80vh; /*(2)! */
padding: 1rem;
}
/* Defines the page layout using a grid */
body {
display: grid; /*(5)! */
grid-template-areas:
"header"
"main"
"footer"; /*(6)! */
grid-template-rows: auto 1fr auto; /* Adjusted to reflect the simplified structure */
}
main {
display: grid; /*(14)! */
grid-template-areas:
"portfolio map"; /*(15)! */
grid-template-columns: 1fr 1fr; /*(16)! */
}
/* Assigns each major section to a part of the grid */
header {
grid-area: header; /*(7)! */
}
main {
grid-area: main; /*(8)! */
}
.portfolio {
grid-area: portfolio; /*(13)! */
}
#map {
grid-area: map; /*(9)! */
height: 80vh; /*(10)! */
}
footer {
grid-area: footer; /*(11)! */
}
- Box-Sizing: Makes sure padding and border sizes are included in the total width and height of elements, ensuring a consistent starting point across all browsers by resetting margins, padding, and box-sizing.
- Viewport Height (VH): Calculates the height of the page to be 80% of the viewport height.
- Background Color: Colors the background of the page in light blue.
- Padding: Adds space around the content inside
html
andbody
. - Grid Display: Turns the
body
into a grid container for layout. - Grid Template Areas: Defines the layout areas for the page sections.
- Header Area: Places the header at the top of the page.
- Main Area: Places the main content in the middle, now using a grid to organize internal sections.
- Map Area: Designates an area for the map.
- Map Height: Specifies how tall the map should be, taking up 80% of the viewport height.
- Footer Area: Places the footer at the bottom of the page.
- (Removed)
- Portfolio Area: Specifies the grid area for the portfolio section within the main area.
- Main Grid Display: Introduces a grid within the
main
area to organize the portfolio and map sections. - Main Grid Template Areas: Defines the layout for the portfolio and map within the main area.
- Main Grid Columns: Sets up two columns of equal width for the portfolio and map sections.
Reminder!
Remember to save the style.css
file (PC:Ctrl+S | Mac:Cmd+S!
Next, go back to the index.html file and replace your entire <style> </style>
content and tags with this code:
This code tells the HTML file to use all of the CSS styles linked in the href
attribute.
More external CSS files?
You can have as many external references as you’d like, as long as you link them in this way. The bottom most CSS file has the most priority because it is the last CSS read and applied!
⚽ In-Class Exercise #2¶
Task
- Link
MapLibreGL
’s CSS that exists at this url:https://unpkg.com/maplibre-gl/dist/maplibre-gl.css
Extra: If you finish early, try to see if you can load MapLibreGL’s CSS locally instead!
We will go into CSS in more detail later, but what you need to know is that CSS has HTML element selectors
which are then followed by the styles in { }
.
🏁Checkpoint¶
- Your
css
file should be in a folder calledstyles
and namedstyle.css
. - Your
index.html
file should have a link to thestyle.css
file. - Your
index.html
file should have a link to themaplibre-gl.css
file.
Check to see if your code looks likes the following before moving on: