Introduction to CSS


Introduction to CSS Interview with follow-up questions

1. What is CSS and why is it important in web development?

CSS (Cascading Style Sheets) is the language used to control the visual presentation of HTML documents. While HTML defines the structure and meaning of content, CSS defines how that content looks — colors, typography, spacing, layout, animation, and more.

Why CSS matters:

  • Separation of concerns — keeping style out of HTML means you can restyle an entire site by changing one CSS file rather than touching every HTML page.
  • Reusability — a single stylesheet can be shared across thousands of pages, keeping the visual language consistent.
  • Performance — browsers cache external CSS files, so returning users don't re-download styles on every page visit.
  • Maintainability — designers can update visual styles without touching structure or logic.

The cascade is the mechanism that determines which rule wins when multiple rules target the same element. It considers three factors in order: origin and importance (browser defaults vs. author styles vs. user styles), specificity (how precisely a selector targets an element), and source order (later rules win when specificity ties).

Modern CSS has expanded significantly. In 2026, CSS is far more powerful than it was even five years ago. Features that used to require JavaScript or complex hacks are now native:

  • Container queries (@container) for component-level responsive design
  • :has() for parent selection
  • CSS nesting (native, no preprocessor required)
  • @layer for cascade layer management
  • color-mix(), oklch(), and wide-gamut color
  • subgrid for cross-component alignment
  • Scroll-driven animations

Interviewers expect you to know that CSS is not just decoration — understanding the cascade, specificity, and layout systems (Flexbox, Grid) is considered core front-end knowledge.

↑ Back to top

Follow-up 1

Can you explain the difference between inline, internal, and external CSS?

Inline CSS is applied directly to an HTML element using the 'style' attribute. It has the highest specificity and overrides any other CSS rules. Internal CSS is defined within the 'style' element in the head section of an HTML document. It applies to the entire document or a specific section defined by a selector. External CSS is stored in a separate file with a .css extension and linked to the HTML document using the 'link' element. It allows for the reuse of styles across multiple web pages.

Follow-up 2

What are some advantages of using external CSS?

Using external CSS has several advantages:

  1. Separation of concerns: External CSS allows for a clear separation between the structure (HTML) and the presentation (CSS) of a web page. This makes it easier to maintain and update the design of a website.
  2. Reusability: External CSS files can be linked to multiple web pages, allowing for the reuse of styles across the entire website.
  3. Caching: External CSS files can be cached by the browser, resulting in faster page load times for subsequent visits to the website.
  4. Modularity: External CSS files can be organized into different modules, making it easier to manage and update specific styles.
  5. Consistency: External CSS promotes consistency in the design of a website, as styles can be applied consistently across multiple pages.

Follow-up 3

What is the syntax of a CSS rule-set?

A CSS rule-set consists of a selector and one or more declarations. The syntax is as follows:

selector {
    property1: value1;
    property2: value2;
    /* more properties and values */
}
  • The selector specifies which HTML elements the rule should apply to.
  • Each declaration consists of a property and its corresponding value, separated by a colon.
  • Multiple declarations are separated by semicolons.

For example, the following CSS rule-set selects all paragraphs and sets their color to red:

p {
    color: red;
}

2. Can you explain what CSS selectors are and give examples of different types?

CSS selectors are patterns that identify which HTML elements a rule applies to. Understanding selector types — and their specificity implications — is a core interview topic.

Basic selectors:

  • Type selectorp targets all <p> elements.
  • Class selector.card targets all elements with class="card".
  • ID selector#header targets the element with id="header". IDs have very high specificity and should be used sparingly in stylesheets.
  • Universal selector* targets every element. Often used in resets (* { box-sizing: border-box; }).

Attribute selectors:

  • [type="email"] — exact match
  • [href^="https"] — starts with
  • [src$=".svg"] — ends with
  • [class*="btn"] — contains

Combinators:

  • Descendantnav a selects all <a> elements anywhere inside ``.
  • Childul &gt; li selects only direct <li> children of <ul>.
  • Adjacent siblingh2 + p selects the <p> immediately after an </p> <h2>.
  • General siblingh2 ~ p selects all </h2> <p> elements that follow an </p> <h2> as siblings.

Pseudo-classes — select based on state or position:

  • :hover, :focus, :active, :visited
  • :nth-child(2n+1), :first-child, :last-child, :not(.disabled)
  • :has() — introduced in 2023 and now baseline across browsers; lets you select a parent based on its children. For example, form:has(input:invalid) selects any form that contains an invalid input.

Pseudo-elements — style a specific part of an element:

  • ::before, ::after, ::placeholder, ::selection, ::first-line

Specificity gotcha: ID selectors (100) beat class selectors (10) beat type selectors (1). Inline styles beat all stylesheet rules. !important overrides everything and creates cascade debt — avoid it except for very specific utility overrides. Using @layer to control cascade priority is the modern alternative to fighting specificity.

↑ Back to top

Follow-up 1

What is the difference between class and id selectors?

The main difference between class and id selectors is that class selectors can be used to select multiple elements, while id selectors can only select a single element.

  • Class selector: .my-class selects all elements with the class my-class.
  • ID selector: #my-id selects the element with the id my-id.

You can apply the same class to multiple elements, allowing you to style them in a similar way. On the other hand, an id should be unique within a webpage, and it is often used to select a specific element for styling or JavaScript manipulation.

Follow-up 2

How does the specificity of CSS selectors work?

Specificity is a way to determine which CSS rule should be applied to an element when multiple rules target the same element. It is calculated based on the types of selectors used in a rule.

The specificity of a selector is represented by four values: inline styles, id selectors, class/selectors, and tag selectors. The higher the value, the more specific the selector is.

For example:

  • Inline styles have the highest specificity (1,0,0,0).
  • ID selectors have a specificity of (0,1,0,0).
  • Class/selectors have a specificity of (0,0,1,0).
  • Tag selectors have the lowest specificity (0,0,0,1).

When two or more rules have the same specificity, the one that appears later in the CSS file will take precedence.

Follow-up 3

Can you give an example of a complex CSS selector?

Sure! Here's an example of a complex CSS selector:

ul.navbar &gt; li.active a[href^='#']::before {
    content: '&gt;&gt;';
    color: red;
}

This selector targets the a element that has an href attribute starting with #, which is a child of an li element with the class active, which is a direct child of a ul element with the class navbar. It then adds a red &gt;&gt; before the content of the a element.

Complex selectors like this can be useful when you need to target specific elements within a complex HTML structure.

3. What are CSS properties and how are they used?

CSS properties are the individual styling instructions applied to elements. Each property accepts a defined set of values and controls a specific aspect of presentation. Properties are written as key-value pairs inside a declaration block:

p {
  color: #333;
  font-size: 1rem;
  line-height: 1.6;
  margin-bottom: 1em;
}

Properties fall into broad categories:

  • Box modelwidth, height, padding, margin, border, box-sizing
  • Typographyfont-family, font-size, font-weight, line-height, letter-spacing, text-align
  • Color and backgroundcolor, background-color, background-image, opacity
  • Layoutdisplay, position, top/right/bottom/left, flex, grid, float
  • Visual effectsbox-shadow, border-radius, transform, filter, clip-path
  • Animation and transitiontransition, animation, animation-timeline

CSS custom properties (variables) are a first-class feature, not a preprocessor trick:

:root {
  --color-primary: oklch(55% 0.2 250);
  --spacing-md: 1rem;
}

button {
  background-color: var(--color-primary);
  padding: var(--spacing-md);
}

Custom properties cascade and are inherited like any other property, can be updated with JavaScript, and enable dynamic theming without reloading styles.

Logical properties are now preferred over physical properties for internationalization. Use margin-inline-start instead of margin-left, block-size instead of height, etc. They adapt automatically to writing direction (LTR vs RTL).

Interviewers may ask about shorthand vs. longhand properties. border: 1px solid red is shorthand for border-width, border-style, and border-color. Shorthands reset unspecified sub-properties to their initial values, which can cause unexpected overrides.

↑ Back to top

Follow-up 1

Can you give examples of commonly used CSS properties?

Certainly! Here are some commonly used CSS properties:

  1. color: Specifies the text color.
  2. font-size: Sets the size of the font.
  3. margin: Controls the space around an element.
  4. padding: Defines the space between the content and the element's border.
  5. background-color: Sets the background color of an element.
  6. border: Specifies the border properties of an element.
  7. display: Determines how an element is displayed.
  8. width and height: Sets the width and height of an element.
  9. text-align: Aligns the text within an element.
  10. position: Defines the positioning of an element.

Follow-up 2

How do you specify multiple properties for a single selector?

To specify multiple properties for a single selector, you can separate each property-value pair with a semicolon. Here's an example:

selector {
    property1: value1;
    property2: value2;
    property3: value3;
}

Follow-up 3

What happens when conflicting properties are specified for the same element?

When conflicting properties are specified for the same element, the CSS rule with the highest specificity takes precedence. If two rules have the same specificity, the rule that appears later in the stylesheet will override the previous rule. In some cases, the !important declaration can be used to give a property the highest priority, regardless of specificity or order.

4. Can you explain what CSS units are and give examples?

CSS units specify the size of lengths, dimensions, and positions. Choosing the right unit is a common interview topic because it directly affects responsiveness and accessibility.

Absolute units — fixed regardless of context:

  • px (pixels) — the most common absolute unit. One CSS pixel is not necessarily one physical pixel on high-DPI screens (device pixel ratio applies).
  • pt, pc, in, cm, mm — used in print stylesheets; rarely in screen CSS.

Relative to font size:

  • em — relative to the font size of the element itself (or its parent for properties like font-size). Compounds when nested — a 1.2em font inside another 1.2em context becomes 1.44em.
  • rem — relative to the root element's font size (``). Does not compound. Preferred for font sizes and spacing to respect user browser font preferences.

Viewport units:

  • vw / vh — 1% of the viewport width / height.
  • vmin / vmax — 1% of the smaller / larger viewport dimension.
  • svh, lvh, dvh — small, large, and dynamic viewport height. dvh resizes when the browser's mobile chrome (address bar) appears or hides; svh is always the smallest possible viewport. These solve the classic iOS Safari "100vh is too tall" bug.

Container query units (new in 2023–2024):

  • cqw, cqh, cqi, cqb — percentage of the nearest @container. Relative to a component's container rather than the viewport, which is far more useful for reusable components.

% — relative to the corresponding measurement of the parent element. For width: 50%, it's 50% of the parent's content width.

ch — width of the "0" character in the current font. Useful for setting max-width on text containers (e.g., max-width: 65ch).

Gotcha: avoid px for font-size in body text — it overrides the user's browser font size preference. Use rem instead.

↑ Back to top

Follow-up 1

What is the difference between absolute and relative units?

The difference between absolute and relative units is how they are calculated and their relationship to other elements on the page.

  • Absolute units (e.g., px, pt, in, cm, mm) are fixed units of measurement that do not change based on the size or properties of other elements. They provide a consistent size regardless of the device or screen size.

  • Relative units (e.g., %, em, rem) are units of measurement that are relative to other elements on the page. They are calculated based on the size or properties of other elements, making them more flexible and responsive.

For example, if you set the font-size of a parent element to 16px and use em units for the child elements, 1em will be equal to 16px. If you change the font-size of the parent element to 20px, 1em will now be equal to 20px.

Follow-up 2

When would you use px, em, and rem units?

The choice of using px, em, or rem units depends on the specific use case and requirements of the design.

  • px (pixels) are commonly used for setting fixed sizes, such as borders, margins, and padding. They provide precise control over the size and are not affected by the size of other elements.

  • em units are relative to the font-size of the parent element. They are commonly used for setting font sizes, as they allow for more flexible and scalable typography. They can also be used for other properties like margins and padding.

  • rem units are relative to the root element (usually the element). They are similar to em units but provide a consistent size regardless of the nesting level. They are commonly used for setting font sizes and spacing in responsive designs.

It's important to consider accessibility and responsiveness when choosing the appropriate unit for each property.

Follow-up 3

How does the viewport unit work in responsive design?

Viewport units (vw, vh, vmin, vmax) are relative units that are based on the size of the viewport (the browser window or the device screen).

  • vw (viewport width) represents a percentage of the viewport's width. For example, 50vw will be equal to 50% of the viewport's width.

  • vh (viewport height) represents a percentage of the viewport's height. For example, 50vh will be equal to 50% of the viewport's height.

  • vmin (minimum of viewport width and height) represents a percentage of the smaller dimension (width or height) of the viewport. For example, 50vmin will be equal to 50% of the smaller dimension.

  • vmax (maximum of viewport width and height) represents a percentage of the larger dimension (width or height) of the viewport. For example, 50vmax will be equal to 50% of the larger dimension.

