Learn about CSS display and visibility properties to control web layout, hide or show elements, create transitions, and enhance accessibility. Understand the differences between display: none and visibility: hidden. Get practical examples and best practices.
The display property in CSS is used to specify how an element should be displayed on a web page.
It determines the type of box used for an HTML element and affects the layout and positioning of the element and its surrounding content.
There are several values for the display property, each with its own behavior and layout characteristics.
Here are some commonly used values:
This value makes an element a block-level element, which means it takes up the full width available and starts on a new line. Block-level elements stack vertically on top of each other. Examples of block-level elements include <div>, <p>, and <h1> to <h6>.
This value makes an element an inline-level element, which means it does not start on a new line and only takes up as much width as necessary. Inline-level elements flow within the text and can appear next to each other.
Examples of inline-level elements include <span>, <a>, and <strong>.
This value combines the characteristics of both block-level and inline-level elements. It allows an element to have a specified width and height like a block-level element while still flowing within the text like an inline-level element. Inline-block elements can appear next to each other and respect margins and paddings.
Examples of inline-block elements include form elements like
<input> and <button>.
This value hides the element completely, as if it does not exist.
The element and its content are removed from the layout, and it occupies no space on the page.
This can be useful for hiding elements dynamically or for accessibility purposes.
This value enables a flex container for an element.
Flexbox is a powerful layout system that provides flexible and responsive ways to distribute space among elements.
The direct children of a flex container become flex items, and you can use various properties to control their alignment, order, and sizing.
This value enables a grid container for an element.
CSS Grid Layout is another powerful layout system that allows you to create complex grid-based designs.
It provides control over both rows and columns, allowing you to arrange elements in a two-dimensional grid.
These are just a few of the display property values available.
Each value has its own purpose and behavior, allowing you to control the layout and positioning of elements on your web page.
It’s important to choose the appropriate value based on your specific layout requirements.
Here’s an example of how you can use the display: block property in HTML:
<!DOCTYPE html> <html> <head> <style> .block-element { display: block; width: 200px; height: 100px; background-color: blue; color: white; text-align: center; padding: 10px; } </style> </head> <body> <div class="block-element"> This is a block-level element. </div> <p> This is some text outside the block element. </p> </body> </html>
Explanation:
1-In this example, we have a <div> element with a class of “block-element”.
2-We apply the display: block property to this element, making it a block-level element.
3-We also set a fixed width and height, background color, text color, text alignment, and padding to give it some styling.
4-The block-level element takes up the full width available and starts on a new line.
5-It has a blue background color, white text color, and centered text content due to the styling applied.
6-The text “This is a block-level element” will be displayed within the block element.
7-Outside the block element, we have a <p> element with some text. Since it is not styled with display: block, it is an inline-level element and will flow alongside the block element if there is enough space.
8-When you open this HTML file in a browser, you should see the block-level element displayed as a blue box with centered text content.
9-The text “This is some text outside the block element” will be displayed below the block element on a new line.
Try to customize the styles and content to suit your needs.
Block-level elements are HTML elements that, by default, create a block-level box on a web page.
They typically start on a new line and take up the full width available. Here are some commonly used block-level elements:
Headings are used to structure the content hierarchy on a web page.
These are just a few examples of block-level elements. Block-level elements can be styled and manipulated using CSS properties to control their appearance, layout, and positioning on a web page.
Here’s an example of HTML code that includes various block-level elements:
<!DOCTYPE html> <html> <head> <style> .box { margin: 10px; padding: 10px; background-color: lightgray; } </style> </head> <body> <div class="box"> <h1>This is a heading</h1> <p>This is a paragraph of text.</p> <ul> <li>List item 1</li> <li>List item 2</li> <li>List item 3</li> </ul> <blockquote>This is a blockquote.</blockquote> <article> <h2>Article Title</h2> <p>This is an article.</p> </article> </div> </body> </html>
Explanation:
1-In this example, we have a <div> element with a class of “box” that serves as a container for our block-level elements.
2-We apply some basic styling to the container using CSS.
3-Inside the container, we have various block-level elements:
Each block-level element starts on a new line and takes up the full width available within the container.
By adding this code to an HTML file and opening it in a browser, you will see the block-level elements displayed with their default styles.
The block-level elements will be enclosed within the container with the light gray background color.
Tryto modify the content, styles, or add more block-level elements as needed.
Inline-level elements in HTML are elements that do not start on a new line and only occupy as much width as necessary to display their content. They flow within the text and can appear next to each other horizontally. Here are some commonly used inline-level elements:
Here’s an example of HTML code that demonstrates the usage of inline-level elements:
<!DOCTYPE html> <html> <head> <style> .inline { margin-right: 10px; background-color: lightgray; padding: 5px; } </style> </head> <body> <p> Inline elements: <span class="inline">Span</span>, <a href="#">Link</a>, <strong>Strong</strong>, <em>Emphasized</em>. </p> <p> <img src="image.png" alt="Image description" class="inline"> This is an example of an inline image. </p> <form> <label for="name" class="inline">Name:</label> <input type="text" id="name" class="inline"> </form> </body> </html>
Explanation:
1-In this example, we have various inline-level elements used within <p> and <form> elements.
2-The elements are styled using CSS, with the inline class applied to add margin, background color, and padding.
3-When you open this HTML code in a browser, you will see the inline-level elements displayed within the text flow.
4-They will appear next to each other horizontally, with the specified styles applied.
Tryto modify the content, styles, or add more inline-level elements as needed.
Here’s an example of HTML code that demonstrates the usage of inline-block elements:
<!DOCTYPE html> <html> <head> <style> .inline-block { display: inline-block; width: 200px; height: 100px; background-color: lightblue; margin: 10px; padding: 10px; } </style> </head> <body> <div class="inline-block"> This is an inline-block element. </div> <div class="inline-block"> Another inline-block element. </div> <div class="inline-block"> Yet another inline-block element. </div> </body> </html>
1-In this example, we have three <div> elements with a class of “inline-block”.
2-We apply the display: inline-block property to these elements, making them inline-block elements.
3-We also set a fixed width and height, background color, margin, and padding to give them some styling.
4-The inline-block elements flow within the text, similar to inline elements, but they can have specified width and height like block-level elements.
5-They will appear next to each other horizontally, maintaining their width and height.
6-When you open this HTML code in a browser, you will see the inline-block elements displayed as three blue boxes with the specified content inside.
7-The elements will be positioned horizontally, with the specified styles applied.
Tryto modify the content, styles, or add more inline-block elements as needed.
Here’s an example of HTML code that demonstrates the usage of flexbox layout using the display: flex property:
<!DOCTYPE html> <html> <head> <style> .flex-container { display: flex; justify-content: space-between; } .flex-item { width: 200px; height: 100px; background-color: lightgreen; margin: 10px; padding: 10px; } </style> </head> <body> <div class="flex-container"> <div class="flex-item"> Flex item 1 </div> <div class="flex-item"> Flex item 2 </div> <div class="flex-item"> Flex item 3 </div> </div> </body> </html>
Explanation:
1-In this example, we have a <div> element with a class of “flex-container” that serves as the flex container.
2-We apply the display: flex property to this element, making it a flex container.
3-We also use the justify-content property with a value of space-between to distribute the flex items horizontally with equal space between them.
4-Inside the flex container, we have three <div> elements with a class of “flex-item”.
5-These are the flex items within the flex container. We set a fixed width and height, background color, margin, and padding to give them some styling.
6-The flex items will be arranged horizontally within the flex container, with equal space between them.
7-The flex container will expand or shrink based on the available space. If the available space is not sufficient, the flex items may wrap onto the next line.
8-When you open this HTML code in a browser, you will see the flex items displayed as three green boxes with the specified content inside. The flex items will be positioned horizontally, with equal space between them.
Tryto modify the content, styles, or add more flex items as needed. You can also explore additional flexbox properties to further customize the layout and alignment of flex items within the flex container.
Here’s an example of HTML code that demonstrates the usage of CSS Grid Layout using the display: grid property:
<!DOCTYPE html> <html> <head> <style> .grid-container { display: grid; grid-template-columns: 1fr 1fr 1fr; grid-gap: 10px; } .grid-item { background-color: lightyellow; padding: 10px; } </style> </head> <body> <div class="grid-container"> <div class="grid-item"> Grid item 1 </div> <div class="grid-item"> Grid item 2 </div> <div class="grid-item"> Grid item 3 </div> <div class="grid-item"> Grid item 4 </div> <div class="grid-item"> Grid item 5 </div> <div class="grid-item"> Grid item 6 </div> </div> </body> </html>
Explanation:
1-In this example, we have a <div> element with a class of “grid-container” that serves as the grid container.
2-We apply the display: grid property to this element, making it a grid container.
3-We also use the grid-template-columns property to define three equal columns within the grid using the 1fr unit.
4-Additionally, we set the grid-gap property to 10px to create a 10-pixel gap between grid items.
5-Inside the grid container, we have six <div> elements with a class of “grid-item”.
6-These are the grid items within the grid container. We apply some basic styling with a yellow background color and padding.
7-The grid items will be arranged in a grid layout based on the specified columns and gaps.
8-In this example, there are three columns with equal widths and a 10-pixel gap between them.
When you open this HTML code in a browser, you will see the grid items displayed as yellow boxes with the specified content inside. The grid items will be positioned in a grid layout, flowing across the columns and rows defined by the grid container.
Tryto modify the content, styles, or add more grid items as needed. You can also explore additional grid properties to further customize the layout and alignment of grid items within the grid container.
The display: none property is used to hide an element completely.
When applied to an element, it removes the element from the normal flow of the document, and it doesn’t take up any space on the page.
Here’s an example of how you can use the display: none property in HTML:
<!DOCTYPE html> <html> <head> <style> .hidden-element { display: none; } </style> </head> <body> <p>This paragraph is visible.</p> <div class="hidden-element"> <p>This paragraph is hidden.</p> </div> <p>This paragraph is also visible.</p> </body> </html>
1-In this example, we have three <p> elements. The second <p> element is enclosed within a <div> element with a class of “hidden-element”.
2-We apply the display: none property to the “hidden-element” class, making it hidden from view.
3-When you open this HTML code in a browser, you will see that the first and third <p> elements are visible, appearing as regular paragraphs. 4-However, the second <p> element within the “hidden-element” div is completely hidden, and it doesn’t occupy any space on the page.
You can use JavaScript or CSS to dynamically toggle the display: none property and control the visibility of elements based on various conditions or user interactions.
Note that applying display: none to an element removes it from the accessibility tree, so screen readers and other assistive technologies will not perceive its content. Use this property with caution and ensure that it doesn’t impact the accessibility of your website or application.
To override the default display value of an element, you can use CSS to explicitly set a different display value.
Here’s an example:
<!DOCTYPE html> <html> <head> <style> .custom-element { display: inline-block; } </style> </head> <body> <span>This is a default inline element.</span> <div class="custom-element">This element overrides the default display value.</div> </body> </html>
Explanation:
1-In this example, we have a <span> element, which is an inline-level element by default.
2-We also have a <div> element with a class of “custom-element”. In the CSS section, we use the .custom-element selector to target the specific element and override its default display value.
3-By setting display: inline-block for the .custom-element, we change its display behavior to be an inline-level element that also respects width and height properties like a block-level element.
4-It will flow inline with other elements but maintain its own specified dimensions.
When you open this HTML code in a browser, you will see that the <span> element behaves as a default inline element, while the <div> element with the “custom-element” class behaves as an inline-block element.
You can adjust the display value to suit your specific layout requirements by modifying the CSS code. Remember that different elements have different default display values, and overriding the default display should be done thoughtfully to ensure proper layout and rendering.
To hide an element, you have two common options: display: none and visibility: hidden.
Here’s an explanation of each option and their differences:
display: none:
Example:
<div style=”display: none;”>This element is hidden.</div>
visibility: hidden:
Example:
<div style=”visibility: hidden;”>This element is hidden but still occupies space.</div>
So, which one should you use? It depends on your specific needs:
Use display: none when you want to completely remove the element from the document flow and don’t want it to take up any space. This is useful when you want to dynamically show or hide an element and ensure it doesn’t affect the layout.
Use visibility: hidden when you want to hide the element while preserving its space in the layout. This is useful when you want to toggle visibility and maintain the position of surrounding elements.
Remember to consider accessibility implications when choosing between these options. If an element needs to be completely hidden from assistive technologies, display: none is the appropriate choice.
Here’s an example of HTML code that demonstrates the use of display: none to hide an element:
<!DOCTYPE html> <html> <head> <style> .hidden-element { display: none; } </style> </head> <body> <p>This paragraph is visible.</p> <div class="hidden-element"> <p>This element is hidden using display: none.</p> </div> <p>This paragraph is also visible.</p> </body> </html>
Explanation:
1-In this example, we have three <p> elements.
2-The second <p> element is enclosed within a <div> element with a class of “hidden-element”.
3-We apply the display: none property to the “hidden-element” class, effectively hiding it from view.
4-When you open this HTML code in a browser, you will see that the first and third <p> elements are visible, appearing as regular paragraphs. However, the second <p> element within the “hidden-element” div is completely hidden, and it doesn’t occupy any space on the page.
5-The element with display: none is effectively removed from the HTML structure and does not appear in the rendered output.
6-This can be useful when you want to hide an element dynamically or toggle its visibility using JavaScript or CSS.
Tryto modify the content or add more elements with the “hidden-element” class as needed.
Here’s an example of HTML code that demonstrates the use of visibility: hidden to hide an element while preserving its space in the layout:
<!DOCTYPE html> <html> <head> <style> .hidden-element { visibility: hidden; } </style> </head> <body> <p>This paragraph is visible.</p> <div class="hidden-element"> <p>This element is hidden using visibility: hidden.</p> </div> <p>This paragraph is also visible.</p> </body> </html>
Explanation:
1-In this example, we have three <p> elements.
2-The second <p> element is enclosed within a <div> element with a class of “hidden-element”.
3-We apply the visibility: hidden property to the “hidden-element” class, effectively hiding it from view.
4-When you open this HTML code in a browser, you will see that the first and third <p> elements are visible, appearing as regular paragraphs. 5-However, the second <p> element within the “hidden-element” div is hidden from view, while still occupying its space in the layout.
The element with visibility: hidden is not visible on the page, but it still exists within the HTML structure. It preserves its space, which means other elements are not repositioned to fill the gap.
This property is useful when you want to toggle the visibility of an element while maintaining its position in the layout. It can be achieved dynamically using JavaScript or CSS.
Tryto modify the content or add more elements with the “hidden-element” class as needed.
The main differences between display: none and visibility: hidden are as follows:
display: none: The element is completely removed from the document flow, and it does not occupy any space on the page. Other elements will adjust their positions accordingly as if the element doesn’t exist.
visibility: hidden: The element is hidden from view, but it still occupies space in the layout. It remains in the document flow, and other elements are not repositioned. The element’s space is preserved, even though it’s not visible.
Accessibility:
display: none: The element is removed from the accessibility tree and is not accessible to screen readers or other assistive technologies. It effectively makes the element invisible and inaccessible.
visibility: hidden: The element remains in the accessibility tree, and it is still accessible to screen readers and other assistive technologies. The content of the hidden element can still be accessed by assistive technology.
Event Handling:
display: none: The element and its children do not receive any events, such as clicks or keyboard events. Event handlers associated with the element are not triggered.
visibility: hidden: The element and its children continue to receive events as if they were visible. Event handlers associated with the element can still be triggered.
Transition and Animation:
display: none: Transitions or animations applied to the element are not visible since the element is removed from the rendering flow.
visibility: hidden: Transitions or animations applied to the element will still take effect, but the element will remain hidden during the transition or animation.
In summary, display: none completely removes the element from the layout, while visibility: hidden hides the element while preserving its space. Additionally, display: none affects accessibility and event handling differently compared to visibility: hidden. The choice between the two depends on your specific requirements for layout, accessibility, and interactivity.
Here’s an example of HTML code that demonstrates the difference in layout and space between display: none and visibility: hidden:
<!DOCTYPE html> <html> <head> <style> .hidden-element-display { display: none; } .hidden-element-visibility { visibility: hidden; } .box { width: 200px; height: 100px; background-color: lightblue; margin: 10px; padding: 10px; } </style> </head> <body> <div class="box">This is a visible box.</div> <div class="box hidden-element-display">This box is hidden using display: none.</div> <div class="box hidden-element-visibility">This box is hidden using visibility: hidden.</div> </body> </html>
Explanation:
1-In this example, we have three <div> elements with the class “box”. The first box is visible, while the second and third boxes are hidden using different techniques.
2-The second box uses display: none with the class “hidden-element-display”. It is completely removed from the layout, and as a result, it doesn’t occupy any space on the page. Other elements will adjust their positions accordingly.
3-The third box uses visibility: hidden with the class “hidden-element-visibility”. It is hidden from view, but it still occupies space in the layout. Other elements are not repositioned, and the space reserved for the box is maintained.
4-When you open this HTML code in a browser, you will see the visible box displayed as a light blue box. The second box, hidden using display: none, will not appear on the page at all. The third box, hidden using visibility: hidden, will take up space in the layout but won’t be visible.
Tryto modify the content or styles as needed to observe the differences in layout and space between display: none and visibility: hidden.
Here’s an example of HTML code that demonstrates the difference in accessibility between display: none and visibility: hidden
<!DOCTYPE html> <html> <head> <style> .hidden-element-display { display: none; } .hidden-element-visibility { visibility: hidden; } </style> </head> <body> <h1>Visible Heading</h1> <div class="hidden-element-display"> <h2>This heading is hidden using display: none.</h2> </div> <div class="hidden-element-visibility"> <h2>This heading is hidden using visibility: hidden.</h2> </div> </body> </html>
Explanation:
1-In this example, we have three heading elements: one visible heading (<h1>) and two hidden headings (<h2>).
2-The second heading is hidden using display: none, and the third heading is hidden using visibility: hidden.
3-When you open this HTML code in a browser, you will see the visible heading displayed as a level 1 heading.
4-The second heading, hidden using display: none, will not appear on the page at all.
5-This means it is also removed from the accessibility tree, and screen readers will not detect it.
6-The third heading, hidden using visibility: hidden, will take up space in the layout but won’t be visible.
However, it remains in the accessibility tree, and screen readers can still detect and read its content.
To observe the accessibility differences, you can use a screen reader to navigate through the page and notice that the hidden heading using display: none is not announced, while the hidden heading using visibility: hidden is still accessible to the screen reader.
Remember that accessibility is crucial, and you should consider the impact of hiding elements on users who rely on assistive technologies. Ensure that your choice of hiding technique aligns with your accessibility requirements.
Event Handling:complete code in html
Here’s an example of HTML code that demonstrates the difference in event handling between display: none and visibility: hidden:
<!DOCTYPE html> <html> <head> <style> .hidden-element-display { display: none; } .hidden-element-visibility { visibility: hidden; } .box { width: 200px; height: 100px; background-color: lightblue; margin: 10px; padding: 10px; } </style> <script> function handleClick() { console.log("Box clicked!"); } </script> </head> <body> <div class="box" onclick="handleClick()">Visible Box (Click me!)</div> <div class="box hidden-element-display" onclick="handleClick()">Box hidden using display: none (Click me!)</div> <div class="box hidden-element-visibility" onclick="handleClick()">Box hidden using visibility: hidden (Click me!)</div> </body> </html>
Explanation:
1-In this example, we have three <div> elements with the class “box”. 2-Each box has an onclick attribute that calls a JavaScript function handleClick() when clicked.
3-The first box is visible, and when you click on it, the function handleClick() is executed, and “Box clicked!” is logged to the console.
4-The second box uses display: none with the class “hidden-element-display”. It is completely removed from the layout, and as a result, it doesn’t trigger any click events.
5-The third box uses visibility: hidden with the class “hidden-element-visibility”. It is hidden from view but remains in the layout.
6-When you click on it, the function handleClick() is executed, and “Box clicked!” is logged to the console.
7-When you open this HTML code in a browser and interact with the boxes, you will observe that only the visible box triggers the click event and logs the message to the console.
8-The hidden box using display: none does not respond to the click event since it is not rendered, while the hidden box using visibility: hidden still triggers the click event even though it’s not visible.
This demonstrates the difference in event handling between display: none and visibility: hidden.
When using display: none, the element and its children do not receive any events, while with visibility: hidden, the element and its children continue to receive events as if they were visible.
Try to modify the content or event handling code as needed to observe the differences in event handling behavior.
Here’s an example of HTML code that demonstrates the difference in transition and animation between display: none and visibility: hidden:
<!DOCTYPE html> <html> <head> <style> .hidden-element-display { display: none; } .hidden-element-visibility { visibility: hidden; } .box { width: 200px; height: 100px; background-color: lightblue; margin: 10px; padding: 10px; transition: opacity 0.5s; } .box:hover { opacity: 0.5; } </style> </head> <body> <div class="box">Visible Box (Hover over me!)</div> <div class="box hidden-element-display">Box hidden using display: none (Hover over me!)</div> <div class="box hidden-element-visibility">Box hidden using visibility: hidden (Hover over me!)</div> </body> </html>
Explanation:
1-In this example, we have three <div> elements with the class “box”. 2-Each box has a CSS transition effect that changes the opacity of the element on hover.
3-The first box is visible, and when you hover over it, it smoothly transitions to a lower opacity due to the transition property set on the .box class.
4-The second box uses display: none with the class “hidden-element-display”.
5-It is completely removed from the layout and does not trigger any transition effect when hovered over because it is not rendered.
6-The third box uses visibility: hidden with the class “hidden-element-visibility”. It is hidden from view but remains in the layout.
7-When you hover over it, the transition effect is triggered, and it smoothly transitions to a lower opacity, just like the visible box.
8-When you open this HTML code in a browser and interact with the boxes, you will observe that only the visible box and the hidden box using visibility: hidden trigger the transition effect on hover.
9-The hidden box using display: none does not trigger any transition effect since it is not rendered.
This demonstrates the difference in transition and animation behavior between display: none and visibility: hidden.
Transitions and animations applied to elements with display: none are not visible since the element is removed from the rendering flow.
On the other hand, transitions and animations applied to elements with visibility: hidden will still take effect, but the element will remain hidden during the transition or animation.
Try to modify the content or transition properties as needed to observe the differences in transition and animation behavior.
You can use CSS classes and JavaScript to dynamically show or hide content on a webpage.
Here’s an example that demonstrates this approach:
<!DOCTYPE html> <html> <head> <style> .hidden { display: none; } </style> </head> <body> <button onclick="showContent()">Show Content</button> <div id="content" class="hidden"> <h1>Hidden Content</h1> <p>This content is initially hidden.</p> </div> <script> function showContent() { var contentElement = document.getElementById("content"); contentElement.classList.remove("hidden"); } </script> </body> </html>
Explanation:
1-In this example, we have a button and a <div> element with an initial CSS class of “hidden” that sets display: none to hide the content.
2-When the button is clicked, the showContent() JavaScript function is triggered.
3-This function gets a reference to the content element using getElementById(“content”) and then removes the “hidden” class from the element using classList.remove(“hidden”).
4-By removing the “hidden” class, the content element becomes visible since the CSS rule that hides it is no longer applied.
5-The content is dynamically shown on the webpage.
6-You can customize the content within the <div> element and modify the JavaScript function to suit your specific requirements.
7-Additionally, you can use CSS animations or transitions to create smooth transitions when showing or hiding content.
Remember to include the JavaScript code within a <script> tag and place it either in the <head> section or just before the closing </body> tag.
Tryto extend this example by adding more content or implementing additional functionality using JavaScript and CSS.
Here’s an example of a complete HTML code demonstrating the application of CSS display and visibility properties:
<!DOCTYPE html> <html> <head> <style> .box { width: 200px; height: 100px; background-color: lightblue; margin: 10px; padding: 10px; transition: opacity 0.5s; } .hidden { display: none; } .visible { visibility: visible; } .fade-in { opacity: 1; } .fade-out { opacity: 0; } </style> <script> function showHiddenElement() { var hiddenElement = document.getElementById("hiddenElement"); hiddenElement.style.display = "block"; } function toggleVisibility() { var visibleElement = document.getElementById("visibleElement"); visibleElement.classList.toggle("hidden"); } function fadeInOut() { var fadingElement = document.getElementById("fadingElement"); fadingElement.classList.toggle("fade-out"); fadingElement.classList.toggle("fade-in"); } </script> </head> <body> <button onclick="showHiddenElement()">Show Hidden Element</button> <div id="hiddenElement" class="box hidden"> <h2>Hidden Element</h2> <p>This element is hidden initially.</p> </div> <button onclick="toggleVisibility()">Toggle Visibility</button> <div id="visibleElement" class="box visible"> <h2>Visible Element</h2> <p>This element can be toggled to be hidden or visible.</p> </div> <button onclick="fadeInOut()">Toggle Fade</button> <div id="fadingElement" class="box fade-out"> <h2>Fading Element</h2> <p>This element fades in and out when the button is clicked.</p> </div> </body> </html>
Explanation:
In this example, we have three different scenarios:
Try to run this code in a browser and interact with the buttons to observe the effects of the display and visibility properties in action.
You can modify the content, styles, and JavaScript functions as needed to suit your specific requirements and explore different applications of display and visibility properties.
Here’s a multi-choice quiz to test your understanding of the CSS display and visibility properties:
1-Which CSS property is used to control the visibility and layout of elements on a webpage?
a) visibility
b) display
c) layout
d) position
2-The display: none property does the following:
a) Makes the element invisible while still occupying space in the layout.
b) Completely removes the element from the document flow and does not occupy any space.
c) Sets the element to display as an inline-level element.
d) Sets the element to display as a block-level element.
3-The visibility: hidden property does the following:
a) Completely removes the element from the document flow and does not occupy any space.
b) Makes the element invisible while still occupying space in the layout.
c) Sets the element to display as an inline-level element.
d) Sets the element to display as a block-level element.
4-Which property is suitable if you want to hide an element from assistive technologies like screen readers?
a) display: none
b) visibility: hidden
c) Both display: none and visibility: hidden hide elements from assistive technologies.
d) Neither display: none nor visibility: hidden hide elements from assistive technologies.
5-What happens to event handling when an element has the CSS property display: none applied to it?
a) The element and its children can still receive events and trigger event handlers.
b) The element and its children cannot receive any events or trigger event handlers.
6-What happens to event handling when an element has the CSS property visibility: hidden applied to it?
a) The element and its children can still receive events and trigger event handlers.
b) The element and its children cannot receive any events or trigger event handlers.
Answers:
Tryto test your knowledge by answering these questions.
7-Which CSS value can be used to make an element display as an inline-level element while still allowing width and height properties to be set?
a) block
b) inline
c) inline-block
d) flex
8-What happens to the layout of other elements when an element with display: none is applied?
a) Other elements adjust their positions to fill the space of the hidden element.
b) Other elements stay in their original positions.
c) Other elements are completely removed from the layout.
9-Which CSS property can be used to toggle the visibility of an element using JavaScript?
a) display
b) visibility
c) opacity
d) position
10-What is the difference between display: none and visibility: hidden regarding accessibility?
a) Both properties have the same impact on accessibility.
b) display: none hides the element from assistive technologies, while visibility: hidden keeps it accessible.
c) visibility: hidden hides the element from assistive technologies, while display: none keeps it accessible.
11-When an element is hidden with display: none, does it trigger any CSS transitions or animations?
a) Yes, transitions and animations are still applied to the element.
b) No, transitions and animations are not applied to the element.
Answers:
12-Which CSS property is used to hide an element from view while still occupying space in the layout?
a) display
b) visibility
c) opacity
d) position
13-When an element is hidden using visibility: hidden, is it still accessible to screen readers and assistive technologies?
a) Yes, the element remains accessible.
b) No, the element is also hidden from assistive technologies.
14-What happens to the space occupied by an element when display: none is applied?
a) The space is preserved, and other elements are adjusted accordingly.
b) The space is removed, and other elements fill in the gap.
15-Which CSS property can be used to dynamically show or hide content based on user interactions or events using JavaScript?
a) display
b) visibility
c) opacity
d) position
16-Which CSS value is used to display an element as a block-level element that starts on a new line?
a) block
b) inline
c) inline-block
d) flex
Answers:
17-Which CSS property is used to hide an element from view, but it can still be interacted with and responds to events?
a) display
b) visibility
c) opacity
d) pointer-events
18-When an element is hidden using display: none, does it still affect the layout of other elements on the page?
a) Yes, it still affects the layout of other elements.
b) No, it does not affect the layout of other elements.
19-Which CSS property is used to transition the visibility of an element with a smooth fade-in or fade-out effect?
a) display
b) visibility
c) opacity
d) transition
20-What is the default value of the display property for most HTML elements?
a) block
b) inline
c) inline-block
d) flex
21-Which CSS property can be used to control the stacking order of elements on the page?
a) display
b) visibility
c) opacity
d) z-index
Answers:
22-Which CSS property can be used to make an element fixed in its position relative to the viewport?
a) display
b) visibility
c) position
d) float
23-When an element is hidden using display: none, is it included in the document flow?
a) Yes, it is included in the document flow.
b) No, it is not included in the document flow.
24-Which CSS property can be used to vertically align inline or inline-block elements?
a) display
b) visibility
c) opacity
d) vertical-align
25-What is the difference between display: inline and display: inline-block?
a) Both properties make elements display inline.
b) display: inline makes elements display as blocks, while display: inline-block makes elements display inline.
c) display: inline does not allow width and height to be set, while display: inline-block does allow width and height to be set.
26-Which CSS property can be used to create flexible and responsive layouts?
a) display
b) visibility
c) opacity
d) flex
Answers:
27-Which CSS property can be used to create a grid-based layout system?
a) display
b) visibility
c) opacity
d) grid-template-columns
28-When an element is hidden using visibility: hidden, does it still take up space in the layout?
a) Yes, it still takes up space.
b) No, it does not take up space.
29-Which CSS property can be used to horizontally align a block-level element within its container?
a) display
b) visibility
c) opacity
d) margin
30-What is the default value of the visibility property?
a) visible
b) hidden
31-Which CSS property can be used to control the order of flex items within a flex container?
a) display
b) visibility
c) opacity
d) order
Answers:
Here are some references that you can explore for further learning and understanding of the CSS display and visibility properties:
CSS-Tricks – CSS Display Property:
CSS-Tricks – CSS Visibility Property:
CSS Layout – display, float, position, and more:
Accessible Rich Internet Applications (ARIA) – WAI-ARIA: