CSS Introduction
What is CSS?
CSS, or Cascading Style Sheets, is a fundamental technology used for describing the presentation of a document written in a markup language like HTML. It controls how HTML elements are displayed on a webpage, including layout, colors, fonts, and more.
Here, we have seen the some basic key Concepts.
- Selectors
- Properties and Values
- Selectors and Declarations
- Cascading
- Inheritance
- Units
- Box model
- Selectors specificity
Selectors
CSS selectors are patterns used to select the elements you want to style. They can be element selectors (e.g., p
for paragraphs), class selectors (e.g., .my-class
), ID selectors (e.g., #my-id
), or more complex selectors.
Properties and Values
CSS properties define the visual aspects of an element, such as color
, font-size
, background-color
, etc. Each property is assigned a value, which determines the specific styling.For example,
p {
color: blue;
font-size: 16px;
}
Selectors and Declarations
CSS rules consist of a selector and one or more declarations enclosed in curly braces {}
. A declaration is made up of a property and its corresponding value.
Cascading
The “C” in CSS stands for cascading. This means that multiple style sheets can be applied to a single HTML document, and styles can cascade from one style sheet to another. The cascade is determined by the specificity of selectors and their order in the stylesheet.
Inheritance
Inheritance is another key principle of CSS. When no value is specified for a property on an element, it inherits the value from its parent element. However, not all properties are inherited by default.
Units
CSS supports various units for specifying lengths and sizes, such as pixels (px
), percentages (%), ems (em
), rems (rem
), and more. Understanding how these units work is crucial for creating responsive and flexible layouts.
Box Model
In CSS, every element is treated as a rectangular box. The CSS box model describes how these boxes are laid out, including content, padding, border, and margin.
Selector Specificity
Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied. It’s crucial to understand specificity to avoid unexpected styling behavior.
These are just the basics of CSS, but they form the foundation for creating visually appealing and well-structured web pages. As you delve deeper into CSS, you’ll encounter more advanced topics like flexbox, grid layout, animations, and responsive design techniques.
CSS History
Emergence(1990)
CSS was introduced as a styling language to complement HTML. Before CSS, styling was done directly within HTML using attributes like <font>
and <color>
. In 1996, the CSS1 specification was published by the W3C (World Wide Web Consortium).
Standardization(2000)
CSS2, the second version of the CSS specification, was introduced in 1998 and provided more advanced styling capabilities like absolute, relative, and fixed positioning, as well as z-index management. However, it wasn’t until later in the 2000s that CSS2 was fully supported by major browsers.
Modernization(2010)
The CSS3 era brought a plethora of new features and capabilities, including rounded corners, gradients, shadows, animations, and transitions. Instead of one monolithic specification, CSS3 was divided into modules, allowing for incremental updates and easier implementation by browsers.
Responsive design and Flexbox/Grid(2010)
With the rise of mobile devices, responsive web design became crucial. Media queries were introduced, allowing developers to apply different styles based on the device’s screen size. Additionally, Flexbox and CSS Grid Layout emerged as powerful layout systems, providing more control over the positioning and alignment of elements.
CSS Preprocessors and Postprocessors(2010)
Tools like Sass, Less, and PostCSS gained popularity, offering features like variables, mixins, and nesting, which made CSS more maintainable and scalable for larger projects.
CSS-In-Js(2010)
With the advent of front-end frameworks like React, developers began exploring new ways to manage CSS within JavaScript. CSS-in-JS libraries such as styled-components and Emotion gained traction, allowing developers to encapsulate styles within their components.
CSS custom properties(variables)(2010)
CSS Custom Properties, often referred to as CSS variables, were introduced, allowing developers to define reusable values directly in CSS. This feature enables more dynamic and maintainable stylesheets.
Future(2020)
The evolution of CSS continues, with ongoing efforts to standardize new features and improve existing ones. Topics such as container queries, aspect ratio control, and more advanced layout systems are being explored to address the needs of modern web development.
Overall, CSS has undergone a remarkable transformation over the years, evolving from a simple styling language to a powerful tool for creating dynamic and responsive web experiences.