Home >Web Front-end >CSS Tutorial >Using CSS to achieve horizontal centering of DIV

Using CSS to achieve horizontal centering of DIV

php中世界最好的语言
php中世界最好的语言Original
2017-11-21 09:46:5424272browse

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 .

Using CSS to achieve horizontal centering of DIV

#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

Using CSS to achieve horizontal centering of DIV

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:

How to convert html to txt file


How to convert table and td Set the background to realize the table border


How to skillfully set the table border for table and td

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more