Use text-align, margin: 0 auto to center in CSS When using text-align or margin: 0 auto to center the page, some invalid situations may occur. Here are two examples to explain and solve the problem. Example 1: Centering an inline element using text-align Let's say I want to center an image within its parent component. First, the parent component should be a block element (div, p...), and then add the attribute text-align:center in the parent component , and then all the inline elements (span, img...) in this component will be displayed in the center. Example 2: Centering a block element using margin The prerequisite for margin: 0 auto is that the element has width. If you want to center a div, you need to set the width for it and then add the margin: 0 auto style. Experimental results: Experiment Code <template> <div class="father"> <div> <img src="~@/assets/img/rules-detail-no-record.png"> <span>I am the inner div content</span> </div> <span>I am the outer div content</span> <div class="bg"></div> </div> </template> <script> export default { name: "test" } </script> <style scoped> .father{ //Center the inline elements surrounded by it background: #2e90fc; text-align: center; } .bg{ //Block element centeredbackground: #fc2e80; width: 60px; height: 60px; margin: 0 auto; } </style> Summarize This is the end of this article about example codes for using text-align and margin: 0 auto to center in CSS. For more relevant content about using text-align and margin: 0 auto to center in CSS, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! |
<<: Discussion on CSS style priority and cascading order
>>: Detailed explanation of Nginx passively checking the server's survival status
error message: ERROR 1862 (HY000): Your password ...
Basic concepts: Macvlan working principle: Macvla...
Table of contents introduction Why bother? Commun...
Table of contents Written in front 1. Ngixn image...
My97DatePicker is a very flexible and easy-to-use...
Effect screenshots: Implementation code: Copy code...
You can install Docker and perform simple operati...
After I installed MySQL, when I tried to save and...
first step: In VMware, click "Edit" - &...
Table of contents 1. Scenario 2. Solution 3. Conc...
1. Node server setup + database connection The op...
Introduction to Flex Layout Flex in English means...
structure body, head, html, title text abbr, acro...
0x00 Introduction A few months ago, I found a vul...
This article uses examples to describe the operat...