前端 CSS 面试题, CSS 中 display 属性的值及其作用
前端 CSS 面试题, CSS 中 display 属性的值及其作用
QA
Step 1
Q:: What is the display
property in CSS and why is it important?
A:: The display
property in CSS defines how an element is displayed on the web page. It is crucial because it determines the layout of the element and how it interacts with other elements on the page.
The display
property can take several values,
such as block``,
inline``,
inline-block``,
none``,
flex``,
grid``, etc., each affecting the layout differently.
Step 2
Q:: What does display: block;
do?
A:: display: block;
makes the element a block-level element, meaning it will occupy the full width available, and starts on a new line. Examples of block-
level elements include <div>``,
<p>``,
and <h1>``.
Step 3
Q:: What does display: inline;
do?
A:: display: inline;
makes the element an inline element, meaning it only takes up as much width as necessary and does not start on a new line.
Examples of inline elements include <span>``,
<a>``,
and <strong>``.
Step 4
Q:: What is the difference between display: inline;
and display: inline-block;``?
A:: display: inline;
elements do not allow you to set width and height properties,
while display: inline-block;
elements do.
inline-block
elements are similar to inline
elements but can have padding, margin, and width/height set.
Step 5
Q:: How does display: none;
differ from visibility: hidden;``?
A:: display: none;
removes the element from the document flow entirely, meaning it won't take up any space in the layout. On the other hand,
visibility: hidden;
hides the element but still occupies space in the layout.
Step 6
Q:: What is the display: flex;
property, and how is it used?
A:: display: flex;
is used to create a flexible layout structure, where child elements (flex items) are arranged within a flex container. It allows for responsive design and easy alignment of elements along horizontal and vertical axes. Flexbox simplifies tasks like centering content, creating equal-width columns, and adjusting layouts for different screen sizes.
Step 7
Q:: Can you explain how display: grid;
works?
A:: display: grid;
is used to create grid-based layouts, allowing for the placement of items into a defined grid with rows and columns. It is highly versatile and provides control over the layout of complex designs, making it easier to create responsive, adaptive designs.