Do you know the centering attribute of the html body tag? Definition and usage of html body tag

寻∝梦
Release: 2018-08-20 14:14:17
Original
25403 people have browsed it

html Do you know the centering attribute of the body tag? The definition and usage of html body tag. The following article mainly explains to you the definition of the html body tag and the two methods of centering attributes, as well as the definition and use of the html body tag

The definition and use of the html body tag Usage:

The body element defines the body of the document.

The body element contains all the content of the document (such as text, hyperlinks, images, tables, lists, etc.)

HTML tag example

A simple HTML document with the most basic required elements:

<html>
<head>
  <title>文档的标题</title>
</head>
<body>
  文档的内容... ...
</body>
</html>
Copy after login

html The centered attribute of the body tag

html body tag The centering attribute (1):

How to use basic centering:

<html>
<head>
<title>居中</title>
</head>
<body style="text-align:center;">
<h2>居中</h2>
</body>
</html>
Copy after login

html The centering attribute of the body tag (2):

## How to use the #div tag to center it horizontally and vertically in the body:

Horizontal centering: The method of centering the div tag in the entire body can be controlled with CSS, body{text-align:center;}.box{margin: 0 auto;} The class box is centered horizontally in the entire body

Vertical centering: The vertical centering of div in the body is not very effective with CSS control, so it is more effective to use js to control, the code is as follows (jQuery ):

function SetPostion(){
        var wheight = $(window).height();    //浏览器的高度
        var boxheight = $(“.box”).height();     //box的高度
         //浏览器的高度若大于box的高度,才设置box垂直居中
       if(wheight > boxheight){            
             var h = (wheight -boxheight)/2;         //计算box距顶端的距离
            $(“.box”).css(“margin-top” ,h+”px”)   //设置box的margin-top属性
         }
}
Copy after login
The css class named box is vertically centered

Browser support

All major browsers support Label.

[Related recommendations]

Is the HTML del tag a block-level element? Specific application method of html del tag

#What are the attributes of html hr tag? Detailed explanation of the style of HTML hr tag

The above is the detailed content of Do you know the centering attribute of the html body tag? Definition and usage of html body tag. 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