DIV常用样式汇总全集

最近在学习JavaScript,在做到个要控制控件样式的例子时,突然有了把常用样式汇总一下的想法,于是乎就写了以下内容,以下是以div为例来汇总的,希望对大家有些帮助。

一、常用属性:

1、Height:设置DIV的高度。

2、Width:设置DIV的宽度。

例:

Code
<div style="width:200px;height:200px;background-color:Black;">
</div>

3、margin:用于设置DIV的外延边距,也就是到父容器的距离。

       例:

Code
<div style="background-color:Black;width:500px;height:500px;">
     <div style="margin:5px 10px 20px 30px;width:200px; height:200px;background-color:White;">
     </div>
</div>

说明:margin:后面跟有四个距离分别为到父容器的上-右-下-左边的距离;可以看例子中的白色DIV到黑色DIV的边距离效果。还可以分别设置这四个边的距离,用到的属性如下:

4、margin-left:到父容器左边框的距离。

5、margin-right:到父容器右边框的距离。

6、margin-top: 到父容器上边框的距离。

7、margin-bottom:到父容器下边框的距离。

例:

用于设置DIV的内边距。

Code
<div style="width:500px;height:500px;background-color:Black;">
      <div style="margin-left:50px; margin-top:50px; width:200px; height:200px;
           background- color:White;">
            </div>
</div>

8、padding:

例:

Code
<div style="padding:5px 10px 20px 30px;background-color:Black;width:500px;height:500px;">
     <div style="width:200px; height:200px;background-color:White;"></div>
</div>

说明:padding的格式和margin的格式一样,可以对照学习。可以看黑色DIV与白色DIV的边距来体会此属性的效果。这是还需要注意的是padding设置的距离不包括在本身的width和height内(在IE7和FF中),比如一个DIV的width设置了100px,而padding-left设置了50px,那么这个DIV在页面上显示的将是150px宽。也可以用以下四个属性来分别设置DIV的内边距:

9、padding-left:左内边距。

10、padding-right: 右内边距。

11、padding-top; 上内边距。

12、padding-bottom: 下内边距。

例:

Code
<div style="padding-left:50px;padding-top:50px;width:150px;height:150px;
background-color:Black;">
     <div style="width:140px; height:140px;background-color:White;">
     </div>
</div>

13、position:设置DIV的定位方式。

例:

Code
<div style="width:200px; height:200px;background-color:Black;">
     <div style="position:relative; top:10px;left:10px; width:140px; height:140px;
background-color:White;">
     </div>
     <div style="position:absolute; top:60px;left:60px; background-color:Silver;
width:100px;height:100px;">
     </div>
     <div style="position:fixed; top:210px;left:210px; background-color:Navy;
width:100px;height:100px;">
     </div>
</div>
<div style="position:absolute; top:50px;left:50px; background-color:Blue;
width:100px;height:100px;">
</div>
<div style="position:fixed; top:200px;left:200px; background-color:Navy;
width:100px;height:100px;">
</div>
<div style="position:static; top:200px;left:100px; background-color:Yellow;
width:100px;height:100px;">
</div>

说明:position的属性中有static、fixed、relative、absolute四个属性。常用relative和absolute。若指定为static时,DIV遵循HTML规则;若指定为relative时,可以用top、left、right、bottom来设置DIV在页面中的偏移,但是此时不可使用层;若指定为absolute时,可以用top、left、right、bottom对DIV进行绝对定位;若指定为fixed时,在IE7与FF中DIV的位置相对于屏屏固定不变,IE6中没有效果(期待高手指点原因);

14、left:设置对象相对于文档层次中最近一个定位对象的左边界的位置。

15、top:设置对象相对于文档层次中最近一个定位对象的上边界的位置。

16、right:设置对象相对于文档层次中最近一个定位对象的右边界的位置。

17、bottom:设置对象相对于文档层次中最近一个定位对象的下边界的位置。

18、z-index:设置DIV的层叠顺序。

例:

Code
<div style="position:absolute; top:50px;left:50px; width:100px; height:100px;background-color:black;">
</div>
<div style="position:absolute; top:60px;left:60px; width:100px; height:100px; 
background-color:Blue;z-index:1;">
</div>
<div style="position:absolute; top:70px;left:70px; background-color:Silver;width:100px;height:100px;">
</div>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值