Preface I have been summarizing my front-end knowledge recently and have also participated in some interviews. During the interview, I encountered a question requiring vertical centering. Now I will summarize this to consolidate my knowledge. CSS vertical centering 1. Use line-height to achieve centering. This method is suitable for pure text. <!-- css --> <style> .parents { height: 400px; line-height: 400px; width: 400px; border: 1px solid red; text-align: center; } .child { background-color: blue; color: #fff; } </style> </head> <body> <!-- html --> <div class="parents"> <span class="child">css layout, vertical centering</span> </div> </body> 2. By setting the relative positioning of the parent container and the absolute positioning of the child, the label is adaptively centered through margin; <!-- css --> <style> .parents { height: 400px; width: 400px; border: 1px solid red; position: relative; } .child { width: 200px; height: 100px; line-height: 100px; text-align: center; color: #fff; background-color: blue; /* Set the four directions to 0, and then use margin to auto to adaptively center*/ position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto; } </style> </head> <body> <!-- html --> <div class="parents"> <span class="child">css layout, vertical centering</span> </div> </body> 3. Flexible layout flex The parent is set to display: flex; the child is set to margin auto to achieve adaptive centering; <!-- css --> <style> .parents { height: 400px; width: 400px; border: 1px solid red; display: flex; } .child { width: 200px; height: 100px; line-height: 100px; text-align: center; color: #333; background-color: yellow; margin: auto; } </style> </head> <body> <!-- html --> <div class="parents"> <span class="child">css layout, vertical centering</span> </div> </body> 4. The parent is set to relative positioning, the child is set to absolute positioning, and it is achieved through displacement transform; <!-- css --> <style> .parents { height: 400px; width: 400px; border: 1px solid red; position: relative; } .child { width: 200px; height: 100px; line-height: 100px; text-align: center; color: #fff; background-color: green; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } </style> </head> <body> <!-- html --> <div class="parents"> <span class="child">css layout, vertical centering</span> </div> </body> 5. The parent sets the elastic box and sets the elastic box related properties; <!-- css --> <style> .parents { height: 400px; width: 400px; border: 1px solid red; display: flex; justify-content: center; /* horizontal */ align-items: center; /* vertical */ } .child { width: 200px; height: 100px; color: black; background-color: orange; } </style> </head> <body> <!-- html --> <div class="parents"> <span class="child"></span> </div> </body> 6. Grid layout, the parent is converted into a table, and then the child is set to inline or inline block. (It should be noted that the prerequisite for using vertical-align: middle is inline elements and elements with a display value of table-cell). <!-- css --> <style> .parents { height: 400px; width: 400px; border: 1px solid red; display: table-cell; text-align: center; vertical-align: middle; } .child { width: 200px; height: 100px; color: #fff; background-color: blue; display: inline-block; /* Child element settings inline or inline block*/ } </style> </head> <body> <!-- html --> <div class="parents"> <span class="child"></span> </div> </body> Summarize The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. |
>>: Web skills: Multiple IE versions coexistence solution IETester
Problem: The website published through IIS is pla...
Table of contents MySQL delete syntax alias probl...
As the cost of building HTTPS websites decreases,...
This article shares the specific code of the WeCh...
Let me briefly explain the functional scenario: T...
Flex layout is undoubtedly simple and easy to use...
Effect diagram: Overall effect: Video loading: Ph...
I divide the whole process into four steps: Downl...
What is a descending index? You may be familiar w...
Today I happened to be helping a friend move his ...
error message: ERROR 2002 (HY000): Can't conn...
1. Introduction to VMware vSphere VMware vSphere ...
The HTML code for intercepting text beyond multipl...
I will explain the installation of MySQL under Wi...
Introduction to AOP The main function of AOP (Asp...