Co-authored-by: Muhammad R <muhammad.rizwan@tryhackme.com>
11 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 671a88d636cebc5fbd407b78 | Lists, Links, Background and Borders Review | 31 | review-css-backgrounds-and-borders |
--interactive--
Styling Lists
line-heightProperty: This property is used to create space between lines of text. The acceptedline-heightvalues include the keywordnormal, numbers, percentages and length units like theemunit.list-style-typeProperty: This property is used to specify the marker for a list item. Acceptable values can include a circle, disc, or decimal.
Here's an example using list-style-type to change the bullet style:
:::interactive_editor
<link rel="stylesheet" href="styles.css">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
ul {
list-style-type: square;
}
:::
list-style-positionProperty: This property is used to set the position for the list marker. The only two acceptable values are inside and outside.
Here's an example showing the difference between inside and outside:
:::interactive_editor
<link rel="stylesheet" href="styles.css">
<ul class="inside-list">
<li>Item with inside position</li>
<li>Item with inside position</li>
</ul>
<ul class="outside-list">
<li>Item with outside position</li>
<li>Item with outside position</li>
</ul>
.inside-list {
list-style-position: inside;
}
.outside-list {
list-style-position: outside;
}
:::
list-style-imageProperty: This property is used to use an image for the list item marker. A common use case is to use theurlfunction with a value set to a valid image location.
Spacing list items using margin
- Apart from
line-height, margins can also be used in CSS to enhance the spacing and readability of list items. - Margins create space outside each
lielement, allowing control over the gap between list items. margin-bottomis used to create space below each list item. For example,margin-bottom: 10px;will create a 10-pixel gap below each list item.
Here's an example using margin-bottom to space list items:
:::interactive_editor
<link rel="stylesheet" href="styles.css">
<ul class="list">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
.list li {
margin-bottom: 20px;
}
:::
Styling Links
pseudo-class: This is a keyword added to a selector that allows you to select elements based on a particular state. Common states would include the:hover,:visitedand:focusstates.:link pseudo-class: This pseudo-class is used to style links that have not been visited by the user.
Here's an example using the :link pseudo-class:
:::interactive_editor
<link rel="stylesheet" href="styles.css">
<a href="/">Example link</a>
a:link {
color: red;
}
:::
:visited pseudo-class: This pseudo-class is used to style links where a user has already visited.:hover pseudo-class: This pseudo-class is used to style elements where a user is actively hovering over them.
Here's an example using the :hover pseudo-class:
:::interactive_editor
<link rel="stylesheet" href="styles.css">
<a href="/">Hover over me!</a>
a:hover {
color: green;
text-decoration: underline;
}
:::
:focus pseudo-class: This pseudo-class is used to style an element when it receives focus. Examples would includeinputorselectelements where the clicks or tabs on the element to focus it.
Here's an example using the :focus pseudo-class:
:::interactive_editor
<link rel="stylesheet" href="styles.css">
<a href="/">Example link</a>
a:focus {
outline: 2px solid orange;
}
:::
:active pseudo-class: This pseudo-class is used to style an element that was activated by the user. A common example would be when the user clicks on a button.
Here's an example using the :active pseudo-class:
:::interactive_editor
<link rel="stylesheet" href="styles.css">
<a href="/">Click me!</a>
a:active {
color: pink;
}
:::
Working with Backgrounds and Borders
background-sizeProperty: This property is used to set the background size for an element. Some common values includecoverfor the background image to cover the entire element andcontainfor the background image to fit within the element.
Here's an example using background-size: contain:
:::interactive_editor
<style>
body {
background-image: url("https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg");
background-size: cover;
background-repeat: no-repeat;
min-height: 100px;
}
</style>
:::
background-repeatProperty: This property is used to determine how background images should be repeated along the horizontal and vertical axes. The default value forbackground-repeatisrepeatmeaning the image will repeat both horizontally and vertically. You can also specify that there should be no repeat by using theno-repeatproperty.
Here's an example using background-repeat: no-repeat:
:::interactive_editor
<style>
body {
background-image: url("https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg");
background-size: contain;
background-repeat: no-repeat;
min-height: 100px;
}
</style>
:::
background-positionProperty: This property is used to specify the position of the background image. It can be set to a specific length, percentage, or keyword values liketop,bottom,left,right, andcenter.
Here's an example using background-position:
:::interactive_editor
<style>
body {
background-image: url("https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg");
background-size: contain;
background-repeat: no-repeat;
background-position: center top;
min-height: 100px;
}
</style>
:::
background-attachmentProperty: This property is used to specify whether the background image should scroll with the content or remain fixed in place. The main values arescroll(default), where the background image scrolls with the content, andfixed, where the background image stays in the same position on the screen.background-imageProperty: This property is used to set the background image of an element. You can set multiple background images at the same time and use either theurl,radial-gradientorlinear-gradientfunctions as values.
Here's an example using background-image:
:::interactive_editor
<style>
body {
background-image: url("https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg");
min-height: 100px;
}
</style>
:::
backgroundProperty: This is the shorthand property for setting all background properties in one declaration. Here is an example of setting the background image and setting it to not repeat:background: no-repeat url("example-url-goes-here");
Here's an example using the background shorthand property:
:::interactive_editor
<style>
body {
background: center top no-repeat url("https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg");
min-height: 100px;
}
</style>
:::
- Good Contrast for Background and Foreground Colors: It is important to ensure that the background and foreground colors have good contrast to make the text readable. The Web Content Accessibility Guidelines (WCAG) recommend a minimum contrast ratio of 4.5:1 for normal text and 3:1 for large text.
Borders
border-topProperty: This property is used to set the styles for the top border of an element.border-top: 3px solid blue;sets a 3-pixel-wide solid blue border on the top side of the element.border-rightProperty: This property is used to set the styles for the right border of an element.border-right: 2px solid red;sets a 2-pixel-wide solid red border on the right side of the element.border-bottomProperty: This property is used to set the styles for the bottom border of an element.border-bottom: 1px dashed green;sets a 1-pixel-wide dashed green border on the bottom side of the element.border-leftProperty: This property is used to set the styles for the left border of an element.border-left: 4px dotted orange;sets a 4-pixel-wide dotted orange border on the left side of the element.
Here's an example using individual border properties:
:::interactive_editor
<link rel="stylesheet" href="styles.css">
<div class="bordered-box">Box with different borders</div>
.bordered-box {
border-top: 3px solid blue;
border-right: 2px solid red;
border-bottom: 1px dashed green;
border-left: 4px dotted orange;
padding: 20px;
}
:::
borderProperty: This is the shorthand property for setting the width, style, and color of an element's border.border: 1px solid black;sets a 1-pixel-wide solid black border.
Here's an example using the border shorthand property:
:::interactive_editor
<link rel="stylesheet" href="styles.css">
<img src="https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg" alt="A cute cat lying on its back.">
img {
border: 2px solid red;
}
:::
border-radiusProperty: This property is used to create rounded corners for an element's border.
Here's an example using border-radius:
:::interactive_editor
<link rel="stylesheet" href="styles.css">
<img src="https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg" alt="A cute cat lying on its back.">
img {
border: 2px solid black;
border-radius: 10px;
}
:::
border-styleProperty: This property is used to set the style of an element's border. Some accepted values includesolid,dashed,dotted, anddouble.
Here's an example using different border-style values:
:::interactive_editor
<link rel="stylesheet" href="styles.css">
<div class="solid-border">Solid border</div>
<div class="dashed-border">Dashed border</div>
<div class="dotted-border">Dotted border</div>
.solid-border {
border: 3px solid blue;
margin-bottom: 10px;
padding: 10px;
}
.dashed-border {
border: 3px dashed red;
margin-bottom: 10px;
padding: 10px;
}
.dotted-border {
border: 3px dotted green;
padding: 10px;
}
:::
Gradients
linear-gradient()Function: This CSS function is used to create a transition between multiple colors along a straight line.
Here's an example using linear-gradient():
:::interactive_editor
<link rel="stylesheet" href="styles.css">
<div class="linear-gradient"></div>
.linear-gradient {
background: linear-gradient(to right, red, blue);
height: 40vh;
}
:::
radial-gradient()Function: This CSS function creates an image that radiates from a particular point, like a circle or an ellipse, and gradually transitions between multiple colors.
Here's an example using radial-gradient():
:::interactive_editor
<link rel="stylesheet" href="styles.css">
<div class="radial-gradient"></div>
.radial-gradient {
background: radial-gradient(circle, red, blue);
height: 40vh;
}
:::
--assignment--
Review the CSS Backgrounds and Borders topics and concepts.