Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Hiding Elements

The last value of display that we'll look at is none. An element with display: none isn't displayed at all, and it takes up no space: the elements after it move up as if it weren't there. It's still in the HTML; the browser just doesn't show it.

Compare it with opacity: 0, which you know from the lesson on colors. That makes an element fully transparent, but the element still takes up its space, so an empty gap stays on the page.

Hiding is useful for content that should appear only at a certain moment: a menu that opens when you hover over it, a tooltip, or the answer to a quiz question. We hide such an element with display: none, and we show it again in a rule with the pseudo-class :hover, which you know from the lesson on selectors.

We've added a short quiz to the Physiology section of the page. Let's hide its answer until the reader hovers over the quiz with the mouse.

Instructions

  1. 1

    Create a rule for .answer that sets display: none. The answer disappears, and the quiz box gets smaller, because the hidden paragraph takes up no space.

  2. 2

    Create a rule for .quiz:hover .answer that sets display: block. Its selector selects the answer in the quiz, but only while the mouse pointer is over the quiz. Its specificity is higher than that of .answer, so while you hover, it wins. Run the code and hover over the quiz: the answer appears.

Back

4/10

  1. 1. Normal Flow and Block Elements
  2. 2. Inline Elements
  3. 3. Inline-Block Elements
  4. 4. Hiding Elements
  5. 5. Relative Position
  6. 6. Absolute Position
  7. 7. Absolute Inside Relative
  8. 8. Fixed Position
  9. 9. Sticky Position
  10. 10. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.