Viewport units are commonly used in responsive design to create fluid layouts and elements that adapt to different screen sizes.

5. How do you specify colors in CSS?

CSS provides several syntaxes for specifying color, and the options have expanded significantly in recent years to support wide-gamut displays.

Named colors — 148 predefined keywords like red, rebeccapurple, transparent. Convenient for quick prototyping.

Hex — a six-digit (or three-digit shorthand) hexadecimal value: #ff0000 (red), #f00 (shorthand). An eight-digit form adds an alpha channel: #ff000080 (red at 50% opacity).

rgb() / rgba()rgb(255, 0, 0) for red; rgba(255, 0, 0, 0.5) for 50% opacity. Modern syntax allows spaces without commas and an optional alpha in the same function: rgb(255 0 0 / 50%).

hsl() / hsla() — Hue (0–360 degrees on the color wheel), Saturation (%), Lightness (%). More intuitive for designers: hsl(0 100% 50%) is red. Easy to create color scales by varying lightness.

oklch() and oklab() — introduced as part of CSS Color Level 4 and now baseline across all major browsers. oklch(55% 0.2 250) specifies Lightness, Chroma, and Hue in a perceptually uniform color space. Colors created in oklch look equally vibrant across the wheel (unlike HSL where certain hues appear lighter or darker at the same L value). Critically, oklch can address colors in the P3 wide-gamut color space, producing more vivid colors on modern displays that support them.

color-mix() — mixes two colors in a given color space: color-mix(in oklch, blue 40%, white). Useful for generating tints and shades without a preprocessor.

CSS custom properties for theming:

:root {
  --color-brand: oklch(55% 0.2 250);
  --color-brand-light: color-mix(in oklch, var(--color-brand) 60%, white);
}

Current best practice: use oklch for new design systems to get perceptually uniform palettes and wide-gamut support. Fall back to hsl if you need to support older environments, though browser support for oklch is now universal in evergreen browsers.

↑ Back to top

Follow-up 1

What are the different ways to specify color in CSS?

The different ways to specify color in CSS are:

  1. Keyword: CSS provides a set of predefined color names such as red, blue, green, etc.

  2. RGB: RGB stands for Red, Green, Blue. It is a color model that uses three values ranging from 0 to 255 to represent the intensity of red, green, and blue colors respectively.

  3. HEX: HEX color values are represented as a six-digit combination of numbers and letters. Each pair of digits represents the intensity of red, green, and blue colors respectively.

  4. HSL: HSL stands for Hue, Saturation, Lightness. It is a color model that represents colors based on their hue, saturation, and lightness values.

Follow-up 2

What is the difference between RGB and HEX color values?

The main difference between RGB and HEX color values is the way they represent colors:

  • RGB: RGB values represent colors using three values ranging from 0 to 255 to represent the intensity of red, green, and blue colors respectively. For example, rgb(255, 0, 0) represents the color red.

  • HEX: HEX color values are represented as a six-digit combination of numbers and letters. Each pair of digits represents the intensity of red, green, and blue colors respectively. For example, #FF0000 represents the color red.

Both RGB and HEX color values can be used interchangeably in CSS, but HEX values are more commonly used due to their shorter syntax.

Follow-up 3

How do you apply opacity to colors?

To apply opacity to colors in CSS, you can use the rgba() or hsla() color functions. These functions allow you to specify an additional value for the opacity of the color.

  • RGBA: RGBA stands for Red, Green, Blue, Alpha. It is similar to the RGB color model, but with an additional value for the opacity. The opacity value ranges from 0 to 1, where 0 represents fully transparent and 1 represents fully opaque. For example, color: rgba(255, 0, 0, 0.5); represents the color red with 50% opacity.

  • HSLA: HSLA stands for Hue, Saturation, Lightness, Alpha. It is similar to the HSL color model, but with an additional value for the opacity. The opacity value ranges from 0 to 1. For example, color: hsla(0, 100%, 50%, 0.5); represents the color red with 50% opacity.

Live mock interview

Mock interview: Introduction to CSS

Intermediate ~5 min Your own free AI key

Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.