CSS Interview Questions
Master the most commonly asked interview questions with comprehensive, expert-crafted answers designed to help you succeed.
What is CSS and Why do we use it?
CSS (Cascading Style Sheets) is a stylesheet language used to control the presentation and layout of HTML elements on a web page. It separates the content from the visual design, allowing developers to style web pages efficiently and consistently.
Why Do We Use CSS?
- Reusability: You can apply the same CSS rules across multiple HTML pages, saving time and effort.
- Easy Maintenance: A single change in the CSS file reflects across all linked HTML files, making updates quick and efficient.
- Improved Readability: CSS helps keep HTML files clean by removing inline styling.
- Better SEO: CSS separates content from styling, making it easier for search engines to index your content.
- Responsive Design: Enables responsive layouts that adjust based on screen size and devices.
CSS can be used to change the font, color, spacing, positioning, and layout of HTML elements — making your website visually attractive and user-friendly.
What are the advantages and disadvantages of CSS?
CSS (Cascading Style Sheets) offers many benefits to web developers by simplifying and streamlining the process of designing and maintaining websites. However, it also comes with a few drawbacks developers need to be aware of.
Advantages of CSS:
- Reusability: A single style rule can be reused across multiple HTML elements and pages, reducing duplication and saving time.
- Consistency: Styles can be applied consistently throughout the website, ensuring a unified look and feel.
- Improved Site Speed: Fewer lines of code and external stylesheets can reduce page load times.
- Ease of Maintenance: A single update in the CSS file reflects across the entire site, making it easier to manage and maintain.
- Separation of Concerns: Keeps HTML content clean and separate from presentation logic, which improves readability and manageability.
Disadvantages of CSS:
- Browser Compatibility Issues: Different browsers may interpret CSS rules differently, which can cause inconsistent layouts.
- Security Limitations: CSS lacks built-in security features, and incorrect implementations can lead to exposure of styling logic.
- Maintenance Challenges: While CSS is easy to maintain, large stylesheets may become difficult to manage without proper structure and naming conventions.
- Need for Testing: Any change made to a CSS rule must be tested across all supported browsers and devices to ensure consistent rendering.
What are the different ways to apply CSS to a webpage?
There are three main ways to apply CSS to an HTML webpage, each offering different use cases and flexibility in styling:
- Inline CSS: Styles are applied directly within an HTML element using the
style
attribute.
Example:<p style="color: red;">This is red text.</p> - Internal CSS: CSS rules are written inside a
<style>
tag placed in the<head>
section of the HTML document.
Example:<style>
p { color: blue; }
</style>
<p>This is blue text.</p> - External CSS: CSS is placed in a separate
.css
file and linked using the<link>
tag in the HTML document’s head.
Example:<link rel="stylesheet" href="styles.css">
Which type of CSS holds the highest priority?
When multiple styles are applied to the same HTML element from different sources, CSS follows a specific order of precedence. The priority from highest to lowest is:
- Inline CSS – written directly in the HTML element using the
style
attribute. It overrides all other styles. - Internal/Embedded CSS – defined within a
<style>
tag in the HTML file’s head section. It overrides external styles. - External CSS – defined in separate
.css
files and linked to the HTML. It has the lowest priority.
Example demonstrating priority:
In the above example, even if internal or external styles define a different color, the inline style will take precedence and render the text in red.
How do you specify units in the CSS? What are the different ways to do it?
In CSS, units are used to define lengths, sizes, and spacing for elements such as font size, width, margin, padding, etc. These units determine how content appears on different screens or devices.
Commonly Used CSS Units:
- px (Pixels): Fixed unit that offers precise control over layout. It is not scalable or relative and is best for exact positioning.
- em: Relative to the font size of the parent element. For example, 2em means 2 times the parent’s font size. It is scalable and useful for responsive design.
- rem: Relative to the font size of the root (html) element. It does not cascade like
em
, making it more predictable for layouts. - % (Percentage): Relative to the size of the parent element. Useful for responsive widths and heights.
- pt (Points): Used mostly for print. 1pt = 1/72 of an inch. Not commonly used in web design due to fixed physical sizing.
Example:
h1 { font-size: 2em; } /* 32px */
p { font-size: 75%; } /* 12px if body is 16px */
.box { width: 300px; padding: 1rem; }
What is the Box model in CSS? Which CSS properties are a part of it?
The CSS Box Model is a fundamental concept that describes how every HTML element is structured and how its total size is calculated on the page. Each element is represented as a rectangular box that consists of several layers.
Parts of the Box Model:
- Content: The actual content of the box, such as text or an image.
- Padding: The space between the content and the border. It clears an area around the content.
- Border: A line that surrounds the padding (if any) and content.
- Margin: The outermost space that clears an area outside the border. It separates the element from others.
CSS Properties that are part of the box model:
width
andheight
padding
,padding-top
,padding-right
,padding-bottom
,padding-left
border
,border-width
,border-style
,border-color
margin
,margin-top
,margin-right
,margin-bottom
,margin-left
Visual Example:
width: 200px;
height: 100px;
padding: 20px;
border: 5px solid black;
margin: 10px;
}
What are CSS Selectors?
CSS Selectors are used to select HTML elements based on their element name, id, attributes, etc. They allow you to target one or more elements simultaneously for styling.
Element Selector: Selects HTML elements by their tag name.
element_name { /* CSS Property */ }
ID Selector: Uses the #
character to select an element with a specific ID.
#id_name { /* CSS Property */ }
Class Selector: Uses the .
character to select elements with a specific class.
.class_name { /* CSS Property */ }
What does the 'a' in rgba mean?
In rgba()
, the 'A' stands for Alpha, which specifies the transparency of an element.
The alpha value ranges between 0.0 (fully transparent) and 1.0 (fully opaque).
h1 { color: rgba(R, G, B, A); }
What are CSS HSL Colors?
HSL stands for Hue, Saturation, and Lightness.
- Hue: Degree on the color wheel (0 = red, 120 = green, 240 = blue).
- Saturation: Percentage value (100% = full color, 0% = gray).
- Lightness: Percentage value (100% = white, 0% = black).
h1 { color: hsl(H, S, L); }
What are CSS backgrounds? List the properties.
CSS background properties define the background effects for elements. Common properties include:
background-color
: Sets the background color.background-image
: Sets an image as the background.background-repeat
: Controls repetition of the background image.background-attachment
: Determines if the background scrolls with the page.background-position
: Positions the background image.
What does margin: 40px 100px 120px 80px signify?
CSS margins create space around elements. The shorthand margin: 40px 100px 120px 80px;
means:
- Top margin: 40px
- Right margin: 100px
- Bottom margin: 120px
- Left margin: 80px
What is the difference between margin and padding?
- Margin: Space outside the element's border.
- Padding: Space inside the element's border.
- Margin can have negative values; padding cannot.
- Both can be applied to all four sides of the element.
- Margins can be set to
auto
; padding cannot.
What are the different CSS link states?
A link is a connection from one web page to another. CSS can style links differently depending on their state, using pseudo-classes.
Four states of links:
a:link
— A normal, unvisited link.a:visited
— A link that has been visited by the user.a:hover
— A link when the mouse pointer is over it.a:active
— A link that is currently being clicked.
Can we add an image as a list item marker?
Yes. To add an image as the list item marker in CSS, use the list-style-image
property.
list-style-image: none | url | initial | inherit;
How can we hide an element in CSS?
We can hide an element by setting the display
property to none
. This removes it from the document flow.
display: none;
To show the element again, set the display
property to an appropriate value such as block
.
display: block;
How can CSS be integrated into an HTML page?
There are three ways to integrate CSS into HTML:
- Internal CSS: Using
<style>
tags inside the<head>
section of the HTML file. - Inline CSS: Applying styles directly to an element using the
style
attribute. - External CSS: Writing CSS in a separate file and linking it to the HTML page with the
<link>
tag.
Explain a few advantages of CSS.
- Allows different documents to be controlled using a single stylesheet.
- Supports grouping styles using selectors and grouping methods.
- Multiple HTML elements can share the same class for consistent styling.
What is meant by RGB stream?
RGB stands for Red, Green, Blue and represents colors in CSS. Each stream's intensity is represented by a value from 0 to 256, allowing for a full spectrum of visible colors.
What was the purpose of developing CSS?
CSS was developed to define the visual appearance of websites. It enables developers to separate the structure and content of a site from its design, which was not possible before CSS.
What is the difference between a class and an ID?
- Class: Can be applied to multiple elements and is not unique.
- ID: Unique and can only be assigned to a single element in a page.
Define z-index.
The z-index
property specifies the stack order of elements that overlap. Higher values are displayed above lower values. It can be set to auto, positive, negative, initial, or inherit.
What are the benefits of CSS Sprites?
- Reduces the number of HTTP requests for multiple images.
- Prevents flickering or blinking when images load.
- Loads assets only when needed, improving performance.
How can you target h3 and h2 with the same styling?
Use a comma-separated list of selectors:
h2, h3 { color: red; }
Name media types allowed by CSS.
- speech
- audio
- visual
- tactile
- continuous or paged media
- grid or bitmap
- interactive
How can you use CSS to control image repetition?
Use the background-repeat
property:
h3 { background-repeat: no-repeat; }
Why Choose Our Question Bank?
Get access to expertly crafted answers and comprehensive preparation materials
Complete Collection
Access all 25 carefully curated questions covering every aspect of CSS interviews
Expert Answers
Get detailed, professional answers crafted by industry experts with real-world experience
Instant Access
Start preparing immediately with instant access to all questions and answers after sign-up