search
HomeWeb Front-endHTML TutorialTypical three rows and two columns centered height adaptive div css layout_html/css_WEB-ITnose

如何使整个页面内容居中,如何使高度自适应内容自动伸缩,这是学习CSS布局最常见的问题。

 

下面给出一个实际的列子,附详细说明。

 

首先可以将下列代码复制到html文件中运行一下,在mozilla、opera和ie浏览其中,均可达到居中且自适应高度的要求。

 

body

{

       background:#999;

       text-align:center;

       color:#333;

       font-family:Arial, Verdana, Sans-serif;

}

#header

{

       width:776px;

       margin-left:auto;

       margin-right:auto;

       padding:0px;

       background:#eee;

       height:60px;

       text-align:center;

}

#container

{

       margin-left:auto;

       margin-right:auto;

       width:776px;

}

#mainbg

{

       width:776px;

       padding: 0px;

       background:#60A179;

       float:left;

}

#right

{

       float:right;

       margin:2px 0px 2px 0px;

       padding:0px;

       width:574px;

       background:#ccd2de;

       text-align:left;

}

#left

{

       float:left;

       margin:2px 2px 0px 0px;

       padding:0px;

       background:#f2f3f7;

       width:200px;

       text-align:left;

}

#footer

{

       clear:both;

       width:776px;

       margin-right:auto;

       margin-left:auto;

       padding:0px;

       background:#eee;

       height:60px;

}

.text

{

       margin:0px;

       padding:20px;

}

      

             

             

                    

left

                    

             

      

First we define the body and top area #header, the key here It is text-align: center in the body; and margin-left: auto and margin-right: auto in the header; the function is similar to centering the header area in the browser. Note: In IE, you only need to define text-align:center of the body to center it, but in mozilla (firefox), margin:auto is required.

Next are the left and right areas. In order to center these two columns, a container is now nested outside them, and margin: auto is set. The principle is the same as above. In this way, left and right are also centered as a whole.

You may be wondering why there is a layer of mainbg between the container and left and right. What is the role of this layer? This layer is used to define the background, so why not define the background directly in the container, but in multiple layers? The reason is that mozilla (firefox) is causing trouble (you don’t want the webpage you made to be displayed differently in firefox and ie). There is no need to nest one more layer in ie. In mozilla, the height value must be defined. The background can be displayed, but if the height is specified, the adaptive height requirement cannot be achieved, so an extra layer of mainbg is added. The trick lies in the float: left of the mainbg layer; defined in this way, the background can be displayed without specifying the height.

The last thing defined is the footer layer at the bottom. The key point of this layer is clear: both; the function of this sentence is to cancel the floating inheritance of the footer layer. Otherwise, you will see that the footer is displayed close to the header, or Mozilla is doing something weird :)

It is said that it is over here, but careful friends will see the difference between left and right. One layer is nested, and both layers use .text{margin:0px;padding:20px}. Why is this? The reason is that Mozilla and IE have different interpretations of the box model. Directly defining margin and padding will cause inconsistencies in the displays in Mozilla and IE. Therefore, when encountering inconsistencies between IE and Mozilla, adding an extra layer will often solve the problem.

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
HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

HTML's Purpose: Enabling Web Browsers to Display ContentHTML's Purpose: Enabling Web Browsers to Display ContentMay 03, 2025 am 12:03 AM

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

Why are HTML tags important for web development?Why are HTML tags important for web development?May 02, 2025 am 12:03 AM

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

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

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

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.