Use CSS to implement various centering methods
This article mainly introduces the methods of using CSS to achieve various centering. It has certain reference value. Now I share it with you. Friends in need can refer to it.
Centering means we use css to layout. A situation that is often encountered. When using CSS for centering, sometimes a single attribute can do it, and sometimes it requires certain skills to be compatible with all browsers. This article will give a brief introduction to some common methods of centering.
Note: Unless otherwise specified, the methods described in this article are compatible with mainstream browsers such as IE6, Google, and Firefox.
First let’s talk about some simple and harmless centering methods
Set margin to auto
Specifically, it means to put the margin of the element to be centered Both margin-left
and margin-right
are set to auto
. This method can only perform horizontal centering and is invalid for floating elements or absolutely positioned elements.
Use text-align:center
There is nothing to say about this. It can only be used for inline elements such as pictures, buttons, and text (display
is inline
or inline-block
, etc.) for horizontal centering. But it should be noted that in the two weird browsers IE6 and 7, it can horizontally center any element.
Use line-height to center a single line of text vertically
Set the line-height
of the text to the height of the text parent container, which is suitable for situations where there is only one line of text.
4. Use tables
If you are using a table, you don’t have to worry about various centering issues at all, as long as you use the td (may also use th) element align="center"
and valign="middle"
These two attributes can perfectly handle the horizontal and vertical centering of the content inside it, and the table will be aligned by default. The content inside it is vertically centered. If you want to control the centering of table content in css
, you can use vertical-align:middle
for vertical centering. As for horizontal centering, it seems that there is no corresponding in css
attribute, but in IE6 and 7 we can use text-align:center
to horizontally center the elements in the table, IE8 and Google, Firefox and other browsers' text-align: center
only works on inline elements and has no effect on block elements.
In IE6 and 7, you can control the horizontal direction of the table content through css
text-algin
Alignment works regardless of whether the content is an inline element or a block element.
But in ie8 and chrome
, firefox
and other browsers, text-align:center
is blocky The element is invalid, you can only use the table's own align
attribute.
Use display:table-cell to center
For those elements that are not tables, we can use display:table-cell
to simulate it as a table cell , so that you can take advantage of the convenient centering feature of the table. For example:
However, this method can only be used on IE8, Google, Firefox and other browsers, and is invalid for IE6 and IE7.
The methods mentioned there are all very basic, and naturally cannot be called fancy tricks. Here are some centering methods that require the use of some skills.
Use absolute positioning for centering
This method only works for elements whose width or height we already know.
The principle of absolute positioning for centering is to set the left
or top
attribute of the absolutely positioned element to 50%
. At this time The element is not centered, but is offset to the right or left from the centered position by half the width or height of the element, so you need to use a negative margin-left
or margin- The value of top
is used to bring it back to the center position. The negative margin
value is half the width or height of the element.
Running effect:
If you only want to achieve centering in one direction, you can only use left
, margin- left
to achieve horizontal centering, and top
, margin-top
to achieve vertical centering.
Another method of using absolute positioning to center
This method also only works for elements whose width or height we already know, and unfortunately it only supports IE9, Google , Firefox and other modern browsers that comply with w3c standards.
Let’s use a piece of code to understand this method:
Running effect:
If the width and height of the element are not defined here, then he The width will be determined by the values of left and right, and the height will be determined by the values of top and bottom, so the height and width of the element must be set. At the same time, if you change the values of left, right, top, and bottom, you can also offset the element in a certain direction. You can try it yourself.
Use floating and relative positioning to perform horizontal centering
This method is also a solution for how to horizontally center floating elements, and we do not need to know the width of the element that needs to be centered.
The principle of floating centering is to position the floating element relatively to the width of the parent element 50%
, but at this time the element is not centered yet, but more than the centered position. half of its own width. At this time, the child elements inside it need to use a relative positioning to pull back the extra half of its own width. And because relative positioning is positioned relative to itself, half of its own width is The width can be obtained by setting left
or right
to 50%
, so there is no need to know what the actual width is.
This method of using floating and relative positioning to center has the advantage that you don’t need to know the width of the element to be centered, even if the width is constantly changing; the disadvantage is that it requires an extra element to wrap the element to be centered. Elements.
Look at the code:
Running effect:
Use font-size to achieve vertical centering
If the parent The height of the element is known. If you want to center the child elements inside it horizontally and vertically, you can use this method, and the width or height of the child elements does not need to be known.
This method is only valid for IE6 and IE7.
The key point of this method is to set a suitable font-size
value for the parent element. The value of this value is the value obtained by dividing the height of the parent element by 1.14, and the child The element must be an inline
or inline-block
element, and the vertical-align:middle
attribute needs to be added.
As for why it is divided by 1.14 instead of other numbers, no one really knows. You only need to remember the number 1.14.
As mentioned in method 5, it can be used in current browsers such as IE8, Firefox and Google.display:table-cell
to perform centering, and the font-size
method here is suitable for IE6 and IE7, so combining these two methods can be compatible with all browsers:
In the above example, because the element to be centered is a block element, we also need to turn it into an inline element. If the element to be centered is an inline element such as a picture, then This step can be omitted.
In addition, if vertical-align:middle
is written in the center of the parent, this is a situation we often encounter when using CSS for layout. When using CSS for centering, sometimes a single attribute can do it, and sometimes it requires certain skills to be compatible with all browsers. This article will give a brief introduction to some common methods of centering.
Note: Unless otherwise specified, the methods described in this article are compatible with mainstream browsers such as IE6, Google, and Firefox.
First let’s talk about some simple and harmless centering methods
Set margin to auto
Specifically, it means to put the margin of the element to be centered Both margin-left
and margin-right
are set to auto
. This method can only perform horizontal centering and is invalid for floating elements or absolutely positioned elements.
Use text-align:center
There is nothing to say about this. It can only be used for inline elements such as pictures, buttons, and text (display
is inline
or inline-block
, etc.) for horizontal centering. But it should be noted that in the two weird browsers IE6 and 7, it can horizontally center any element.
Use line-height to center a single line of text vertically
Set the line-height
of the text to the height of the text parent container, which is suitable for situations where there is only one line of text.
4. Use the form
If you are using a table, you don’t have to worry about various centering issues at all. Just use the align="center" of the td (and possibly th) element
andvalign="middle"
These two attributes can perfectly handle the horizontal and vertical centering of the content inside it, and the table will vertically center the content inside it by default. If you want to control the centering of table content in css
, you can use vertical-align:middle
for vertical centering. As for horizontal centering, it seems that there is no corresponding in css
attribute, but in IE6 and 7 we can use text-align:center
to horizontally center the elements in the table, IE8 and Google, Firefox and other browsers' text-align: center
only works on inline elements and has no effect on block elements.
In IE6 and 7, you can control the horizontal direction of the table content through css
text-algin
Alignment works regardless of whether the content is an inline element or a block element.
But in ie8 and chrome
, firefox
and other browsers, text-align:center
is blocky The element is invalid, you can only use the table's own align
attribute.
Use display:table-cell to center
For those elements that are not tables, we can use display:table-cell
to simulate it as a table cell , so that you can take advantage of the convenient centering feature of the table. For example:
However, this method can only be used on IE8, Google, Firefox and other browsers, and is invalid for IE6 and IE7.
The methods mentioned there are all very basic, and naturally cannot be called fancy tricks. Here are some centering methods that require the use of some skills.
Use absolute positioning for centering
This method only works for elements whose width or height we already know.
The principle of absolute positioning for centering is to set the left
or top
attribute of the absolutely positioned element to 50%
. At this time The element is not centered, but is offset to the right or left from the centered position by half the width or height of the element, so you need to use a negative margin-left
or margin- The value of top
is used to bring it back to the center position. The negative margin
value is half the width or height of the element.
Running effect:
If you only want to achieve centering in one direction, you can only use left
, margin- left
to achieve horizontal centering, and top
, margin-top
to achieve vertical centering.
Another method of using absolute positioning to center
This method also only works for elements whose width or height we already know, and unfortunately it only supports IE9, Google , Firefox and other modern browsers that comply with w3c standards.
The following is a piece of code to understand this method:
Running effect:
If the width and height of the element are not defined here , then its width will be determined by the values of left and right, and its height will be determined by the values of top and bottom, so the height and width of the element must be set. At the same time, if you change the values of left, right, top, and bottom, you can also offset the element in a certain direction. You can try it yourself.
Use floating and relative positioning to perform horizontal centering
This method is also a solution for how to horizontally center floating elements, and we do not need to know the width of the element that needs to be centered.
The principle of floating centering is to position the floating element relatively to the width of the parent element 50%
, but at this time the element is not centered yet, but more than the centered position. half of its own width. At this time, the child elements inside it need to use a relative positioning to pull back the extra half of its own width. And because relative positioning is positioned relative to itself, half of its own width is The width can be obtained by setting left
or right
to 50%
, so there is no need to know what the actual width is.
This method of using floating and relative positioning to center has the advantage that you don’t need to know the width of the element to be centered, even if the width is constantly changing; the disadvantage is that it requires an extra element to wrap the element to be centered.
Look at the code:
Running effect:
Use font-size to achieve vertical centering
If the parent The height of the element is known. If you want to center the child elements inside it horizontally and vertically, you can use this method, and the width or height of the child elements does not need to be known.
This method is only valid for IE6 and IE7.
The key point of this method is to set a suitable font-size
value for the parent element. The value of this value is the value obtained by dividing the height of the parent element by 1.14, and the child The element must be an inline
or inline-block
element, and the vertical-align:middle
attribute needs to be added.
As for why it is divided by 1.14 instead of other numbers, no one really knows. You only need to remember the number 1.14.
As mentioned in method 5, it can be used in current browsers such as IE8, Firefox and Google.display:table-cell
to perform centering, and the font-size
method here is suitable for IE6 and IE7, so combining these two methods can be compatible with all browsers:
In the above example, because the element to be centered is a block element, we also need to turn it into an inline element. If the element to be centered is an inline element such as a picture, then This step can be omitted.
In addition, if vertical-align:middle
is written in the parent element instead of the child element, this is also possible, but when calculating font-size
The value of 1.14 used should be changed to a value of approximately 1.5.
The above are some common centering methods. If there are any omissions or errors, please correct them!
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Use css to achieve shadow effects
CSS3
Implement sidebar expansion and collapse animation
The above is the detailed content of Use CSS to implement various centering methods. For more information, please follow other related articles on the PHP Chinese website!

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

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Implementing a full-screen mask layout is one of the common requirements in web design, which can add a strong sense of mystery and unique effects to the web page. In this article, HTML and CSS will be used to implement a simple full-screen mask layout, and specific code examples will be given. First, let's create the HTML structure. In the HTML file, we will use a div element as a container for the mask and add content inside it, as shown below: <!DOCTYPEhtml><html>

Methods and techniques on how to achieve floating animation effects through pure CSS. In modern web design, animation effects have become one of the important elements that attract users’ attention. One of the common animation effects is the floating effect, which can add a sense of movement and vitality to the web page, making the user experience richer and more interesting. This article will introduce how to achieve floating animation effect through pure CSS, and provide some code examples for reference. 1. Use the transition attribute of CSS to achieve the floating effect. The transition attribute of CSS can

As the functions of WPS become more and more powerful, we encounter more and more problems about the use of functions. In WPS, we often use WPS tables. If we need to print the WPS table, in order to make the table look beautiful, we need to center the table at this time. So, the question is, how do we center the WPS table? Today I am sharing a tutorial here, I hope it can help you! Step details: 1. I will explain it through practical operations. The following is a simple table I made using a WPS table. 2. Through print preview, we can find that the WPS table is on the left by default. What if we want to center the table? 3. At this time, we need to click [Page Layout] in the [Toolbar]

Introduction Center alignment of divs is one of the most important aspects of front-end development. In this article, we will look at the technique of placing one div inside another div using HTML and CSS. In this tutorial we will have a parent div which should have child divs. Our task is to place the child div in the center of the parent div. Using Transform translation and position syntax this is not a very popular way to center align one div into another div syntax left:50%;top:50%;Transform:translate(-50%,-50%);above The syntax does the following - The CSS rule "left:50%;" sets the horizontal position of the element to

