Mastering CSS: Stylesheets, Syntax, and Visual Enhancements

CSS Essentials

Why Use CSS?

  • Greater control over typography and layout
  • Separates style from structure
  • Allows styles to be stored externally, making documents smaller and easier to maintain

Types of CSS

  1. Inline Styles: Written within HTML tags
  2. Embedded Styles: Added within <style> tags in the <head>
  3. External Styles: Stored in separate .css files, linked to HTML
  4. Imported Styles: Styles from one CSS file imported into another

CSS Syntax

  • Rule Structure: Each rule has a selector and a declaration
  • Example: body { color: blue; background-color: yellow; }

Common CSS Properties

  • Background Color: background-color
  • Font: font-family, font-size, font-weight, font-style
  • Text: text-align, text-decoration, line-height, text-indent
  • Spacing: margin, padding
  • Width: width

Styling Techniques

Color in CSS

  • RGB colors range from 0 to 255 or hexadecimal (#RRGGBB format)
  • Examples:
    • Black: #000000, White: #FFFFFF, Red: #FF0000
  • Tools for color and contrast:

Using Inline CSS

<h1 style="color:#FF0000; background-color:#CCCCCC">Red Heading</h1>

Embedded Styles

  • Placed in <style> within the <head>

External Stylesheets

  • Saved as .css files; linked in HTML with <link>

CSS Selectors and Properties

CSS Selectors

  1. Element Selector: Targets HTML tags p { color: blue; }
  2. Class Selector: Targets elements with a class .new { color: red; font-style: italic; }
  3. ID Selector: Targets a single element with a unique ID #main { background-color: gray; }
  4. Descendant Selector: Targets elements within a parent element #content p { color: green; }

Text Properties

  • Font Weight: font-weight: bold;
  • Font Style: font-style: italic;
  • Font Size: font-size: 16px; (use em or % for accessibility)
  • Font Family: font-family: Arial, sans-serif;

Centering Content with CSS

  • Center content with text-align or margin: auto for block elements.


Configuring Borders and Padding

  • Borders: Borders can be applied to all sides or specific sides of elements using border properties, e.g., border: 2px solid #ff0000;
  • Padding: padding configures space between an element’s content and its border, either uniformly (e.g., padding: 5px;) or per side with properties like padding-left.
  • Shorthand Notation: You can specify two (top/bottom and left/right) or four values (top, right, bottom, left) to streamline padding configurations.

Images and Visual Enhancements

Image Optimization

  • Common formats include GIF (supports animation and transparency), JPEG (suitable for photos, uses lossy compression), and PNG (supports transparency and lossless compression).
  • To optimize images for web display, use tools like GIMP, Adobe Photoshop, or online editors (e.g., Pixlr) to reduce file size and dimensions.

HTML Image Element

  • The <img> element displays images with attributes such as src, alt (for accessibility), height, and width.
  • Ensure that the alt attribute provides meaningful descriptions for screen readers; use alt="" for purely decorative images.

HTML5 Media Elements

  • Figure and Figcaption: <figure> and <figcaption> group images with captions, improving semantic structure.
  • Meter and Progress: <meter> and <progress> visually represent numerical data ranges or progress levels.

CSS Background Properties

  • Backgrounds can include images and colors with properties like background-image, background-repeat, and background-color.
  • Multiple Backgrounds: CSS3 allows layering images, with positioning controls like no-repeat for specific placements.

CSS3 Visual Enhancements

  • Rounded Corners: border-radius rounds element corners, with one or four values to define the radius of each corner.
  • Shadows: box-shadow applies shadows around elements with configurable offsets and blur radius.
  • Opacity and RGBA Colors: The opacity property (0 to 1) adjusts transparency; rgba values add transparency to color definitions.
  • Gradients: Use linear-gradient and radial-gradient for smooth color transitions in backgrounds.

Accessibility and Best Practices

Accessibility for Visual Elements

  • Always provide text equivalents (alt attributes) for images to aid visually impaired users.
  • Avoid using color alone to convey meaning; ensure adequate contrast between background and text for readability.

Checkpoints and Questions

  • Checkpoint 4.1: Discusses coding consistency across browsers, identifying errors in CSS, and confirming that CSS can shape visual elements.
  • Checkpoint 4.2: Includes sample CSS to display background images and addresses how layering images and colors affect rendering.
  • Checkpoint 4.3: Evaluates an example site with image-based navigation, checking for accessibility practices like contrast and alt attributes.

Summary

Visual elements enhance web pages but should be optimized for performance. Always consider accessibility, balance image quality with load time, and follow best practices for reusable, descriptive, and properly-sized graphics.