Implementing the centering of HTML elements seems simple, but in fact it is not.
Horizontal centering is easy, vertical centering is more difficult, and horizontal and vertical centering is even more difficult. In this era of responsive layout, it is difficult to fix the width and height of elements. I have counted several current methods. This article introduces each step from the shallower to the deeper, using the same HTML code:
<div class="center">
<img src="/static/imghwm/default1.png" data-src="jimmy-choo-shoe.jpg" class="lazy" alt="">
</div>
The shoe image below will change but the original size is always 500px × 500px, and the theme background color is used HSL colors
1. Center horizontally? Use text-align
In some scenarios the simple method is the best method
div.center { text-align: center; background: hsl(0, 100%, 97%); }
div.center img { width: 33%; height: auto; }
But this method cannot center the image vertically: you need to add padding to the div or add margin-top and margin-bottom to the elements in the div
2. margin: auto center
is also horizontally centered, with the same limitations as the first method:
div.center { background: hsl(60, 100%, 97%); }
div.center img { display: block; width: 33%; height: auto; margin: 0 auto; }
Note that display: block, in this case there must be margin: 0 auto.
3. table-cell centered
Using display: table-cell, you can achieve horizontal and vertical centering. Often it is necessary to add an additional empty element.
<div class="center-aligned">
<div class="center-core">
<img src="/static/imghwm/default1.png" data-src="jimmy-choo-shoe.jpg" class="lazy" alt="7 ways to achieve centering with CSS_html/css_WEB-ITnose" >
</div>
</div>
CSS code:
.center-aligned { display: table; background: hsl(120, 100%, 97%);width: 100%; }
.center-core { display: table-cell; text-align: center; vertical-align:middle; }
.center-core img { width: 33%; height: auto; }
Note that width: 100% is to prevent the div from collapsing, and the outer container needs a height to be vertically centered. If the vertically centered element is placed in . body, you need to set height for html and body. Valid in all browsers, including IE8.
4. Absolute centering
A new technology recently promoted by Stephen Shaw can be well compatible with various browsers. The only drawback is that the outer container must declare height
.absolute-aligned {
position: relative; min-height: 500px;
background: hsl(200, 100%, 97%);
.absolute-aligned img {
width: 50%;
min-width: 200px;
height: auto;
overflow: auto; margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
Stephen verified many versions of this code in his article
5. Use translate to center
Chris Coiyer proposed a new method: using CSS transforms . Supports both horizontal and vertical centering:
.center { background: hsl(180, 100%, 97%); position: relative; min-height: 500px; }
.center img { position: absolute; top: 50%; left: 50%;
transform: translate(-50%, -50%); width: 30%; height: auto; }
has the following disadvantages:
6. Use Flexbox to center
This approach will likely become the preferred centering solution once attribute variables and specific prefixes disappear.
.center { background: hsl(240, 100%, 97%); display: flex; justify-content: center; align-items: center; }
.center img { width: 30%; height: auto; }
in In many ways flexbox is the simplest solution, but the constraints are various archaic syntax and lower versions of IE (although display: table-cell is an acceptable solution). Full CSS code:
.center { background: hsl(240, 100%, 97%);
display: -webkit-box; /* OLD: Safari, iOS 6 and earlier; Android browser, older WebKit */
display: -moz-box; /* OLD: Firefox (can be buggy) */
display: -ms-flexbox; /* OLD: IE 10 */
display: -webkit-flex; /* FINAL, PREFIXED, Chrome 21+ */
display: flex; /* FINAL: Opera 12.1+, Firefox 22+ */
-webkit-box-align: center;
-moz-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
-webkit-box-pack: center;
-moz-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
Now that the specification has been formed and browsers support it, I have written extensively on flexbox layout and its uses.
7. Use calc to center
In a More versatile than flexbox in some scenarios:
.center { background: hsl(300, 100%, 97%); min-height: 600px; position:relative; }
.center img { width: 40%; height: auto; position: absolute; top:calc(50% - 20%); left: calc(50% - 20%); }
Obviously, calc allows calculations to be performed on the current page layout. In the above example, 50% is the midpoint of the element in the container, but used alone would make the upper left corner of the image in the middle of the
top: calc(50% - (40% / 2)); left: calc(50% - (40% / 2));
In current browsers, you can find that this technology works best when the content is fixed and the size is known:
.center img { width: 500px; height: 500px; position: absolute; top:calc(50% - (300px / 2)); left: calc(50% - (300px ? 2)); }
calc This method also has many potential shortcomings like flexbox: it supports Firefox 4 and higher browsers. For earlier browsers, you need to add a prefix, and IE8 is not supported. The complete code for image centering:
.center img { width: 40%; height: auto; position: absolute;
top: -webkit-calc(50% - 20%); left: -webkit-calc(50% - 20%);
top: -moz-calc(50% - 20%); left: -moz-calc(50% - 20%);
top: calc(50% - 20%); left: calc(50% - 20%); }
Of course there are many other methods, such as using pseudo elements to vertically center, understanding these techniques can Let web developers deal with centering issues without getting stuck!
Original text here
</div>
The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
