Summary of two DIV highly adaptive Methods

Source: Internet
Author: User

In Seo, we mentioned the optimization of website styles, that is, in the layout design of websites, div + CSS is used for layout. In website systems, we often implement the same high effect on the two divs that are displayed side by side, that is, the height of each column (which column is not determined beforehand) is the same, there are several methods (the method comes from the Internet ):
1. js implementation (determine the height of two DIV );
2. pure CSS Method;
3. Add a background image.

Div + CSS basic layout:

<Div id = "mm">
<Div id = "MM1"> </div>
<Div id = "mm2"> </div>
</Div>

1. Javascript achieves Div adaptive height

CodeAs follows:
<SCRIPT type = "text/JavaScript">
<! --
Window. onload = Window. onresize = function (){
If (document. getelementbyid ("mm2"). clientheight <document. getelementbyid ("MM1"). clientheight ){
Document. getelementbyid ("mm2"). style. Height = Document. getelementbyid ("MM1"). offsetheight + "PX ";
}
Else {
Document. getelementbyid ("MM1"). style. Height = Document. getelementbyid ("mm2"). offsetheight + "PX ";
}
}
-->
</SCRIPT>

(Note: many methods are published on the Internet, but the code is more or less incorrect. The above code is error-free. The code above passes the test under ie6.0/ie7.0 and is not tested under opera and firefoxs .)

2. pure CSS Method

Code in CSS (ie passed the test, but the lower border of div is not displayed, that is, border-bottom ):

/* Adaptive start with the same height on left and right */
# M1, # m2
{
Padding-bottom: 32767px! Important;
Margin-bottom:-32767px! Important;
}
@ Media all and (min-width: 0px ){
# M1, # m2
{
Padding-bottom: 0! Important;
Margin-bottom: 0! Important;
}
# M1: before, # M2: before
{
Content: '[do not leave it is not real]';
Display: block;
Background: Inherit;
Padding-top: 32767px! Important;
Margin-bottom:-32767px! Important;
Height: 0;
}
}
/* Adaptive end of the left and right sides at the same height */

3. Add a background image

This method is used by many large network sites, such as 163 and Sina.

XHTML code:

<Div id = "Wrap">
<Div id = "column1"> This is the first column </div>
<Div id = "column1"> This is the second column </div>
<Div class = "clear"> </div>
</Div>

CSS code:

# wrap {width: 776px; Background: url(bg.gif) Repeat-y 300px ;}< BR ># column1 {float: Left; width: 300px ;}
# column2 {float: Right; width: 476px ;}< br>. clear {clear: Both ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.