Home Web Front-end HTML Tutorial Introduction to the method of setting the center in html (code)

Introduction to the method of setting the center in html (code)

Sep 01, 2018 am 10:49 AM
html center

The content of this article is about the introduction (code) of the centering setting method in HTML. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Horizontal centering

In the actual development process, we will encounter many situations where elements need to be horizontally centered, such as article titles, etc. Common horizontal centering situations here include inline elements and block-level elements. Block-level elements are further divided into fixed-width block-level elements and variable-width block-level elements. Fixed-width block-level elements, as the name suggests, mean that the width of block-level elements is a fixed value; then, for variable-width block-level elements, we know that the width of block-level elements is not a fixed value.

Inline elements

When the element being set is an inline element such as text or picture, we do this by setting text-align:center to the parent element .

<body>
    <div class="textCenter">这是一个在父元素中居中元素</div>
</body>
<style>
    textCenter{
    text-align:center;
    }
</style>
Copy after login

It can be seen from the above code that "This is a centered element in the parent element". The CSS style of the parent element of this text adds the text-align:center; attribute, so the text is displayed in the center. However, this method is not applicable when the element to be set is a block-level element. Block-level elements are divided into two types: fixed-width block-level elements and variable-width block-level elements.

Fixed-width block-level elements

To meet the two conditions of "fixed-width" and "block-level elements" for fixed-width block-level elements, you can set the left and right margin values For auto to achieve centering.

<body>
    <div>水平居中的定宽块级元素</div>
</body>
<style>
    div{
        border:1px solid blue;
        width:100px;    /*宽度设置固定值*/
        margin:10px auto;
    }
</style>
/*或者也可以写成 margin-left:auto;
               margin-right:auto;*/
/* 元素的上下margin属性可以照常设置,不受影响 */
Copy after login

Variable-width block-level elements

There are three ways to center variable-width block-level elements: the first is to add the table tag; the second is to set the display: The inline method is similar to the first method. The display type is set to inline elements and the attributes of the variable-width elements are set. The third method is to set position:relative and left:50%, and use relative positioning to shift the element to the left. Move 50% to achieve centering.

Add table tag

Joining table tag takes advantage of the length adaptability of table tag (its length is not defined and the length of the parent element body is not defaulted. The length of table is Determined according to the length of the inner text), it can be regarded as a fixed-width block-level element, and then use the margin method of the fixed-width block-level element to center it horizontally.

The first step to use is to add a table tag outside the element that needs to be centered, and then set "left and right margins to be centered" for the table

<div>
    <table>
        <tbody>
            <tr><td>
            <ul>
                <li>锄禾日当午</li>
                <li>汗滴禾下土</li>
                <li>谁知盘中餐</li>
                <li>粒粒皆辛苦</li>
            </ul>
            </td></tr>
        </tbody>
    </table>
</div>
<style>
    table{
    border:1px solid;
    margin:0 auto;
    }
</style>
Copy after login

Set the display:inline method

Change the display of block-level elements to inline type, set it to display inline elements, and then use text-align:center to achieve centered display. The advantage of this method compared to the table method is that there is no need to add unsemantic tags, but this method also has certain problems, that is, it changes the display of block elements to inline, and the elements will be less visible after they become inline elements. Function usage.

<body>
    <div class="container">
        <ul>
            <li><a href="#">First</a></li>
            <li><a href="#">Second</a></li>
            <li><a href="#">Third</a></li>
        </ul>
    </div>
</body>
 
<style>
.container{
    text_align:center;
}
.container ul{
    list-style:none;
    margin:0;
    padding:0;
    display:inline;
}
 
.container li{
margin-right:10px;
display:inline;
}
</style>
Copy after login

Set position:relative and left:50%

By setting float to the parent element, then setting position:relative and left:50%, the child element sets position :relative and left:50% to achieve horizontal centering.

<body>
<div class="container">
    <ul>
        <li><a href=""#>First</a></li>
        <li><a href=""#>Second</a></li>
        <li><a href=""#>Third</a></li>
        <li><a href=""#>Fourth</a></li>
    </ul>
</div>
</body>
 
<style>
.container{
    float:left;
    position:relative;
    left:50%;
}
 
.container ul{
    list-style:none;
    margin:0;
    padding:0;
 
    position:relative;
    left:-50%
}
 
.container li{
    float:left;
    display:inline;
    margin-right:8px
}
</style>
Copy after login

Vertical centering

Vertical centering is divided into two situations: single-line text with a determined height of the parent element and multi-line text with a determined height of the parent element.

A single line of text whose height is determined by the parent element

The method for vertically centering a single line of text whose height is determined by the parent element is to set the height of the parent element to be consistent with line-height. to achieve. height is the height of the element, line-height is the line height, that is, line spacing, which is the distance between the baselines between lines. The calculated difference between line-height and font-size is divided in half (called "line spacing" in CSS) and added to the top and bottom of a line of text. The smallest box that can contain this content is a line box. This kind of text line height and block height brings a disadvantage, that is, when the length of the text content is greater than the width of the block, some content will break away from the block.

<div class="container">
    hello,world!
</div>
 
<style>
.container{
    height:10px;
    line-height:10px;
}
</style>
Copy after login

Multi-line text with a certain height of the parent element

There are two ways to vertically center multi-line text, pictures, etc. with a certain height of the parent element. The first is Insert the table tag and set vertical-align:middle. There is an attribute vertical-align in CSS for vertical centering. When the parent element sets this style, it will be useful for all inline-block type child elements.

/* 方式一 */
<body>
<table><tbody><tr><td class="wrap">
<div>
    <p>居中一下</p>
</div>
</td></tr></tbody></table>
</body>
 
<style>
table td{
    height:500px;
    background:#ccc;
}
</style>
 
/* 方式二 */
<div class="container">
    <div>
        <p>居中一下下</p>
        <p>居中一下下</p>
        <p>居中一下下</p>
        <p>居中一下下</p>
        <p>居中一下下</p>
    </div>
</div>
<style>
.container{
    height:300px;
    background:#ccc;
    display:table-cell;/*IE8以上及Chrome、Firefox*/
    vertical-align:middle;/*IE8以上及Chrome、Firefox*/
}
</style>
Copy after login

In browsers such as chrome, firefox and IE8 or above, you can set the display of block-level elements to table-cell and activate the vertical-align attribute, but note that IE6 and 7 do not support this style.

Invisibly changing the display type

When we set the position:absolute or float:left attribute for an element during our development process, the display type of the element will automatically change to display:inline_block (block-level element), you can set the width and height of the element.

<div class="container">
    <a href="#" title="">点这里看看</a>
</div>
<style>
.container a{
    position;absolute;
    width:200px;
    background:#ccc;
}
</style>
Copy after login

Related recommendations:

The difference between css html element centering and html element content centering

html elements are horizontal and vertical How to set the centering

The above is the detailed content of Introduction to the method of setting the center in html (code). For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles