How to set the centering of a div using css: first set "text-align:center" on the body; then set the css style to "margin:0 auto" for the div box that needs to be centered to center the corresponding div horizontally .
#How to use CSS to center a DIV? I have made an easy-to-understand code for everyone, using CSS to realize the horizontal center display of DIV. Friends in need can refer to it.
There are two main css codes required, one is text-align:center (content is centered), the other is margin:0 auto; the other two Styles need to be used together to achieve centered display layout of the div box.
First we set text-align:center on the body, and then set the css style margin:0 auto on the div box that needs to be centered, so that the corresponding div can be horizontally centered.
div centering code application, in order to observe the div centering effect, we set a div named ".div" for the div, use class="div" in the div tag in the html, and set its width to 400px; height to 100px with red border. So that we can observe the effect.
1. Complete html+css code
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style> body{ text-align:center} .div{ margin:0 auto; width:400px; height:100px; border:1px solid #F00} /* css注释:为了观察效果设置宽度 边框 高度等样式 */ </style> </head> <body> <div class="div"> DIVCSS5实例 </div> </body> </html>
2. Screenshot of div+css centering example
div css screenshot to achieve centering effect
div to achieve centering effect Screenshot
3. Application features of div centering code
This centering method is to make the div centering effect perfectly compatible with all major platforms and browsers, no matter how high or low Both versions of IE and higher versions of IE are compatible.
This is how to use CSS to achieve horizontal centering of DIV.
Related reading:
The above is the detailed content of Using CSS to achieve horizontal centering of DIV. For more information, please follow other related articles on the PHP Chinese website!

In this week's roundup: Firefox gains locksmith-like powers, Samsung's Galaxy Store starts supporting Progressive Web Apps, CSS Subgrid is shipping in Firefox

In this week's roundup: Internet Explorer finds its way into Edge, Google Search Console touts a new speed report, and Firefox gives Facebook's notification

You’re probably already at least a little familiar with CSS variables. If not, here’s a two-second overview: they are really called custom properties, you set

Building websites is programming. Writing HTML and CSS is programming. I am a programmer, and if you're here, reading CSS-Tricks, chances are you're a

Here's what I'd like you to know upfront: this is a hard problem. If you've landed here because you're hoping to be pointed at a tool you can run that tells

Picture-in-Picture made its first appearance on the web in the Safari browser with the release of macOS Sierra in 2016. It made it possible for a user to pop

Gatsby does a great job processing and handling images. For example, it helps you save time with image optimization because you don’t have to manually

I learned something about percentage-based (%) padding today that I had totally wrong in my head! I always thought that percentage padding was based on the


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment