


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

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.

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 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.

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.

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

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.

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.

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


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

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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
Powerful PHP integrated development environment