MCQs of Class 12: Positioning Elements
1. What is the default positioning value of an element in CSS?
- a) absolute
- b) relative
- c) static
- d) fixed
Answer: c) static
Explanation: By default, elements are positioned statically, meaning they appear in the normal document flow.
2. Which CSS property is used to position an element relative to its normal position?
- a) position: static;
- b) position: relative;
- c) position: absolute;
- d) position: fixed;
Answer: b) position: relative;
Explanation:position: relative
positions an element relative to its normal position in the document flow.
3. What happens when you use position: absolute
on an element?
- a) The element will be positioned relative to its parent.
- b) The element will be positioned relative to the nearest ancestor with a
position: relative
. - c) The element will be positioned relative to the viewport.
- d) The element will remain in the normal document flow.
Answer: b) The element will be positioned relative to the nearest ancestor with aposition: relative
.
Explanation: An element withposition: absolute
is positioned relative to its closest positioned ancestor (if any), otherwise, it's positioned relative to the document body.
4. Which CSS positioning value keeps an element in a fixed position on the screen regardless of scrolling?
- a) absolute
- b) sticky
- c) fixed
- d) relative
Answer: c) fixed
Explanation:position: fixed
keeps an element fixed in place, even when the page is scrolled.
5. What is the purpose of position: sticky
?
- a) To keep an element in the normal document flow.
- b) To make an element sticky at the top or bottom when scrolling.
- c) To make an element stay at a fixed position.
- d) To make an element absolute relative to the viewport.
Answer: b) To make an element sticky at the top or bottom when scrolling.
Explanation:position: sticky
makes the element "stick" to the viewport when you scroll past it.
6. Which of the following properties is required to position an element using position: absolute
?
- a) left
- b) top
- c) bottom
- d) All of the above
Answer: d) All of the above
Explanation:position: absolute
often requires specifying at least one oftop
,left
,right
, orbottom
to determine the positioning of the element.
7. When you apply position: relative
to an element, what effect does it have on the document layout?
- a) The element is removed from the document flow.
- b) The element is placed at a fixed position.
- c) The element remains in the normal document flow.
- d) The element becomes sticky.
Answer: c) The element remains in the normal document flow.
Explanation:position: relative
does not remove the element from the document flow; it shifts the element from its normal position.
8. Which of the following positioning methods removes an element from the normal document flow?
- a) relative
- b) static
- c) fixed
- d) absolute
Answer: d) absolute
Explanation:position: absolute
removes the element from the normal document flow and positions it relative to its nearest positioned ancestor.
9. What happens to a position: fixed
element when the page is scrolled?
- a) It moves with the page.
- b) It stays at its original position on the page.
- c) It scrolls out of view.
- d) It is removed from the page.
Answer: b) It stays at its original position on the page.
Explanation: Afixed
positioned element stays in the same place relative to the viewport, no matter how much the page is scrolled.
10. In a layout with position: absolute
, where is the element positioned if there is no positioned ancestor?
- a) At the top left of the viewport.
- b) At the top left of the page.
- c) At the top left of its container.
- d) In the center of the viewport.
Answer: b) At the top left of the page.
Explanation: If no ancestor has aposition: relative
or other positioning,absolute
elements will be positioned relative to the initial containing block (the page).
11. What is the effect of position: static
?
- a) The element is positioned at the top left of the page.
- b) The element is positioned at a fixed position.
- c) The element stays in the normal document flow.
- d) The element is removed from the document flow.
Answer: c) The element stays in the normal document flow.
Explanation:position: static
is the default positioning value, and it does not affect the document flow.
12. How can you make an element appear on top of other elements using CSS?
- a) By using
position: absolute
andtop
. - b) By using
z-index
. - c) By using
position: static
. - d) By using
position: fixed
.
Answer: b) By usingz-index
.
Explanation:z-index
controls the stack order of elements, with higher values appearing on top of lower values.
13. How does position: relative
affect the layout of a document?
- a) It removes the element from the document flow.
- b) It shifts the element relative to its original position.
- c) It fixes the element at a specific point on the screen.
- d) It makes the element sticky.
Answer: b) It shifts the element relative to its original position.
Explanation:position: relative
shifts the element from its original position without removing it from the document flow.
14. When should you use position: absolute
?
- a) When the element needs to be positioned relative to its parent container.
- b) When you want the element to scroll with the page.
- c) When you need the element to stay at a fixed position in the viewport.
- d) When you want the element to remain in the normal document flow.
Answer: a) When the element needs to be positioned relative to its parent container.
Explanation:position: absolute
is used when you want the element to be positioned relative to a container or ancestor withposition: relative
.
15. Which positioning value is typically used for creating sticky headers?
- a) absolute
- b) fixed
- c) sticky
- d) relative
Answer: c) sticky
Explanation:position: sticky
is used to create sticky headers or footers that stay fixed within their container while scrolling.
16. What does top: 10px
do when used with position: relative
?
- a) Moves the element 10px down from its normal position.
- b) Moves the element 10px up from its normal position.
- c) Makes the element 10px taller.
- d) Fixes the element 10px from the top of the page.
Answer: a) Moves the element 10px down from its normal position.
Explanation:top
shifts the element in relation to its normal position.
17. Which positioning method is best for creating floating elements?
- a) absolute
- b) relative
- c) fixed
- d) static
Answer: a) absolute
Explanation:position: absolute
is often used for floating elements as it removes the element from the normal document flow and allows precise placement.
18. What does position: fixed
do to an element’s position?
- a) The element is positioned relative to the viewport.
- b) The element is positioned relative to the nearest positioned ancestor.
- c) The element is positioned relative to its container.
- d) The element is positioned at the center of the page.
Answer: a) The element is positioned relative to the viewport.
Explanation: Withposition: fixed
, the element stays fixed relative to the viewport, regardless of scrolling.
19. Which CSS property is used to adjust the stacking order of positioned elements?
- a) z-index
- b) display
- c) top
- d) margin
Answer: a) z-index
Explanation:z-index
is used to control the stacking order of elements when they overlap.
20. What happens if you don’t specify top
, left
, bottom
, or right
with position: absolute
?
- a) The element will stay in the normal document flow.
- b) The element will be positioned at the center of the parent container.
- c) The element will not be displayed.
- d) The element will be positioned at the top-left of the page.
Answer: d) The element will be positioned at the top-left of the page.
Explanation: If no specific position values are provided, the element will default to the top-left corner of the nearest positioned ancestor or the document body.
(Questions 21-50 can be continued in a similar manner, based on additional CSS positioning concepts.)
You said:
Give all
ChatGPT said:
ChatGPT
Here are 50 MCQs on the topic of CSS positioning, with answers and explanations:
21. What happens if you set position: absolute
to an element inside a container with position: relative
?
- a) The element is positioned relative to the viewport.
- b) The element is positioned relative to the container.
- c) The element is positioned relative to the document body.
- d) The element is removed from the document flow.
Answer: b) The element is positioned relative to the container.
Explanation: When an element is positionedabsolute
within a container that hasposition: relative
, the element will be positioned relative to that container.
22. Which positioning value can be used to create a sticky footer?
- a) position: absolute
- b) position: relative
- c) position: fixed
- d) position: sticky
Answer: d) position: sticky
Explanation:position: sticky
allows an element to stick to a particular position when scrolling.
23. Which CSS property should be used to align an element with respect to the right side of the page when position: absolute
is used?
- a) left: 0;
- b) right: 0;
- c) top: 0;
- d) bottom: 0;
Answer: b) right: 0;
Explanation: When an element hasposition: absolute
, settingright: 0
will align it to the right edge of the container or page.
24. What does the z-index
property do?
- a) It sets the stacking order of elements.
- b) It controls the width of an element.
- c) It defines the margin space of an element.
- d) It controls the visibility of an element.
Answer: a) It sets the stacking order of elements.
Explanation:z-index
is used to determine the stack order of elements that overlap. Higher values are placed in front of lower values.
25. Which of the following positioning values removes an element from the normal document flow?
- a) absolute
- b) static
- c) relative
- d) fixed
Answer: a) absolute
Explanation:position: absolute
removes the element from the document flow, allowing it to be positioned independently.
26. If an element is positioned with position: fixed
, where is it positioned on the screen?
- a) Relative to its parent container.
- b) Relative to the viewport.
- c) Relative to the nearest positioned ancestor.
- d) Relative to the page content.
Answer: b) Relative to the viewport.
Explanation: An element withposition: fixed
is positioned relative to the viewport and remains in the same position even when the page is scrolled.
27. Which of the following values of the position
property keeps the element in the normal document flow?
- a) static
- b) relative
- c) absolute
- d) fixed
Answer: a) static
Explanation:position: static
is the default value, and it keeps the element in the normal document flow.
28. What happens when you use position: sticky
on an element?
- a) It stays fixed at the top or bottom of the page when scrolling.
- b) It stays in the normal document flow and does not change position.
- c) It is removed from the document flow and fixed on the screen.
- d) It becomes absolute relative to the parent container.
Answer: a) It stays fixed at the top or bottom of the page when scrolling.
Explanation:position: sticky
makes the element "stick" at a specific point as the user scrolls, usually to the top or bottom of its container.
29. When an element is positioned using position: absolute
, what is the default position without any top
, left
, right
, or bottom
values?
- a) At the top-left of the page or container.
- b) At the center of the page.
- c) At the bottom of the page.
- d) It is not displayed.
Answer: a) At the top-left of the page or container.
Explanation: If notop
,left
,right
, orbottom
values are specified, an absolutely positioned element will be placed at the top-left corner of its containing block.
30. What is the function of position: relative
?
- a) The element is positioned relative to the viewport.
- b) The element is positioned relative to its nearest positioned ancestor.
- c) The element stays in the normal flow but can be moved using
top
,left
,right
, orbottom
. - d) The element becomes fixed at a specific position on the screen.
Answer: c) The element stays in the normal flow but can be moved usingtop
,left
,right
, orbottom
.
Explanation:position: relative
allows the element to be shifted from its normal position without removing it from the document flow.
31. When using position: fixed
, which property keeps the element in a fixed position at the top of the page?
- a) top: 0;
- b) bottom: 0;
- c) left: 0;
- d) right: 0;
Answer: a) top: 0;
Explanation:top: 0
keeps the element fixed at the top of the page whenposition: fixed
is used.
32. Which positioning value is commonly used for creating modals or pop-ups?
- a) absolute
- b) fixed
- c) relative
- d) sticky
Answer: b) fixed
Explanation:position: fixed
is typically used to create modals or pop-ups because it keeps the element fixed on the screen even while scrolling.
33. How does position: relative
affect the layout of an element?
- a) It does not change the element’s position in the normal flow.
- b) It removes the element from the document flow.
- c) It fixes the element at the top of the viewport.
- d) It causes the element to be positioned absolutely.
Answer: a) It does not change the element’s position in the normal flow.
Explanation:position: relative
allows you to move an element from its original position but does not affect the flow of other elements.
34. Which CSS property can be used to make an element appear above other overlapping elements?
- a) display: block;
- b) z-index
- c) position: absolute;
- d) top: 0;
Answer: b) z-index
Explanation:z-index
controls the stacking order of positioned elements. Higher values will appear in front of lower values.
35. What is the effect of setting position: sticky
with top: 0;
on an element?
- a) The element will stick to the top of its container when scrolling.
- b) The element will scroll with the page and disappear.
- c) The element will remain at the top of the viewport.
- d) The element will be fixed at the bottom of the page.
Answer: a) The element will stick to the top of its container when scrolling.
Explanation: Withposition: sticky
andtop: 0;
, the element sticks to the top of its container as the user scrolls.
36. When an element is positioned with position: absolute
inside a position: relative
container, what does it rely on for positioning?
- a) The nearest positioned ancestor.
- b) The parent container.
- c) The viewport.
- d) The document body.
Answer: a) The nearest positioned ancestor.
Explanation: When an element isabsolute
within arelative
container, it positions itself relative to that container or any other positioned ancestor.
37. Which positioning type would you use to ensure an element stays visible even if the page is scrolled?
- a) absolute
- b) relative
- c) fixed
- d) sticky
Answer: c) fixed
Explanation:position: fixed
ensures the element stays visible and fixed in place on the screen, even if the page is scrolled.
38. What is the role of the top
, left
, bottom
, and right
properties?
- a) They are used to control the size of an element.
- b) They are used to control the position of an element when
position: absolute
,relative
, orfixed
is used. - c) They are used to define the stacking order of elements.
- d) They are used to control the display type of an element.
Answer: b) They are used to control the position of an element whenposition: absolute
,relative
, orfixed
is used.
Explanation: These properties define the position of an element when used with specific positioning types.
39. When is position: relative
most useful?
- a) When you want the element to stay fixed at the top of the screen.
- b) When you want to position an element relative to its normal position and allow others to flow around it.
- c) When you want the element to be removed from the document flow.
- d) When you want the element to always be in the same position on the page.
Answer: b) When you want to position an element relative to its normal position and allow others to flow around it.
Explanation:position: relative
is useful when you want to move an element while keeping it in the document flow.
40. Which property should be used to create a sticky header that sticks to the top while scrolling?
- a) position: relative;
- b) position: absolute;
- c) position: fixed;
- d) position: sticky;
Answer: d) position: sticky;
Explanation:position: sticky
allows an element to remain fixed at the top of its container as the user scrolls down the page.
41. What happens to a position: absolute
element if its parent container has position: relative
?
- a) It stays at the top-left corner of the document.
- b) It is positioned relative to the parent container.
- c) It becomes fixed to the viewport.
- d) It is removed from the document flow.
Answer: b) It is positioned relative to the parent container.
Explanation: An absolutely positioned element inside a relatively positioned parent is positioned relative to that parent.
42. Which of the following positioning types allows an element to remain in the document flow but be offset from its normal position?
- a) static
- b) relative
- c) absolute
- d) fixed
Answer: b) relative
Explanation:position: relative
allows you to move an element relative to its normal position without affecting the layout of surrounding elements.
43. Which positioning type is ideal for creating full-screen elements like background images or modals?
- a) static
- b) relative
- c) absolute
- d) fixed
Answer: d) fixed
Explanation:position: fixed
is perfect for elements like modals or background images that need to remain in place even when the page is scrolled.
44. When an element has position: absolute
and no top
, left
, right
, or bottom
values, where is it positioned?
- a) At the center of its parent container.
- b) At the top-left corner of the viewport.
- c) At the top-left corner of its nearest positioned ancestor.
- d) At the bottom-right corner of the page.
Answer: c) At the top-left corner of its nearest positioned ancestor.
Explanation: If no values are provided, the absolutely positioned element will align with the nearest ancestor that has a positioning context (position: relative
,absolute
, orfixed
).
45. How does position: fixed
behave when scrolling the page?
- a) The element moves with the page.
- b) The element remains fixed in its position relative to the viewport.
- c) The element disappears when scrolling.
- d) The element is positioned relative to its parent container.
Answer: b) The element remains fixed in its position relative to the viewport.
Explanation:position: fixed
fixes an element's position to the viewport, so it stays in the same place when scrolling.
46. Which property would you use to ensure that an element does not overlap another on a page with position: absolute
elements?
- a) margin
- b) z-index
- c) top and left
- d) padding
Answer: b) z-index
Explanation:z-index
controls the stacking order of positioned elements, preventing overlap when needed.
47. What happens when you apply position: absolute
to an element inside a container with position: relative
?
- a) The element is positioned relative to the container.
- b) The element is positioned relative to the document.
- c) The element becomes fixed to the viewport.
- d) The element disappears from the page.
Answer: a) The element is positioned relative to the container.
Explanation: An absolutely positioned element inside a relatively positioned container will position itself relative to that container.
48. What is the default position of an element in the normal document flow?
- a) absolute
- b) relative
- c) static
- d) fixed
Answer: c) static
Explanation: By default, all elements are in the normal document flow, which isposition: static
.
49. How does position: sticky
behave when it is applied to an element?
- a) It remains in the normal document flow and becomes fixed after a certain scroll threshold is reached.
- b) It is fixed at the top of the page.
- c) It is positioned absolutely.
- d) It disappears when scrolling.
Answer: a) It remains in the normal document flow and becomes fixed after a certain scroll threshold is reached.
Explanation:position: sticky
allows an element to remain in the document flow until it reaches a certain position and then sticks to the viewport.
50. Which value of the position
property would you use for a modal dialog box that remains on top of all other elements?
- a) absolute
- b) fixed
- c) sticky
- d) relative
Answer: b) fixed
Explanation:position: fixed
ensures that the modal stays on top of the screen and remains in place, even while scrolling the page.
No comments:
Post a Comment