Contact Us
Email: info@mohitdesigns.com
Mobile: +91-9718991639
Contact Us
Email: info@mohitdesigns.com
Mobile: +91-9718991639
Are you gearing up for a front-end developer interview in 2024? HTML and CSS are foundational skills that every web developer must master. To help you succeed, we’ve compiled the top 30 HTML/CSS interview questions with explanations and examples. Whether you’re a beginner or an experienced developer, these questions will sharpen your skills and give you confidence in your interviews.
The <!DOCTYPE>
declaration defines the document type and version of HTML being used. It helps the browser render the page correctly.
Example:
<!DOCTYPE html>
<html>
<head>
<title>Document Title</title>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
The above code ensures the browser uses the HTML5 standard.
Example of Block Element:
<div>This is a block-level element.</div>
Example of Inline Element:
<span>This is an inline element.</span>
The box model is the layout structure of HTML elements, comprising content, padding, border, and margin.
Example:
div {
width: 200px;
padding: 10px;
border: 5px solid black;
margin: 20px;
}
Here, the total width of the div
is 250px (200 + 10 + 5 + 20).
To center a div
, you can use the following CSS properties:
margin: 0 auto;
for horizontal centering.display: flex; justify-content: center; align-items: center;
for both horizontal and vertical centering.Example:
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
Example of Class:
<div class="box"></div>
Example of ID:
<div id="unique-box"></div>
CSS specificity determines which styles are applied to an element based on the importance of selectors.
Specificity hierarchy:
Example:
/* Class selector */
p.text {
color: blue;
}
/* ID selector */
#unique-text {
color: red;
}
Here, the #unique-text
rule will override .text
if applied to the same element.
Flexbox is a CSS layout module that makes it easier to design flexible, responsive layouts.
Example:
.container {
display: flex;
justify-content: space-between;
align-items: center;
}
The above code distributes space evenly between flex items and aligns them in the center.
relative
, absolute
, and fixed
positioning?Example:
div {
position: absolute;
top: 50px;
left: 100px;
}
The z-index
property controls the vertical stacking order of elements. Higher z-index
values appear on top of lower ones.
Example:
.box1 {
position: absolute;
z-index: 1;
}
.box2 {
position: absolute;
z-index: 2;
}
:hover
, :focus
).::before
, ::after
).Example:
a:hover {
color: red;
}
p::before {
content: "Note: ";
font-weight: bold;
}
Media queries allow you to apply different styles based on the screen size or device type.
Example:
@media (max-width: 600px) {
body {
background-color: lightblue;
}
}
CSS Grid allows you to create complex grid-based layouts easily.
Example:
.container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
rem
and em
units.Example:
body {
font-size: 16px;
}
h1 {
font-size: 2rem; /* 32px */
margin-bottom: 1em; /* relative to h1 font-size */
}
alt
attribute in images?The alt
attribute provides alternative text for images, improving accessibility and SEO.
Example:
<img src="image.jpg" alt="Description of image">
<meta>
tag.<meta>
tags provide metadata about the HTML document, such as character encoding, viewport settings, and SEO descriptions.
Example:
<meta name="description" content="Top HTML/CSS Interview Questions">
<section>
, <div>
, and <article>
?<section>
: Used for grouping related content, usually with a heading.<div>
: A generic container for styling or scripting purposes.<article>
: Represents a self-contained piece of content, such as a blog post or news article.Example:
<section>
<h2>About Us</h2>
<p>Information about the company...</p>
</section>
<div class="container">Content here...</div>
<article>
<h2>Blog Title</h2>
<p>Blog content...</p>
</article>
The float
property allows elements to be positioned to the left or right, allowing text or other elements to wrap around them.
Example:
img {
float: left;
margin-right: 10px;
}
visibility: hidden
and display: none
.visibility: hidden
hides the element but maintains its space in the layout.display: none
removes the element entirely from the document flow.Example:
.hidden {
visibility: hidden;
}
.none {
display: none;
}
<link>
and <@import>
in CSS?<link>
: External stylesheets are linked in the head of the document.@import
: Used inside CSS files to import other stylesheets, but is less efficient since it blocks rendering.Example:
<!-- Using link -->
<link rel="stylesheet" href="style.css">
/* Using @import */
@import url('style.css');
min-width
and max-width
work in responsive design?min-width
: Sets the minimum width an element can shrink to.max-width
: Sets the maximum width an element can grow to.Example:
.container {
width: 100%;
max-width: 1200px;
min-width: 300px;
}
Example of Reset:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
ARIA (Accessible Rich Internet Applications) roles define elements for screen readers and other assistive technologies, improving accessibility for users with disabilities.
Example:
<nav role="navigation">
<!-- Menu items -->
</nav>
position: sticky
, and how does it work?position: sticky
allows an element to switch between relative
and fixed
positioning based on the user’s scroll position.
Example:
header {
position: sticky;
top: 0;
}
Example:
.container {
margin: 20px;
padding: 15px;
}
Dark mode can be implemented by detecting the user’s system preferences using media queries or by toggling classes.
Example using media query:
@media (prefers-color-scheme: dark) {
body {
background-color: black;
color: white;
}
}
CSS specificity determines which styles are applied when there are conflicting styles. Specificity is calculated based on the types of selectors used:
Example:
/* ID selector */
#example {
color: red;
}
/* Class selector */
.example {
color: blue;
}
Here, #example
will have higher specificity and thus the color red will be applied.
Example of CSS Shorthand:
/* Longhand */
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
margin-left: 10px;
/* Shorthand */
margin: 10px;
Example of Transition:
button {
background-color: blue;
transition: background-color 0.5s;
}
button:hover {
background-color: green;
}
Example of Animation:
@keyframes example {
from {background-color: red;}
to {background-color: yellow;}
}
div {
animation-name: example;
animation-duration: 2s;
}
Example of Grid:
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
CSS variables allow you to store values that can be reused throughout your stylesheet, making your code easier to maintain.
Example:
:root {
--primary-color: #3498db;
}
button {
background-color: var(--primary-color);
}
Mastering these HTML/CSS Interview Questions 2024 will give you a solid foundation for any technical interview. Whether you’re a junior developer or have years of experience, these questions will prepare you for what to expect in front-end development interviews. Keep practicing and refining your skills, as the web development landscape continues to evolve.
By being well-versed in these key HTML and CSS concepts, you’ll increase your chances of success and demonstrate your technical expertise in interviews.