CSS interview questions
CSS interview questions
for both applicants preparing for CSS interviews and hiring managers
evaluating candidates' abilities. This post will give you a thorough list of
CSS interview questions, whether you're seeking to brush up on your
CSS skills or prepare for a forthcoming interview. We've got you
covered on everything from fundamentals to advanced topics. Apply now
Let's get started and look at some of the most important CSS interview
questions that can help you succeed in your next CSS endeavor. Apply for the latest remote jobs
Table of contents
Full-Stack Back-End-Heavy…
Basic CSS interview questions (30) FULL-TIME REMOTE JOB
This website utilizes technologies such as cookies to enable essential site functionality, as well as for
analytics, personalization, and targeted advertising purposes. You may change your settings at any time Save
or accept the default settings. You may close this banner to continue with only essential cookies.
BASIC CSS INTERVIEW QUESTIONS
Privacy Policy Accept All
Storage Preferences
Reject All
1. Targeted
What isAdvertising
CSS and whatPersonalization
is its purpose? Analytics
Hide Answer
Modularity: With CSS, you can break courses down into modules
that can be easily reused, combined, and updated
independently of each other.
Hide Answer
Inline CSS: Adding the style directly to an HTML element using the
"style" attribute.
Internal CSS: Placing the CSS code within the < style > tags in the
< head > section of an HTML document.
Hide Answer
To select an element with a specific class in CSS, you use the dot (.)
followed by the class name. For example, to select all elements with
the class "example-class", the CSS selector would be ".example-
class".
4. What is the box model in CSS?
Hide Answer
The Box Model in CSS refers to the concept that organizes and
structures HTML elements on a web page in the form of rectangular
boxes. Every element in a page is comprised of a rectangular box,
which includes content, padding, border, and margin. These
components contribute collectively to the element's dimensions
and positioning.
Hide Answer
Set the left and right margins to "auto" and specify a width for the
element.
Hide Answer
.example {
background-color: red; /* color name */
/* or */
background-color: #ff0000; /* hexadecimal value */
/* or */
background-color: rgb(255, 0, 0); /* RGB value */
/* or */
background-color: hsl(0, 100%, 50%); /* HSL value */
}
Apply the class to an HTML element:
<div class="example">
This element's background color is red.
</div>
Hide Answer
In CSS, padding and margin are properties that control the space
around an element, but they serve different purposes:
Hide Answer
.box {
border: 2px solid red; /* border-width, border-style, and
border-color */
}
Or you can set the properties individually:
.box {
border-width: 2px;
border-style: solid;
border-color: red;
}
Both of these examples will result in a red, solid border with a width
of 2 pixels around an element. To apply the style, add the class to
an HTML element:
Hide Answer
10. How do you apply CSS styles to only the first letter of a
paragraph?
Hide Answer
To apply CSS styles to only the first letter of a paragraph, you can
use the ::first-letter pseudo-element. It allows you to target and
style the first letter of a text element without altering the HTML
structure. Here's an example:
p::first-letter {
font-size: 2em;
font-weight: bold;
color: blue;
}
In this example, the first letter of every < p > (paragraph) element
will be styled with a larger font size (2 times the normal font size),
bold font-weight, and blue color.
Hide Answer
To apply CSS styles inline, you can use the ‘style’ attribute directly
on an HTML element. Inline styles are written as part of the
element's opening tag, and the CSS properties are defined within
the ‘style’ attribute. Here's an example:
Hide Answer
Hide Answer
To change the font size of an element in CSS, you can use the font-
size property. The font-size property can accept various units like px
(pixels), em, rem, % (percentage), and more. Here's an example of
how to set the font size for different elements:
Hide Answer
Hide Answer
To change the text color of an element in CSS, you use the color
property and set it to the desired color. You can use color names,
hexadecimal values, RGB, or HSL values as the color value. Here's an
example of how to set the text color of an element with a class
name "example":
.example {
color: green; /* color name */
/* or */
color: #008000; /* hexadecimal value */
/* or */
color: rgb(0, 128, 0); /* RGB value */
/* or */
color: hsl(120, 100%, 25%); /* HSL value */
}
Apply the class to an HTML element:
<div class="example">
The text color of this element is green.
</div>
Hide Answer
Hide Answer
/* Unvisited link */
a:link {
color: blue;
text-decoration: none;
}
/* Visited link */
a:visited {
color: purple;
}
/* Hover effect */
a:hover {
color: red;
text-decoration: underline;
}
/* Active link */
a:active {
color: orange;
}
19. Explain the difference between px, %, em, and rem units
in CSS.
Hide Answer
em: The em unit is relative to the font size of the element itself or
the nearest parent element with a specified font size. 1em is
equal to the current font size, so if the font-size of the document
is 16px, then 1em will equal 16px.
rem: Similar to em, the rem unit is relative to the font size.
However, rem is always relative to the base (root) font size of the
document, usually defined on the element. This unit provides a
consistent way to scale elements based on the overall font size
of the page, without being affected by the font size of parent
elements.
Hide Answer
To add a background image to an element in CSS, you use the
background-image property. You'll need to provide the URL of the
image file within the url() function as the property's value. Here's an
example:
.selector {
background-image: url('path/to/image.jpg');
Replace .selector with the appropriate CSS selector for the element
you want to apply the background image to, and replace
'path/to/image.jpg' with the actual path to the image file.
Hide Answer
Hide Answer
In CSS, you can set the height and width of an element using the
height and width properties. These properties can be applied to
most elements, except non-replaced inline elements and table
columns.
.my-element {
width: 200px;
height: 100px;
}
In this example, the element with the class my-element has a width
of 200 pixels and a height of 100 pixels. You can use different units,
such as px (pixels), em, rem, % (percentage), and vw/vh (viewport
units), depending on your design requirements.
Hide Answer
Hide Answer
To create a list without bullets in CSS, you need to target the list
element(s) and set the list-style-type property to none. Typically,
you will either work with unordered lists (< ul >) or ordered lists (< ol
>). Here's how you can remove bullets (or numbers) from an
unordered list:
ul {
list-style-type: none;
}
This CSS rule targets all unordered lists (< ul >) in the document and
removes the bullets by setting the list-style-type to none.
Hide Answer
Hide Answer
To align text to the center of an element in CSS, you can use the
text-align property with the value center. This will horizontally
center the text within the specified container element. Here's an
example:
CSS:
.center-text {
text-align: center;
}
HTML:
<div class="center-text">
This text is horizontally centered within the div.
</div>
In this example, the CSS rule targets any element with the class
center-text and applies the text-align: center; styling to it. When
you add the class center-text to an HTML element, the text content
within that element will be horizontally centered.
Hide Answer
Hide Answer
To style all links within a specific container in CSS, you can use the
descendant selector. This allows you to target all the anchor
elements () that are descendants of the container element,
regardless of their nesting level.
.container a {
/* Your styles here */
color: red; /* For example, change the link color to red */
}
In this example, all anchor elements (< a >) inside the .container
element will have the text color set to red. You can replace
.container with other selectors such as #container-id if your
container is identified with an ID instead of a class.
Hide Answer
Hide Answer
HTML:
First, define the HTML structure of the menu using an unordered list
(< ul >) and list items (< li >). Wrap each list item in an anchor tag (<
a >) to create a link:
<nav>
<ul class="navigation-menu">
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></-li>
<li><a href="#services">Services</a>li>
<li><a href="#contact">Contact</a></-ti>
</ul>
</nav>
CSS:
Next, apply CSS styles to convert the vertical list into a horizontal
navigation menu:
.navigation-menu a {
display: block; /* Make the link fill the entire list item
*/
padding: 10px; /* Add padding around the link text */
text-decoration: none; /* Remove the default link underline
*/
color: #000; /* Set link color */
}
Hide Answer
The more specific a selector is, the higher its specificity value. When
multiple conflicting rules target the same element, the one with the
highest specificity will take precedence.
Hide Answer
Hide Answer
Hide Answer
Both display: none; and visibility: hidden; are CSS properties used to
hide elements on a webpage, but they do so in different ways:
Hide Answer
Hide Answer
Hide Answer
em: The em unit represents the size relative to the font-size of the
current element. In other words, if you set the font size to 2em, it
means the font size will be 2 times the computed size of the font of
the current element. When applied to elements other than font-
size, em is still calculated based on the font-size of the current
element.
rem: The rem unit represents the size relative to the font-size of the
root element (). This means when you set a font size to 2rem, it is 2
times the font size defined in the root element, regardless of the
current element's parent styles.
Hide Answer
Hide Answer
The CSS float property is used to make an element float within its
parent container, allowing other content to wrap around it. The float
property accepts values like "left" or "right" to determine the
direction of the float.
Hide Answer
Hide Answer
To apply CSS styles to only the last child of an element, you can use
the ":last-child" pseudo-class selector. For example:
.parent-element :last-child {
Hide Answer
CSS vendor prefixes are used to add support for specific CSS
features in different web browsers. They are used before the
standard property name and are specific to certain browser
engines. Here are examples of some common CSS vendor prefixes:
.element {
-webkit-animation: myAnimation 1s;
-moz-animation: myAnimation 1s;
animation: myAnimation 1s;
}
Hide Answer
Hide Answer
Hide Answer
Use CSS Grid and the align-self property to vertically center the
element within the grid container.
16. What is the CSS box-sizing property used for and what
are its different values?
Hide Answer
The CSS box-sizing property is used to control how the total width
and height of an element are calculated. It has three possible
values:
To create a fixed footer that stays at the bottom of the page in CSS,
you can use the following approach:
Hide Answer
Hide Answer
Use a nested HTML structure with lists (ul and li) to represent the
dropdown menu items.
Hide Answer
Hide Answer
Hide Answer
23. How do you create a sticky sidebar that scrolls with the
content in CSS?
Hide Answer
To create a sticky sidebar that scrolls with the content in CSS, you
can use the following approach:
Apply "position: sticky;" to the sidebar element.
Set "top: 0;" or "bottom: 0;" to define where the sticky element
should stick within its containing block.
24. Explain the concept of the CSS calc() function and how
it can be used.
Hide Answer
Hide Answer
To create a print-friendly CSS style sheet for a web page, you can
use media queries specifically targeting print media. Here are
some considerations:
Hide Answer
Use the @font-face rule to define a custom font and specify its
source (either hosted on a server or provided as a local file).
Link to external font files using the tag, specifying the font type
and source URL.
Utilize web font services like Google Fonts or Adobe Fonts which
provide easy-to-use methods for including custom fonts on a
web page.
Hide Answer
.tooltip {
position: relative;
}
.tooltip:hover::after {
content: "Additional information";
position: absolute;
background-color: #333;
color: #fff;
padding: 5px;
border-radius: 3px;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
}
Hide Answer
a:hover {
color: red;
}
:focus: This pseudo-class targets an element when it receives
focus, usually by clicking or navigating with the Tab key. It's
commonly used to provide visual indication for form inputs or
interactive elements.
input:focus {
outline: none;
border-color: blue;
}
:active: This pseudo-class targets an element when it's being
activated (pressed or clicked). It's used to give the effect of user
interaction, such as buttons being pressed.
button:active {
background-color: darkgray;
}
:nth-child(n): This pseudo-class targets elements based on their
position within their parent container. n can be a number, a
keyword, or an equation, allowing for flexible targeting. It's
commonly used to style every odd/even element or apply
distinct styles at certain positions.
li:nth-child(odd) {
background-color: lightgray;
}
li:nth-child(2n) {
font-weight: bold;
}
Hide Answer
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px,
1fr));
gap: 20px;
}
This creates a grid container with a minimum column width of 250
pixels and allows the columns to automatically adjust based on
available space. The "1fr" unit ensures the columns take up equal
space. The "gap" property adds spacing between the columns.
Hide Answer
CSS custom properties, also known as variables, allow you to define
reusable values that can be used throughout your CSS stylesheets.
They are declared using the -- prefix and can be assigned values
using the var() function. For example, to define a custom property:
:root {
--primary-color: #ff0000;
}
And to use it:
.element {
color: var(--primary-color);
}
Hide Answer
Hide Answer
Hide Answer
The CSS transform property is used to apply transformations to an
element, such as rotation, scaling, skewing, or translating. Here are
some examples:
.rotate {
transform: rotate(45deg);
}
.scale {
transform: scale(1.5);
}
.skew {
transform: skew(20deg, -10deg);
}
.translate {
transform: translate(50px, 20px);
}
Hide Answer
.element {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
This positions the element at 50% from the top and left edges of its
containing element and then uses the translate() function to move
it back by 50% of its width and height, effectively centering it.
35. What are CSS counters and how can they be used?
Hide Answer
body {
counter-reset: section;
}
h2::before {
counter-increment: section;
content: "Section " counter(section) ": ";
}
This example resets a counter on the body element and increments
it for each h2 element. The content property is then used to display
the counter value before each h2 element.
Hide Answer
Hide Answer
<ul class="menu">
<li>
<a href="#">Menu 1</a>
<ul>
<li><a href="#">Submenu 1</a></li>
<li><a href="#">Submenu 2</a></li>
</ul>
</li>
<li><a href="#">Menu 2</a></li>
</ul>
CSS
.menu ul {
display: none;
}
.menu li:hover > ul {
display: block;
}
This example hides the submenus by default (display: none) and
displays them when hovering over their parent menu item (li:hover
> ul { display: block; }).
38. What are the CSS blend modes and how do you use
them?
Hide Answer
CSS blend modes allow you to define how elements blend with their
background or other elements. They provide various blending
options such as multiply, screen, overlay, and more. Blend modes
are set using the mix-blend-mode property. Here's an example:
cssCopy code
.element {
mix-blend-mode: multiply;
}
In this example, the element will blend with its background using
the multiply blend mode.
Hide Answer
<div class="video-wrapper">
<video controls>
<source src="video.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</div>
CSS
.video-wrapper {
position: relative;
padding-bottom: 56.25%; /* 16:9 aspect ratio */
overflow: hidden;
}
.video-wrapper video {
position: absolute;
width: 100%;
height: 100%;
}
This example sets the aspect ratio of the video wrapper using the
padding-bottom technique and makes the video element fill the
wrapper.
Hide Answer
The CSS box-sizing property is used to control how the width and
height of an element are calculated, including its padding and
border. The default value is content-box, which means the width
and height only include the content and exclude the padding and
border. However, when box-sizing is set to border-box, the width
and height of the element will include the padding and border.
Here's an example:
.box {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 1px solid black;
}
In the above example, the total width of the .box element would be
200px, including the padding and border. Without box-sizing:
border-box, the total width would be 242px (200px width + 20px left
padding + 20px right padding + 1px left border + 1px right border).
Hide Answer
/* CSS Animation */
@keyframes slide-in {
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(0);
}
}
.element {
animation: slide-in 1s ease-in-out;
}
/* CSS Transition */
.element {
transition: width 0.3s ease-in-out;
}
.element:hover {
width: 200px;
}
In the above example, the CSS animation slide-in is applied to the
.element class which animates the element to slide in from the left
side. The CSS transition is applied to the .element class as well,
causing the width of the element to smoothly transition to 200px
when hovered.
Hide Answer
Stylus
$primary-color: #ff0000;
$secondary-color: #00ff00;
.element {
color: $primary-color;
background-color: $secondary-color;
}
In the above example, Sass variables $primary-color and
$secondary-color are used to define colors, which can be easily
reused throughout the codebase.
Hide Answer
.container {
column-count: 3;
column-gap: 20px;
}
In the above example, the .container class creates a three-column
layout with a gap of 20 pixels between each column. The content
inside the container will automatically flow into multiple columns
based on the available space.
5. Explain the concept of CSS specificity and inheritance.
Hide Answer
CSS specificity refers to the set of rules that determines which CSS
styles should be applied to an element when multiple styles are
defined. It is a way of resolving conflicts when different selectors
target the same element.
For example, in the selector h1.title, the class component (.title) has
a higher specificity than the element type (h1).
Hide Answer
Remove unused CSS to reduce the file size and improve loading
speed.
Avoid using complex selectors that can slow down the rendering
process.
7. What are CSS media queries and how are they used?
Hide Answer
CSS media queries are used to apply different CSS styles based on
the characteristics of the device or viewport. They allow you to
create responsive designs that adapt to different screen sizes and
orientations.
Hide Answer
static: The default value. The element follows the normal flow of
the document.
Hide Answer
Use CSS vendor prefixes for properties that are not fully supported
across different browsers. For example, -webkit- prefix for WebKit-
based browsers like Chrome and Safari, -moz- prefix for Mozilla
Firefox, etc. However, it's recommended to use autoprefixer tools or
CSS preprocessors that automatically add necessary prefixes
based on your browser support configuration.
Test your CSS on different browsers and devices to identify and fix
any rendering issues.
Use feature detection libraries like Modernizr to check for specific
CSS features and apply fallback styles or alternative solutions for
unsupported browsers.
These practices can help ensure that your CSS styles are rendered
consistently across various browsers and devices.
Hide Answer
Hide Answer
Modularity: With preprocessors, you can split your CSS code into
modular files and import them as needed, which makes it easier to
manage large codebases.
12. What are the CSS units vw and vh used for and how do
they work?
Hide Answer
Hide Answer
@keyframes example-animation {
0% {
background-color: red;
transform: translateY(0);
}
50% {
background-color: blue;
transform: translateY(50px);
}
100% {
background-color: green;
transform: translateY(0);
}
}
@keyframes example-animation {
0% {
background-color: red;
transform: translateY(0);
}
50% {
background-color: blue;
transform: translateY(50px);
}
100% {
background-color: green;
transform: translateY(0);
}
}
Apply the animation to an element: Use the animation shorthand
property or its sub-properties (e.g., animation-name, animation-
duration, animation-timing-function, etc.) to apply the custom
animation to an element.
.my-element {
animation: example-animation 2s ease-in-out infinite;
}
In this example, we apply the example-animation keyframe
animation we defined earlier to the element with the class .my-
element. The animation duration is set to 2 seconds, the timing
function is ease-in-out, and infinite makes the animation loop
indefinitely.
Hide Answer
Hide Answer
Hide Answer
Hide Answer
Hide Answer
You can use the transition shorthand property to define all of these
properties in a single declaration.
HTML:
.my-button {
background-color: blue;
color: white;
font-size: 1em;
padding: 0.5em 1em;
border: none;
border-radius: 4px;
cursor: pointer;
.my-button:hover {
background-color: darkblue;
color: lightgray;
}
In this example, we have a button with a class .my-button. We
apply a CSS transition that animates the background-color and
color properties over 0.3s using the ease timing function. When the
button is hovered with the mouse cursor, the background-color
and color properties change smoothly over the specified transition
duration.
Hide Answer
HTML:
<!DOCTYPE html>
<html>
<head>
<!-- Add the meta viewport tag to ensure responsiveness
on mobile devices -->
<meta name="viewport" content="width=device-width,
initial-scale=1">
</head>
<body>
<div class="page-container">
<div class="content-wrap">
<!-- Your main content goes here -->
<h1>Responsive Sticky Footer</h1>
</div>
<footer>
<!-- Footer content goes here -->
<p>Copyright © 2023 Your Site</p>
</footer>
</div>
</body>
</html>
CSS:
File organization
CSS methodologies
Modular CSS
CSS frameworks
It's also critical to grasp how to write flawless CSS code. Ensuring
accurate syntax, consistent naming conventions, efficient selectors,
and optimized styles can all add significantly to the CSS
codebase's overall efficiency and quality.
Hide Answer
You can hide the content by setting the initial height of the
containers to 0 and applying overflow: hidden;. When the
associated checkbox is checked, the container's height is set to
auto with a smooth transition, revealing the content.
Hide Answer
.parallax-section {
background-image: url(background.jpg);
background-attachment: fixed;
background-position: center;
background-size: cover;
}
In this example, the background image is set to fixed attachment,
and the background-position is adjusted to create the parallax
scrolling effect.
Hide Answer
Hide Answer
HTML code
<div class="masonry-container">
<div class="masonry-item">Item 1</div>
<div class="masonry-item">Item 2</div>
<div class="masonry-item">Item 3</div>
<!-- more items -->
</div>
CSS Code
.masonry-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px,
1fr));
grid-auto-rows: 10px;
grid-gap: 10px;
}
.masonry-item {
grid-row-end: span 2; /* adjust to desired height */
}
In this example, the .masonry-container is set to a grid layout with
auto-fit columns and a minimum width of 250px. The .masonry-
item elements are then placed within the grid and their heights can
be adjusted using grid-row-end to span multiple rows.
26. Explain the concept of CSS grid-template-areas and
provide an example of usage.
Hide Answer
.container {
display: grid;
grid-template-areas:
"header header"
"sidebar content"
"footer footer";
}
.header {
grid-area: header;
}
.sidebar {
grid-area: sidebar;
}
.content {
grid-area: content;
}
.footer {
grid-area: footer;
}
In this example, the grid-template-areas property defines a grid
layout with named areas for the header, sidebar, content, and
footer. The individual grid items are then assigned to their
respective areas using the grid-area property.
Hide Answer
To create a responsive timeline using CSS and HTML, you can use a
combination of HTML lists and CSS styles. Here's a basic example:
<ul class="timeline">
<li>
<div class="timeline-date">January 2023</div>
<div class="timeline-content">Event description</div>
</li>
<li>
<div class="timeline-date">February 2023</div>
<div class="timeline-content">Event description</div>
</li>
<!-- more timeline items -->
</ul>
CSS
.timeline {
list-style: none;
padding: 0;
margin: 0;
}
.timeline li {
display: flex;
margin-bottom: 20px;
}
.timeline-date {
width: 100px;
font-weight: bold;
}
.timeline-content {
flex: 1;
}
In this example, the timeline items are structured using an
unordered list (< ul >) and list items (< li >). The timeline items are
displayed in a flex container to align the date and content sections.
The CSS styles can be adjusted according to the desired design.
Hide Answer
Sass and Less are both CSS preprocessors that extend the
functionality of CSS. Here are some differences between them:
Hide Answer
To create a responsive modal dialog using CSS and HTML, you can
use a combination of HTML structure and CSS styles along with
JavaScript to toggle its visibility. Here's a basic example:
<div class="modal">
<div class="modal-content">
<h2>Modal Title</h2>
<p>Modal content goes here.</p>
<button class="modal-close">Close</button>
</div>
</div>
.modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: none;
justify-content: center;
align-items: center;
}
.modal-content {
background-color: #fff;
padding: 20px;
}
.modal-close {
position: absolute;
top: 10px;
right: 10px;
}
In this example, the modal is positioned fixed to cover the entire
viewport. The modal content is centered using flexbox. The display:
none property is used to hide the modal by default, and JavaScript
can be used to toggle its visibility on button click.
Hide Answer
Hide Answer
To create a CSS-only sticky table header, you can use the CSS
position: sticky property along with appropriate styles. Here's an
example:
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
</thead>
<tbody>
<!-- table rows -->
</tbody>
</table>
cssCopy code
thead th {
position: sticky;
top: 0;
background-color: #fff;
}
In this example, the table header (< thead >) is set to position: sticky
with top: 0 to keep it fixed at the top of the table. The background-
color property can be used to style the sticky header.
WRAPPING UP
These CSS interview questions are a valuable asset for both applicants
and hiring managers. Candidates can use these questions as a
guideline to adequately prepare for CSS-related interviews, ensuring
they effectively exhibit their knowledge and abilities. Hiring managers,
on the other hand, can employ these questions to assess candidates'
CSS competency and find the top CSS developers for their teams.
If you're a hiring manager looking for top CSS developers and don't
want to go down the tedious route of recruiting, and wasting resources
and time, Turing has you covered. Our AI-powered talent cloud
enables Silicon Valley organizations to hire top developers from across
the world in just a few clicks.
Job description templates → CSS developer resume tips →
Learn how to write a clear and comprehensive Turing.com lists out the do’s and don’ts behind a
job description to attract highly skilled CSS great resume to help you find a top CSS
developers to your organization. developer job.
Search skills... |
PHP
Hire Developers
Help center
On-demand
talent
Technical
professionals and
teams