HTML & CSS interview questions
Prepare for your front-end interview with these common HTML & CSS questions.
01What is the difference between `id` and `class` attributes in HTML?
An id is unique within a page and can only be applied to one element. It's used for specific element styling or for JavaScript to manipulate a single element.
A class can be applied to multiple elements. It's used for styling groups of elements that share common visual properties.
02What is the CSS Box Model?
The CSS box model is a box that wraps around every HTML element. It consists of four parts, from inside out:
- Content: The content of the box, where text and images appear.
- Padding: Clears an area around the content. Padding is transparent.
- Border: A border that goes around the padding and content.
- Margin: Clears an area outside the border. The margin is transparent.
03What are CSS pseudo-classes?
A CSS pseudo-class is a keyword added to a selector that specifies a special state of the selected element(s). For example, :hover can be used to change a button's color when the user's pointer hovers over it. Other examples include :focus, :first-child, and :nth-child().
04What is the difference between `display: none;` and `visibility: hidden;`?
display: none; removes the element from the document flow completely. It takes up no space.
visibility: hidden; hides the element, but it still takes up space in the layout. The element is not visible, but it affects the layout.
05What are semantic HTML tags? Give some examples.
Semantic HTML tags are tags that define the meaning of the content they contain. They make the web more accessible and improve SEO. Examples include:
<header><footer><nav><article><section><aside>
06What is CSS specificity and how is it calculated?
Specificity is the algorithm used by browsers to determine which CSS rule is applied to an element. It's a weight that is applied to a given CSS declaration. The calculation is based on the number of ID, class/attribute, and type selectors.
- Inline styles (highest)
- IDs
- Classes, pseudo-classes, attribute selectors
- Type selectors, pseudo-elements (lowest)
07What is the difference between `position: relative`, `position: absolute`, and `position: fixed`?
position: relative;: The element is positioned relative to its normal position.position: absolute;: The element is positioned relative to its nearest positioned ancestor (notstatic). If none exists, it uses the document body.position: fixed;: The element is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled.
08What is Flexbox?
Flexbox (Flexible Box Layout) is a one-dimensional layout model that offers an efficient way to lay out, align, and distribute space among items in a container, even when their size is unknown or dynamic. It's ideal for creating responsive layouts.
09What is CSS Grid?
CSS Grid Layout is a two-dimensional layout system for the web. It lets you lay content out in rows and columns, and has many features that make building complex layouts straightforward. It's more powerful than Flexbox for overall page layouts.
10What is the difference between `em` and `rem` units?
em is a relative unit that is relative to the font-size of its direct or nearest parent.
rem (root em) is a relative unit that is relative to the font-size of the root element (<html>). rem is often preferred for creating scalable layouts because it's not affected by nesting.
11What are CSS preprocessors? Name a few.
CSS preprocessors are scripting languages that extend the default capabilities of CSS. They allow you to use features like variables, nesting, mixins, and functions. The preprocessor compiles the code into standard CSS. Examples include Sass, Less, and Stylus.
12What is the `z-index` property used for?
The z-index property specifies the stack order of an element. An element with a greater stack order is always in front of an element with a lower stack order. z-index only works on positioned elements (position: absolute, relative, fixed, or sticky).
13What is responsive web design?
Responsive Web Design (RWD) is an approach to web design that makes web pages render well on a variety of devices and window or screen sizes. It uses techniques like flexible grids, fluid images, and media queries.
14What are media queries?
Media queries are a feature of CSS3 that allow you to apply CSS rules based on the characteristics of the device, such as its width, height, orientation, or resolution. They are the cornerstone of responsive design.
15What is the `viewport` meta tag?
The viewport meta tag gives the browser instructions on how to control the page's dimensions and scaling. <meta name="viewport" content="width=device-width, initial-scale=1.0"> sets the width of the page to follow the screen-width of the device, which will vary depending on the device.
16What is the difference between `block`, `inline`, and `inline-block` display values?
block: Starts on a new line and takes up the full width available.inline: Does not start on a new line and only takes up as much width as necessary. You cannot set width or height.inline-block: It's formatted as an inline element, but you can set width and height values.
17What are data attributes?
Data attributes (data-*) are used to store custom data private to the page or application. They give us the ability to embed custom data attributes on all HTML elements. The stored data can be used in the page's JavaScript to create a more engaging user experience.
18What is BEM methodology?
BEM (Block, Element, Modifier) is a popular naming convention for classes in HTML and CSS. It helps to create reusable components and solve scoping issues.
- Block: A standalone entity (e.g.,
.card). - Element: A part of a block (e.g.,
.card__title). - Modifier: A flag on a block or element to change appearance or behavior (e.g.,
.card--large).
19What is the purpose of the `alt` attribute on an `<img>` tag?
The alt attribute provides alternative text for an image if the user for some reason cannot view it (because of slow connection, an error in the src attribute, or if the user uses a screen reader). It's crucial for accessibility and SEO.
20What are CSS variables (Custom Properties)?
CSS variables are entities defined by CSS authors which contain specific values to be reused throughout a document. They are set using custom property notation (e.g., --main-color: black;) and are accessed using the var() function (e.g., color: var(--main-color);).
21What is the difference between `*`, `>` and `+` selectors in CSS?
*(Universal selector): Selects all elements.>(Child selector): Selects all direct children of a specified element.+(Adjacent sibling selector): Selects the element that is immediately preceded by the former element.
22What is image spriting?
Image spriting is a technique where multiple images are combined into a single larger image (a sprite sheet). It's used to reduce the number of HTTP requests for images, which can improve website performance. CSS background-position is used to display the desired part of the sprite.
23How do you center a block element horizontally?
For a block-level element with a specified width, you can center it by setting margin-left and margin-right to auto. A common shorthand is margin: 0 auto;.
24What is the `box-sizing` property?
The box-sizing property defines how the width and height of an element are calculated.
content-box(default): The width and height properties include only the content. Border and padding are not included.border-box: The width and height properties include content, padding, and border.
25What are CSS transitions?
CSS transitions allow you to change property values smoothly, over a given duration. They provide a way to control animation speed when changing CSS properties. You specify the property to transition, the duration, and optionally the timing function and delay.
26What are CSS animations?
CSS animations are more powerful than transitions. They allow you to create complex animations using keyframes (@keyframes). With keyframes, you can control the intermediate steps in an animation sequence, not just the start and end states.
27What is the `!important` rule in CSS?
The !important rule is used to add more importance to a property/value than normal. It will override all previous styling rules for that specific property on that element. Using !important is generally considered bad practice because it makes debugging difficult by breaking the natural cascading in your stylesheets.
28What is the difference between `link` and `@import` for including stylesheets?
<link> is an HTML tag that is placed in the <head> section. It loads stylesheets in parallel with the page content.
@import is a CSS rule that is placed at the top of a CSS file. It can cause a delay in page rendering because it loads the imported stylesheet sequentially.
29What is a CSS pseudo-element?
A pseudo-element is used to style specified parts of an element. For example, ::before can be used to insert some content before the content of an element, and ::first-line can be used to style the first line of a paragraph.
30What is the `float` property in CSS?
The float property is used for positioning and formatting content, e.g., let an image float left to the text in a container. However, for layout purposes, modern techniques like Flexbox and Grid are now preferred over floats.
Preparing for a role?
Tell us what you’re aiming for. A mentor can help you close the gaps before the interview.