Notes on CSS
Notes on CSS
• CSS (Cascading Style Sheets) is a style sheet language used to describe the presentation of a
document written in HTML or XML.
• It separates the content (HTML) from its visual presentation, making web design more flexible and
maintainable.
• CSS enhances the visual appeal of web pages by defining styles for elements like layout, colors,
and fonts.
Types of CSS
• Inline CSS: Style is applied directly within the HTML tag using the style attribute.
• Internal CSS: Defined within a <style> tag in the <head> section of an HTML document.
• External CSS: Written in a separate file with the .css extension and linked to the HTML document
using the <link> tag.
CSS Syntax
• CSS uses the box model to represent the structure of each element, consisting of:
4. Margin: Space outside the border, separating the element from others.
CSS Properties
• Text Properties: Control font, size, color, alignment, and spacing (e.g., font-size, color, text-align).
• Background Properties: Set background color, image, and other effects (e.g., background-color,
background-image).
1
• Positioning Properties: Define how elements are positioned (e.g., position, top, left, z-index).
• Display Properties: Control element visibility and layout behavior (e.g., display, visibility).
Advantages of CSS
• Accessibility: Improves accessibility by providing alternate styles for different devices or users.
CSS Limitations
Inline CSS is a method of applying CSS directly within an HTML element. It is used to style a specific
element by adding the style attribute within the opening tag of the HTML element.
Inline CSS is typically used for quick styling changes or for applying unique styles to a single element, rather
than managing styles across multiple elements or pages.
The syntax for Inline CSS involves specifying a style attribute inside an HTML element. The attribute
contains one or more CSS property-value pairs separated by semicolons.
Content
</element>
2
</p>
1. HTML Element: The element to which the style is applied (e.g., <p>).
3. Property-Value Pair: Specifies the CSS properties and their values (e.g., color: blue).
Use Cases:
Internal CSS refers to a style sheet embedded directly within an HTML document. It is defined within the
<style> tag in the <head> section of the document. Internal CSS is used when you want to apply styles to
a single HTML page without linking an external stylesheet.
This method allows you to keep your CSS centralized for a specific page while maintaining some separation
from the HTML content.
2. Styles apply to all elements of the HTML document that match the defined selectors.
3. It is more structured and maintainable compared to inline CSS but less reusable than external CSS.
3
Syntax of Internal CSS:
<head>
<style>
selector {
property1: value1;
property2: value2;
...
</style>
</head>
Example –
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightblue;
h1 {
color: navy;
text-align: center;
p{
font-size: 16px;
</style>
</head>
4
<body>
</body>
</html>
Use Cases:
Internal CSS provides a balance between inline and external CSS, offering maintainable styles for single
pages while keeping the HTML document self-contained.
External CSS refers to a style sheet that is written in a separate file with a .css extension and linked to an
HTML document. It allows you to define and apply styles to multiple HTML pages, ensuring consistency
across a website.
External CSS is considered the most efficient and scalable method for styling websites, especially for
projects with multiple pages.
2. Linked to an HTML document using the <link> tag in the <head> section.
3. Styles can be reused across multiple HTML pages, promoting modularity and maintainability.
4. Enhances separation of concerns by keeping content (HTML) and presentation (CSS) distinct.
5
Syntax of External CSS File:
<head>
</head>
<head>
</head>
body {
background-color: white;
h1 {
color: navy;
text-align: center;
p{
font-size: 14px;
6
Advantages of External CSS:
2. Separation of Concerns: Keeps HTML content and CSS styling separate, improving readability and
maintainability.
3. Scalability: Ideal for large projects where consistent styling is required across numerous pages.
4. Easier Maintenance: Updating the external CSS file automatically reflects changes across all linked
pages.
Use Cases:
• Websites with Multiple Pages: Ensures a consistent look and feel across the entire site.