Top 25+ HTML  CSS Interview Question & Answers | Personalized AI Support

Ace Your HTML & CSS Interview: Top 20 Questions and Answers

Q1: What is the difference between block-level and inline elements in HTML?

Block-level elements start on a new line and take up the full width available (100% of the container), while inline elements do not start on a new line and only take up as much width as necessary.

Examples:

  • Block-level elements:
    ,

    -

    ,

    ,
    ,
    , etc.
  • Inline elements: , , , , , etc.

Q2: What is the box model in CSS?

The box model in CSS describes how the elements on a web page are structured and displayed. It consists of four parts:

  • Content: The actual content of the box, where text and images appear.
  • Padding: The space between the content and the border.
  • Border: The line surrounding the padding (and content).
  • Margin: The space outside the border, separating the element from other elements.

Q3: Explain the difference between classes and IDs in HTML and CSS.

ID: Unique identifier for an element. It should be unique within the document. Used for a single element.
Class: Used to apply styles to multiple elements. Can be used on multiple elements within the document.

Example Code:


This has a unique ID.
This has a class.
This also has the same class.
/* CSS */ #unique-element { color: blue; } .common-element { color: green; }

Q4: What are semantic HTML elements? Provide examples.

Semantic HTML elements clearly describe their meaning in a human- and machine-readable way. These elements define the structure and content of the web page.

Examples:

  • : Defines a header for a document or section.
  • : Defines a navigation block.
  • : Defines a section in a document.
  • : Defines an article.
  • : Defines a footer for a document or section.

Q5: What are CSS selectors and how do they work?

CSS selectors are patterns used to select the elements you want to style. There are several types of selectors:

  • Element Selector : Selects all elements of a given type.
  • p { color: blue; }
  • Class Selector : Selects all elements with a given class.
  • .classname { color: red; }
  • ID Selector : Selects a single element with a given ID.
  • #idname { color: green; }
  • Attribute Selector : Selects elements based on an attribute or attribute value.
  • a[target="_blank"] { color: yellow; }
  • Pseudo-class Selector : Selects elements based on their state.
  • a:hover { color: pink; }

Request question

Please fill in the form below to submit your question.

Q6: What is the purpose of the alt attribute in images?

The alt attribute in images provides alternative text for an image if it cannot be displayed. It is also used by screen readers to describe images for visually impaired users, improving accessibility.

Example:

A description of the image

Q7: Explain the use of the flexbox layout in CSS.

The flexbox layout is a CSS module that provides a more efficient way to lay out, align, and distribute space among items in a container, even when their size is unknown and/or dynamic. The main components are:

  • display: flex; : Defines a flex container and enables a flex context for all its direct children.
  • flex-direction: Defines the direction of the flex items.
  • justify-content: Aligns flex items along the main axis.
  • align-items: Aligns flex items along the cross axis.
  • flex-wrap: Specifies whether the flex items should wrap or not.

Example:

.container {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

Q8: What is the difference between padding and margin in CSS?

Padding: The space between the content of the element and its border. It affects the element's internal space.

Margin: The space outside the border of the element. It affects the element's external space, creating space between elements.

Q9: How do you create a table in HTML?

A table in HTML is created using the

tag along with for table rows, for table headers, and for table data cells.

Example:

Header 1 Header 2
Data 1 Data 2

Q10: Explain the difference between position: relative; and position: absolute; in CSS.

position: relative; : The element is positioned relative to its normal position. Offsets (top, right, bottom, left) will move it from its normal position without affecting the layout of other elements.

position: absolute; : The element is positioned relative to its nearest positioned ancestor (not static). If no such ancestor exists, it uses the document body, and it will be placed in relation to the initial containing block. Absolute positioning removes the element from the normal document flow.

Example:

.relative-element {
  position: relative;
  top: 10px;
  left: 20px;
}

.absolute-element {
  position: absolute;
  top: 50px;
  left: 100px;
}

Request question

Please fill in the form below to submit your question.

Q11: What is the difference between external, internal, and inline CSS?

  • External CSS: Styles are defined in an external stylesheet file with a .css extension, linked to the HTML document using the tag.
  • Internal CSS: Styles are defined within the
  • Inline CSS: Styles are applied directly to HTML elements using the style attribute.
  • This is a red paragraph.

Q12: What are media queries and how are they used in responsive design?

Media queries are used in CSS to apply different styles based on the characteristics of the device, such as its width, height, orientation, and resolution. They are essential for creating responsive web designs that adapt to various screen sizes.

Example:

/* Default styles */
body {
  background-color: white;
}

/* Styles for screens wider than 600px */
@media (min-width: 600px) {
  body {
    background-color: lightblue;
  }
}

Q13: What is the purpose of the z-index property in CSS?

The z-index property in CSS controls the stacking order of elements that overlap. Higher values are placed on top of lower values. It only works on positioned elements ( position: relative , absolute , fixed , or sticky ).

Example:

.div1 {
  position: absolute;
  z-index: 1;
}

.div2 {
  position: absolute;
  z-index: 2;
}

Q14: Explain the concept of CSS specificity.

CSS specificity determines which CSS rule is applied by the browser when multiple rules target the same element. It is calculated based on the selectors used:

  • Inline styles have the highest specificity (e.g., style="color: red;" ).
  • ID selectors ( #id ) have higher specificity than class selectors ( .class ), attribute selectors ( [attribute] ), and pseudo-classes ( :hover ).
  • Element selectors (e.g., div , p ) and pseudo-elements ( ::before , ::after ) have the lowest specificity.

Example Calculation:

/* Specificity: 0-1-0-0 (one ID selector) */
#header { color: blue; }

/* Specificity: 0-0-1-0 (one class selector) */
.header { color: green; }

/* Specificity: 0-0-0-1 (one element selector) */
header { color: red; }

Q15: What are CSS preprocessors and why are they useful?

CSS preprocessors are scripting languages that extend CSS by allowing variables, nested rules, mixins, functions, and more. They help to make CSS more maintainable, reusable, and easier to write. Popular preprocessors include Sass, LESS, and Stylus.

Example using Sass:

$primary-color: blue;

body {
  color: $primary-color;
}

.navbar {
  background-color: $primary-color;
}

Request question

Please fill in the form below to submit your question.

Q16: What is the purpose of the DOCTYPE declaration in HTML?

The DOCTYPE declaration is used to specify the HTML version and type for the browser. It helps the browser to render the document correctly. The declaration should be the very first thing in an HTML document, before the tag.

Example:





  Document


  

Q17: Explain the concept of CSS Grid Layout.

CSS Grid Layout is a two-dimensional layout system for the web. It allows developers to create complex, responsive web layouts more easily. It uses rows and columns to define the structure of the layout.

Basic Example:

.container {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  grid-template-rows: auto;
  gap: 10px;
}

.item {
  background-color: lightblue;
}

Q18: What is the difference between visibility: hidden; and display: none; in CSS?

visibility: hidden; Hides the element, but it still takes up space in the layout.

display: none; Removes the element from the document flow entirely, so it does not take up any space.

Example:

.hidden-element {
  visibility: hidden;
}

.none-element {
  display: none;
}

Q19: What are pseudo-classes in CSS? Provide examples.

Pseudo-classes are keywords added to selectors that specify a special state of the selected elements. They are used to define the styles for special states of elements.

Examples:

a:hover {
  color: red;
}

input:focus {
  border-color: blue;
}

li:nth-child(2) {
  background-color: yellow;
}

Q20: Explain the use of the em and rem units in CSS.

em: A relative unit that is based on the font size of the element it is used on. If no font size is set on the element, it will inherit the font size from its parent.

/* If the parent element has a font size of 16px */
.child {
  font-size: 2em; /* 32px */
}

rem: A relative unit that is based on the root element's ( html ) font size. It is not affected by the parent element's font size.

/* If the root element has a font size of 16px */
.child {
  font-size: 2rem; /* 32px */
}

Request question

Please fill in the form below to submit your question.

Request question

Please fill in the form below to submit your question.

Challenge Yourself : 10 Practical HTML & CSS Interview Q&A

Q1: Create an HTML form that includes a text input field for the user's name and a submit button.
(Basic)





  Simple Form


  
Q2: Debug the following CSS code to correctly center an element horizontally.
(Basic)

.center-element {
  margin-left: auto;
  margin-right: auto;
}


.center-element {
  margin-left: auto;
  margin-right: auto;
  display: block; /* Added to ensure the element is block-level */
}
Q3: Optimize the following HTML and CSS code to reduce redundancy and improve performance.
(Intermediate)





  


  
  
  
  







  


  
  
  
  


Q4: Write CSS code that creates a responsive grid layout with three columns, which collapses to a single column on screens smaller than 600px.
(Intermediate)

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

@media (max-width: 600px) {
  .container {
    grid-template-columns: 1fr;
  }
}
Q5: Analyze the given code and predict the background color of the div elements.
(Intermediate)





  


  
Box 1
Box 2

Initially, the background color of both div elements will be red. When a user hovers over either box, its background color will change to green.

Q6: Write CSS code to create a vertically centered element within a parent div.
(Advanced)

CSS


.parent {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px; /* Example height */
  border: 1px solid black;
}

.child {
  width: 100px;
  height: 100px;
  background-color: lightblue;
}

HTML


Centered
Q7: The given CSS grid layout code contains an error that prevents it from displaying a grid layout. Identify and fix the error.
(Advanced)

.container {
  display: grid;
  grid-template-column: repeat(3, 1fr); /* Incorrect property name */
  gap: 10px;
}

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr); /* Corrected property name */
  gap: 10px;
}
Q8: Write the HTML and CSS code to create a fixed header that remains at the top of the page when scrolling.
(Advanced)

