


How to achieve CSS centering? ?Common methods for CSS centering_html/css_WEB-ITnose
Source: http://www.ido321.com/824.html
1. Horizontal centering
1. Centering of inline elements: relative to parent Block-level elements are centered and aligned
1: .center-children {
2: text-align: center;
3: }
2. Block-level elements are centered: set margin-left and Set margin-right to auto to center it (width must also be set, otherwise it will fill the entire container and the centering effect cannot be seen)
1: .center-me {
2: margin: 0 auto;
3: }
If there are many block-level elements that need to be centered horizontally in a row, it is best to use a different display type. Here is an example using inline-block and flex.
Demo address: http://jsfiddle.net/Web_Code/5fvrwwk1/embedded/result/
1: <main class="inline-block-center">
2: <div>
3: I'm an element that is block-like with my siblings and we're centered in a row.
4: </div>
5: <div>
6: I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
7: </div>
8: <div>
9: I'm an element that is block-like with my siblings and we're centered in a row.
10: </div>
11: </main>
12: <main class="flex-center">
13: <div>
14: I'm an element that is block-like with my siblings and we're centered in a row.
15: </div>
16: <div>
17: I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
18: </div>
19: <div>
20: I'm an element that is block-like with my siblings and we're centered in a row.
21: </div>
22: </main>
css:
1: body {
2: background: #f06d06;
3: font-size: 80%;
4: }
5: main {
6: background: white;
7: margin: 20px 0;
8: padding: 10px;
9: }
10: main div {
11: background: black;
12: color: white;
13: padding: 15px;
14: max-width: 125px;
15: margin: 5px;
16: }
17: .inline-block-center {
18: text-align: center;
19: }
20: .inline-block-center div {
21: display: inline-block;
22: text-align: left;
23: }
24: .flex-center {
25: display: flex;
26: justify-content: center;
27: }
2. Vertical centering
1. Inline elements: Set equal upper and lower padding, or use line-height
1: .link {
2: padding-top: 30px;
3: padding-bottom: 30px;
4: }
When the text will not wrap, you can use line-height to align the text by making it equal to height.
1: .center-text-trick {
2: height: 100px;
3: line-height: 100px;
4: white-space: nowrap;
5: }
Multi-line text can also use upper and lower padding You can also center multiple lines, but if this method doesn't work, you can make the containers of these words display in table cell mode, and then set the vertical-align attribute of the text to align,
Demo address: http:/ /jsfiddle.net/Web_Code/5fvrwwk1/1/embedded/result/
html:
1: <table>
2: <tr>
3: <td>
4: I'm vertically centered multiple lines of text in a real table cell.
5: </td>
6: </tr>
7: </table>
8: <div class="center-table">
9: <p>I'm vertically centered multiple lines of text in a CSS-created table layout.</p>
10: </div>
css
1: body {
2: background: #f06d06;
3: font-size: 80%;
4: }
5: table {
6: background: white;
7: width: 240px;
8: border-collapse: separate;
9: margin: 20px;
10: height: 250px;
11: }
12: table td {
13: background: black;
14: color: white;
15: padding: 20px;
16: border: 10px solid white;
17: /* default is vertical-align: middle; */
18: }
19: .center-table {
20: display: table;
21: height: 250px;
22: background: white;
23: width: 240px;
24: margin: 20px;
25: }
26: .center-table p {
27: display: table-cell;
28: margin: 0;
29: background: black;
30: color: white;
31: padding: 20px;
32: border: 10px solid white;
33: vertical-align: middle;
34: }
If you don’t know the element height, then like this
1: .parent {
2: position: relative;
3: }
4: .child {
5: position: absolute;
6: top: 50%;
7: height: 100px;
8: margin-top: -50px; /* 如果没有使用: border-box; 的盒子模型则需要设置这个 */
9: }
1: .parent {
2: position: relative;
3: }
4: .child {
5: position: absolute;
6: top: 50%;
7: transform: translateY(-50%);
8: }
1: <main>
2: <div>
3: I'm a block-level element with an unknown height, centered vertically within my parent.
4: </div>
5: </main>
1: body {
2: background: #f06d06;
3: font-size: 80%;
4: }
5: main {
6: background: white;
7: height: 300px;
8: width: 200px;
9: padding: 20px;
10: margin: 20px;
11: display: flex;
12: flex-direction: column;
13: justify-content: center;
14: resize: vertical;
15: overflow: auto;
16: }
17: main div {
18: background: black;
19: color: white;
20: padding: 20px;
21: resize: vertical;
22: overflow: auto;
23: }
1: //这种方案有极好的跨浏览器支持。
2: .parent {

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-
