Basic Interview Questions
What is CSS and why is it used?
CSS (Cascading Style Sheets) describes how HTML elements are displayed: colors, fonts, spacing and layout. It separates presentation from content, so one style sheet can style a whole website and be changed in one place.
What are the ways to add CSS to a page?
- Inline:
styleattribute on an element. - Internal:
<style>in the head. - External:
<link rel="stylesheet" href="...">, preferred for real sites because it is reusable and cacheable.
Explain the box model.
Every element is a box of content, padding, border and margin. With the default box-sizing: content-box, width covers only the content; with border-box it includes padding and border, which makes sizing predictable.
Difference between margin and padding?
Padding is space inside the border (it shows the element's background); margin is space outside the border, between elements. Vertical margins of adjacent blocks collapse; padding never collapses.
display: none vs visibility: hidden?
display: none removes the element from the layout (no space). visibility: hidden hides it but keeps its space. Both hide it from screen readers; opacity: 0 hides it visually but keeps it clickable and readable.
What are pseudo-classes and pseudo-elements?
A pseudo-class (one colon) selects an element in a state or position: :hover, :focus, :nth-child(2). A pseudo-element (two colons) styles a part of an element: ::before, ::after, ::first-letter, ::placeholder.