DEV Community

Cover image for Interview Questions for Web Developer - CSS
Mykhailo Toporkov
Mykhailo Toporkov

Posted on

Interview Questions for Web Developer - CSS

1. What does CSS mean? - Answer

2. How to add CSS for a webpage?

  • Inline CSS: You can use the style attribute directly within HTML elements to apply CSS styles. For example:
    <p style="color: blue;">Inline CSS</p>

  • Internal CSS: You can embed CSS styles within the <style> tag in the <head> section of your HTML document. This method allows you to define styles for multiple elements in the same document.

<!DOCTYPE html>
<html>
<head>
    <style>
        p {
            color: blue;
        }
    </style>
</head>
<body>
    <p>Internal CSS</p>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode
  • External CSS: Create a separate .css file containing your styles and link it to your HTML document using the <link> tag in the <head> section.

Example HTML file (index.html):

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
    <p>External CSS</p>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Example CSS file (styles.css):

p {
    color: blue;
    font-size: 16px;
}
Enter fullscreen mode Exit fullscreen mode

3. Whatโ€™s the difference between resetting and normalizing CSS? Which would you choose, and why?

Resetting CSS:

  • Objective: Resetting CSS aims to remove all default browser styling, setting all elements to a consistent baseline.

  • Implementation: It involves explicitly zeroing out margins, padding, and other styles for HTML elements. Commonly used reset CSS files include Eric Meyer's CSS Reset or Normalize.css with its reset portion.

  • Result: It gives complete control to the developer but might require redefining styles for most elements.

Normalizing CSS:

  • Objective: Normalizing CSS aims to make default styles consistent across different browsers while preserving useful browser defaults.

  • Implementation: It involves selectively resetting only those styles that vary between browsers. Normalize.css is a popular example of a normalization CSS file that focuses on ensuring consistent styles across different elements.

  • Result: It establishes a more consistent baseline for styles across browsers without completely erasing default styles, allowing for easier and more predictable styling.

Which to Choose and Why:

  • Resetting CSS: Offers complete control but requires more effort to redefine styles for most elements. Use when you need a completely clean slate and prefer to explicitly define all styles yourself.

  • Normalizing CSS: Provides a consistent starting point across browsers, preserving some useful defaults. It's more user-friendly when consistency matters but still allows for customization. Use when you want a more consistent appearance across browsers without starting entirely from scratch.

The choice depends on your project's needs. If you need complete control over styling and don't mind defining all styles yourself, a reset might be suitable. However, if consistency across browsers while preserving useful defaults is your priority, normalization might be a better choice. Often, a mix of both approaches is used depending on the project's requirements.

4. Whatโ€™s the difference between pseudo-element & pseudo-classes in CSS?

Pseudo-classes:

Pseudo-classes target specific states or behaviours of an element. They represent different states that an element can be in, such as being hovered over by the mouse, being the first child, being visited, or being focused.

Examples of pseudo-classes include :hover, :active, :visited, :first-child, :last-child, :focus, :nth-child(), etc.

Pseudo-elements:

Pseudo-elements, on the other hand, target specific parts of an element. They allow styling of a specific part of an element, such as the first line of text, the first letter, or generating additional content.

Examples of pseudo-elements include ::first-line, ::first-letter, ::before, ::after, etc.

Key Differences:

  • Targeting: Pseudo-classes target specific states or behaviors of elements, such as their interactions or relationships with the document structure. Pseudo-elements target specific parts or segments of elements, allowing for more granular styling.

  • Usage: Pseudo-classes are used to style elements based on their state or position in the document tree. Pseudo-elements are used to style specific parts or generate additional content within an element.

In summary, pseudo-classes are used to style elements based on certain states or interactions, while pseudo-elements are used to style specific parts or generate additional content within elements. Both pseudo-classes and pseudo-elements are powerful tools in CSS, offering ways to apply styles beyond just targeting elements themselves

5. Explain the position property in CSS & its values.
Explain the position property in CSS & its values. - Answer

6. Describe z-index and how stacking context is formed. - Answer

7. What is margin collapse?
Margin collapse is a behavior in CSS where the vertical margins of adjacent or nested elements collapse, resulting in a smaller combined margin instead of being added together.

Margin collapse occurs under certain conditions:

  • Adjacent Sibling Elements: When the top margin of one block-level element touches the bottom margin of its adjacent sibling element (no content, padding, or border separates them), the margins collapse. Only the larger of the two margins is preserved, and the smaller one collapses to zero. This is known as adjoining margins.
<div style="margin-bottom: 20px;">Div 1</div>
<div style="margin-top: 30px;">Div 2</div>
Enter fullscreen mode Exit fullscreen mode

In this example, the vertical space between the two <div> elements will be 30px (the larger margin), not 50px (sum of margins).

  • Parent-Child Relationships: Margin collapse can also occur between a parent element and its first or last child element when there are no padding, borders, or clearance separating them. If the top margin of the child and the bottom margin of the parent are adjoining, the margins may collapse.
<div style="margin-bottom: 20px;">
  <p style="margin-top: 30px;">Paragraph</p>
</div>
Enter fullscreen mode Exit fullscreen mode

In this case, the margin between the outer <div> and the inner <p> will collapse to 30px.

Margin collapsing can sometimes lead to unexpected layout results when designing web pages. To prevent or control margin collapse, there are a few techniques you can use:

  • Borders or Padding: Adding borders or padding between elements can prevent margin collapse.

  • Clearfix or Overflow: Using clearfix techniques or setting the parent element's overflow to "hidden" or "auto" can contain margins and prevent collapse.

  • Setting Display or Float: Changing the display property (e.g., display: inline-block;) or using floats can alter the element's layout, affecting margin collapse behavior.

8. What are media queries? Explain with syntax? - Answer

Top comments (1)

Collapse
 
robinamirbahar profile image
Robina

good try