search
HomeWeb Front-endCSS TutorialAbout centered layout and IE double margin bug_CSS/HTML

Today I googled the problem of CSS centered layout, and the results almost all quoted the same article. To center in IE, just define the text-align:center; attribute in the

element. In Firefox, a wrapper needs to be defined. Then specify the margin-left:auto;margin-right:auto; properties to center the block in Firefox. In fact, I myself use the margin attribute, which is the abbreviation of margin:auto;, to set the center. The margin abbreviation attribute sets the page margins in four directions in clockwise order: top, right, bottom, and left. It should be noted that if you think based on this that you just want to center the block after setting the margin-top:auto; attribute, then you are wrong. When using this method, the margin-left:auto;margin-right:auto; attributes must be set at the same time to be centered. However, you can also center it by setting margin: 0 auto;.
Another way to center is to use the combination of left: 50% + margin-left: -375px; under the premise of using absolute positioning. The two centering methods are as follows.
Copy code The code is as follows:

/* Method 1 */
body {
background: #00FF7F;
text-align: center; /* hack for ie center alignlayout */
}
#wrapper{
background: Aqua;
width: 750px;
margin:auto; /* or margin-left:auto;margin-right:auto;*/
}
/* Method 2 */
#wrapper2 {
position: absolute;
width:750px;
left:50%;
margin-left:-375px;
background: Orange;
}


I personally always Use method one to center it, because I feel that the blocks in method two lack the necessary rigidity, and the code is not easy to understand. If you reduce the form in Method 2 to a certain limit, you will see that the entire layout will flow. Of course some people want this effect. :), another advantage of method 2 is that there is no need to override the text-align: center; setting of the body element in other nested blocks, because most text will still be left-aligned. This can reduce redundancy to a certain extent. But I think this benefit is negligible. In short, everyone just takes what they need according to their own preferences.

The other is the double margin bug of IE. The solution is to add display:inline; setting. In fact, this parsing error bug will affect many aspects. If you encounter abnormal spacing in CSS layout in IE, you can try adding the attribute declaration of display:inline;. And for certain specific layout issues, you need to use display:block; and display:inline; in combination to make the layout as you want. I encountered a similar problem in a picture menu controlled by an unorder list.

As soon as possible, various browsers have css bugs, but the css page layout method has developed to this day and is relatively mature. The chance of making you crazy because of bugs is no longer high. If you have questions, check the search engine to solve most of your problems. If you can do some experiments during your free time, you may be able to remain calm in emergencies.
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
CSS Flexbox vs Grid: a comprehensive reviewCSS Flexbox vs Grid: a comprehensive reviewMay 12, 2025 am 12:01 AM

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

How to Include CSS Files: Methods and Best PracticesHow to Include CSS Files: Methods and Best PracticesMay 11, 2025 am 12:02 AM

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Flexbox vs Grid: should I learn them both?Flexbox vs Grid: should I learn them both?May 10, 2025 am 12:01 AM

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)May 09, 2025 am 09:57 AM

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSS Animations: Is it hard to create them?CSS Animations: Is it hard to create them?May 09, 2025 am 12:03 AM

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframes CSS: The most used tricks@keyframes CSS: The most used tricksMay 08, 2025 am 12:13 AM

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSS Counters: A Comprehensive Guide to Automatic NumberingCSS Counters: A Comprehensive Guide to Automatic NumberingMay 07, 2025 pm 03:45 PM

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Modern Scroll Shadows Using Scroll-Driven AnimationsModern Scroll Shadows Using Scroll-Driven AnimationsMay 07, 2025 am 10:34 AM

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

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

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor