Home Web Front-end CSS Tutorial How to set the suspension effect in css

How to set the suspension effect in css

May 06, 2021 pm 06:15 PM
css levitate

How to set the suspension effect in css: first add the "position: fixed;" style to the element to fix the position of the element so that the element is suspended in the page and does not change with the scroll bar of the browser window; then use The top, bottom, left, and right attributes can set the floating position of the element.

How to set the suspension effect in css

The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.

css setting suspension effect (fixed position)

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
		<title>DIV悬浮示例-纯CSS实现</title>

		<style type="text/css">
			/*设置了高度,可以上下滚动*/
			
			body {
				height: 1800px;
				background: #dddddd;
			}
			/*div通用样式*/
			
			div {
				background: #1a59b7;
				color: #ffffff;
				overflow: hidden;
				z-index: 9999;
				position: fixed;  /*DIV悬浮(固定位置)*/
				padding: 5px;
				text-align: center;
				width: 175px;
				height: 22px;
				border-bottom-left-radius: 4px;
				border-bottom-right-radius: 4px;
				border-top-left-radius: 4px;
				border-top-right-radius: 4px;
			}
			/*右上角*/
			
			div.right_top {
				right: 10px;
				top: 10px;
			}
			/*右下角*/
			
			div.right_bottom {
				right: 10px;
				bottom: 10px;
			}
			/*屏幕中间*/
			
			div.center_ {
				right: 45%;
				top: 50%;
			}
			/*左上角*/
			
			div.left_top {
				left: 10px;
				top: 10px;
			}
			/*左下角*/
			
			div.left_bottom {
				left: 10px;
				bottom: 10px;
			}
		</style>

	</head>

	<body>
		<div class="right_top"> 我是右上角悬浮的div</div>
		<div class="right_bottom"> 我是右下角悬浮的div</div>
		<div class="center_"> 我是屏幕中间悬浮的div</div>
		<div class="left_top"> 我是左上角悬浮的div</div>
		<div class="left_bottom"> 我是左下角悬浮的div</div>
	</body>

</html>
Copy after login

Rendering:

How to set the suspension effect in css

Description: Fixed positioning (position: fixed;)

fixed generates fixed positioning elements. The elements are separated from the document flow and do not occupy the position of the document flow. It can be understood as floating above the document flow, relative to The browser window is positioned.

If you want to set a fixed positioning in the layer model for an element, you need to set position:fixed; and directly use the browser window as a reference for positioning. It floats in the page, and the element position will not follow the browser window. The scroll bar changes as you scroll, unless you move the screen position of the browser window on the screen, or change the display size of the browser window, so fixedly positioned elements will always be located somewhere in the view within the browser window and will not be affected by the document flow effects.

For example, if we prepare a long string of text so that the text exceeds the width of one screen, set the left and top of the floating element div1 to 100px, and drag the browser's scroll bar, the position of the floating element div1 will not change. Variety.

(Learning video sharing: css video tutorial)

The above is the detailed content of How to set the suspension effect in css. 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 Article Tags

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)

What does placeholder mean in vue What does placeholder mean in vue May 07, 2024 am 09:57 AM

What does placeholder mean in vue

How to write spaces in vue How to write spaces in vue Apr 30, 2024 am 05:42 AM

How to write spaces in vue

How to get dom in vue How to get dom in vue Apr 30, 2024 am 05:36 AM

How to get dom in vue

What does span mean in js What does span mean in js May 06, 2024 am 11:42 AM

What does span mean in js

What does rem mean in js What does rem mean in js May 06, 2024 am 11:30 AM

What does rem mean in js

How to introduce images into vue How to introduce images into vue May 02, 2024 pm 10:48 PM

How to introduce images into vue

What is the function of span tag What is the function of span tag Apr 30, 2024 pm 01:54 PM

What is the function of span tag

What language is the browser plug-in written in? What language is the browser plug-in written in? May 08, 2024 pm 09:36 PM

What language is the browser plug-in written in?

See all articles