February 20, 2025

CSS Interview Question

css interview question
What is CSS?

CSS stands for Cascading Style Sheets. It is used to style and layout web pages, including design elements such as color, font, and spacing.

There are three types of CSS:

  1. Inline CSS
  2. Internal CSS
  3. External CSS
  • id is used to target a unique element, and it must be unique within a page.
  • class can be reused multiple times in different elements.

The CSS box model defines rectangular boxes generated for elements in the document tree. These are: content, padding, border, and margin

You can include a CSS file using the <link> tag inside the <head> section

				
					<link rel="stylesheet" type="text/css" href="styles.css">
				
			

The z-index property specifies the stack order of the elements. Elements with higher z-index are displayed in front of elements with a lower z-index.

  • absolute: Positioned relative to the nearest ancestor.
  • relative: Positioned relative to its normal position.
  • fixed: Positioned relative to the browser window.
  • sticky: Acts like relative until a defined scroll point, then behaves like fixed.

Pseudo-classes define a special state of an element. Examples include :hover, :focus, :active, :nth-child(), etc.

Media queries are used to apply different styles based on conditions such as screen width, device type, or orientation. Example:

				
					@media (max-width: 600px) {
  body {
    background-color: lightblue;
  }
}
				
			
  • display: none: The element is removed from the document flow, and it takes up no space.
  • visibility: hidden: The element is still in the document flow, but it is invisible.

Flexbox is a layout model in CSS that allows you to more easily create complex layouts by distributing space along rows or columns.

CSS Grid is a two-dimensional layout system that allows creating complex layouts using rows and columns. Unlike Flexbox, which is one-dimensional (either row or column), Grid can handle both dimensions at the same time.

  • For horizontal centering: margin: 0 auto;
  • For vertical centering: Use Flexbox or Grid or apply position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);

::before and ::after are used to insert content before or after an element’s content. They are commonly used to add icons, quotes, or other visual elements.

  • position: sticky; allows an element to “stick” to a defined location as the user scrolls next to it. This is useful for creating sticky headers or navigation bars.
  • block: Elements that take up the entire width and start on a new line.
  • inline: Elements that take up only as much width as necessary and do not start on a new line.
  • inline-block: Elements that behave like inline elements but allow block-level styling.

The !important rule is used to give a CSS rule higher priority than other conflicting rules. This is generally avoided as it may cause maintenance problems.

  • em: Related to the font-size of its parent element.
  • rem: Related to the font-size of the parent element (<html>).
  • px: Absolute units, fixed in size.
  • %: Relative to the parent element’s size.

The calc() function is used to perform calculations to set CSS property values, such as width: calc(100% - 20px).

CSS animations allow smooth changes of styles over time. You create them using the @keyframes rule and apply the animation with the animation property.