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
- Inline Styles: Written within HTML tags
- Embedded Styles: Added within
<style>tags in the<head> - External Styles: Stored in separate
.cssfiles, linked to HTML - 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 (
#RRGGBBformat) - Examples:
- Black:
#000000, White:#FFFFFF, Red:#FF0000
- Black:
- 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
.cssfiles; linked in HTML with<link>
CSS Selectors and Properties
CSS Selectors
- Element Selector: Targets HTML tags
p { color: blue; } - Class Selector: Targets elements with a class
.new { color: red; font-style: italic; } - ID Selector: Targets a single element with a unique ID
#main { background-color: gray; } - 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;(useemor%for accessibility) - Font Family:
font-family: Arial, sans-serif;
Centering Content with CSS
- Center content with
text-alignormargin: autofor block elements.
Configuring Borders and Padding
- Borders: Borders can be applied to all sides or specific sides of elements using
borderproperties, e.g.,border: 2px solid #ff0000; - Padding:
paddingconfigures space between an element’s content and its border, either uniformly (e.g.,padding: 5px;) or per side with properties likepadding-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 assrc,alt(for accessibility),height, andwidth. - Ensure that the
altattribute provides meaningful descriptions for screen readers; usealt=""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, andbackground-color. - Multiple Backgrounds: CSS3 allows layering images, with positioning controls like
no-repeatfor specific placements.
CSS3 Visual Enhancements
- Rounded Corners:
border-radiusrounds element corners, with one or four values to define the radius of each corner. - Shadows:
box-shadowapplies shadows around elements with configurable offsets and blur radius. - Opacity and RGBA Colors: The
opacityproperty (0 to 1) adjusts transparency;rgbavalues add transparency to color definitions. - Gradients: Use
linear-gradientandradial-gradientfor smooth color transitions in backgrounds.
Accessibility and Best Practices
Accessibility for Visual Elements
- Always provide text equivalents (
altattributes) 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
altattributes.
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.
