.
and
.Show Worth | Begins New Line? | Width/Peak Settable? | Instance Parts |
---|---|---|---|
block |
Sure | Sure |
|
inline |
No | No | , ,
|
inline-block |
No | Sure | Customized buttons, pictures, icons |
box-sizing: border-box
do? (Medium)
6. What does By default, CSS makes use of the content-box mannequin, which implies that width and top solely apply to the content material, excluding padding and border. box-sizing: border-box
modifications this in order that width and top embrace the padding and border, making sizing extra predictable.

Right here’s an instance of how that could be demonstrated in CSS:
/* Apply border-box sizing to all parts and their pseudo-elements */
*,
*::earlier than,
*::after {
box-sizing: border-box; /* Width and top now embrace padding and border */
}
/* Demo: With out border-box (the default, content-box) */
.box-content {
box-sizing: content-box;
width: 200px;
padding: 20px;
border: 4px stable #2563eb;
background: #f0f4ff;
margin-bottom: 16px;
/* The true rendered width might be: 200px (content material) + 40px (padding) + 8px (border) = 248px */
}
/* Demo: With border-box */
.box-border {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 4px stable #16a34a;
background: #e7faed;
/* The rendered width might be precisely 200px, since padding and border are included within the width */
}
With border-box
, you keep away from the basic difficulty the place including padding or a border makes your containers overflow their dad or mum or break your structure. It’s now a regular finest apply. You’ll be able to even say that Chris Coyier has deemed February 1 “Worldwide box-sizing
Consciousness Day” which completely must be an actual factor.
7. How would you go about making a picture responsive in CSS? (Medium)
This can be a deceptively laborious query as a result of responsive pictures is a subject large enough for a whole information. The basic method is to make sure that photographs by no means exceed the width of their container. For many instances, meaning setting a max-width
on the picture factor and guaranteeing it maintains it’s proportions:
/* 1. Make pictures aware of their container width */
img {
max-width: 100%; /* Prevents the picture from overflowing its dad or mum */
top: auto; /* Maintains facet ratio */
show: block; /* Removes backside whitespace that inline pictures have */
}
For pictures that want to take care of a selected facet ratio (like a 16:9 video thumbnail), you should utilize the padding-bottom
trick:
/* 2. Preserve a selected facet ratio (e.g., 16:9) utilizing the padding-bottom trick */
.responsive-img-container {
place: relative; /* Wanted for completely positioning the img */
width: 100%; /* Full width of the dad or mum container */
padding-bottom: 56.25%; /* 16:9 facet ratio (9/16 = 0.5625) */
overflow: hidden; /* Ensures picture doesn’t overflow container */
}
.responsive-img-container img {
place: absolute; /* Take the picture out of the traditional circulate */
prime: 0;
left: 0;
width: 100%; /* Stretch to fill container */
top: 100%; /* Stretch to fill container */
object-fit: cowl; /* Make sure the picture covers the realm, cropping if wanted */
}
Fashionable CSS additionally has the aspect-ratio
property for this:
/* 3. Use the aspect-ratio property for a cleaner method (trendy browsers) */
.aspect-ratio-img {
aspect-ratio: 16 / 9; /* Preserve 16:9 ratio robotically */
width: 100%;
top: auto;
show: block;
}
Responsive pictures usually use the HTML srcset
attribute and the
factor as properly for high-DPI and numerous display screen sizes. There’s a whole CSS-Methods information on these options. And, in fact, there are efficiency issues to take note of as a result of the objective is to serve the perfect picture format and measurement to the fitting machine.
8. How would you make CSS extra performant? (Onerous)
CSS efficiency is essential for delivering quick and clean experiences, particularly on large-scale web sites or functions. Poor CSS practices can decelerate web page masses, improve rendering instances, and make upkeep more durable. There are a number of methods you should utilize to maintain your CSS environment friendly and your web site responsive.
On the identical time, CSS is commonly not the supply of efficiency bottlenecks. It definitely contributes to it, however efficiency is a extra nuanced area the place many elements most definitely affect efficiency than CSS.
1. Decrease your bundle measurement
Massive CSS recordsdata decelerate preliminary web page masses. Eradicating unused CSS (additionally referred to as “useless code elimination”) can considerably cut back file measurement. Instruments like PurgeCSS, UnCSS, or the built-in options of frameworks like Subsequent.js and Tailwind can scan your HTML/JSX and solely maintain kinds which are used.
2. Break up and lazy-load CSS
As an alternative of delivery all CSS directly, cut up your kinds by web page or function (“code splitting”). Fashionable bundlers (similar to webpack and Vite) and frameworks (like React, Vue, and Subsequent.js) help the dynamic import()
function, permitting solely the CSS required for the present route or part to be loaded.
// Dynamically load kinds when the web page masses
import("./kinds/web page.css");
This method improves “first paint” instances and reduces bandwidth, particularly for customers who by no means go to sure pages.
3. Use easy, shallow selectors
Browsers learn CSS from proper to left and consider deeply nested or advanced selectors extra slowly. For finest efficiency, use flat selectors like .btn
as an alternative of one thing like .header nav ul li a.energetic
.
4. Minify and compress CSS
Earlier than deploying, all the time minify your CSS utilizing instruments like cssnano or clean-css. Gzip or Brotli compression (dealt with by your server or CDN) will additional shrink the payload despatched to customers.
5. Use vital CSS (or not!)
Essential CSS refers to inlining the minimal CSS required for above-the-fold content material within the preliminary HTML. This enables the browser to render the seen a part of the web page instantly, whereas loading the remainder of the CSS asynchronously.
I’d say it is a nice-to-have form of factor, as it’s a fragile and tough technique to implement and preserve.
6. Scale back using costly properties
Particular CSS properties, similar to heavy field shadows, filters, or animations on important parts, may cause “repaints” and decelerate rendering. Use these results thoughtfully, and like rework and opacity for animating parts — the browser’s compositor can usually optimize these.
!necessary
and overly particular selectors
7. Keep away from Frequent use of !necessary
and specific selectors could make your CSS laborious to override and debug, resulting in extra duplicated or conflicting guidelines.
8. Optimize unused CSS
Let’s face it. As a web site is iterated, CSS usually turns into bigger, not smaller. Types that have been related at one level are outdated by new ones with out absolutely changing the older kinds, usually for worry of introducing surprising modifications in unknown locations.
We have now heaps and plenty of instruments for detecting and eradicating unused CSS. There are limitations and doable trade-offs, in fact, so your mileage might range.
There there’s the case of UI kits or part libraries that import quite a few unused kinds. It’s simple (and perhaps even tempting) to make use of all the kinds offered by a framework, however strive importing solely what you want, or use tree-shaking to strip unused elements. Many frameworks mean you can configure precisely what you want, like Bootstrap does.
10. Audit CSS recurrently
Fashionable browser DevTools (like Chrome’s Protection tab, Efficiency panel, and Rendering panel) allow you to see which kinds are used on a web page, serving to you determine and take away useless code.
There are on-line instruments as properly, just like the Specificity Visualizer, CSS Specificity Graph Generator, and CSS Stats. You could find extra info on these and extra in “Instruments for Auditing CSS”.
9. What are the professionals and cons of CSS-in-JS vs. exterior CSS imports, and which might you select? (Onerous)
CSS-in-JS might not be the new matter it was a number of years go, however you’re nonetheless very more likely to see it pop up in an interview. It’s not a lot your obligation to rail for or in opposition to it, however exhibit your understanding of the idea and the way it compares to exterior CSS imports.
Right here’s how I’d break it out.
CSS-in-JS (like styled-components, Emotion, or Stitches)
Professionals | Cons |
---|---|
Types are scoped to elements, stopping undesirable unwanted effects. | Provides runtime overhead and should improve JS bundle measurement. |
Dynamic styling based mostly on part state or props. | Types might not seem instantly on server-rendered pages with out further setup. |
Simple to take care of kinds near your part logic. | It may be more durable to debug within the browser inspector. |
.css
recordsdata, international or CSS Modules):
Exterior CSS imports (basic Professionals | Cons |
---|---|
CSS is loaded by the browser in parallel, permitting for sooner rendering. | Threat of fashion collision in international CSS. |
Simpler to cache and cut up CSS for giant tasks. | Much less dynamic—more durable to do conditional kinds based mostly on state. |
Nice for international themes, resets, or utility lessons. |
In apply, most trendy groups use a mixture of worldwide kinds and resets in CSS recordsdata, together with component-level kinds utilizing CSS-in-JS or CSS Modules.
10. Are you able to construct this structure in CSS? (Onerous)

You’ll virtually all the time be requested to construct layouts on the fly.
Bear in mind, a query like it is a nice alternative as a result of there’s a couple of approach to clear up it. On this case, we’re a reasonably basic “Holy Grail” structure, one thing Geoff has written about earlier than and demonstrated numerous methods to go about it utilizing CSS Grid.
You could possibly go together with a Flexbox method as properly:
It might be simple to fall into the entice of discovering the “finest” answer, however this maybe is one case the place demonstrating how one can suppose like a front-end internet developer is equally, if no more, necessary than arising with a single definitive answer.
Conclusion
These are merely instance of the form of core CSS questions you’re more likely to encounter in front-end interviews, together with sensible examples and the reasoning behind every method. Should you’re comfy answering these in depth and might code out the examples below time stress, you’ll be well-prepared.
For extra front-end interview questions, take into account exploring frontendlead.com, which helps you put together for front-end interviews throughout prime tech firms. In case you have extra matters you’d wish to see coated or encounter difficult interview questions, please be happy to publish them within the feedback — I’d like to see them.
And, in fact, better of luck in your interviews!