When we design a page, we often need to center the DIV, and center it horizontally and vertically relative to the page window, such as centering the login window. So far, many methods have been explored. HTML: XML/HTML CodeCopy content to clipboard
Effect diagram (the following methods have the same effect diagram): The first one: CSS absolute positioning Mainly use absolute positioning, and then use margin to adjust to the middle position. Parent element: CSS CodeCopy content to clipboard
Child Elements: CSS CodeCopy content to clipboard
Horizontal and vertical center alignment: CSS CodeCopy content to clipboard
The second method: CSS absolute positioning + Javascript/JQuery Mainly use absolute positioning, and then use Javascript/JQuery to adjust it to the middle position. Compared with the first method, this method improves the flexibility of the class. Parent element: CSS CodeCopy content to clipboard
Child Elements: CSS CodeCopy content to clipboard
Horizontal and vertical center alignment: CSS CodeCopy content to clipboard
JQuery: JavaScript CodeCopy content to clipboard
The third type: CSS3 absolute positioning + displacement The same effect can be achieved by using absolute positioning and CSS3's transform: translate. Parent element: CSS CodeCopy content to clipboard
Child Elements: CSS CodeCopy content to clipboard
Horizontal and vertical center alignment: CSS CodeCopy content to clipboard
Fourth: Flexbox: [Flexible Layout Box Model] It's too easy to make elements horizontal and vertical with the Flexbox model. Here we need to change the HTML: XML/HTML CodeCopy content to clipboard
Parent element: CSS CodeCopy content to clipboard
Child Elements: C# Code to copy content to the clipboard
Horizontal and vertical center alignment: CSS CodeCopy content to clipboard
Comparison of several methods: The first type of CSS absolute positioning margin adjustment has good compatibility but lacks flexibility. If there are many boxes that need to be centered horizontally and vertically, different .align-center should be written because of their different widths and heights. The above is the full content of this article. I hope it will be helpful for everyone’s study. |
<<: CSS delivery address parallelogram line style example code
>>: MySQL query statement grouped by time
I have read a lot of knowledge and articles about...
During today's lecture, I talked about the di...
Container lifecycle The life cycle of a container...
Preface One of the functions of an interceptor is...
This article shares the specific code for impleme...
Table of contents Overview Form validation withou...
Project Scenario Add a custom watermark to the en...
Table of contents background Understanding compos...
1. Write a backup script rem auther:www.yumi-info...
Table of contents 1. What are microtasks? 2. What...
1. Use CSS to draw a small pointed-corner chat di...
When you use HTML div blocks and the middle of th...
Table of contents 1. Introduction 2. Basic Concep...
Main library execution CREATE DATABASE test CHARA...
I am currently developing a new app project. This...