Cascade Style Sheet
3 min read
CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of a document written in HTML or XML. CSS is a cornerstone technology of the World Wide Web, alongside HTML and JavaScript. It allows web developers to control the layout, colors, fonts, and overall visual appearance of web pages. Hello world
Introduction to CSS
CSS is designed to enable the separation of presentation and content, including layout, colors, and fonts. This separation can improve content accessibility, provide more flexibility and control in the specification of presentation characteristics, enable multiple web pages to share formatting by specifying the relevant CSS in a separate .css file, and reduce complexity and repetition in the structural content.
Basic Syntax
A CSS rule consists of a selector and a declaration block. The selector points to the HTML element you want to style. The declaration block contains one or more declarations separated by semicolons. Each declaration includes a CSS property name and a value, separated by a colon.
Example
In this example, the body
selector applies a light blue background color to the entire web page, and the h1
selector changes the color of all <h1>
elements to navy and adds a left margin of 20 pixels.
CSS Selectors
CSS selectors are used to "find" (or select) the HTML elements you want to style. Here are some common selectors:
Element Selector: Selects all elements of a given type.
ID Selector: Selects a single element with a specific id.
Class Selector: Selects all elements with a specific class.
Attribute Selector: Selects elements with a specific attribute.
CSS Box Model
All HTML elements can be considered as boxes. The CSS box model is essentially a box that wraps around every HTML element. It consists of: margins, borders, padding, and the actual content. The box model allows us to add a border around elements and define space between elements.
Content: The actual content of the box, where text and images appear.
Padding: Clears an area around the content. The padding is transparent.
Border: A border that goes around the padding (if any) and content.
Margin: Clears an area outside the border. The margin is transparent.
Responsive Design
Responsive web design makes web pages look good on all devices (desktops, tablets, and phones). CSS media queries are a key component of responsive design. They allow you to apply different styles for different devices or screen sizes.
In this example, if the screen width is 600 pixels or less, the background color of the body will change to light green.
Conclusion
CSS is a powerful tool for web developers to create visually appealing and responsive web pages. By understanding and utilizing CSS, you can significantly enhance the user experience and accessibility of your web content. Whether you are styling text, creating layouts, or making your site responsive, CSS provides the flexibility and control needed to bring your web designs to life.