Css Codecademy
Css Codecademy
CSS classes can be reusable and applied to many /* Selects all elements with
elements. Class selectors are denoted with a period .
class="column" */
followed by the class name. CSS ID selectors should be
unique and used to style only a single element. ID .column {
selectors are denoted with a hash sign # followed by the }
id name.
Selector Chaining
CSS selectors can be chained so that rule sets apply only /* Select h3 elements with the section-
to elements that match all criteria. For instance, to select
heading class */
<h3> elements that also have the section-
heading class, the selector h3.section- h3.section-heading {
heading can be used. color: blue;
}
CSS type selectors are used to match all elements of a /* Selects all <p> tags */
given type or tag name. Unlike for HTML syntax, we do not
p {
include the angle brackets when using type selectors for
tag names. When using type selectors, elements are }
matched regardless of their nesting level in the HTML.
Some HTML attributes can have multiple attribute values. <div class="value1 value2 value3"></div>
Multiple attribute values are separated by a space
between each attribute.
Selector Specificity
CSS ID selectors
The <link> element is used to link HTML documents <!-- How to link an external stylesheet
to external resources like CSS files. It commonly uses:
with href, rel, and type attributes -->
href attribute to specify the URL to the
external resource
rel attribute to specify the relationship of the <link
linked document to the current document
href="./path/to/stylesheet/style.css"
type attribute to define the type of content
being linked rel="stylesheet" type="text/css">
Separating HTML code from CSS code
Print Share
Cheatsheets / Learn CSS
Visual Rules
CSS declarations
/* CSS declarations */
text-align: center;
color: purple;
width: 100px;
Font Size
Background Color
Opacity
Font Weight
Text Align
span {
color : green ;
}
Resource URLs
Background Image
li {
color: khaki;
}
Print Share
Cheatsheets / Learn CSS
The CSS box model is a box that wraps around an HTML .container {
element and controls the design and layout. The property
box-sizing: border-box;
box-sizing controls which aspect of the box is
determined by the height and width properties. }
The default value of this property is content-box ,
which renders the actual size of the element including the
content box; but not the paddings and borders. The value
border-box , on the other hand, renders the actual
size of an element including the content box, paddings,
and borders.
CSS margin collapse occurs when the top and bottom /* The vertical margins will collapse to
margins of blocks are combined into a single margin equal
30 pixels
to the largest individual block margin.
Margin collapse only occurs with vertical margins, not for instead of adding to 50 pixels. */
horizontal margins. .block-one {
margin: 20px;
}
.block-two {
margin: 30px;
}
The CSS min-width and min-height /* Any element with class "column" will be
properties can be used to set a minimum width and
at most 200 pixels wide, despite the width
minimum height of an element’s box. CSS max-width
and max-height properties can be used to set property value of 500 pixels. */
maximum widths and heights for element boxes.
.column {
max-width: 200px;
width: 500px;
}
Print Share
Cheatsheets / Learn CSS
The CSS z-index property specifies how far back or //`element1` will overlap `element2`
how far forward an element will appear on a web page
.element1 {
when it overlaps other elements.
The z-index property uses integer values, which can position: absolute;
be positive or negative values. The element with the z-index: 1;
highest z-index value will be at the foreground, while
}
the element with the lowest z-index value will be at
the back.
.element2 {
position: absolute;
z-index: -1;
}
The CSS float property determines how far left or /* The content will float to the left side
how far right an element should float within its parent
of the container. */
element. The value left floats an element to the left
side of its container and the value right floats an .left {
element to the right side of its container. For the float: left;
property float , the width of the container must }
be specified or the element will assume the full width of
its containing element.
/* The content will float to the right
side of the container. */
.right {
float: right;
}
The CSS clear property
The CSS clear property specifies how an element /*This determines that no other elements
should behave when it bumps into another element
within the same containing element are
within the same containing element.The clear is
usually used in combination with elements having the CSS allowed to float on the left side of this
float property. This determines on which sides element.*/
floating elements are allowed to float.
.element {
clear: left;
}
Print Share
Cheatsheets / Learn CSS
Colors
.transparent {
color: transparent;
}
CSS colors can be declared with the HSL color system .light-blue {
using hsl() syntax. This syntax contains three values:
background-color: hsl(200, 70%, 50%);
hue (the color value itself), saturation (intensity), and
lightness. }
Hue values range from 0 to 360 while saturation and
lightness values are represented as percentages.
.green {
color: rgb(0, 255, 0);
}
li {
color: khaki;
}
Print Share
Cheatsheets / Learn CSS
Typography
The CSS font-weight property declares how thick /* Sets the text as bolder. */
or thin should be the characters of a text. Numerical
p {
values can be used with this property to set the thickness
of the text. The numeric scale range of this property is font-weight: 700;
from 100 to 900 and accepts only multiples of 100. The }
default value is normal while the default numerical
value is 400 . Any value less than 400 will have text
appear lighter than the default while any numerical value
greater than the 400 will appear bolder.
In the given example, all the <p> elements will appear
in a bolder font.
The CSS @font-face rule allows external fonts or font files @font-face {
to be imported directly into stylesheets.The location of
font-family: 'Glegoo';
the font file must be specified in the CSS rule so that the
files can be loaded from that location. This rule also src: url('../fonts/Glegoo-Regular.ttf')
allows locally hosted fonts to be added using a relative file format('truetype');
path instead of a web URL.
}
CSS Fallback Fonts
The CSS font-family property can have multiple /* Here `Arial` is the fallback font for
fonts declared in order of preference. In this case the
<p> tags */
fonts following the initial font are known as the fallback
fonts. p {
If the initial value of the property font-family fails font-family: "Helvetica", "Arial";
to load to the webpage, the fallback fonts will be used.
}
Linking fonts allow user to use web fonts in the document. <head>
They can be imported in an HTML document by using the
<link
<link> tag. Once the web font URL is placed within
the href attribute, the imported font can then be href="https://fonts.googleapis.com/css?
used in CSS declaration. family=Droid%20Serif" rel="stylesheet">
</head>
Print Share
Cheatsheets / Learn CSS: Responsive Design
When writing media queries in CSS we use the only @media only screen and (min-width: 600px)
keyword to specify the type of device. Initially, CSS
{
incorporated a variety of media types such as screen ,
print and handheld . In order to ensure /* ruleset for >= 600px */
responsive design and to accommodate a variety of }
screen sizes the keyword screen is now always used
for displaying content.
Through using the and operator when writing media @media only screen and (max-width: 480px)
queries in CSS, we can specify that multiple media
and (min-resolution: 300dpi) {
features are required. For example we could require a
certain width as well as a specific resolution for a CSS /* CSS ruleset */
ruleset to apply. The and operator when chaining }
together multiple media features allows us to be more
specific with the screens that we are targeting as we build
responsive designs.
css media query
A CSS media query can be used to adapt a website’s /* For screen sizes less than or equal to
display and layout to different screen sizes. A media query
480px (most likely a mobile device), the
begins with the @media keyword and is followed by
one or more conditions that check screen size, body element's font size will be set to
orientation, resolution, and/or other properties. If these 12px and the #photo element's width will
conditions are met, all CSS rules within the media query
be set to 100% */
will be applied to the page. Media queries are used for
responsive web design by tailoring specific stylesheets to @media only screen and (max-width: 480px)
different types of devices such as laptops, tablets, {
mobiles, and more.
body {
font-size: 12px;
}
#photo {
width: 100%;
}
}
Media queries can use CSS to target screen sizes within a @media only screen and (min-width: 480px)
certain range through using multiple widths and/or
and (max-width: 600px) {
heights. This is an effective tool for responsive design that
will address a variety of screen sizes in one CSS media /* ruleset for 480px - 600px */
query. In order to set a range for width or the height, set }
the minimum screen size through using min-width
and/or min-height and then set the maximum
through using max-width or max-height .
These properties are used in combination with the and
operator.
CSS unit em
.news-row .news-column {
height: 80%; /* 240px */
width: 50%; /* 250px */
}
background-size: cover;
The CSS unit rem can be used to set the font size of html {
HTML elements relative to the font size of the root
font-size: 18px;
element. 1 rem represents the size of the base font
within the root element - the <html> tag. }
In the example code block, the font size for all the
<span> elements will be twice the size of the font size span {
declared for the root element.
font-size: 2rem;
}
Print Share
Cheatsheets / Intermediate CSS: CSS Grid
fr Relative Unit
.grid {
display: grid;
width: 100px;
grid-template-columns: 1fr 60px 1fr;
}
Grid Gap
The CSS grid-gap property is a shorthand way of // The distance between rows is 20px
setting the two properties grid-row-gap and
// The distance between columns is 10px
grid-column-gap . It is used to determine the size
of the gap between each row and each column. The first
value sets the size of the gap between rows and while the #grid-container {
second value sets the size of the gap between columns.
display: grid;
grid-gap: 20px 10px;
}
CSS grid-row
The CSS Grid minmax() function accepts two /* In this example, the second column will
parameters:
vary in size between 100px and 500px
The first parameter is the minimum size of a row
or column. depending on the size of the web browser"
The second parameter is the maximum size. */
The grid must have a variable width for the minmax()
function.
If the maximum value is less than the minimum, then the .grid {
maximum value is ignored and only the minimum value is display: grid;
used.
grid-template-columns: 100px
The function can be used in the values of the grid-
template-rows , grid-template-columns minmax(100px, 500px) 100px;
and grid-template properties. }
CSS grid-row-gap
Justify Items
Align Self
The CSS grid-template-areas property allows /* Specify two rows, where "item" spans
the naming of sections of a webpage to use as values in
the first two columns in the first two
the grid-row-start , grid-row-end ,
grid-column-start , grid-column-end , rows (in a four column grid layout)*/
and grid-area properties. They specify named grid .item {
areas within a CSS grid. grid-area: nav;
}
.grid-container {
display: grid;
grid-template-areas:
'nav nav . .'
'nav nav . .';
}
CSS grid-auto-flow
Some times the total size of the grid items can be smaller
than the grid container. If this is the case, the CSS
property align-content can be used to position
the entire grid along the column axis of the grid
container.
The property is declared on the grid container.
The value start aligns the grid to the top of the grid
container.
The value end aligns the grid to the bottom of the grid
container.
The value center centers the grid vertically in the
grid container.
The value stretch stretches the grid items to
increase the size of the grid to expand vertically across
the container.
The value space-around includes an equal amount
of space on each side of a grid element, resulting in
double the amount of space between elements as there
is before the first and after the last element.
The value space-between includes an equal
amount of space between grid items and no space at
either end.
The value space-evenly places an even amount of
space between grid items and at either end.
CSS grid-auto-rows
The CSS justify-self property is used to set how // The grid items are positioned to the
an individual grid item positions itself along the row or
right (end) of the row.
inline axis. By default grid items inherit the value of the
justify-items property on the container. So if
the justify-self value is set, it would over-ride #grid-container {
the inherited justify-items value.
display: grid;
The value start positions grid items on the left side
justify-items: start;
of the grid area.
The value end positions the grid items on the right side }
of the grid area.
The value center positions grid items on the center
.grid-items {
of the grid area.
The value stretch positions grid items to fill the grid justify-self: end;
area (default). }
CSS grid-area
Align Items
Print Share
0
Cheatsheets / Learn Intermediate CSS
CSS Flexbox
justify-content Property
The CSS justify-content flexbox property /* Items based at the center of the parent
defines how the browser distributes space between and
container: */
around content items along the main-axis of their
container. This is when the content items do not use all div {
available space on the major-axis (horizontally). display: flex;
justify-content can have the values of:
justify-content: center;
flex-start
flex-end }
center
space-between /* Items based at the upper-left side of
space-around
the parent container: */
div {
display: flex;
justify-content: flex-start;
}
flex Property
The flex CSS property specifies how a flex item will /* Three properties declared on three
grow or shrink so as to fit within the space available in its
lines: */
flex container. This is a shorthand property that
declares the following properties in order on a single line: .first-flex-item {
flex-grow flex-grow: 2;
flex-shrink flex-shrink: 1;
flex-basis
flex-basis: 150px;
}
flex-direction Property
align-content Property
flex-shrink Property
.item-b {
flex-shrink: 2;
/* The value 2 indicates that the item
should shrink twice than the element item-
a. */
}
Css flex-basis property
The flex-basis CSS property sets the initial base // Default Syntax
size for a flex item before any other space is distributed
flex-basis: auto;
according to other flex properties.
The CSS property flex-flow provides a shorthand // In this example code block, "column" is
for the properties flex-direction and flex-
the value of the property "flex-direction"
wrap . The value of the flex-direction
property specifies the direction of the flex items and the and "wrap" is the value of the property
value of the flex-wrap property allows flex items to "flex-wrap".
move to the next line instead of shrinking to fit inside the
flex container. The flex-flow property should be
.container {
declared on the flex container.
display: flex;
flex-flow: column wrap;
}
Print Share
Cheatsheets / Learn CSS-in-JS
Styled components are React components that have const ContentWrapper = styled.div`
styles attached to them.
width: 100vw;
height: 100vh;
display: grid;
`
function App() {
return (
<ContentWrapper>Content</ContentWrapper>
)
}
Composition
Compositions can be used to group styles together and const Button = styled.button`
be used in other style blocks.
color: black;
font-size: 1em;
margin: 1em;
padding: 0.25em 1em;
border: 2px solid black;
border-radius: 3px;
`;
In Emotion, themes are used to specify styles that are import { ThemeProvider } from
used throughout the whole application. Styles defined in
'@emotion/react'
the theme can be used as variables in other style blocks.
import styled from '@emotion/styled'
const theme = {
colors: {
primary: 'tomato'
}
}
render(
<ThemeProvider theme={theme}>
<AlertText>alert text</AlertText>
</ThemeProvider>
)
keyframes Helper
In Emotion, the keyframes helper can be used to import { jsx, css, keyframes } from
define animations and returns an object that can be used
'@emotion/react'
in other style blocks.
render(
<div
css={css`
animation: ${partyText} 1s ease
infinite;
`}
>
party text!
</div>
)
Print Share
Cheatsheets / Learn CSS: Box Model and Layout
The CSS box model is a box that wraps around an HTML .container {
element and controls the design and layout. The property
box-sizing: border-box;
box-sizing controls which aspect of the box is
determined by the height and width properties. }
The default value of this property is content-box ,
which renders the actual size of the element including the
content box; but not the paddings and borders. The value
border-box , on the other hand, renders the actual
size of an element including the content box, paddings,
and borders.
CSS margin collapse occurs when the top and bottom /* The vertical margins will collapse to
margins of blocks are combined into a single margin equal
30 pixels
to the largest individual block margin.
Margin collapse only occurs with vertical margins, not for instead of adding to 50 pixels. */
horizontal margins. .block-one {
margin: 20px;
}
.block-two {
margin: 30px;
}
The CSS min-width and min-height /* Any element with class "column" will be
properties can be used to set a minimum width and
at most 200 pixels wide, despite the width
minimum height of an element’s box. CSS max-width
and max-height properties can be used to set property value of 500 pixels. */
maximum widths and heights for element boxes.
.column {
max-width: 200px;
width: 500px;
}
Print Share
Cheatsheets / Learn CSS: Box Model and Layout
The CSS z-index property specifies how far back or //`element1` will overlap `element2`
how far forward an element will appear on a web page
.element1 {
when it overlaps other elements.
The z-index property uses integer values, which can position: absolute;
be positive or negative values. The element with the z-index: 1;
highest z-index value will be at the foreground, while
}
the element with the lowest z-index value will be at
the back.
.element2 {
position: absolute;
z-index: -1;
}
The CSS float property determines how far left or /* The content will float to the left side
how far right an element should float within its parent
of the container. */
element. The value left floats an element to the left
side of its container and the value right floats an .left {
element to the right side of its container. For the float: left;
property float , the width of the container must }
be specified or the element will assume the full width of
its containing element.
/* The content will float to the right
side of the container. */
.right {
float: right;
}
The CSS clear property
The CSS clear property specifies how an element /*This determines that no other elements
should behave when it bumps into another element
within the same containing element are
within the same containing element.The clear is
usually used in combination with elements having the CSS allowed to float on the left side of this
float property. This determines on which sides element.*/
floating elements are allowed to float.
.element {
clear: left;
}
Print Share
Cheatsheets / Learn CSS: Transitions and Animations
CSS Transitions
css-transitions-animatable-properties
css-transitions-four-components
transition-duration-default-value
css-transition-delay
transition-properties-comma
transition-property-all
css-transition-shorthand-syntax
Signifiers
Link States
Anchor Text
Tooltips
Skeuomorphism
Flat Design
Print Share