Win11 brings a brand new interface, including changes to the taskbar. However, many users feel that the taskbar icons are too big and are not used to them when using them. For this reason, the following will bring you the win11 centered taskbar icon change. Come and learn the little operation methods together. How to make the center taskbar icon smaller in win11: 1. First, you need to open the registry editor. 2. Then expand: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\. 3. Then create a new DWORD value of "TaskbarSi" on the right, and add

CSS Tips: How to Implement Center-Aligned Layout In web design, center-aligned layout is often used. Whether it is center-aligning text, images, or the entire page layout, it can all be achieved through CSS. This article will introduce several CSS techniques to achieve center-aligned layout and provide specific code examples. First, let’s look at how to implement a horizontally centered layout. Here are code examples for some common elements: Text-center alignment: .text-center{text-align:ce

How to center an HTML text box requires specific code examples. In web design, center-aligning elements is a common layout requirement. For HTML text boxes, there are several ways to make them appear centered. The following will introduce you to a commonly used centering method in detail, and attach specific code examples. Method 1: Use the centering attribute in the CSS style sheet. To achieve centered display of the text box, you can use the text-align attribute in the CSS style sheet. Set the parent container element where the text box is located to center alignment.

Front-end skill sharing: Use CSS3fit-content to center elements horizontally In front-end development, we often encounter situations where we need to center an element horizontally. This effect can be easily achieved using the fit-content property of CSS3. This article will introduce how to use the fit-content attribute and provide code examples. The fit-content attribute is a length value relative to the element's parent container, which can automatically calculate the width of the element based on the actual size of the content. By converting the element
