Cascading Style Sheets (CSS)

CSS is a programming language used to style web pages. It controls the appearance of HTML elements, such as fonts, colors, layout, and spacing. By applying CSS rules to HTML elements, you can transform a basic web page into a visually appealing and interactive experience. CSS allows you to create custom designs, make elements responsive to different screen sizes, and enhance the overall user experience of a website.

Select by Tag


A fundamental method for targeting elements based on their HTML tag name. This allows you to style all elements of a particular type within your document.

Key Points:

  • Specificity: Tag selectors have a relatively low specificity, meaning they are less likely to conflict with other selectors.
  • Universality: They apply to all elements of a given tag, regardless of their location in the document.
  • Efficiency: They can be efficient for styling large numbers of elements of the same type.
<!DOCTYPE html>
<html>
	<head>
		<title>Select by Tag Example</title>
		<script>
			p {
				color: #ff0000;
			}
		</script>
	</head>
	<body>
		<p>This is now red</p>
		<p>This is also red</p>
		<p>All p tags are now red</p>
	</body>
</html>

While tag selectors are useful for basic styling, they may not be the most specific or efficient choice for complex layouts or styling requirements. In such cases, consider using more targeted selectors like class or ID selectors.

Select by Class


A method for targeting elements based on a class attribute that they share. This allows you to style a group of elements, regardless of their tag name, as long as they have the same class.

Key Points:

  • Specificity: Class selectors have a higher specificity than tag selectors, making them more likely to override conflicting styles.
  • Flexibility: They can be used to style elements of different tag names, providing more flexibility in your styling.
  • Reusability: Classes can be applied to multiple elements, promoting code reusability and maintainability.
<!DOCTYPE html>
<html>
	<head>
		<title>Select by Class Example</title>
		<script>
			.text-red {
				color: #ff0000;
			}
		</script>
	</head>
	<body>
		<p class="text-red">This is now red</p>
		<p>This is the default color</p>
		<p class="text-red">This is also red.'</p>
	</body>
</html>

While class selectors are a powerful tool, it's important to use them judiciously to avoid excessive complexity and maintainability issues. Consider using a combination of tag and class selectors, along with ID selectors, to achieve the desired styling effects.

Select by ID


A method for targeting a specific element within your HTML document based on its unique ID attribute. IDs are intended to be used only once per document to identify a single element.

Key Points:

  • Specificity: ID selectors have the highest specificity of all CSS selectors, making them the most powerful way to target a specific element.
  • Uniqueness: IDs should be unique within a document to ensure that styles are applied correctly.
  • Accessibility: IDs are often used to link to specific sections of a page, making them important for accessibility purposes.
<!DOCTYPE html>
<html>
	<head>
		<title>Select by ID Example</title>
		<script>
			#main-header {
				background-color: #000000;
				color: #ff0000;
				font-size: 32pt;
				text-align: center;
			}
		</script>
	</head>
	<body>
		<p id="main-header">This has a unique style</h2>
		<p>This is the default color</p>
		<p>Also default color'</p>
	</body>
</html>

While ID selectors provide great specificity and control, overuse can lead to complex and difficult-to-maintain stylesheets. Use them sparingly for elements that truly need to be uniquely identified. Consider using class selectors for more general styling needs.

The Cascade


The set of rules that determine the order in which styles are applied to elements. It ensures that the most specific and important styles take precedence over conflicting styles.

Order of Precedence

  1. User Styles: Styles defined by the user (e.g., through browser extensions) have the highest priority, but they are often disabled by default for security reasons.
  2. Importance: Styles declared with the !important keyword have higher importance than other styles, regardless of specificity.
  3. Specificty: The most specific selector wins. This means that selectors with more components (e.g., #id .class) have higher specificity than those with fewer components (e.g., .class).
  4. Inheritence: Styles can be inherited from parent elements. If a child element doesn't have a specific style for a property, it will inherit the style from its parent.
  5. Order: Classes listed later in the order contribute more to specificity.
<!DOCTYPE html>
<html>
	<head>
		<title>Cascade Example</title>
		<script>
			.container p {
			  color: blue;
			}

			.highlight {
			  color: red;
			}
		</script>
	</head>
	<body>
		<div class="container">
			<p class="highlight">This paragraph is highlighted.</p>
		</div>
	</body>
</html>

Key Points:

  • The cascade is a complex system that can be difficult to understand, especially for beginners.
  • Understanding the cascade is essential for writing efficient and maintainable CSS.
  • Using the !important keyword should be avoided whenever possible, as it can make your stylesheets difficult to maintain.
  • It's often helpful to use a CSS preprocessor like Sass or Less to manage complex stylesheets and make them easier to understand.

Multi-File Websites


Websites that are composed of multiple individual files, each serving a specific purpose. This approach is essential for modern web development, as it allows for better organization, maintainability, and performance optimization.

Key Components:

  • HTML Files: These files contain the structural elements of the website, such as headings, paragraphs, lists, and images. They define the content and layout of the pages.
  • CSS Files: These files contain the styling rules that define how the HTML elements should be displayed. They control the appearance of the website, including colors, fonts, spacing, and layout.
  • JavaScript Files: These files contain scripts that add interactivity and functionality to the website. They can be used to create dynamic elements, handle user interactions, and communicate with the server.
  • Image Files: These files contain images that are used on the website. They can be of various formats, such as JPEG, PNG, or GIF.
  • Other File Types: Websites may also include other file types, such as audio files, video files, or PDF documents.

Benefits of Multi-File Websites:

  • Improved Organization: By dividing the website into separate files, it becomes easier to manage and maintain.
  • Enhanced Readability: HTML, CSS, and JavaScript files can be edited and understood independently, making the codebase more readable.
  • Better Performance: By separating content from presentation, the browser can load and render the website more efficiently.
  • Easier Collaboration: Multiple developers can work on different parts of the website simultaneously without stepping on each other's toes.

Unit Project


This objective of this project is to redesign your website that currently styles all elements directly within the HTML code into a more organized and maintainable structure using separate CSS files. This transformation will enhance the website's readability, scalability, and overall performance.

Project Tasks

  1. Analyze the Existing Website:
    • Identify all HTML elements that are currently styled directly within the code.
    • Assess the complexity and volume of the inline styles.
    • Determine the potential benefits of moving these styles into a separate CSS file.
  2. Create a CSS File:
    • Create a new CSS file (styles.css) to store all the styling rules.
    • Extract the inline styles from the HTML files and transfer them to the CSS file.
    • Organize the styles within the CSS file using appropriate selectors (e.g., classes, IDs, tag names) to improve readability and maintainability.
  3. Update HTML Files:
    • Link the CSS file in the head of your HTML file.
    • <link href="styles.css" rel="stylesheet">
    • Remove all inline styles from the HTML files.
    • Apply the corresponding CSS classes or IDs to the HTML elements to reference the styles defined in the CSS file.
  4. Test and Refine:
    • Thoroughly test the website to ensure that all elements are styled correctly after the transition to the multi-file structure.
    • Make any necessary adjustments to the CSS file or HTML files to address any styling issues or inconsistencies.