What is the difference between visibility:hidden and display:none?

前端 未结 18 1589
余生分开走
余生分开走 2020-11-21 04:50

The CSS rules visibility:hidden and display:none both result in the element not being visible. Are these synonyms?

18条回答
  •  天涯浪人
    2020-11-21 05:21

    One thing worth adding, though it wasn't asked, is that there is a third option of making the object completely transparent. Consider:

    1st unseen link.
    2nd unseen link.
    3rd unseen link.

    (Be sure to click "Run code snippet" button above to see the result.)

    The difference between 1 and 2 has already been pointed out (namely, 2 still takes up space). However, there is a difference between 2 and 3: in case 3, the mouse will still switch to the hand when hovering over the link, and the user can still click on the link, and Javascript events will still fire on the link. This is usually not the behavior you want (but maybe sometimes it is?).

    Another difference is if you select the text, then copy/paste as plain text, you get the following:

    1st link.
    2nd  link.
    3rd unseen link.
    

    In case 3 the text does get copied. Maybe this would be useful for some type of watermarking, or if you wanted to hide a copyright notice that would show up if a carelessly user copy/pasted your content?

提交回复
热议问题