CSS - Basic CSS Part 4
Instructors: Jess, Carmen, and Eda
Basic CSS
Before this session, please:
- Read Working with Backgrounds and Borders: How Do Background Image Size, Repeat, Position, and Attachment Work? and take notes
- Read Working with Backgrounds and Borders: What Is a Background Gradient, and How Does It Work? and take notes
- Read Working with Backgrounds and Borders: What Are Some Accessibility Considerations for Backgrounds? and take notes
- Read Working with Backgrounds and Borders: What Are the Different Ways You Can Add Borders Around Images? and take notes
- Complete Design a Blog Post Card solo over the next few days. There’s a lot of space to be creative with this lab.
- Read Lists, Links, CSS Background and Borders Review and take notes
- Complete CSS Backgrounds and Borders Quiz solo to test your understanding of CSS backgrounds and borders
During this session, we’ll:
- Review notes
- Review notes
- Review notes
- Review notes
- We’ll talk through Design a Blog Post Card to help prepare you to build your own creative approach on your own over the next few days
- Review notes
After this session, please:
- Read User Interface Design Fundamentals: What Are Common Design Terms to Help You Communicate with Designers? and take notes
- Read User Interface Design Fundamentals: How Do You Create Good Background and Foreground Contrast in Your Designs? and take notes
- Read User Interface Design Fundamentals: What Is the Importance of Good Visual Hierarchy in Design? and take notes
- Read User Interface Design Fundamentals: How Does Scale Work in Design? and take notes
- Read User Interface Design Fundamentals: How Does Alignment Work in Design? and take notes
- Read User Interface Design Fundamentals: What Is the Importance of Whitespace in Design? and take notes
- Read User Interface Design Fundamentals: What Are Best Practices for Working with Images in Your Designs? and take notes
- Read User Interface Design Fundamentals: What Is Progressive Enhancement? and take notes
Instructor notes
Working with Backgrounds and Borders: How Do Background Image Size, Repeat, Position, and Attachment Work?
The background-image property sets a background for the specified element.
1div {
2 background-image: url("https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg");
3}We can adjust the size of the background image with the background-size property. The possible values are:
contain: scales the image as large as possible within its container without cropping or stretching the image.cover: scales the image (while preserving its ratio) to the smallest possible size to fill the container, leaving no empty space.auto: scales the background image in the corresponding direction such that its intrinsic proportions are maintained.<length>(specified length): stretches the image in the corresponding dimension to the specified length.<percentage>(specified percentage): Stretches the image in the corresponding dimension to the specified percentage of the background positioning area.
We can also control how the image is repeated, using the background-repeat property. The possible values are:
repeat: The default value. The background image is repeated both vertically and horizontally. The last image will be clipped if it does not fit.no-repeat: The image is not repeated.space: The image is repeated as much as possible without clipping.round: As the allowed space increases in size, the repeated images will stretch (leaving no gaps) until there is room for another one to be added.repeat-x: The background image repeats only horizontally.repeat-y: The background image repeats only vertically.
The position of the background image can also be controlled by the background-position property. We can use keywords like top, bottom, left, right, and center, or pixel or percentage values.
To set the scroll behavior of the background image in relation to content, we can use the background-attachment property. The possible values are:
fixed: The background image will not scroll with the page.local: The background image will scroll with the element’s contents.scroll: The background image will scroll with the page. It’s the default value.
We can use the shorthand background property. For example, to set the background-image, background-position, and background-attachment all in one go, we can do this:
1body {
2 background: center top fixed
3 url("https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg");
4}Working with Backgrounds and Borders: What Is a Background Gradient, and How Does It Work?
A gradient is a progressive transition between two or more colors. In CSS, there are two types:
- linear gradients
- radial gradients
A linear gradient transitions colors along a straight line.
1background: linear-gradient(<direction>, <color-stop1>, <color-stop2>, ...);<direction> can be an angle (for example, 45deg), keyword (to right, to bottom, etc.) or a side/corner.
<color-stop1>, <color-stop2>, … define the colors where transitions occur.
A radial gradient transitions colors from an origin outward in a circular shape.
1background: radial-gradient(<shape> <size> at <position>, <color-stop1>, <color-stop2>, ...)<shape> can be either circle or ellipse.
<size> is the size of the gradient’s ending shape and can be one of closest-side, closest-corner, farthest-side or farthest-corner.
<position> is the position of the gradient’s center and can be defined using keywords (center, top left, bottom right, etc.) or values (such as 50% 50%, 10px 20px).
<color-stop1>, <color-stop2>, … again, define the colors where transitions occur.
Working with Backgrounds and Borders: What Are Some Accessibility Considerations for Backgrounds?
We should always keep accessibility in mind when working with backgrounds. Some things to consider are:
- Ensuring that the background and the text have enough contrast.
The recommended minimum contrast ratio by The Web Content Accessibility Guidelines (WCAG) is 4.5:1 for normal text and 3:1 for large text.
You can use the WAIM Contrast Checker to check for the contrast ratio between a background and a text color.
Avoiding the use of text on complex and busy backgrounds.
Avoiding depending on color alone to convey information.
For example, displaying an error message in red color only is not the best choice; we can also add an icon or use bold text to indicate the error.
- Avoiding distracting background audio and videos
Background audio and auto-playing videos can be distracting and should be given control elements to make sure users can control their behavior.
Working with Backgrounds and Borders: What Are the Different Ways You Can Add Borders Around Images?
We can add borders to elements using the border property. It’s a shorthand for the border-width, border-style, and border-color.
1img {
2 border: 2px dashed purple;
3}The outline property can also be used to define an outline for the element. It’s also a shorthand for outline-width, outline-style, outline-color.
1img {
2 outline: 3px solid gold;
3}If the border is already defined, we can also specify the border-radius, which can be used to create rounded corners of the border.
1img {
2 border: 2px solid black;
3 border-radius: 10px;
4}Design a Blog Post Card
We’ll take a look at this exercise and practice styling backgrounds and borders!
Lists, Links, CSS Background and Borders Review
Here are the topics we’ve explored in this section:
- Styling lists
- Spacing list items using margin
- Styling links
- Working with backgrounds
- Borders
- Gradients