search
HomeWeb Front-endHTML TutorialCompatible with unknown height div vertical centering effect of various browsers_html/css_WEB-ITnose

Compatible with the vertical centering effect of divs of unknown height in various browsers:
Centering a div vertically in its parent object is not as simple as centering the div horizontally in the parent element. Just simply set margin:0px auto for this element. Although you can vertically center the div with the parent element using some attributes, it is not compatible with some browsers. Here is a method that is compatible with all major browsers. browser way.
Let’s look at a code example first:

<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="author" content="http://www.51texiao.cn/" /><title>蚂蚁部落</title> <style type="text/css"> .parent {   width:200px;   height:200px;   background-color:green;   display:table-cell;   vertical-align:middle; } .children {   background-color:red;   width:80px;   text-align:center;   margin:0px auto; } </style> </head> <body> <div class="parent">   <div class="children">蚂蚁部落</div> </div> </body> </html>

The above code runs normally in standard browsers and can vertically center align child elements in the parent element, but IE6 and IE7 is not so powerful, so the code needs to be modified to be compatible with IE6 and IE7 browsers. However, if you just add CSS compatible code without making structural changes, it will be difficult to achieve compatibility with IE6 and IE7. Here is the above code Adjust as follows:

<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="author" content="http://www.51texiao.cn/" /><title>蚂蚁部落</title> <style type="text/css"> .parent {   width:200px;   height:200px;   background-color:green;   position:relative;   display:table; } .middle {   display:table-cell;   vertical-align:middle;   *position:absolute;   *top:50%;   *left:50%; } .children {   background-color:red;   width:80px;   text-align:center;   margin:0px auto;   *position:relative;   *top:-50%;   *left:-50%; } </style> </head> <body> <div class="parent"> <div class="middle">    <div class="children">蚂蚁部落</div> </div> </div> </body> </html>

The above code achieves the effect of vertically centering the div. You do not need to regard this piece of code as a modified version of the first piece of code, but as a completely new version. Here is a brief introduction to how the next code achieves this effect:
1. Implementation ideas:
Since IE6 and IE7 do not support display:table-cell and display:table, the height of the middle object is the same as the height of the children object. Of course, if the width of the children object is not set, their sizes are exactly the same. Use absolute positioning for the middle object in IE6 and IE7, and the reference object is the parent object, and set its top value to 50%, so that the upper edge of the middle object is exactly in the middle of the vertical position of the parent object. Also use positioning for the children object, and then set its top value to -50%, so that it moves up by half the height of the middle, so that the center of the children object is located in the middle of the vertical direction of the parent object. This achieves the vertical centering effect of the children object. Some friends here may ask why other standard browsers do not use this method. This is because if the height of the middle object is not set, the set top attribute will not take effect. The following pictures are the renderings of running under IE6 and IE7 after setting the middle background color to blue:

2. Explanation of knowledge points:
1. The asterisk (*) is In order to be compatible with IE6 and IE7 browsers.
2. Display:table should be added to the parent object corresponding to the display:table-cell in the middle object. Otherwise, the size and content of the middle element will be adaptive instead of the same size as the parent element, so it will not be possible to implement Standard browsers center the children object vertically.

The original address is: http://www.51texiao.cn/div_cssjiaocheng/2015/0501/503.html

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
The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment