Brief Summary
This comprehensive CSS course, taught by Dave Gray, is designed for beginners and builds upon HTML knowledge. It covers fundamental concepts like CSS syntax, selectors, and the box model, progressing to more advanced topics such as responsive design, animations, and CSS organization. The course emphasizes practical application through mini-projects, including styling a website for a taco shop, and encourages experimentation to solidify understanding.
- CSS is a stylesheet language used to describe the presentation of a document.
- There are three ways to apply CSS: external stylesheet, internal stylesheet, and inline CSS.
- CSS selectors target HTML elements for styling, with element, class, and ID selectors being the most common.
- The CSS box model consists of content, padding, border, and margin.
- Media queries enable responsive design by applying different styles based on device characteristics.
What is CSS
CSS, or Cascading Style Sheets, is a stylesheet language used to describe the presentation of a document, most commonly HTML. HTML provides the foundation and structure, while CSS handles the visual aspects like colors, fonts, and layout. To begin writing CSS, tools like the Chrome browser and Visual Studio Code are recommended. There are three ways to apply CSS to HTML: external stylesheets, internal stylesheets, and inline CSS. External stylesheets are preferred for maintaining a separation of concerns, keeping CSS code separate from HTML.
CSS Selectors
CSS selectors target HTML elements for styling. The three basic types are element selectors (e.g., body
, p
), class selectors (e.g., .gray
), and ID selectors (e.g., #second
). Element selectors apply styles to all elements of a specific type, while class selectors allow for reusable styles across multiple elements. ID selectors are unique and should be used sparingly in CSS. Selectors can be grouped using commas to apply the same styles to multiple elements. Nesting selectors allows targeting elements within other elements (e.g., p span
). The universal selector (*
) selects all elements on a page and is often used for CSS resets.
The Cascade, Inheritance and Specificity
CSS follows a cascading order, applying styles from top to bottom, with later rules overriding earlier ones. Specificity determines which styles take precedence, with classes being more specific than element selectors. Inheritance allows elements to inherit properties from their parent elements, particularly those related to font and typography. The !important
flag overrides all other specificity rules but should be used sparingly. Tools like specificity calculators can help understand why certain styles are being applied.
CSS Colors
CSS colors can be specified using color names (e.g., blue, papayawhip), RGB values (e.g., rgb(255, 0, 0)
for red), RGBA values (adding an alpha channel for transparency), and hexadecimal codes (e.g., #000000
for black). Visual Studio Code provides a color palette tool for selecting colors and viewing their corresponding codes. HSL (Hue, Saturation, Lightness) is another color model. Tools like coolers.co and webaim.org can check color contrast ratios for accessibility.
CSS Units
CSS units define the size of elements on a page. Absolute length units include pixels (px), which represent a fixed size on the screen. Relative length units, such as percentages (%), em, rem, and viewport units (vw, vh), are relative to other values or the viewport size. Pixels are suitable for borders, while relative units like rem are preferred for font sizes to maintain accessibility. Viewport units (vw, vh) are relative to the viewport's width and height.
CSS Box Model
The CSS box model describes the structure of HTML elements as boxes, comprising content, padding, border, and margin. The content is the actual text or image, padding adds space around the content, the border is a visible outline, and the margin provides space outside the border. CSS resets, such as setting margin: 0
and padding: 0
, help remove default browser styles. The box-sizing
property, set to border-box
, includes padding and border in an element's total width and height.
CSS Typography
CSS typography involves arranging and presenting text using various properties. Font size is set using font-size
, and the font-family
property specifies the font. Text properties include text-decoration
(underline, overline, line-through), text-transform
(uppercase, lowercase, capitalize), text-align
(left, right, center, justify), and text-indent
. Non-font properties affecting typography include line-height
, letter-spacing
, and word-spacing
. External fonts can be imported from Google Fonts using the <link>
element in HTML or the @import
rule in CSS.
Styling Links with CSS
Styling hypertext links involves modifying their default appearance using CSS. The text-decoration
property controls the underline, while the color
property sets the text color. Pseudo-classes like :visited
, :hover
, :focus
, and :active
define styles for different link states. The :focus-visible
pseudo-class ensures that focus styles are only applied when the element is focused via keyboard input. The order of pseudo-class declarations is important due to CSS specificity rules.
Styling Lists with CSS
Styling lists with CSS involves modifying the appearance of ordered (<ol>
) and unordered (<ul>
) lists. The list-style-type
property changes the bullet or numbering style, while list-style-position
controls the position of the marker. The list-style-image
property sets a custom image for the marker. The ::marker
pseudo-element allows styling the marker itself, including its color, font, and content. The nth-child
pseudo-class targets specific list items for styling.
Mini Project: Profile Cards
This mini-project involves creating profile cards for a company's staff using CSS. The project utilizes concepts such as the box model, CSS resets, and responsive design. The goal is to create a visually appealing and accessible layout that adapts to different screen sizes. The project also demonstrates the use of pseudo-classes and CSS variables for styling and maintaining consistency.
CSS Display Types
CSS display types determine how elements are rendered on a page. Block-level elements (e.g., <p>
, <div>
) take up the full width available and stack on top of each other. Inline elements (e.g., <span>
, <a>
) only take up the space of their content and flow within the text. The display
property can be set to block
, inline
, or inline-block
. The inline-block
value allows elements to flow like inline elements but retain block-level characteristics like setting width and height.
CSS Floats
CSS floats allow elements to be positioned to the left or right, with surrounding content wrapping around them. The float
property can be set to left
or right
. To prevent content from wrapping, the clear
property can be used. To ensure that a container element contains floated elements, the display: flow-root
property should be applied to the container.
CSS Columns
CSS columns enable the creation of multi-column layouts. The column-count
property specifies the number of columns, while column-width
sets the minimum width of each column. The columns
shorthand property combines both. The column-rule
property adds a visual divider between columns, and column-gap
sets the spacing between them. The break-inside: avoid
property prevents elements from being split across columns. The column-span: all
property allows an element to span all columns.
CSS Position
The CSS position property controls how elements are positioned on a page. The static
value is the default, while relative
positions an element relative to its normal position. The absolute
value positions an element relative to its nearest positioned ancestor. The fixed
value positions an element relative to the viewport, and it remains in the same spot even when the page is scrolled. The sticky
value positions an element based on the user's scroll position.
CSS Transforms, Transitions and Animations
CSS transforms alter the appearance of elements, including their position, size, and orientation. Common transform functions include translateX
, translateY
, rotate
, scale
, and skew
. Transitions create smooth changes between different CSS property values. The transition
property specifies the property to transition, the duration, and the timing function. Animations define a sequence of CSS property changes over time using keyframes.
Organizing Your CSS
Organizing CSS is crucial for maintainability and collaboration. Suggestions include following team conventions, using comments to create headers, sorting properties alphabetically, and adopting a naming convention methodology like BEM (Block Element Modifier). BEM involves naming CSS classes based on the block (component), element (part of the component), and modifier (variation of the component).
Final Project
The final project involves overhauling a basic HTML website for a taco shop using modern CSS techniques. The project incorporates concepts such as responsive design, CSS variables, and animations. The goal is to create a visually appealing and user-friendly website that adapts to different screen sizes and devices. The project also demonstrates the use of CSS functions and pseudo-elements for advanced styling.