如何用DIV+CSS布局例子

html-css014

如何用DIV+CSS布局例子,第1张

举例(两列布局):

<!DOCTYPE html>

<html>

 

    <head>

        <meta charset="utf-8">

        <title>练习使用HTML</title>

        <link rel="stylesheet" href="css/index.css" />

    </head>

 

    <body>

        <!-- DIV -->

        <div id="d1">

            <span>DIV</span>

        </div>

        <div id="d2">

            <span>DIV</span>

        </div>

        <div id="d3">

            <span>DIV</span>

        </div>

    </body>

 

</html>

css代码:

css文件:

#d1{

    position: absolute

    width: 100px

    height: 100px

    background-color: red

}

#d2{

    position: absolute

    margin-left: 100px

    width: 500px

    height: 100px

    background-color: blue

}

#d3{

    position: absolute

    margin-top: 100px

    width: 600px

    height: 100px

    background-color: yellow

}

效果:

CSS

/*基本信息*/

body

{font:12px

Tahomabackground:#CCCCCC}/*适用于页面整体:字体及字体大小,页面页边距全部设置为0,文字对齐方式为居中,背景颜色*/

*

{

margin:0

padding:0

border:0}/*全局设定,外边距,内边距和表格边框全部为0*/

html,body

{

height:100%}

/*全局设定高度为100%*/

/*--------------------------------------*/

/*页面主体*/

#pagebody

{

width:780px

/*设定宽度*/

margin:0px

auto

/*居中*/

}

#mainleft

{

width:192px

/*设定宽度*/

float:left

/*浮动居左*/

clear:left

/*不允许左侧存在浮动*/

overflow:hidden

/*超出宽度部分隐藏*/

border:1px

solid

#006699

margin:5px

auto

height:auto

}

#mainright

{

width:580px

text-align:left

float:right

/*浮动居右*/

clear:right

/*不允许右侧存在浮动*/

overflow:hidden

border:0px

solid

#E00/*初始使用表格,留存使用*/

margin:5px

auto

}

#mainright2

{

width:578px

float:right

/*浮动居右*/

clear:right

/*不允许右侧存在浮动*/

overflow:hidden

border:0px

solid

#006699

margin:2px

auto

height:auto

}

#mainright3

{

width:285px

/*设定宽度*/

float:left

/*浮动居左*/

clear:left

/*不允许左侧存在浮动*/

overflow:hidden

/*超出宽度部分隐藏*/

border:1px

solid

#006699

margin:2px

auto

height:auto

}

#mainright4

{

width:285px

/*设定宽度*/

float:right

/*浮动居左*/

clear:right

/*不允许左侧存在浮动*/

overflow:hidden

/*超出宽度部分隐藏*/

border:1px

solid

#006699

margin:2px

auto

height:auto

}

/*字体设置*/

#biaoti

{

font-size:10px

font-weight:bold

padding-left:10px

color:#FFf

background-color:#006699

height:20px

}

页面

<!DOCTYPE

html

PUBLIC

"-//W3C//DTD

XHTML

1.0

Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html

xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta

http-equiv="Content-Type"

content="text/html

charset=gb2312"

/>

<title>无标题文档</title>

<link

href="css.css"

rel="stylesheet"

type="text/css"

/>

</head>

<body>

<div

id="pagebody"><!--页面主体-->

<div

id="mainleft">

左侧显示

</div><!--左侧关闭-->

<div

id="mainright2">

<div

id="mainright3">

右侧一显示

</div>

<div

id="mainright4">

右侧二显示

</div>

</div>

</div>

</body>

</html>

首先要认识DIV是什么,div是HTML标签“<div>”。

DIV用法的语法

<div>内容</div>

div作为html网页中常用的标签,其默认样式是独占一行,其CSS样式需要重新赋予。比如对div宽度、高度等样式设置、内部字体大小、字体颜色都需要通过CSS来实现。

通俗认识div,div作用就是实现布局、实现对内容样式控制、实现各种各样的布局效果。

DIV的用法实例,这里通过对div设置不同CSS样式,观察其效果。

DIV+CSS实例完整HTML源代码:

<!DOCTYPE html>

<html>

<head>

<meta charset="gb2312" />

<title>div的用法在线演示www.divcss5.com</title>

<style>

div{ margin-top:10px}/* css注释说明:对div都设置上间距10px */

.divcss5-1{font-size:16px}/* 设置css字体大小16px */

.divcss5-2{color:#F00}/* 设置css字体颜色为红色 */

.divcss5-3{ background:#000color:#FFF}/* 设置CSS背景颜色为黑色和字体颜色为白色 */

.divcss5-4{ border:1px solid #F00height:60px}/* 设置css边框和CSS高度60px */

</style>

</head>

<body>

<div>普通内容一</div>

<div class="divcss5-1">我字体大小为16px</div>

<div class="divcss5-2">我字体颜色为红色</div>

<div class="divcss5-3">我背景为黑色字体为白色</div>

<div class="divcss5-4">布局设置边框和高度</div>

</body>

</html>