Home Web Front-end HTML Tutorial 简单的CSS网页布局一二列布局_html/css_WEB-ITnose

简单的CSS网页布局一二列布局_html/css_WEB-ITnose

Jun 24, 2016 am 11:26 AM

1.一列布局

  (一)一列自适应

  自适应浏览器,随着浏览器的拉伸而变化,一般宽度采用百分比的写法,很简

1

<html><head lang="en">    <meta charset="UTF-8">    <title>一列布局自适应</title>    <style type="text/css"> body{ margin: 0;      /*清除浏览器默认样式*/ padding: 0;        } div{ text-align: center;  /*字体居中*/ font-size: 30px; font-weight: bold;        } .head,.middle,.foot{ width: 50%;          /*百分比宽度*/ margin: 0 auto;      /* 典型的设置居中*/        } .head{ height: 200px; background: #F0B6CF;        } .middle{ height: 500px; background: tan;        } .foot{ height: 200px; background-color: #57A9D1;        }    </style></head><body>        <div class="head">head</div>        <div class="middle">middle</div>         <div class="foot">foot</div></body></html>

Copy after login

  (二)一列固定

  顾名思义,固定布局的宽度,设置固定的PX值。

  只需要在上面一列自适应的HTML代码中,把width:50% 修改成 width:960px即可,当然,各部分如果要求设置的宽度不同,在每个部分的类选择器上进行适当修改即可。

2.二列布局

  (一)二列自适应

   二列的自适应,这时候要用到float属性。

  

1

<!DOCTYPE html><html><head lang="en">    <meta charset="UTF-8">    <title>二列自适应</title>    <style type="text/css">        body{            margin: 0;      /*清除浏览器默认样式*/            padding: 0;        }        div{            text-align: center;  /*字体居中*/            font-size: 30px;            font-weight: bold;        }        .left{            width: 30%;                             background-color: #CCFF00;            height: 500px;            float: left;         /*先左浮动,使之靠浏览器左侧*/        }        .right{            width: 70%;            height: 500px;            background-color: bisque;            float: right;       /*先右浮动,使之靠浏览器右侧*/        }    </style></head><body>        <div class="left">left</div>        <div class="right">right</div></body></html>

Copy after login

注意:以上百分比30%和70%加起来刚好是100%,宽度会充满整个浏览器页面,如果加起来没有100%,则他们中间会空出一栏的空间出来。

  (二)二列居中自适应

  在左右部分包裹成一个div,在该div类选择器中选择 margrin:0,auto;设置宽度为:80%;则子代标签的宽度会基于80%的浏览器宽度来定。

1

1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4     <meta charset="UTF-8"> 5     <title>二列居中自适应</title> 6     <style type="text/css"> 7         body{ 8             margin: 0;      /*清除浏览器默认样式*/ 9             padding: 0;10         }11         div{12             text-align: center;  /*字体居中*/13             font-size: 30px;14             font-weight: bold;15         }16         .main{17             width:80%;18             height: 500px;19             margin: 0 auto;      /*居中*/20         }21         .left{22             width: 30%;23             background-color: #CCFF00;24             height: 500px;25             float: left;         /*先左浮动,使之靠浏览器左侧*/26         }27         .right{28             width: 70%;29             height: 500px;30             background-color: bisque;31             float: right;       /*先右浮动,使之靠浏览器右侧*/32         }33     </style>34 </head>35 <body>36     <div class="main">37         <div class="left">left</div>38         <div class="right">right</div>39     </div>40 </body>41 </html>

Copy after login

  (三)二列居中固定

  

1

1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4     <meta charset="UTF-8"> 5     <title>二列居中固定宽度</title> 6     <style type="text/css"> 7         body{ 8             margin: 0;      /*清除浏览器默认样式*/ 9             padding: 0;10         }11         div{12             text-align: center;  /*字体居中*/13             font-size: 30px;14             font-weight: bold;15         }16         .main{17             width:960px;18             height: 500px;19             margin: 0 auto;20         }21         .left{22             width: 360px;23             background-color: #CCFF00;24             height: 500px;25             float: left;         /*先左浮动,使之靠浏览器左侧*/26         }27         .right{28             width: 600px;29             height: 500px;30             background-color: bisque;31             float: right;       /*先右浮动,使之靠浏览器右侧*/32         }33     </style>34 </head>35 <body>36     <div class="main">37         <div class="left">left</div>38         <div class="right">right</div>39     </div>40 </body>41 </html>

Copy after login

 

   

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

See all articles