CSS:


header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  background-color: darkblue;
  color: white;
  padding: 10px;
  z-index: 1000;
}

body {
  margin-top: 50px; /* Add margin to avoid content being hidden behind the fixed header */
}

HTML:






  


  
Fixed Header
Q9: Improve the performance of the following CSS animation code.
(Advanced)

@keyframes move {
  0% { left: 0; }
  100% { left: 100px; }
}

.element {
  position: relative;
  animation: move 2s infinite;
}

To improve performance, use transform instead of left for smoother animations and better performance.


@keyframes move {
  0% { transform: translateX(0); }
  100% { transform: translateX(100px); }
}

.element {
  position: relative;
  animation: move 2s infinite;
}
Q10: Analyze the given code and predict the appearance of the div elements, including their position and visibility.
(Advanced)





  


  
Box 1
Box 2

The .box2 div will be positioned on top of the .box1 div due to the higher z-index value. Both divs will be positioned inside the .container div, with .box1 being partially overlapped by .box2 .

Request question

Please fill in the form below to submit your question.

Take Your Coding to the Next Level – Explore Workik AI Now!

Join developers who are using Workik’s AI assistance everyday for programming

Sign Up Now

Overview of HTML & CSS

What are HTML and CSS?

What is the history and latest trends in HTML and CSS?

What are some of the popular frameworks and libraries associated with HTML and CSS?

  • Bootstrap: A popular front-end framework for developing responsive and mobile-first websites.
  • Foundation: A responsive front-end framework similar to Bootstrap.
  • Bulma: A modern CSS framework based on Flexbox.
  • Tailwind CSS: A utility-first CSS framework for rapid UI development.
  • Materialize: A CSS framework based on Google's Material Design.

What are the use cases of HTML and CSS?

  • Web Development: Creating and styling websites and web applications.
  • Email Templates: Designing responsive email templates for marketing and communication.
  • User Interface Design: Building the front-end of applications and interfaces.
  • Prototyping: Quickly developing prototypes for web projects.
  • E-commerce Sites: Designing and structuring online stores.

What are some of the tech roles associated with expertise in HTML and CSS?

  • Front-End Developer: Focuses on implementing visual and interactive elements using HTML, CSS, and JavaScript.
  • Web Designer: Designs the layout and visual appearance of websites.
  • UI/UX Designer: Ensures the usability and aesthetics of web interfaces.
  • Email Developer: Specializes in creating responsive and interactive email templates.
  • Full-Stack Developer: Works on both the front-end and back-end of web applications, utilizing HTML and CSS for the client-side.

What pay package can be expected with experience in HTML and CSS?


Source: salary.com as of 2024

  • Junior HTML/CSS Developer (0-2 years experience): $55,000 - $70,000 per year.
  • Mid-Level HTML/CSS Developer (3-5 years experience): $70,000 - $90,000 per year.
  • Senior HTML/CSS Developer (5+ years experience): $90,000 - $120,000 per year.
  • Front-End Developer: $75,000 - $110,000 per year.
  • Web Designer: $60,000 - $80,000 per year.