Detailed explanation of the simple method of centering CSS elements
This article mainly introduces a simple method to explain in detail the centered layout of CSS elements. The article introduces three situations of inline elements, block elements and inline blocks. Friends in need can refer to it
First we need to know What types of elements are there?
Inline elements (display:inline;) such as a, span, b, i [a non-customizable box]
[The default peers can continue to follow the same type of tags]
[ Content expansion width]
[Does not support width and height]
[Does not support upper and lower margins and padding]
[Code wrap will be parsed into empty space]
Block element (display:block ;) Such as p, p, h1-h6
[Display in one line by default]
[Basically supports all css commands]
Inline block (display:inline-block;) such as img [img is such a magical thing. It is neither inline nor block, but an inline block】
[Block is displayed in one line]
[Supports width and height]
[Content expands width when there is no width]
Then let’s center the above three elements in sequence
1. Single-line text of embedded elements
The most common solution is to use text-align and line-height
line-height:200px; text-align:center;
But is this solution necessarily perfect? I don’t think so (I guess someone complained about my obsessive-compulsive disorder)
Anyway, every time I select text, I feel very unhappy when I see non-text areas are also selected, but IE6-8 only selects text
2. Center the block element
Solution: Use positioning element + negative margin value
width:100px; height:100px; position:relative; left:100px; top:100px; margin-left:-50px; margin-top:-50px;
Disadvantage: Requires knowing the box The width and height
3. Center the inline block
(1) Convert the img into a background image, and then use background-position:center; However, it should be noted that because Picture links generally change frequently, so you need to do this:
<img src="/static/imghw/default1.png" data-src="img.png" class="lazy" style=”background-img:url(imgURL)” / alt="Detailed explanation of the simple method of centering CSS elements" >
Does it violate the principle of separation of content and style?
(2) Auxiliary tag
html code:
<p class="box"> <img src="/static/imghw/default1.png" data-src="img.png" class="lazy" / alt="Detailed explanation of the simple method of centering CSS elements" ><span></span> </p>
CSS code:
.box{width:200px;height:200px;border:1px solid #333;margin:0 auto;text-align:center;} .box img{vertical-align:middle;border:1px solid #999;padding:2px;} .box span{display:inline-block;height:100%;background:#333;vertical-align:middle;}
Effect :
Nonsense: The img element and the auxiliary element span must be on one line, otherwise the horizontal centering will not be complete. When using inline-block, line breaks will be parsed into spaces. In fact, there are other methods on the Internet, such as the table method that has been popular for a long time. There are a lot of them online so I won’t show them here.
The above is the detailed content of Detailed explanation of the simple method of centering CSS elements. 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

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-
