search
HomeWeb Front-endCSS TutorialCSS layout--various centers

CSS layout--various centers

Dec 12, 2016 pm 02:14 PM

Centering 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

1. Set the margin to auto

Specifically, set the margin-left and margin-right of the element to be centered to auto. This method can only perform horizontal centering, and is invalid for floating elements or absolutely positioned elements.

2. Use text-align:center

There is nothing to say about this. It can only horizontally center inline elements such as pictures, buttons, and text (display is inline or inline-block, etc.). But it should be noted that in the two weird browsers IE6 and 7, it can center any element horizontally.

3. Use line-height to vertically center a single line of text.

Set the line-height of the text to the height of the text’s parent container, which is suitable for situations where there is only one line of text.

4. Use tables

If you are using tables, you don’t have to worry about various centering issues at all. Just use align="center" and valign= of the td (and possibly th) elements. The two attributes "middle" 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 attribute in CSS, but in IE6 and 7 we can use text-align:center To horizontally center the elements in the table, text-align:center in IE8+ and Google, Firefox and other browsers only works on inline elements and is invalid on block elements.

CSS layout--various centers

CSS layout--various centers

In IE6 and 7, you can control the horizontal alignment of the table content through CSS text-algin, whether the content is an inline element or a block element.

CSS layout--various centers

But text-align:center in IE8+, Chrome, Firefox and other browsers is not valid for block elements, and you can only use the table's own align attribute.

5. 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 we can take advantage of the convenient centering feature of the table . For example:

CSS layout--various centers

CSS layout--various centers

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.

6. Use absolute positioning for centering

This method only applies to elements whose width or height we already know.

The principle of centering with absolute positioning 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 by the width of the element compared to the centered position. Or half the height, so you need to use a negative margin-left or margin-top value to bring it back to the centered position. This negative margin value takes half the width or height of the element.

CSS layout--various centers

Operation effect:

CSS layout--various centers

If you only want to achieve centering in one direction, you can only use left and margin-left to achieve horizontal centering, and use top and margin-top to achieve vertical centering.

7. Another method of using absolute positioning to center

This method is also only applicable to those elements whose width or height we already know, and unfortunately it only supports IE9+, Google, Firefox, etc. A modern browser based on w3c standards.

The following is a piece of code to understand this method:

CSS layout--various centers

Operation effect:

CSS layout--various centers

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 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.

8. Use floating and relative positioning to perform horizontal centering

This method is also a solution to 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: position the floating element relatively to 50% of the width of the parent element, but at this time the element is not centered yet, but is half its width wider than the centered position. In this case, it is necessary The child element inside uses a relative positioning to bring back the extra half of its width. Since relative positioning is relative to itself, half of its width only needs to be set to 50% left or right. You can get it, so you don't need to know what its 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:

CSS layout--various centers

Operation effect:

CSS layout--various centers

9. Use font-size to achieve vertical centering

If the height of the parent element is known, make the child elements inside it horizontal and vertical Centered, you can use this method without knowing the width or height of the child elements.

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 height of the parent element divided by 1.14, and the child element must be an inline or inline-block element. , you need to add the vertical-align:middle attribute.

As for why it is divided by 1.14 instead of other numbers, no one really knows. You just need to remember the number 1.14.

CSS layout--various centers

CSS layout--various centers

It was said in method 5 that display:table-cell can be used for centering in current browsers such as IE8+, Firefox, Google, etc., and the font-size method here is suitable for IE6 and IE7. So combining these two methods can be compatible with all browsers:

CSS layout--various centers

CSS layout--various centersCSS layout--various centers

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, you can omit this step.

In addition, if vertical-align:middle is written in the parent element instead of the child element, this is also possible, but the value of 1.14 used when calculating font-size will become approximately 1.5.


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
How much specificity do @rules have, like @keyframes and @media?How much specificity do @rules have, like @keyframes and @media?Apr 18, 2025 am 11:34 AM

I got this question the other day. My first thought is: weird question! Specificity is about selectors, and at-rules are not selectors, so... irrelevant?

Can you nest @media and @support queries?Can you nest @media and @support queries?Apr 18, 2025 am 11:32 AM

Yes, you can, and it doesn't really matter in what order. A CSS preprocessor is not required. It works in regular CSS.

Quick Gulp Cache BustingQuick Gulp Cache BustingApr 18, 2025 am 11:23 AM

You should for sure be setting far-out cache headers on your assets like CSS and JavaScript (and images and fonts and whatever else). That tells the browser

In Search of a Stack That Monitors the Quality and Complexity of CSSIn Search of a Stack That Monitors the Quality and Complexity of CSSApr 18, 2025 am 11:22 AM

Many developers write about how to maintain a CSS codebase, yet not a lot of them write about how they measure the quality of that codebase. Sure, we have

Datalist is for suggesting values without enforcing valuesDatalist is for suggesting values without enforcing valuesApr 18, 2025 am 11:08 AM

Have you ever had a form that needed to accept a short, arbitrary bit of text? Like a name or whatever. That's exactly what is for. There are lots of

Front Conference in ZürichFront Conference in ZürichApr 18, 2025 am 11:03 AM

I'm so excited to be heading to Zürich, Switzerland for Front Conference (Love that name and URL!). I've never been to Switzerland before, so I'm excited

Building a Full-Stack Serverless Application with Cloudflare WorkersBuilding a Full-Stack Serverless Application with Cloudflare WorkersApr 18, 2025 am 10:58 AM

One of my favorite developments in software development has been the advent of serverless. As a developer who has a tendency to get bogged down in the details

Creating Dynamic Routes in a Nuxt ApplicationCreating Dynamic Routes in a Nuxt ApplicationApr 18, 2025 am 10:53 AM

In this post, we’ll be using an ecommerce store demo I built and deployed to Netlify to show how we can make dynamic routes for incoming data. It’s a fairly

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools