Display, Visibility, and Opacity

An interesting thing about web-page elements: they can be completely transparent and invisible, but still affect the layout of the page. The reason is that invisibility/transparency and display/lack of display are not the same thing in CSS.

An element can be hidden by setting visibility to hidden, or shown by setting visibility to visible. The property can also be set to inherit, and the element inherits its visible property setting from the containing element.

As demonstrated in Chapter 11, an element’s opacity can also be altered until it is completely transparent, making it invisible. However, just as with the visibility property, the element still maintains its position within the page flow.

If an element’s display property is set to none, it’s also hidden; however, any effect the element has on the page layout is also removed. To make it visible, you have a couple of options. You can make it visible and have it act as a block-level element (line breaks before and after the element) by setting display to block. If you don’t want block behavior, you can set display to inline, and it’s displayed in-place and not as a block.

In addition, you can display the element using the default display characteristics of several HTML elements, which include inline-block, table, table-cell, list-item, compact, run-in, etc. It’s a rather powerful attribute, and one worth playing around with until you’re comfortable with its modifying results.

Right ...

Get Learning JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.