CSS Questions
CSS Questions
Easy Level:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<!-- Content goes here -->
</body>
</html>
p {
color: blue;
}
div {
width: 200px;
margin: 0 auto;
}
CSS Questions 1
div {
margin: 10px;
padding: 10px;
}
#myElement {
/* Styles for the element with id "myElement" */
}
.myClass {
/* Styles for elements with class "myClass" */
}
img {
float: left;
}
div {
width: 200px;
CSS Questions 2
padding: 10px;
border: 1px solid black;
margin: 20px;
}
Medium Level:
11. Q: How do you apply CSS styles only to the first child of an element?
A: Use the :first-child pseudo-class to target the first child element.
ul li:first-child {
font-weight: bold;
}
1. Q: Explain the difference between display: block , display: inline , and display:
inline-block .
.block-element {
display: block;
}
.inline-element {
display: inline;
}
.inline-block-element {
display: inline-block;
}
1. Q: How can you create a responsive layout in CSS without using frameworks?
A: Use media queries to apply different styles based on the device's screen size.
.container {
width: 100%;
}
CSS Questions 3
@media (max-width: 400px) {
.container {
width: 80%;
}
}
1. Q: What are media queries in CSS, and how do you use them for responsive
design?
A: Media queries allow you to apply different styles based on the device's
characteristics such as screen size, resolution, or orientation.
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.fade-in-element {
animation: fadeIn 2s ease-in-out;
}
CSS Questions 4
1. Q: What is the purpose of the z-index property in CSS?
A: The z-index property determines the stacking order of positioned elements.
Elements with a higher z-index value appear on top of elements with a lower
value.
div {
position: relative;
z-index: 2;
}
span {
position: absolute;
z-index: 1;
}
1. Q: How do you apply different styles based on the device's screen size using
CSS?
A: Use media queries to apply specific styles based on the screen size or device
characteristics.
/* Specificity: 0-0-1 */
p {
CSS Questions 5
color: red;
}
/* Specificity: 0-1-0 */
.container p {
color: blue;
}
/* Specificity: 1-0-0 */
#myElement {
color: green;
}
/* Specificity: 0-1-1 */
.container #myElement {
color: orange;
}
: center;
}
p {
display: table-cell;
vertical-align: middle;
}
CSS Questions 6
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 100px 200px;
}
.item {
/* Styles for grid items */
}
Hard Level:
21. Q: What are CSS preprocessors, and what are some popular ones?
A: CSS preprocessors are scripting languages that extend the capabilities of CSS by
adding variables, functions, and reusable code. Some popular CSS preprocessors
are Sass, Less, and Stylus.
.button {
background-color: $primary-color;
}
:root {
--primary-color: #007bff;
}
.button {
background-color: var(--primary-color);
}
/* Relative positioning */
.relative-element {
CSS Questions 7
position: relative;
top: 20px;
left: 10px;
}
/* Absolute positioning */
.absolute-element {
position: absolute;
top: 50px;
left: 0;
}
/* Fixed positioning */
.fixed-element {
position: fixed;
top: 0;
right: 0;
}
1. Q: How can you create a sticky/fixed header in CSS that stays at the top of the
page while scrolling?
A: Use position: fixed and top: 0 to create a sticky header that stays at the top
of the page.
header {
position: fixed;
top: 0;
left: 0;
width: 100%;
background-color: #fff;
z-index: 999;
}
1. Q: What are CSS transitions, and how do you use them for smooth animations?
A: CSS transitions allow you to animate the changes of CSS properties over a
specified duration. Use the transition property to specify the properties and
duration of the animation.
.button {
background-color: #007bff;
transition: background-color 0.3s ease-in-out;
}
.button:hover {
background-color: #ff0000;
}
CSS Questions 8
1. Q: How can you implement a responsive font size that adjusts based on the
device's screen size?
A: Use the vw unit to set the font size based on the viewport width.
h1 {
font-size: 4vw;
}
p {
font-size: 2vw;
}
1. Q: Explain the concept of CSS specificity and the different methods to increase
or decrease specificity.
A: CSS specificity determines which styles take precedence when multiple rules
target the same element. Specificity is calculated based on the number of IDs,
classes, and element selectors in the rule. To increase specificity, use more
specific selectors (e.g., IDs over classes). To decrease specificity, use less
specific selectors (e.g., classes over IDs).
/* Specificity: 0-0-1 */
p {
color: red;
}
/* Specificity: 0-1-0 */
.container p {
color: blue;
}
/* Specificity: 1-0-0 */
#myElement {
color: green;
}
/* Specificity: 0-1-1 */
.container #myElement {
color: orange;
}
1. Q: How do you implement a CSS flexbox layout, and what are its main
properties?
A: Use the display: flex property on the container to create a flexbox layout.
The main properties of flexbox are flex-direction , flex-wrap , flex-flow , justify-
content , align-items , and align-content .
CSS Questions 9
.container {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
/* main.css */
@import url('variables.css');
body {
background-color: var(--bg-color);
}
1. Q: How can you create a CSS dropdown menu without using JavaScript?
A: Use the :hover pseudo-class to display the dropdown menu when the parent
element is hovered.
.dropdown-content {
display: none;
position: absolute;
top: 100%;
left: 0;
}
CSS Questions 10
/* Select every second element among all children */
div:nth-child(2n) {
/* Styles */
}
1. Q: How do you create CSS custom properties that can be used for dark mode or
theming?
A: Use CSS custom properties to define the theme colors, and then use
JavaScript to toggle between themes or use media queries to implement dark
mode.
:root {
--primary-color: #007bff;
--background-color: #ffffff;
}
body {
background-color: var(--background-color);
color: var(--text-color);
}
.dark-mode {
--background-color: #111111;
--text-color: #ffffff;
}
1. Q: Explain the concept of CSS custom selectors (Level 4 Selectors) and how
they can be useful.
A: CSS custom selectors allow you to create your own reusable selectors with
custom logic. They are prefixed with :-- and can be useful for applying styles to
specific groups of elements or for complex selections.
1. Q: How can you create a CSS-only modal dialog that does not use JavaScript?
A: Use the :target pseudo-class to style a modal dialog that becomes visible
CSS Questions 11
when the target is activated by a URL fragment identifier.
">Open Modal</a>
<div id="myModal">
<!-- Modal content goes here -->
</div>
/* CSS */
#myModal {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
}
#myModal:target {
display: block;
}
.container {
column-count: 3;
}
1. Q: What are CSS grid-template-areas and how can they be used to create
complex layouts?
A: grid-template-areas is a property of CSS Grid Layout that allows you to define
named grid areas. You can use them to create complex and responsive layouts
easily.
.container {
display: grid;
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
}
CSS Questions 12
.header {
grid-area: header;
}
.sidebar {
grid-area: sidebar;
}
.main {
grid-area: main;
}
.footer {
grid-area: footer;
}
1. Q: How can you implement a CSS-only "hamburger" menu icon using CSS for
mobile navigation?
A: Use the :before and :after pseudo-elements to create the hamburger icon,
and use the :checked pseudo-class with an input checkbox to toggle the menu
visibility.
/* CSS */
.menu-icon {
display: block;
width: 30px;
height: 3px;
background-color: #000;
position: relative;
cursor: pointer;
}
.menu-icon:before,
.menu-icon:after {
content: '';
display: block;
width: 30px;
height: 3px;
background-color: #000;
position: absolute;
left: 0;
}
CSS Questions 13
.menu-icon:before {
top: -10px;
}
.menu-icon:after {
top: 10px;
}
1. Q: Explain the concept of stacking contexts in CSS and how they affect the
rendering of elements.
A: Stacking contexts determine the order in which elements are displayed on top
of each other. Elements with a higher z-index value or positioned with position:
absolute or position: fixed are displayed on top of other elements in the same
stacking context. Child elements create new stacking contexts relative to their
parent.
/* Stacking context 1 */
.parent {
position: relative;
z-index: 1;
}
/* Stacking context 2 */
.child {
position: absolute;
top: 0;
left: 0;
z-index: 2;
}
CSS Questions 14
<div class="slide" style="background-image: url(image2.jpg);"></div>
<div class="slide" style="background-image: url(image3.jpg);"></div>
</div>
/* CSS */
.slider {
display: flex;
overflow: hidden;
}
.slide {
width: 100%;
height: 300px;
animation: slide 10s infinite;
}
@keyframes slide {
0%, 100% {
transform: translateX(0);
}
25% {
transform: translateX(-100%);
}
50% {
transform: translateX(-200%);
}
75% {
transform: translateX(-300%);
}
}
1. Q: What are the benefits and drawbacks of using CSS frameworks like
Bootstrap?
A: Benefits of using CSS frameworks like Bootstrap include faster development,
consistent styling, and responsive design. However, they may also add
unnecessary CSS code, increase file size, and limit customization flexibility.
CSS Questions 15
Expert Level:
41. Q: Explain the concept of CSS-in-JS, and what are some popular libraries for
implementing it?
A: CSS-in-JS is an approach where CSS is written using JavaScript instead of
separate CSS files. Popular libraries for implementing CSS-in-JS are styled-
components, Emotion, and CSS Modules.
/* CSS */
.scroll-to-top {
display: none;
position: fixed;
bottom: 20px;
right: 20px;
padding: 10px;
background-color: blue;
color: white;
}
.scroll-to-top:target {
display: block;
}
1. Q: What are the different methods for handling CSS specificity conflicts in large
projects?
CSS Questions 16
A: Some methods to handle CSS specificity conflicts in large projects include
using BEM (Block Element Modifier) methodology, using CSS custom properties
to reduce reliance on specific class names, and avoiding excessive nesting in
CSS
.block {
color: var(--primary-color);
}
1. Q: How can you implement CSS modules or scoped CSS to avoid global style
conflicts?
A: Use CSS modules or scoped CSS to scope the CSS styles to a specific
component or module, avoiding conflicts with other parts of the application.
// Component.js
import styles from './styles.module.css';
function Component() {
return <div className={styles.container}>Content goes here</div>;
}
1. Q: How do you create a custom CSS theme switcher that allows users to change
the color scheme of a website?
CSS Questions 17
A: Use CSS custom properties (variables) to define the theme colors, and use
JavaScript to toggle the theme by changing the custom property values.
:root {
--primary-color: blue;
}
body {
background-color: var(--background-color);
color: var(--text-color);
}
// JavaScript
const toggleButton = document.getElementById('toggle-theme');
toggleButton.addEventListener('click', () => {
document.documentElement.classList.toggle('dark-mode');
});
/* Transform example */
.box {
transform: translateX(50px);
}
/* Transition example */
.box {
transition: background-color 0.3s ease-in-out;
}
.box:hover {
background-color: blue;
}
1. Q: How can you implement CSS grid fallbacks for browsers that do not support
grid layouts?
A: Use feature queries ( @supports rule) to provide fallback styles for browsers
that do not support CSS grid.
CSS Questions 18
/* Default styles for all browsers */
.container {
display: block;
width: 100%;
}
/* CSS */
.tooltip {
position: relative;
display: inline-block;
}
.tooltip .tooltip-text {
display: none;
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
background-color: black;
color: white;
padding: 5px;
}
.tooltip:hover .tooltip-text {
display: block;
}
1. Q: Explain the concept of CSS variable scoping and how it differs from regular
CSS properties.
A: CSS variables, or custom properties, are scoped to the element on which they
CSS Questions 19
are defined. If a variable is not defined on an element, the browser will look for it
in the parent element. Regular CSS properties, on the other hand, cascade
down the DOM tree.
/* CSS */
.parent {
--color: blue;
}
.child {
color: var(--color); /* The value of --color is inherited from the parent */
}
1. Q: How do you optimize CSS performance for large websites to reduce loading
times?
A: Optimize CSS performance by minimizing the number of HTTP requests,
using CSS minification, leveraging browser caching, and implementing critical
CSS to load essential styles first.
CSS Questions 20