Home Web Front-end CSS Tutorial How to hide page elements with css? Four implementation methods of css hidden elements and the differences between them (code examples)

How to hide page elements with css? Four implementation methods of css hidden elements and the differences between them (code examples)

Sep 13, 2018 am 10:17 AM
css hidden element

In CSS, there are many ways to hide elements (meaning invisible to the naked eye within the screen), some occupy space, some do not occupy space; some can respond to clicks, and some cannot respond to clicks. So how do these CSS methods implement hidden elements, and what are the differences between them. This chapter will introduce to you how to hide page elements in css? Four implementation methods of css hidden elements and the differences between them (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Four implementations of css hidden elements

1. visibility: hidden, set the element to be hidden

Code example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>元素隐藏--visibility: hidden</title>
		<style>
			.demo{
				width: 800px;
				height: 200px;
				margin: 50px auto;
			}
			span{
				visibility: hidden;
			}
		</style>
	</head>
	<body>
		<div class="demo">
			<p>元素隐藏1--visibility: hidden,元素隐藏2--visibility: hidden,元素隐藏3--visibility: hidden</p>
			<p>元素隐藏1--visibility: hidden,<span>元素隐藏2--visibility: hidden</span>,元素隐藏3--visibility: hidden</p>
		</div>
	</body>
</html>
Copy after login

Rendering:

How to hide page elements with css? Four implementation methods of css hidden elements and the differences between them (code examples)

The visibility attribute specifies whether the element is visible. Setting its value to hidden only visually hides the element. The hidden element itself still occupies its own position and contributes to our web page layout.

2. Opacity: 0, set the element to hide

Code example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>元素隐藏--opacity: 0</title>
		<style>
			.demo{
				width: 800px;
				height: 200px;
				margin: 50px auto;
			}
			span{
				opacity: 0;
			}
		</style>
	</head>
	<body>
		<div class="demo">
			<p>元素隐藏1--opacity: 0,元素隐藏2--opacity: 0,元素隐藏3--opacity: 0</p>
			<p>元素隐藏1--opacity: 0,<span>元素隐藏2--opacity: 0</span>,元素隐藏3--opacity: 0</p>
		</div>
	</body>
</html>
Copy after login

Rendering:

How to hide page elements with css? Four implementation methods of css hidden elements and the differences between them (code examples)

opacity The property means to set the transparency of an element. It is not designed to change the bounding box of an element. This means that setting opacity to 0 only visually hides the element. The element itself still occupies its own position and contributes to the layout of the web page. This is similar to visibility: hidden above.

3. display:none, set the element to hide

Code example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>元素隐藏--display:none</title>
		<style>
			.demo{
				width: 800px;
				height: 200px;
				margin: 50px auto;
			}
			span{
				opacity: 0;
			}
		</style>
	</head>
	<body>
		<div class="demo">
			<p>元素隐藏--display:none,元素隐藏--display:none,元素隐藏--display:none</p>
			<p>元素隐藏--display:none,<span>元素隐藏--display:none</span>,元素隐藏--display:none</p>
		</div>
	</body>
</html>
Copy after login

Rendering:

How to hide page elements with css? Four implementation methods of css hidden elements and the differences between them (code examples)

##display Attributes truly hide elements according to the meaning of the word. Setting the display attribute to none ensures that the element is not visible and even the box model is not generated. Using this attribute, hidden elements do not occupy any space. Not only that, once display is set to none, any direct user interaction operations on the element will not be effective.

4. position: absolute, set the element to hide

Code implementation:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>元素隐藏--position: absolute</title>
		<style>
			.demo{
				width: 800px;
				height: 200px;
				margin: 50px auto;
			}
			span{
				position: absolute;
				top: -9999px;
   				left: -9999px;
			}
		</style>
	</head>
	<body>
		<div class="demo">
		<p>元素隐藏1--position: absolute,元素隐藏2--position: absolute,元素隐藏3--position: absolute</p>
		<p>元素隐藏1--position: absolute,<span>元素隐藏2--position: absolute</span>,元素隐藏3--position: absolute</p>
		</div>
	</body>
</html>
Copy after login
Rendering:

How to hide page elements with css? Four implementation methods of css hidden elements and the differences between them (code examples)##position : absolute, the main principle of setting the hidden element is to make it invisible on the screen by setting the top and left of the element to a large enough negative number.

2. Difference

1.visibility:hidden The element will be hidden, but it will not disappear and still occupy space. The original HTML style will not be changed after hiding; It will be inherited by descendants, who can also unhide it by setting visibility: visible;

Events that have been bound to this element will not be triggered. Dynamically modifying this property will cause redrawing.



2. Opacity:0
Only the transparency is 100%, the element is hidden, and it still occupies space. The original html style will not be changed after hiding;
will be inherited by child elements, and the child Elements cannot be unhidden through opacity=1;

Elements with opacity:0 can still trigger bound events.



3. display:none
does not occupy space and hides the element, so dynamically changing this attribute will cause rearrangement (change the page layout), which can be understood as placing the element on the page. The same thing happens when the element is deleted;
will not be inherited by descendants, but its descendants will not be displayed. After all, they are all hidden together.


4. position: absolute

position: absolute, by setting the top and left of the element to a large enough negative number, it will be invisible on the screen and have a dynamic interactive effect. .

The above is the detailed content of How to hide page elements with css? Four implementation methods of css hidden elements and the differences between them (code examples). For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1245
24
How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

See all articles