div的定位(3)

(7)给包含块一个高度,或者说给定位对象的父级设定一个高度。不然也许显示的结果并不是你想要的。在下面的例子中,如果你本意要实现下面效果:把小盒子在父盒子中的左上角垂直往下偏移,上方留出一块区域,如果你删除了box1的高度会怎么样呢?自己测试一下吧。

<! 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 >
< style  type ="text/css" >
<!--
body 
{
    margin
:0px;
    font-size
: 9pt;
    line-height
:12pt;
    margin-top
: 150px;
    margin-left
: 150px;
}

.box1 
{
    background-color
: #33CCFF;
    width
: 200px;
        height
: 200px;/*如果不设定这个高度,也许显示的结果并不是你想要的*/
    
}

.box2 
{
    background-color
: #CC99FF;
    height
: 100px;
    width
: 100px;
    position
:relative;
    bottom
:-100px;
}

-->
</ style >
</ head >

< body >
< div  class ="box1" >
  
< div  class ="box2" ></ div >
</ div >
</ body >
</ html >
本文转中国秀未来设计  http://www.showweb.cn ,转载请注明出处。

(8)放置绝对定位对像到可视区外会出现滚动条。一般情况下,绝对定位是用来方便定位网页要看得见元素的,而不是拿它来隐藏对象的,一般隐藏对象常用到display与margin,如果你试图把绝对定位的对象移出可视区域,嘿嘿,浏览器会为你准备滚动条去看望它。

<! 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 >
< style  type ="text/css" >
<!--
body 
{
    margin
:0px;
    font-size
: 9pt;
    line-height
:12pt;
}

.box
{
    background-color
: #996699;
    height
: 200px;
    width
: 270px;
    background-image
: url(http://farm1.static.flickr.com/48/172522117_410a1e87c1_m.jpg);
    background-repeat
: no-repeat;
    background-position
: center center;
    position
: absolute;
    left
: 900px;
    top
:0px;
}

.box2
{
    height
: 200px;
    width
: 270px;
    border
: 1px dashed #996699;
}

-->
</ style >
</ head >

< body >
< div  class ="box2" > 这是右边对象原来所在的位置,即没有被赋予定位属性前的位置 </ div >
< div  class ="box" ></ div >
</ body >
</ html >
本文转中国秀未来设计  http://www.showweb.cn ,转载请注明出处。

9)放置相对定位对像到可视区外不会出现滚动条。此现象很好理解,因为相对定位对象原来位置没有变,原来位置没有出去滚动条就不会出现。在下面实例中,如果你缩小浏览器窗口,相对定位的对象还会出现完全消失的情况呢。

<! 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 >
< style  type ="text/css" >
<!--
body 
{
    margin
:0px;
    font-size
: 9pt;
    line-height
:13pt;
}

.box
{
    background-color
: #996699;
    height
: 200px;
    width
: 270px;
    background-image
: url(http://farm1.static.flickr.com/48/172522117_410a1e87c1_m.jpg);
    background-repeat
: no-repeat;
    background-position
: center center;
    position
: relative;
    left
: 900px;
}

.box2
{
    height
: 200px;
    width
: 270px;
    position
:absolute;
    border
: 1px dashed #996699;
}

-->
</ style >
</ head >

< body >
< div  class ="box2" > 虚线框是右边盒子原来的位置,即用了相对定位后,把对象重新定位到了右边,但对象的位置其实还在这里,或者说对象实际上并没有越出浏览器可视区域,所以不会出现滚动条。 < br  />
我说过相对定位的的盒子原有位置是不会被占用的,但我为什么可以在这里写说明呢,这是因为我用了绝对定位,我只是为了方便说明才这样做的,请不要误解了。
</ div >
< div  class ="box" ></ div >
</ body >
</ html >
本文转中国秀未来设计  http://www.showweb.cn ,转载请注明出处。

10)定位的特殊值情况。如果定位设置是这样的:position:relative ,right:200px, left:10px会出现什么情况呢?一边叫对象向右偏移10px,另一边又叫对象向左偏移200px,到底是听那边的呢,还是那个先,那个后呢。针对这种矛盾情况,CSS规定只听左边的left,而右边怎么设置都被重定为-left,即-left =right。上下值top与bottom矛盾的话,就以top为准,所以在下面的实例中,你如何改变right的值,对象位置不会发生改变的。

<! 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 >
< style  type ="text/css" >
<!--
body 
{
    margin
:0px;
    font-size
: 9pt;
    line-height
:12pt;
    margin-top
: 150px;
    margin-left
: 150px;
}

.box1 
{
    background-color
: #33CCFF;
    width
: 200px;
    height
: 200px;
}

.box2 
{
    background-color
: #CC99FF;
    height
: 100px;
    width
: 100px;
    position
:relative;
    right
:200px;/*此值总被认为是-left=-10px,所以你怎么改它都不会影响到布局*/
    left
:10px;/*这里设定了两个定位值,但是它们是矛盾的,所以此时将以左边为准,如果是上下类型错误,则以上边定位数值为准*/

}

-->
</ style >
</ head >

< body >
< div  class ="box1" >
  
< div  class ="box2" ></ div >
</ div >
</ body >
</ html >
本文转中国秀未来设计  http://www.showweb.cn ,转载请注明出处。

 

 

<! 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 >
< style  type ="text/css" >
<!--
*
{
    margin
:0px;
    padding
:0px;
}

body 
{
    margin
:10px;
    font-size
: 13px;
}

a:link 
{
    color
: #666;
    text-decoration
: none;/*去除链接下划线*/
}

a:visited 
{
    color
: #666;
    text-decoration
: none;
}

a:hover 
{
    color
: #F90;
}

h3 
{
    color
: #FFF;
    background-color
: #F90;
    width
: 100px;
    padding-top
:3px;
    text-align
:center;
}

ul 
{
    width
: 300px;
    border-top
: 1px solid #F60;/*使其上边有一线条,与标题h3吻合*/
}

ul li 
{
    padding
:5px;
    border-bottom
: 1px solid #CCC;
    list-style
:none;/*去除列表样式,这对于标准浏览很重要*/
}

{
    position
: relative;/*设置其定位方法为相对定位,等一下内部信息面板就可以相对它定位*/
    display
:block;/*让链接以块状呈现,这样不用点中链接文字就可以响应链接*/
}

a div 
{
    display
: none;/*初始化信息面板不显示*/
}

a:hover 
{background:#fff;}/*IE7以下版本A状态伪类bug*/
a:hover div 
{
    position
: absolute;
    padding
:5px;
    display
:block;
    width
: 245px;/*只给出宽度,高让它随内容多少自动调整*/
    left
:150px;/*这是相对父级A的定位*/
    top
: 20px;
    border
: 1px solid rgb(91,185,233);
    background-color
: rgb(228,246,255);
    z-index
:999;/*把信息面板提到一个较高的位置,使链接文字过长时不会与面板重叠,但这只对FF有效*/
}

a img 
{
    width
:80px;
    height
:80px;
    border
:none;/*去除图片边框,默认情况下,链接内的图片在标准浏览器会出现边框*/
    display
:block;
    position
: absolute;/*用绝对定位抽离正常文本流,不然在设计的时候考虑到不同浏览器正常显示会更麻烦*/
    top
:5px;/*这里的5px是与信息面板大盒子的填充一样的*/
    left
:5px;
}

dl 
{
    width
: 160px;
    float
:right;
    color
: #999;
    line-height
:20px;
}

dl dd span 
{
    font-weight
: bold;
    color
: #639;
}

-->
</ style >
</ head >

< body >
< h3 > 最新单曲 </ h3 >
< ul >

< li >< href ="#" > 01 爱的文身 黄圣依 < div >< img  src ="http://imgcache.qq.com/music/photo/singer/54/singerpic_5554_0.jpg"  alt =""   />
< dl >
    
< dd >< span > 歌名: </ span > 爱的文身 </ dd >
    
< dd >< span > 歌手: </ span > 黄圣依 </ dd >
    
< dd >< span > 介绍: </ span > 黄圣依唱片主打歌的确是她个人的内心写照,《爱的文身》由香港音乐大师金培达作曲,制作人陈少琪亲自填词。 </ dd >
</ dl ></ div ></ a ></ li >

< li >< href ="#" > 02 累了 阿信 < div >< img  src ="http://imgcache.qq.com/music/photo/singer/47/singerpic_6547_0.jpg"  alt =""   />
< dl >
    
< dd >< span > 歌名: </ span > 累了 </ dd >
    
< dd >< span > 歌手: </ span > 阿信 </ dd >
    
< dd >< span > 介绍: </ span > 青春校园偶像剧----【熱情仲夏】片尾曲 </ dd >
</ dl ></ div ></ a ></ li >

< li >< href ="#" > 03 漫漫 慢慢 阿朵 < div >< img  src ="http://imgcache.qq.com/music/photo/singer/61/singerpic_6361_0.jpg"  alt =""   />
< dl >
    
< dd >< span > 歌名: </ span > 漫漫 慢慢 </ dd >
    
< dd >< span > 歌手: </ span > 阿朵 </ dd >
    
< dd >< span > 介绍: </ span > 阿朵抢听版最新单曲《漫漫 慢慢》让你认识阿朵柔情的一面,展现阿朵百变的风格。 </ dd >
</ dl ></ div ></ a ></ li >

< li >< href ="#" > 04 我怀念的 孙燕姿 < div >< img  src ="http://imgcache.qq.com/music/photo/singer/9/singerpic_109_0.jpg"  alt =""   />
< dl >
    
< dd >< span > 歌名: </ span > 我怀念的 </ dd >
    
< dd >< span > 歌手: </ span > 孙燕姿 </ dd >
    
< dd >< span > 介绍: </ span > 令人感同身受的抒情歌,在故事性的架构中,有着平凡但又能扣人心弦的情感,是一首高度共鸣的抒情歌。 </ dd >
</ dl ></ div ></ a ></ li >

< li >< href ="#" > 05 听,花期越来越近 后弦 < div >< img  src ="http://imgcache.qq.com/music/photo/singer/33/singerpic_4733_0.jpg"  alt =""   />
< dl >
    
< dd >< span > 歌名: </ span > 花期越来越近 </ dd >
    
< dd >< span > 歌手: </ span > 后弦 </ dd >
    
< dd >< span > 介绍: </ span > 后弦参与《花开的声音》这个舞台剧里的一部分,邀请了后弦去演唱这首歌,此歌就是为舞台剧《花开的声音》而创作。 </ dd >
</ dl ></ div ></ a ></ li >

</ ul >
</ body >
</ html >
本文转中国秀未来设计  http://www.showweb.cn ,转载请注明出处。

 

 

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值