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

How to set the suspension effect in css

青灯夜游
青灯夜游Original
2021-05-06 18:15:1720594browse

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>

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:
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