Home Web Front-end CSS Tutorial css:margin:0 auto can't be centered in ie8

css:margin:0 auto can't be centered in ie8

Jul 21, 2017 pm 01:43 PM
auto margin

Today I wrote a div, using margin:0 auto; to define its attributes and center it, but it turned out to be invalid.
At first, I thought it was a code conflict in the css. I checked it several times and found that there was no problem. Then I tested it with Firefox and Google Chrome and found that it was centered. . .
It seems to be a browser compatibility issue. I suddenly remembered that I am using win7 and ie has been upgraded to ie8. It seems that the problem is the compatibility of ie8. Baidu. . . .

The solution is copied below:
To achieve centering under IE6, 7, and Firefox, margin: auto is generally used to achieve it, but under IE8, this is not enough. Two solutions are provided:

Method 1 (recommended):

First, the document type must be set at the top of the page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transition al.dtd">
Copy after login

and add:

in the head
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/> 即可!
Copy after login


Method 2:

Body settings {text-align:center;width:100%;}
Include all divs in

, wrap is set as follows:

 #wrap    
 { 
 text-align:left; 
 width:***;  
 margin:0 
 auto; 
  }
Copy after login

and add in the head:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
Copy after login

That’s it!

Method three:

Use

to replace the wrap layer in the second one.

The above is the detailed content of css:margin:0 auto can't be centered in ie8. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of CSS border properties: padding, margin and border Detailed explanation of CSS border properties: padding, margin and border Oct 21, 2023 am 11:07 AM

CSS border properties explained in detail: padding, margin and borderCSS is a style sheet language used to control and layout web page elements. In web design, the border attribute is one of the most important parts. This article will introduce in detail how to use the border attribute in CSS and provide specific code examples. padding The padding property is used to set the padding of an element, which is the space between the element's content and the element's borders. We can set padding using positive numbers or percentage values

What does margin mean in css What does margin mean in css Dec 18, 2023 am 10:30 AM

In CSS, margin is a property used to set the outer margins of an element. Margins are the space between an element's border and its content. Margin can accept the following values: 1. A single value: for example, margin: 10px; Set all four margins (top, right, bottom, left) to 10 pixels; 2. Two values: for example, margin : 10px 20px; Set the top and bottom margins to 10 pixels, and the left and right margins to 20 pixels; 3, four values, and so on.

Porsche leak suggests EV Boxster, Cayman could replace petrol models as soon as October 2025 Porsche leak suggests EV Boxster, Cayman could replace petrol models as soon as October 2025 Jun 15, 2024 pm 04:02 PM

We'vepreviouslyreportedonleaksofanupcomingelectricPorscheBoxster,andPorschehaspreviouslycommittedtoEVsmakingup80%ofsalesby2030andconfirmedthatelectricBoxsterandCaymanmodelswouldbeintroducedalongsideitsregularpetrol-p

What coin is AUTO? What coin is AUTO? Feb 22, 2024 pm 09:40 PM

What is AUTO coin? AUTO coin is the native token of the Autonio ecosystem. As a cryptocurrency, it aims to provide users with decentralized trading and automated trading services. The Autonio platform is based on blockchain technology and realizes intelligent transactions through smart contracts, providing users with a more efficient and secure transaction experience. Autonio Ecosystem The Autonio Ecosystem was developed by the Autonio Foundation and aims to provide automated trading and investment tools in a decentralized manner to assist users in optimizing their trading strategies and achieving higher returns. This ecosystem integrates AUTO tokens, smart contracts, trading robots and the Autonio trading platform. The goal of Autonio is to

Detailed explanation of CSS text layout properties: text-overflow and white-space Detailed explanation of CSS text layout properties: text-overflow and white-space Oct 20, 2023 am 11:19 AM

Detailed explanation of CSS text layout properties: text-overflow and white-space In web design, text layout is a very important link. Reasonable layout can make the text more readable and beautiful. CSS provides several properties to control how text is displayed, including text-overflow and white-space. This article will detail the usage and sample code of these two properties. 1. text-overflow attribute text

The margin attribute does not affect inline elements The margin attribute does not affect inline elements Feb 18, 2024 pm 04:36 PM

The effect of margin on inline elements is different from that of block-level elements. In inline elements, the margin attribute only affects the vertical top and bottom margins, not the horizontal left and right margins. For example, if there is a paragraph element in HTML, we can set some styles for it and observe the effect of the margin attribute on it. The HTML code looks like this:

What does margin mean in html What does margin mean in html Sep 13, 2021 pm 04:26 PM

In HTML, margin means "margin", which refers to the blank area surrounding the border of an element; setting margins will create additional "blank" outside the element, allowing a "blank" distance between boxes. . To set margins, you need to use the css margin property, which accepts any length unit, percentage value, or even negative value.

Exploring CSS box model properties: padding, margin and border Exploring CSS box model properties: padding, margin and border Oct 20, 2023 pm 03:09 PM

Exploration of CSS box model properties: padding, margin and border The CSS box model is one of the important concepts in web page layout. In front-end development, understanding and correctly using padding, margin and border attributes is key. This article will delve into the usage and correlation of these three properties, and provide specific code examples. 1. Introduction to the box model The box model consists of four parts: content, padding, bo

See all articles