Refugees Code
Contents
Contents
01. Introduction to CSS
Basic CSS styling
This is how CSS looks like:
h1 { color: blue; background-color: yellow; border: 1px solid black;}
p { color: red;}Let's read it:
- First, we have two selectors (
h1andp). Each one will affect allh1andptags in our page. - The first one will look for all
h1tags and will apply the properties between the brackets to those tags.- The first one sets the text color to blue.
- The second sets the background color to yellow.
- The third one puts a border around the header that is 1 pixel wide, solid (not dotted, or dashed, etc.), and colored black. Important notice: in this example and over the course we will use shorthands, which is shorten code to specify all the individual properties in one property. The
borderproperty is a shorthand property for the following individual border properties:border-widthborder-style(required)border-color
- The second selector (
p) will change the text to red.
Read this article to learn how CSS syntax works and see examples.
CSS Selector
One of the core things about CSS is to address the element that you want to style. If you fail to do so, you could be altering many other elements that you didn't want to style that way or not style anything at all due to lack of selector accuracy.
Every selector of CSS is composed of a selector and its attributes. There are multiple selector types and hundreds of attributes, but most of the time it will follow the same structure.
This is how a CSS selector looks like:
Important: If a property is unknown or if a value is not valid for a given property, the declaration is deemed invalid and is wholly ignored by the browser's CSS engine.
You will read more about CSS selectors in the next section, this was just an introduction.
Adding a stylesheet
There are three different ways to apply CSS to an HTML document that you'll commonly come across, some more useful than others.
Three Different Types of CSS Styles: The Complete Guide
- Why would you choose one over another?
- What do you think is the cleanest way?
Comments
Comments are parts of our CSS that will not affect the HTML. It is used to write messages for the programmer of the website. A comment looks like this:
/* This is a CSS comment */Content inside comments will not have any consequence on the styling.
Exercises
- Change the Color of Text
- Use CSS Selectors to Style Elements
- Headline with the h2 Element
- Inform with the Paragraph Element
- Fill in the Blank with Placeholder Text
- Uncomment HTML
- Comment out HTML
- Delete HTML Elements
02. CSS Selectors
In CSS, selectors are used to target the HTML elements we want to style on our web pages.
There is a wide variety of CSS selectors available, allowing for fine-grained precision when selecting elements to style.
Each selector can access the HTML items by different means; by id (#) by class (.), by HTML tag (<>) and there are even more specific selectors which you will read about in the next section.
Main Selectors
Main selectors include IDs, classes, attributes or HTML tags.
Other selectors
However, there's plenty of other selectors which will help you attack the element or elements that you want to style in particular circumstances. Some of these are called pseudo-class or pseudo selectors.
CSS4-Selectors | All selectors from level 4 to 1
Combining selectors
Selectors can be used individually or collectively. They can be very powerful when combined.
Multiple Class / ID and Class Selectors | CSS-Tricks
Speed Up by Grouping Multiple CSS Selectors
- Can you use several selectors at the same time?
- Why would you attempt to do this?
- Is there a possible way of selecting all
<p>on a website except the last one?
Exercises
- Use a CSS Class to Style an Element
- Style Multiple Elements with a CSS Class
- Change the Font Size of an Element
- Set the Font Family of an Element
- Import a Google Font
- Specify How Fonts Should Degrade
- Size Your Images
- Add Borders Around Your Elements
- Add Rounded Corners with border-radius
- Make Circular Images with a border-radius
- Give a Background Color to a div Element
- Use Attribute Selectors to Style Elements
Also, here is a game to help you assimilate all these concepts of selectors, multiple selectors, pseudo selectors. Have a try and let's see if you can get to the end!! ⬇️
Further Reading
Extra!
If you want to have a broader understanding of CSS and how it works, check this article out. However, this is not part of the mandatory materials of the course.
03. Box Model
Every HTML element is represented as a rectangular box, with the box's content, padding, border, and margin built up around one another like the layers of an onion. Before understanding how to create CSS layouts, you need to understand the box model.
CSS Box model
This representation of elements as boxes allows us to effectively distribute content within our webpage. There are different approaches to do so, but before we get to that part, make sure you understand how this works:
Introduction to the CSS basic box model
See an example of how box model works here
CSS Units
Some CSS attributes require a unit value. Some that you might come across already is the pixel value (px) or the percentage (%). There are many, we will review the most important.
Just like we measure meters in a room to find furniture that fits in it, we need to measure the elements on our webpage. CSS Units allow us to measure and compare different items.
Read about absolute and relative numeric values and percentages:
Box model example
As we saw at the beginning of this chapter, the actual width and height of a box will be
Width = Content-box + padding + border + margin
So if we do this:
.box { width: 200px; border: 1px solid green; padding: 1rem; margin: 2rem;}The actual rendered (as seen on screen) width would be
width = 200px + 1rem + 1px + 2rem
Hard to calculate, isn't it?
Box sizing
To avoid the confusion we've seen before, most developers change the default browser behavior.
box-sizing is a CSS property that sets how the total width and height of an element is calculated. When we set the value of box-sizing to border-box the final calculation will differ. The width and height properties will include the content, padding, and border, but do not include the margin.
This part can be easy to implement but hard to understand, so read the article below to make sure you acquire the knowledge before executing it.
Exercises
- Set the id of an Element
- Use an id Attribute to Style an Element
- Adjust the Padding of an Element
- Adjust the Margin of an Element
- Add Different Padding to Each Side of an Element
- Add Different Margins to Each Side of an Element
- Use Clockwise Notation to Specify the Padding of an Element
- Use Clockwise Notation to Specify the Margin of an Element
- Use Clockwise Notation to Specify the Margin of an Element
04. Cascade and inheritance
Cascade and inheritance are very important in order to determine which selector will have more power over the rest.
Sometimes, different styles are applied to the same element. In order to decide which style should prevail, cascade and inheritance will have their say. Read these articles to understand this conflicting rules.
Exercises
- Inherit styles from the body element
- Prioritize one style over another
- Override styles in subsequent CSS
- Override class declarations by styling ID attributes
- Override class declarations with inline styles
- Override all other styles by using importants
- What happens when different opposite styles (such as color: red, color: green) are applied to the same element?
- Do you think how cascade and inheritance can affect a project with 200 stylesheets?
Further reading
If you want to learn advanced topics on cascade and inheritance, check this article
How CSS works: Understanding the cascade - LogRocket Blog
05. CSS Layout
CSS page layout techniques allow us to take elements contained in a web page and control where they are positioned relative to their default position in normal layout flow.
As aforementioned, distributing content in our webpage flow can be dealt by several means. Have a look at the main options.
Default layout flow: inline vs block
Normal flow is how the browser lays out HTML pages by default when you do nothing to control page layout.
Inline vs Inline-block vs Block
Positioning
Positioning allows you to take elements out of the normal document layout flow, and make them behave differently; for example sitting on top of one another, or always remaining in the same place inside the browser viewport.
Flexbox
The main goal of flexbox is to craft one-dimensional layout structures (vertical or horizontal). Items in flex layouts will shrink or grow to fill the space available and will position relative to the parent container.
A Complete Guide to Flexbox | CSS-Tricks
- What do you find it easier? Positioning left and right or using flexbox?
- Can you think of a clear example of webpage where flexbox can be used?
Exercises
- Change an element's relative position
- Move a relatively positioned element with CSS offsets
- Lock an element to its parent with absolute positioning
- Lock an element to the browser window with fixed positioning
- Center an element horizontally using the margin property
- Use
display: flexto position two boxes - Add flex superpowers to the tweet embed
- Apply the
flex-directionproperty to create rows in the tweet embed - Apply the
flex-directionproperty to create a column in the tweet embed - Use the
justify-contentproperty in the tweet embed - Use the
align-itemsproperty in the tweet embed - Use the
align-selfproperty - Get to the end of this game to master Flexbox! ⬇️
Further reading
A Complete Guide to Flexbox | CSS-Tricks
06. Responsive
It is fairly common to create a Website or app that changes its user interface depending on the browser or device accessing the site to provide an optimized experience.
Developers usually create a single version of the website which doesn't care about what browser or platform is accessing the site, but instead uses feature tests to find out what code features the browser supports or what the values of certain browser features are, and then adjusts the code appropriately.
Basics
Please read these articles to know the basic concepts of responsive designs.
The building blocks of responsive design
Using Developer Tools Responsive Design Mode
Responsive Design Mode is a tool from your browser developer tools that gives you a simple way to simulate a range of layouts of different devices — particularly mobile phones and tablets as well as desktops and laptops.
Read this article to understand how it works
Media queries
Media queries lets you adapt your site or app depending on the presence or value of various device characteristics and parameters.
See an example of its syntax:
.box { width: 75%;}
@media all and (max-width: 1024px) { .box { width: 50%; }}In order for you to master media queries, read the following article. Be ready to start typing!
Exercises
- Understand absolute versus relative units
- Create a media query
- Make an image responsive
- Use a retina image for higher resolution displays
- Make typography responsive
- What platforms do you use to access the internet?
- Do you know phone applications which also have a webpage? Are they the same or different? Do they look the same? Do they have similar functionalities?
07. Custom Properties
Custom properties (sometimes referred to as CSS variables) are variables defined by CSS authors that contain specific values to be reused throughout a document (similar to JS variables). They are set using custom property notation (e.g., --main-color: black;) and are accessed using the var() function (e.g., color: var(--main-color);).
Complex websites have very large amounts of CSS, often with a lot of repeated values. For example, the same color might be used in hundreds of different places, requiring global search and replace if that color needs to change. Custom properties allow a value to be stored in one place, then referenced in multiple other places.
Using CSS custom properties (variables)
.parent-element { --main-color: #000;}
.child-element { background: var(--main-color);}
/* Custom properties using fallback value */.sibling-element[attr] { background: var(--main-color, #fff);}Exercises
- Use CSS Variables to change several elements at once
- Create a custom CSS Variable
- Use a custom CSS Variable
- Attach a Fallback value to a CSS Variable
- Inherit CSS Variables
Well done! You've just arrived to the end part of the CSS syllabus. Congratulations!
Before continuing, make sure you can tick all the goals (except the project one, which comes next), and you don't have any doubts. Otherwise, ask the teachers! 🤓