Clean • Professional
Ques: What is the position property in CSS?
Ans: The position property defines how an element is placed in a web page layout. It determines where and how the browser renders an element.
Common position values:
| Value | Description |
|---|---|
static | Default; positioned normally in the document flow |
relative | Positioned relative to its normal position |
absolute | Positioned relative to the nearest positioned ancestor |
fixed | Positioned relative to the browser window |
sticky | Switches between relative & fixed, depending on scroll position |
Example:
div {
position: absolute;
top: 20px;
left: 30px;
}
Ques: What is position: static?
Example:
div {
position: static; /* default */
}
Ques: How does position: relative work?
Example:
div {
position: relative;
top: 10px; /* moves down */
left: 20px; /* moves right */
}
Ques: What is position: absolute in CSS?
Ans: An element with position: absolute is:
relative, absolute, fixed, or sticky)Example:
.container {
position: relative;
}
.box {
position: absolute;
top: 0;
right: 0;
}
Ques: What does position: fixed mean?
Example:
.navbar {
position: fixed;
top: 0;
width: 100%;
background: #333;
color: white;
}
Ques: What is position: sticky in CSS?
Ans: It behaves like relative until a given scroll position, then becomes fixed.
Example:
header {
position: sticky;
top: 0;
background: white;
}
Ques: What are top, right, bottom, and left used for?
Ans: They define the offset of an element from its reference point (depending on its positioning context).
Example:
.box {
position: absolute;
top: 50px;
left: 100px;
}
Ques: What is z-index in CSS?
Ans: The z-index property controls the stacking order of overlapping elements on the z-axis (front-to-back).
Example:
div {
position: relative;
z-index: 2;
}
Ques: Does z-index work on static elements?
Ans: No, z-index works only on elements with a position value other than static.
Ques: What is a stacking context?
Ans: A stacking context is a 3D space in which elements are stacked according to z-index.
It’s created when an element has:
position + z-index value (other than auto)opacity < 1transform, filter, or will-change propertyisolation: isolateExample:
.container {
position: relative;
z-index: 1;
opacity: 0.9; /* new stacking context */
}
Ques: What is the float property in CSS?
Ans: Used to push elements to the left or right, allowing other content to wrap around it.
Example:
img {
float: right;
margin: 10px;
}
Ques: What does the clear property do?
Ans: clear stops elements from flowing next to floated elements.
Example:
footer {
clear: both;
}
Ques: Why is float considered legacy for layouts?
Ans: Because it was originally designed for text wrapping, not layout design.
Modern layouts use Flexbox and Grid instead.
Ques: What is display: inline-block?
Ans: It allows an element to:
Example:
button {
display: inline-block;
padding: 10px;
}
Ques: What is the CSS Anchor Position API?
Ans: The Anchor Position API lets you position elements (like popovers, tooltips, dropdowns) relative to another element, without JavaScript.
Example:
button {
position-anchor: pop;
}
.tooltip {
position: absolute;
position-area: right;
anchor-name: --pop;
}