CSS basic question and answers for interview ?

 ere are some common CSS basic questions and answers that you might encounter in an interview:

1. What does CSS stand for?

Answer: CSS stands for Cascading Style Sheets. It is used to style and layout web pages, including changes in color, font, layout, and spacing.

2. What is the difference between classes and IDs in CSS?

Answer:

  • Classes: Defined with a dot (.) and can be applied to multiple elements (e.g., .classname). They are used for styling groups of elements.
  • IDs: Defined with a hash (#) and should be unique within a page (e.g., #idname). They are used for styling a single element.

3. What are the different ways to apply CSS to a web page?

Answer:

  1. Inline CSS: Using the style attribute within an HTML tag.
    html
    <h1 style="color: blue;">Hello</h1>
  2. Internal CSS: Using a <style> tag within the <head> section.
    html
    <style> h1 { color: blue; } </style>
  3. External CSS: Linking to an external CSS file using the <link> tag.
    html
    <link rel="stylesheet" href="styles.css">

4. What is the CSS box model?

Answer: The CSS box model describes how the elements on a web page are structured. It consists of:

  • Content: The inner part where text and images appear.
  • Padding: The space between the content and the border.
  • Border: A border surrounding the padding and content.
  • Margin: The outermost space, creating distance between the element and others.

5. What are CSS selectors? Can you name a few?

Answer: CSS selectors are patterns used to select the elements you want to style. Some common selectors include:

  • Universal Selector: * (selects all elements).
  • Type Selector: div, h1, etc. (selects all instances of that element type).
  • Class Selector: .classname (selects all elements with that class).
  • ID Selector: #idname (selects the element with that specific ID).
  • Attribute Selector: [type="text"] (selects elements with a specific attribute).

6. What is the difference between padding and margin?

Answer:

  • Padding: The space between the content and the border of an element. It is inside the element's border.
  • Margin: The space outside the element’s border that separates it from other elements.

7. What are media queries in CSS?

Answer: Media queries are a feature of CSS that allow styles to be applied based on the conditions such as screen size, resolution, or orientation. They are often used for responsive design.

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

8. Explain the CSS position property.

Answer: The position property specifies how an element is positioned in the document. It can take values like:

  • static: Default position; elements are placed according to the normal flow.
  • relative: Positioned relative to its normal position.
  • absolute: Positioned relative to the nearest positioned ancestor (not static).
  • fixed: Positioned relative to the viewport, remaining in the same place even when scrolling.
  • sticky: A hybrid that switches between relative and fixed, depending on the scroll position.

9. What is a CSS preprocessor?

Answer: A CSS preprocessor is a scripting language that extends CSS with variables, nested rules, mixins, functions, and more, which compile into standard CSS. Popular preprocessors include Sass, Less, and Stylus.

10. What is Flexbox and Grid in CSS?

Answer:

  • Flexbox: A one-dimensional layout model that allows you to design layouts along a row or column. It is great for aligning items and distributing space within a container.
  • Grid: A two-dimensional layout system that enables you to create complex layouts using rows and columns. It offers more control over both dimensions compared to Flexbox.

These questions cover fundamental CSS concepts and should help you prepare for an interview. Good luck!

Comments

Popular posts from this blog

PrimeNG tutorial with examples using frequently used classes

Docker and Kubernetes Tutorials and QnA

Building strong foundational knowledge in frontend development topics