CSS Interview Questions(2025)
Question: What is the CSS Grid system?
Answer:
The CSS Grid system is a powerful two-dimensional layout system in CSS that allows you to create complex and flexible web layouts with ease. Unlike Flexbox, which is primarily one-dimensional (working either in rows or columns), CSS Grid enables you to design layouts both in rows and columns simultaneously, providing more control over the placement and alignment of elements within a container.
CSS Grid is ideal for creating web pages with complex structures, such as magazine layouts, dashboards, or responsive grid-based designs, where you need to manage both the horizontal and vertical positioning of items.
Key Features of CSS Grid:
-
Two-Dimensional Layouts:
- CSS Grid enables you to define both rows and columns, giving you complete control over the layout structure.
-
Grid Container and Grid Items:
- The grid container is the parent element, and its children (the grid items) are placed within the grid using grid lines, which define the boundaries of the rows and columns.
-
Explicit and Implicit Grids:
- Explicit Grid: Defined using CSS properties for rows and columns, such as
grid-template-rows
,grid-template-columns
, andgrid-template-areas
. - Implicit Grid: Automatically created if the content exceeds the specified number of rows or columns. You can control the behavior of the implicit grid using
grid-auto-rows
andgrid-auto-columns
.
- Explicit Grid: Defined using CSS properties for rows and columns, such as
-
Grid Lines:
- The grid is composed of grid lines that define the boundaries of the grid. These lines are numbered starting from
1
and can be used to position items within the grid.
- The grid is composed of grid lines that define the boundaries of the grid. These lines are numbered starting from
-
Grid Template Properties:
grid-template-columns
: Defines the column sizes of the grid.grid-template-rows
: Defines the row sizes of the grid.grid-template-areas
: Allows you to define the layout with named grid areas.grid-auto-columns
: Defines the size of columns for implicit grid items.grid-auto-rows
: Defines the size of rows for implicit grid items.
-
Placement of Grid Items:
- Grid Item Placement: Items within the grid can be placed anywhere within the grid by specifying their position using
grid-column
andgrid-row
. - You can also place grid items using
grid-area
, which combines bothgrid-row
andgrid-column
properties.
- Grid Item Placement: Items within the grid can be placed anywhere within the grid by specifying their position using
-
Alignment and Spacing:
justify-items
: Aligns grid items along the horizontal (inline) axis.align-items
: Aligns grid items along the vertical (block) axis.justify-content
: Aligns the entire grid container’s items horizontally.align-content
: Aligns the entire grid container’s items vertically.
-
Responsive Design:
- CSS Grid allows you to create responsive designs that adapt to different screen sizes. You can use media queries in combination with the grid system to rearrange grid items, change the number of columns or rows, or adjust the grid layout based on the viewport size.
Basic Example of CSS Grid:
.container {
display: grid;
grid-template-columns: repeat(3, 1fr); /* Creates 3 equal columns */
grid-template-rows: repeat(2, 100px); /* Creates 2 rows with 100px height */
gap: 10px; /* Sets the gap between grid items */
}
.item {
background-color: #ccc;
padding: 20px;
text-align: center;
}
In this example:
.container
is the grid container, which has 3 columns and 2 rows.- The
repeat(3, 1fr)
function creates 3 equal-width columns, whilerepeat(2, 100px)
creates 2 rows with 100px height. - The
gap
property defines the space between grid items.
Advanced Example of Named Grid Areas:
.container {
display: grid;
grid-template-areas:
"header header header"
"sidebar content content"
"footer footer footer";
gap: 10px;
}
.header {
grid-area: header;
background-color: #f4f4f4;
}
.sidebar {
grid-area: sidebar;
background-color: #ddd;
}
.content {
grid-area: content;
background-color: #bbb;
}
.footer {
grid-area: footer;
background-color: #f4f4f4;
}
In this example:
- The
grid-template-areas
property is used to define a named grid layout. The grid is divided into areas likeheader
,sidebar
,content
, andfooter
. - Each grid item is then assigned to these named areas using the
grid-area
property.
Key Benefits of CSS Grid:
- Simplified Layouts: CSS Grid makes creating complex layouts much simpler by allowing you to define both rows and columns at the same time.
- Flexibility: It offers flexibility to create responsive designs with ease, without needing to rely on
float
orposition
hacks. - Control Over Alignment: CSS Grid allows for precise control over the alignment and spacing of elements both horizontally and vertically.
- Reduces Need for Complex CSS: CSS Grid removes the need for additional wrapper elements or complex positioning to achieve multi-column layouts.
When to Use CSS Grid vs Flexbox:
- CSS Grid is best for two-dimensional layouts, where you need to control both rows and columns.
- Flexbox is ideal for one-dimensional layouts, such as simple row or column-based designs.
Summary:
The CSS Grid system is a robust and flexible layout model that provides you with precise control over both horizontal and vertical element positioning. It is particularly useful for creating complex, responsive grid-based layouts with minimal code. By defining both rows and columns, as well as offering powerful alignment and spacing options, CSS Grid allows developers to build modern, clean, and adaptive web designs.
Question: What are pseudo-classes in CSS?
Answer:
Pseudo-classes in CSS are used to define the special states or conditions of an element that cannot be targeted directly with regular selectors. They allow you to apply styles to elements based on their state, position in the document, user interaction, or other dynamic conditions, such as when a user hovers over a link or focuses on an input field. Pseudo-classes are prefixed with a colon (:
) and can be used in combination with other CSS selectors.
Types of Pseudo-Classes:
-
User Action-based Pseudo-Classes:
:hover
: Applied when the user hovers over an element (commonly used with links, buttons, and other interactive elements).a:hover { color: red; }
:focus
: Applied when an element receives focus, such as an input field or a link.input:focus { border-color: blue; }
:active
: Applied when an element is being activated (for example, when a button is clicked).button:active { background-color: darkgray; }
-
Structural Pseudo-Classes:
:first-child
: Applied to the first child of a parent element.p:first-child { font-weight: bold; }
:last-child
: Applied to the last child of a parent element.p:last-child { color: green; }
:nth-child(n)
: Applied to the nth child element of a parent. You can specify a number or use patterns likeeven
,odd
, or3n
(for every third element).li:nth-child(odd) { background-color: lightgray; } li:nth-child(2n) { font-style: italic; }
:nth-of-type(n)
: Similar to:nth-child()
, but applies only to elements of a specific type.div:nth-of-type(2) { background-color: yellow; }
:first-of-type
: Applies to the first element of a specific type within its parent.p:first-of-type { margin-top: 0; }
-
State-based Pseudo-Classes:
:checked
: Applied to elements that are checked or selected, such as checkboxes, radio buttons, and option elements.input:checked { background-color: green; }
:disabled
: Applied to disabled form elements like buttons, inputs, etc.button:disabled { opacity: 0.5; }
:enabled
: Applied to enabled form elements.input:enabled { border: 1px solid #ccc; }
-
Form Pseudo-Classes:
:valid
: Applied when an input element is valid according to its constraints (such astype="email"
for email validation).input:valid { border-color: green; }
:invalid
: Applied when an input element is invalid.input:invalid { border-color: red; }
:required
: Applied to input elements that have therequired
attribute.input:required { background-color: lightyellow; }
:optional
: Applied to input elements that do not have therequired
attribute.input:optional { background-color: lightblue; }
-
Other Common Pseudo-Classes:
:empty
: Applied to elements that do not have any child nodes (including text nodes).div:empty { display: none; }
:not(selector)
: Applied to elements that do not match a specific selector.p:not(.highlight) { color: gray; }
-
CSS3 Pseudo-Classes:
:root
: Targets the highest-level parent element (<html>
in HTML) and is often used in combination with custom properties (CSS variables).:root { --main-color: blue; }
:target
: Applied to an element when its ID matches the fragment identifier of the URL (e.g.,#section2
).#section2:target { background-color: lightblue; }
Example of Pseudo-Classes in Use:
/* Change color of a link when hovered */
a:hover {
color: red;
}
/* Style the first child of a container */
div:first-child {
font-weight: bold;
}
/* Style the second item in a list */
li:nth-child(2) {
background-color: yellow;
}
/* Style checked checkbox */
input[type="checkbox"]:checked {
background-color: green;
}
/* Style valid form input */
input:valid {
border: 2px solid green;
}
Summary:
Pseudo-classes in CSS provide a way to apply styles based on the state or position of an element, user interactions, or the document structure. They are incredibly useful for creating dynamic, interactive web pages without the need for JavaScript. You can target elements that are in specific conditions (such as hovered or focused), structural positions (such as the first or last child), or form states (such as valid, checked, or disabled).
Read More
If you can’t get enough from this article, Aihirely has plenty more related information, such as CSS interview questions, CSS interview experiences, and details about various CSS job positions. Click here to check it out.
Tags
- CSS
- CSS interview questions
- CSS box model
- CSS positioning
- CSS Flexbox
- CSS Grid
- CSS media queries
- Inline vs block elements
- CSS specificity
- CSS `!important`
- Pseudo classes in CSS
- Pseudo elements in CSS
- CSS `z index`
- CSS `display` property
- CSS `visibility` vs `display`
- CSS `margin` vs `padding`
- CSS `em` vs `rem`
- CSS layout
- Responsive design in CSS
- CSS selectors
- CSS rulesets
- CSS font size units
- CSS external vs embedded styles
- CSS framework
- CSS troubleshooting