Home Web Front-end CSS Tutorial Reference method and application method of absolute positioning

Reference method and application method of absolute positioning

Jan 23, 2024 am 08:50 AM
Applications relative positioning reference element

Reference method and application method of absolute positioning

Reference method and application of absolute positioning

Absolute positioning (Absolute Positioning) is a commonly used layout method in CSS. By specifying an element relative to its nearest non- Static (default positioning method) is positioned at the position of the parent element or document. Using absolute positioning, elements can be placed at any location without being affected by other elements, providing a more flexible layout.

Reference method of absolute positioning
In CSS, absolutely positioned elements have the following characteristics:

  1. Absolutely positioned elements are specified through the top, right, bottom, and left attributes. s position.
  2. The position of an absolutely positioned element is relative to its nearest non-statically positioned parent element or document.
  3. If no non-statically positioned parent element is found, the position of the absolutely positioned element is relative to the position of the document.

Application scenarios of absolute positioning

  1. Precise layout: Absolute positioning can separate elements from the document flow and achieve precise layout effects. For example, we can use absolute positioning to implement elements such as navigation bars and floating advertisements in web pages that need to be fixed in a certain position and not scroll with the page.
  2. Picture album: Using absolute positioning, we can achieve the layout effect in the picture album. Each picture element can be positioned by setting the position attribute to achieve the effect of free placement.
  3. Modal box: Absolute positioning can be used to implement a modal box. By setting the position attribute of the modal box to center, it will always be in the center of the page and remain fixed no matter how the scroll bar scrolls. Location.
  4. Suspended prompt box: When an element needs to be suspended, a prompt box will pop up, which can be achieved by using absolute positioning. By setting the position property of the prompt box, position it relative to the floating element and display it.

Code examples for absolute positioning
The following are code examples for absolute positioning in several common application scenarios:

  1. Navigation bar layout

HTML:

<nav class="navbar">
  <ul class="navbar-list">
    <li>首页</li>
    <li>关于我们</li>
    <li>产品服务</li>
    <li>联系我们</li>
  </ul>
</nav>
Copy after login

CSS:

.navbar {
  position: absolute;
  top: 20px;
  left: 20px;
}

.navbar-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.navbar-list li {
  display: inline-block;
  margin-right: 10px;
}
Copy after login
  1. Picture Album Layout

HTML:

<div class="photo-gallery">
  <img  class="photo lazy"  src="/static/imghw/default1.png"  data-src="photo1.jpg"    style="max-width:90%" alt="Reference method and application method of absolute positioning" >
  <img  class="photo lazy"  src="/static/imghw/default1.png"  data-src="photo2.jpg"    style="max-width:90%" alt="Reference method and application method of absolute positioning" >
  <img  class="photo lazy"  src="/static/imghw/default1.png"  data-src="photo3.jpg"    style="max-width:90%" alt="Reference method and application method of absolute positioning" >
  <img  class="photo lazy"  src="/static/imghw/default1.png"  data-src="photo4.jpg"    style="max-width:90%" alt="Reference method and application method of absolute positioning" >
</div>
Copy after login

CSS:

.photo-gallery {
  position: relative;
  width: 500px;
  height: 500px;
}

.photo {
  position: absolute;
  width: 200px;
  height: 200px;
  border: 1px solid #000;
}
Copy after login

Absolute positioning is a commonly used layout method in CSS and is suitable for many scenarios. By setting relative position attributes, elements can be freely placed on the page to achieve precise layout effects. In actual development, we can reasonably use absolute positioning to achieve various layout effects of web pages according to specific needs.

The above is the detailed content of Reference method and application method of absolute positioning. 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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
Does sticky positioning break away from the document flow? Does sticky positioning break away from the document flow? Feb 20, 2024 pm 05:24 PM

Does sticky positioning break away from the document flow? Specific code examples are needed. In web development, layout is a very important topic. Among them, positioning is one of the commonly used layout techniques. In CSS, there are three common positioning methods: static positioning, relative positioning and absolute positioning. In addition to these three positioning methods, there is also a more special positioning method, namely sticky positioning. So, does sticky positioning break away from the document flow? Let’s discuss it in detail below and provide some code examples to help understand. First, we need to understand what document flow is

Analyzing Python application cases in IoT security Analyzing Python application cases in IoT security Jun 30, 2023 pm 05:18 PM

As a high-level programming language, Python plays an important role in the field of IoT security. This article will analyze the application of Python in IoT security from the perspective of practical application cases. 1. Embedded device firmware hardening Many devices in the Internet of Things, such as cameras and smart home devices, run their own embedded operating systems and firmware. These devices are often exposed on public networks and are easy targets for hackers. In order to improve the security of the device, the firmware needs to be hardened. Via Python you can

How to center the box in html5 How to center the box in html5 Apr 05, 2024 pm 12:27 PM

To center the box in HTML5, there are the following methods: horizontal centering: text-align: centermargin: autodisplay: flex; justify-content: center; vertical centering: vertical-align: middletransform: translate(-50%, -50%); position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);

How to position elements in css How to position elements in css Apr 26, 2024 am 10:24 AM

There are four methods of CSS element positioning: static, relative, absolute, and fixed positioning. Static positioning is the default and the element is not affected by positioning rules. Relative positioning moves an element relative to itself without affecting document flow. Absolute positioning removes an element from the document flow and positions it relative to its ancestor elements. Fixed positioning positions an element relative to the viewport, always keeping it in the same position on the screen.

bottom attribute syntax in CSS bottom attribute syntax in CSS Feb 21, 2024 pm 03:30 PM

Bottom attribute syntax and code examples in CSS In CSS, the bottom attribute is used to specify the distance between an element and the bottom of the container. It controls the position of an element relative to the bottom of its parent element. The syntax of the bottom attribute is as follows: element{bottom:value;} where element represents the element to which the style is to be applied, and value represents the bottom value to be set. value can be a specific length value, such as pixels

What are relative positioning technologies? What are relative positioning technologies? Oct 19, 2023 pm 05:42 PM

Relative positioning technologies include radio ranging, sonar, laser ranging, cameras, inertial navigation, satellite navigation, indoor positioning, ultrasonic, infrared, electromagnetic waves, Bluetooth positioning, Wi-Fi positioning, ultrasonic ranging, infrared ranging, and laser ranging. , camera visual positioning, RSSI-based positioning, TOA-based positioning, TDOA-based positioning, AOA-based positioning, etc. Detailed introduction: 1. Radio ranging: Calculate the distance by measuring the time difference of radio waves from the transmitting point to the receiving point; 2. Sonar technology, etc.

Application cases of WebSocket in real-time game development Application cases of WebSocket in real-time game development Oct 15, 2023 am 09:59 AM

Introduction to the application case of WebSocket in real-time game development: With the continuous development of network technology, the demand for real-time games is also growing. The traditional HTTP protocol often cannot meet the requirements of immediacy and real-time performance in real-time game scenarios. As an emerging communication protocol, WebSocket has been widely used in real-time game development. This article will use specific cases and sample code to explore the application of WebSocket in real-time game development. 1. What is WebSocketWebSo

The role and application cases of Redis in the Internet of Things system The role and application cases of Redis in the Internet of Things system Nov 07, 2023 am 09:52 AM

The role and application cases of Redis in the Internet of Things system. With the rapid development of Internet of Things technology, people's demand for data storage and processing is increasing. As a high-performance in-memory database, Redis is widely used in Internet of Things systems. This article will introduce in detail the role and application cases of Redis in the Internet of Things system, and give specific code examples. 1. The role of Redis in the Internet of Things system Redis is a high-performance in-memory database. Its main function is to accelerate the reading and writing speed of data and improve the data processing speed.

See all articles