What are the various ways to hide a
?

后端 未结 4 1182
孤独总比滥情好
孤独总比滥情好 2021-01-25 14:40

Right, I am a CSS noob. I am trying to collate the various ways to hide a div.

For example:

display:none;
visibility:hidden;

Are there

相关标签:
4条回答
  • 2021-01-25 15:05

    Not a good practice though, you can use

    opacity:0
    
    0 讨论(0)
  • 2021-01-25 15:10

    I have no idea why do you ask question like that, but here is another way to hide an element:

    element.style {
        width: 0px;
        height: 0px;
        overflow: hidden;
    }
    
    0 讨论(0)
  • 2021-01-25 15:12

    Several possibilities:

    • display: none - This will cause the browser to not render it. It would also make it completely vanish from screen readers as well, so beware.
    • visibility: hidden - This will cause the browser to render it, it wouldn't be visible, but would leave a space corresponding to the element's size.
    • position: absolute and the send it to a ridiculous location (for example, left: -99999px; assuming parent's overflow isn't set to auto or scroll). - This works well when you want the element not actually visible, but still exist in the source or the DOM.
    • Set width and height to 0, and ensure overflow: hidden - Same as above, the element would be completely invisible, but still exist at the DOM or source.
    • opacity: 0 - Would achieve the same effect of visibility: hidden only through different means (i.e. changing the actual opacity of the element).

    Now this all depends on why you need it.

    • Do you want to make the element completely disappear? display: none (and making it reappear with display: block) is your choice.
    • Do you want to animate it? Going with opacity: 0 or width&height is probably a better choice, perhaps with some JavaScript.
    • Want it to be accessible to screen readers, but not actually visible (for example, hidden image captions?), going with position: absolute; left: -99999px works well.
    0 讨论(0)
  • 2021-01-25 15:17

    Another one:

    element.style {
        position: absolute;
        left: -10000000px;
    }
    
    0 讨论(0)
提交回复
热议问题