How to center div in html

藏色散人
Release: 2023-01-06 11:15:09
Original
37690 people have browsed it

htmlHow to center the div: 1. Center the div by adding the "

content
" tag; 2. Add the "margin:0 auto attribute;" to the div to automatically adjust it Centered.

How to center div in html

The operating environment of this article: Windows7 system, HTML5&&CSS3 version, Dell G3 computer.

There are two methods for DIV centering:

1. The simple and quick method is to add the

content
tag.

Example:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>center居中</title>
</head>
 
<body>
<center>
<div>我要居中啦</div>
</center>
</body>
</html>
Copy after login

2. Add the margin:0 auto attribute to the div; automatically adjust the center.

Example 2:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>margin居中</title>
<style type="text/css">
.d1{
border:1px solid red;
width:200px;
height:200px;
text-align:center;
line-height:40px;
margin:0 auto;
}
</style>
</head>
<body>
<div class="d1">
我是div,我居中啦...
</div>
</body>
</html>
Copy after login

Recommended tutorials: html video tutorial, css video tutorial

The above is the detailed content of How to center div in html. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!