CSS display:none and visibility:hidden

2019-01-26 08:07发布

I have a div that I use to display alerts when needed.

If I want to close it after a while can I use display:none or should I use display:none as well as visibility:hidden?

So one or both.

Thank you.

标签: css hide hidden
3条回答
2楼-- · 2019-01-26 08:48

If your hidden content needs to be accessible—to those with screen readers, for example—then you should not use display: none or visibility: hidden, as both can potentially hide content from screen readers. Instead, you should use a more accessible approach, such as moving the content off screen with a negative margin. See the following links for more information:

456 Berea Street: Hiding with CSS: Problems and solutions
WebAIM Blog: Hiding content for screen readers

查看更多
唯我独甜
3楼-- · 2019-01-26 08:51

Visibility:hidden hides the element but it still takes up space in the layout. Display:none removes it completely.

In your case, I would use Display:none

查看更多
Deceive 欺骗
4楼-- · 2019-01-26 09:06

Depends. If you need the space to be left blank, that is, the space won't be taken up by other elements below or around it, you'll need visibility: hidden. Otherwise, use display: none, which will allow other elements to move into the element's place.

There's no reason to use both.

查看更多
登录 后发表回答