CSS

From Elixir Wiki
Jump to navigation Jump to search

CSS[edit]

CSS, short for Cascading Style Sheets, is a fundamental component of web development. It is a styling language used for describing the presentation of a document written in HTML. With CSS, developers can control the layout, colors, fonts, and other visual aspects of web pages.

Basic Syntax[edit]

CSS uses a simple syntax with rulesets, selectors, and declarations. A ruleset consists of a selector and one or more declarations enclosed in curly braces.

```css selector {

 property: value;
 /* more declarations */

} ```

Selectors are used to target specific HTML elements or groups of elements. The declarations within a ruleset define the styling properties and their respective values.

Styling HTML Elements[edit]

CSS allows developers to style HTML elements individually or apply styles to groups of elements using selectors. Some common selectors include:

- *Element Selectors*: Target elements based on their tag name, such as `p` for paragraphs or `h1` for headings. - *Class Selectors*: Target elements with a specific `class` attribute using `.` followed by the class name, such as `.container`. - *ID Selectors*: Target a specific element with a unique `id` attribute using `#` followed by the id name, such as `#header`.

CSS provides a wide range of properties to style elements. Some commonly used properties include:

- `color`: Sets the text color. - `background-color`: Sets the background color. - `font-family`: Specifies the font family for text. - `font-size`: Sets the font size. - `margin`: Sets the margin around an element. - `padding`: Sets the padding inside an element.

Cascading and Specificity[edit]

CSS follows the cascade principle, where multiple styles can be applied to an element, and the final result is determined by a set of rules. The cascade order is: 1. Inline styles (styles applied directly to the HTML element using the `style` attribute). 2. Internal stylesheet (styles defined within the `<style>` tags in the HTML file). 3. External stylesheet (styles defined in a separate CSS file and linked to the HTML file).

In addition to the cascade, CSS also considers specificity when determining which styles to apply. Specificity is based on the selector used and determines the weight of the style rule. Generally, the more specific a selector is, the higher its specificity.

Responsive Design[edit]

With the increasing popularity of mobile devices, responsive design has become crucial. CSS offers several techniques to create responsive layouts:

- *Media Queries*: Allows different styles to be applied based on the device's characteristics, such as screen size or orientation. - *Flexbox*: A CSS layout model that provides a flexible and efficient way to align and distribute space among items in a container. - *Grid*: A two-dimensional layout system that allows elements to be laid out in columns and rows, providing advanced control over the design.

CSS Frameworks[edit]

CSS frameworks provide pre-defined CSS styles and components, making it easier to develop web applications. Some popular CSS frameworks include:

- [Bootstrap](https://en.wikipedia.org/wiki/Bootstrap_(front-end_framework)): A feature-rich, mobile-first framework that provides a wide range of pre-designed components. - [Bulma](https://en.wikipedia.org/wiki/Bulma_(CSS_framework)): A lightweight framework focused on simplicity and responsiveness. - [Tailwind CSS](https://en.wikipedia.org/wiki/Tailwind_CSS): A utility-first framework that allows developers to directly specify styling classes in HTML.

Conclusion[edit]

CSS is a powerful language that enables developers to style and design web pages. With its selectors, properties, and cascade order, CSS allows for the creation of visually appealing and responsive websites. Combined with the use of CSS frameworks, developers can streamline the development process and create consistent and professional-looking web applications.