CSS

Q1. What is CSS, and why is it used?

Answer:

CSS (Cascading Style Sheets) is a stylesheet language used to control the visual presentation of HTML documents, allowing styling elements with colors, layouts, fonts, and more.

Answer:
  • Inline: Styles applied directly to elements via the style attribute.
  • Internal: Styles placed in a <style> tag within the HTML <head>.
  • External: Styles in a separate .css file linked to the HTML document.
Answer:

The box model describes how an element is rendered with content, padding, border, and margin around it.

Answer:

Classes (.class-name) are reusable and can be applied to multiple elements, while IDs (#id-name) are unique and apply to only one element per page.

Answer:

Pseudo-classes target elements in specific states, such as :hover for when an element is hovered over.

Answer:

Use margin: auto on a block-level element with a defined width or use Flexbox (justify-content: center) for flexibility.

Answer:

The display property defines how elements are displayed. Common values include:

  • block: Takes up the full width.
  • inline: Takes up only as much space as needed.
  • flex: Enables Flexbox layout.

Answer:

Adjacent vertical margins between elements can merge into one larger margin, typically the larger of the two.

Answer:

Padding is the space inside an element between its content and border, while margin is the space outside an element’s border.

Answer:

A CSS reset removes browser default styling, allowing consistent styling across different browsers.

Answer:
  • relative: Positions relative to itself.
  • absolute: Positions relative to the nearest positioned ancestor.
  • fixed: Positions relative to the viewport, staying in place while scrolling.
  • sticky: Switches between relative and fixed depending on scroll.
Answer:

Media queries apply styles based on device characteristics (e.g., width). They are essential for responsive design.

Example:

@media (max-width: 600px) { body { background-color: lightblue; } }

Answer:

Flexbox arranges items along a flexible row or column with properties like justify-content, align-items, and flex-wrap.

Answer:

em is relative to the font size of its nearest parent, while rem is relative to the root (html) font size.

Answer:

CSS animations are defined with @keyframes and applied with properties like animation-name, animation-duration, and animation-timing-function.

Answer:

z-index controls the stack order of positioned elements. Higher values appear on top.

Answer:

Specificity determines which CSS rule applies when multiple rules target the same element. IDs have higher specificity than classes, which are higher than element selectors.

Answer:

:nth-child() targets the nth child regardless of type, while :nth-of-type() targets the nth child of a specific type.

Answer:

CSS Grid divides layouts into rows and columns, creating complex, responsive layouts with properties like grid-template-columns and grid-gap.

Answer:

@import imports a CSS file into another, allowing modularization. However, it can slow down performance compared to <link> tags.

Answer:

The subgrid value allows child elements to inherit the grid layout of their parent, useful for maintaining alignment across nested grids.

Answer:

The contain property improves performance by isolating rendering to specified elements, reducing layout calculations.

Answer:

will-change tells the browser to optimize certain properties that will change, such as transform or opacity, enhancing animations and transitions.

Answer:

Houdini APIs let developers extend CSS, allowing access to the browser’s rendering engine for custom styling, animations, and layout calculations.

Answer:

Variable fonts store multiple font styles in a single file, reducing HTTP requests and enabling smoother transitions between weights and styles.

Answer:

mix-blend-mode defines how content blends with background content, used for creative effects like overlays and complex visual styling.

Answer:

CSS Scoping limits styles to a particular section. The :scope pseudo-class targets the scoped element, usually within a shadow DOM or component-based structure.

Answer:

calc() performs calculations within CSS, allowing for responsive adjustments like width: calc(100% - 50px);.

Answer:

::part styles custom elements’ shadow DOM, while ::slotted styles elements passed into slots within Web Components.

Answer:

clamp() constrains a CSS value between a minimum, preferred, and maximum value, making responsive design easier without media queries.

Answer:

Custom properties, defined with --property-name, allow variables in CSS, making code more maintainable and reusable.

Answer:

CSS Layers allow controlled stacking of CSS rules for better organization and specificity management, prioritizing specific layers.

Answer:

prefers-reduced-motion detects if the user prefers less motion and adapts animations for accessibility.

Answer:

Critical CSS loads above-the-fold styles inline to minimize render-blocking, speeding up initial page load.

Answer:

backdrop-filter applies effects like blur and brightness behind an element, creating visually appealing UI effects.

Answer:

:has() is a relational selector allowing CSS to apply styles based on the presence of specific child elements.

Answer:

The aspect-ratio property ensures elements maintain proportional dimensions, improving responsive designs.

Answer:

A polyfill is JavaScript that replicates CSS functionality not natively supported by browsers, ensuring compatibility.

Answer:

The Paint API allows custom CSS graphics directly within stylesheets, enabling unique backgrounds and borders dynamically.