


Horizontal centering--inline elements, fixed-width blocks, variable-width blocks_html/css_WEB-ITnose
Statement: From http://www.imooc.com/learn/9 Learn
Inline elementsIf the set element is an inline element such as text, image, etc., horizontal centering is This is achieved by setting text-align:center to the parent element .
Fixed-width block elementElements that meet the two conditions of fixed width and block can be centered by setting the "left and right margin" value to "auto".
1 margin:0 auto;
Block elements with variable widthThere are three methods for centering block elements with variable width (these three methods are currently used more often):
- Add table tag
- Set display; inline method
- Set position:relative and left:50%;
1.HTML code:
1 <div> 2 <table> 3 <tbody> 4 <tr><td> 5 <ul> 6 <li><a href="#">1</a></li> 7 <li><a href="#">2</a></li> 8 <li><a href="#">3</a></li> 9 </ul>10 </td></tr>11 </tbody>12 </table>13 </div>
CSS code:
1 table{2 margin:0 auto;3 }4 ul{list-style:none;margin:0;padding:0;}5 li{float:left;display:inline;margin-right:8px;}
2.
html code:
<body><div class="container"> <ul> <li><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> </ul></div></body>
css code:
<style>.container{ text-align:center;}.container ul{ list-style:none; margin:0; padding:0; display:inline;}.container li{ margin-right:8px; display:inline;}</style>
The advantage of this method compared to the first method is that it does not need to add semantic tags and simplifies the nesting depth of tags. However, there are also some problems: it changes the display type of block elements to inline, turning them into inline elements. , so some functions are missing, such as setting the length value.
3.HTML code
By setting float for the parent element, then setting position:relative and left:50% for the parent element, and setting position:relative and left:-50% for the child element. Achieve horizontal centering.
The code is as follows:
<body><div class="container"> <ul> <li><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> </ul></div></body>
css code:
<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>
This method can retain block elements and still display them in the form of display:block. The advantage is that it does not add silent discussion tags and does not increase the nesting depth. However, its disadvantage is that position:relative is set, which brings certain side effects.
These three methods are widely used and each has its own advantages and disadvantages. Which method to choose depends on the specific situation.

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

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

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.

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

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

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
