1. Set the parent container to a table and the child to an inline element. Suitable for sub-content to display text. <!-- css --> <style> #parent { height: 200px; width: 200px; border: 1px solid red; display: table-cell; /* Transform into a table */ text-align: center; /* horizontal */ vertical-align: middle; /* vertical */ } #child { background-color: blue; color: white; display: inline; /* Child elements are set to inline or inline block*/ } </style> <!-- html --> <div id="parent"> <div id="child">Content</div> </div> 2. Set relative positioning for the parent container and absolute positioning for the child container, and then center it using margins. <!-- css --> <style> #parent { height: 200px; width: 200px; border: 1px solid red; position: relative; /* Set relative positioning */ } #child { height: 50px; width: 50px; color: white; background-color: blue; /* Absolute positioning, after setting the 4 directions to 0, the margin is set to auto */ position: absolute; left: 0; top: 0; right: 0; bottom: 0; margin: auto; } </style> <!-- html --> <div id="parent"> <div id="child"></div> </div> 3. The parent container is set to a flexible box and the child sets the margins. <!-- css --> <style> #parent { height: 200px; width: 200px; border: 1px solid red; display: flex; /* Convert the parent element to a flexible box*/ display: -webkit-flex; /* Safari */ } #child { height: 50px; width: 50px; background-color: blue; margin: auto; } </style> <!-- html --> <div id="parent"> <div id="child"></div> </div> 4. Set relative positioning for the parent container, absolute positioning for the child, and negative half width for the left and top margins. Suitable for situations where the width and height of the child are fixed. <!-- css --> <style> #parent { height: 200px; width: 200px; border: 1px solid red; position: relative; /* Set relative positioning */ } #child { /* When the child element knows its own width and height*/ background-color: blue; width: 50px; height: 50px; margin-top: -25px; margin-left: -25px; position: absolute; left: 50%; top: 50%; } </style> <!-- html --> <div id="parent"> <div id="child"></div> </div> 5. Set relative positioning for the parent container and absolute positioning for the child container, and use the transformation attribute to set the horizontal and vertical directions to negative half. Suitable for situations where the width and height of the child are not fixed. <!-- css --> <style> #parent { height: 200px; width: 200px; border: 1px solid red; position: relative; /* Set relative positioning */ } #child { /* Child element does not know its own width and height, use transform's translate */ border: 1px solid blue; position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%,-50%); -ms-transform: translate(-50%,-50%); -o-transform: translate(-50%,-50%); transform: translate(-50%,-50%); } </style> <!-- html --> <div id="parent"> <div id="child"> <div id="content"> Content 1 <br/> Content 2 </div> </div> </div> 6. Set the parent to a flexible box and set the alignment properties. <!-- css --> <style> #parent { height: 200px; width: 200px; border: 1px solid red; display: flex; /* Convert the parent element to a flexible box*/ display: -webkit-flex; /* Safari */ justify-content: center;/* horizontal */ align-items: center; /* vertical */ } #child { height: 50px; width: 50px; background-color: blue; } </style> <!-- html --> <div id="parent"> <div id="child"></div> </div> The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. |
<<: 3 simple ways to achieve carousel effects with JS
>>: Tips on HTML formatting and long files for web design
1. Problem During development, when inserting dat...
This article records the complete uninstallation ...
Table of contents 1. Project Prospects 2. Knowled...
1. Download Go to the Apache official website htt...
Let's make a simple 3D Rubik's Cube today...
Sometimes you need to use links, but you don't...
Pop-up windows are often used in actual developme...
Today we will make a simple case, using js and jq...
I spent almost two hours trying various methods. ...
This article shares with you the graphic tutorial...
There are two types of html tags, inline elements...
This article shares the specific code of vue echa...
Possible solutions 1. Math.random generates rando...
1. Introduction Presto is an open source distribu...