A piece of code to clarify the difference between CSS attributes display and visibility_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:50:00
Original
1122 people have browsed it

If you want to hide a DOM element with CSS, there are two methods: set the display attribute to none, or set the visibility attribute to hidden. The html code below does not set the display and visibility attributes.

<div id="outA" style="width:400px; height:400px; background:#CDC9C9;position:relative;">	<div id="outB" style="height:200; background:#0000ff;top:100px;position:relative;">	</div>	<div id="outC" style="height:100px; background:#FFB90F;top:50px;position:relative;">	</div></div>
Copy after login


Use display:none to hide outB

<div id="outA" style="width:400px; height:400px; background:#CDC9C9;position:relative;">	<div id="outB" style="height:200; background:#0000ff;top:100px;position:relative;display:none;">	</div>	<div id="outC" style="height:100px; background:#FFB90F;top:50px;position:relative;">	</div></div>
Copy after login

Use visibility:hidden to hide outB

<div id="outA" style="width:400px; height:400px; background:#CDC9C9;position:relative;">	<div id="outB" style="height:200; background:#0000ff;top:100px;position:relative;visibility:hidden;">	</div>	<div id="outC" style="height:100px; background:#FFB90F;top:50px;position:relative;">	</div></div>
Copy after login

Comparing the results, it is easy to draw the conclusion: with display:none, the element is actually removed from the page, and the elements behind it will automatically move up; while visibility:hidden only hides the element, it The page space occupied still exists.

For the outB element, what will be the result if display and visibility are combined?

display:block;visibility:hidden; outB is invisible, but still takes up page space.

display:none;visibility:hidden; outB is invisible and does not occupy page space.

display:none;visibility:visible; outB is invisible and does not occupy space.

display:block;visibility:visible; outB is visible and definitely takes up space.

The above test results are consistent under IE11/FF17/Chrome39.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!