用css来设置div的位置

html-css017

用css来设置div的位置,第1张

设置*{margin:0padding:0}给它个初始化就行了。这个就是预定义。我通常用个定义一个站所有的初始化*{margin:0padding:0}

img{border:0}

td { line-height:25px}

ul,li{ list-style-type:none}

body {

margin: 0px

background-color:#ffffff

font-size: 12px

height:100%

background-repeat:repeat-x

background-position:center top

}

html {

height:100%

}

.clear { clear:both}

a {color:#f00font-size:12pxtext-decoration:underline}

a:hover {color:#f00text-decoration:none}

ul {

margin: 0px

padding: 0px

}

li {

margin: 0px

padding: 0px

}

CSS中的position property一共有四种:

postion: static

postion: relative

position: fixed

position: absolute

如果设置div为static postion, div的位置将不受top,right,left,button等变量的影响,而是按照正常的页面布局进行排版。例:

div.static {

        position: static

        border: 3px solid #8AC007

    }

如果设置div为relative position, 其变量的值将会使div的位置相对其正常(default)位置进行移动。例:

div.relative {

        position: relative

        left: 30px

        border: 3px solid #8AC007

    }

如果设置div为fixed position, div将会被固定在窗口的固定位置。也就是说无论你如何上下移动页面, div在屏幕上显示的位置始终不变。

div.fixed {

        position: fixed

        bottom: 0

        right: 0

        width: 300px

        border: 3px solid #8AC007

    }

如果设置div为absolute position, div将会相对于其最近的position ancestor定位。absolute position是可以随页面移动而移动在屏幕上的位置的。

div.absolute {

        position: absolute

        top: 80px

        right: 0

        width: 200px

        height: 100px

        border: 3px solid #8AC007

    }

以上CSS你可以放到自己的网页应用里试一下,区别就很明显了。

参考资料:http://www.w3schools.com/css/css_positioning.asp

在css中设置字体和div的位置:

在css中设置字体,一般情况下,我们都是通过font这个css属性来完成,font-family:"字体名字(如:微软雅黑)"还可以使用font-size对字体的大小做限制,如font-size:12px;

div的位置,我们首先需要知道你要将你的这个div固定的位置,也就是left和top,在使用position的定位来做就行,这里给出具体的代码:

<html>

<head>

<style>

.headr{

width:300px

height:200px

border:1px

solid

#f00

position:absoulte

left:100px

//只是假定的值,具体需测量

top:200px

}

</head>

<body>

<div

class="headr"

>

//页头

</div>

</body>

</html>