HTML 中 使用CSS样式(下)

上节内容回顾

  • 补充:默认img标签,有一个1px的边框

如果点击图片调转到连接,a标签下套img标签,在IE有的版本中,会有蓝色边框。

    <a href="http://blog.csdn.net/fgf00" target="_blank">
        <img src="1.png" style="width: 300px; height: 200px" />
    </a>

因为a标签默认字体颜色就是蓝色,img标签继承了父级标签,而IE浏览器默认给边框加了宽度。解决:

<head>
    <style>
        img {
            border: 0;
        }
    </style>
</head>
<body>
    <a href="http://blog.csdn.net/fgf00" target="_blank">
        <img src="作业--效果图.png" style="width: 300px; height: 200px" />
    </a>
</body>
  • 回顾
    1、块级和行内
    2、form标签 提交表单
            <form action='http://sssss' methed='GET' enctype="multipart/form-data">
                <div>asdfasdf</div>
                <input type='text' name='q' />
                # 上传文件
                <input type='file' name='f' />  <!--依赖form表单属性 enctype-->
                <input type='submit' />
            </form>

            GET: http://sssss?q=用户输入的值&b=用户输入的内容
            POST:
                请求头
                请求内容
    3、display: block;inline;inline-block
    4、float:
        <div>
            <div style='float:left;'>f</div>
            <div style='clear:both;'></div>
        </div>

    5、margin: 0 auto;  <!--整个页面居中-->
    6、padding, ---> 内边距。自身发生变化

一、css 之 position 分层

1、posttion:fixed 永远固定位置

返回顶部和上方的菜单,永远固定在一个位置

网页有好几屏,不管怎么拉,右下角都有个返回顶部,网页的菜单也一直固定在上方。

前边的内容,所有的页面都是在一层上的。实现返回顶部,就需要position:fixed分层了。

CSS全称:层叠样式表

  • 返回顶部

position:fixed ==> 固定在页面某个位置

<div onclick="GoTop();" style="width: 40px; height: 40px; background-color: #0000cc; color: white;
    position: fixed;
    bottom: 20px;
    right: 20px;
    ">返回顶部</div>
<div style="height: 5000px; background-color: #dddddd;">FGFGF</div>
<script>
    function GoTop(){
        document.body.scrollTop = 0;
    }
</script>
  • 菜单永远在顶部

菜单position固定,内容margin设定外边距

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .pg-header{
            height:48px;  background-color:#2459a2;  color: #dddddd;
            position:fixed;
            top:0;  right:10px;  left:10px;
        }
        .pg-body{
            background-color: #dddddd;  height: 5000px;
            margin-top: 50px;
        }
    </style>
</head>
<body>
    <div class="pg-header">头部</div>
    <div class="pg-body">内容</div>
</body>

2、posttion:relative + absolute 相对定位

posttion: absolute :一次性固定在网页某个位置,再移动滑轮跟着动

posttion: relative :单独使用没有任何变化。

组合(relative + absolute) :固定在父类标签的某个位置。

<div style="position: relative; width:500px;height:200px; background-color: #b3b3b3; margin:auto;">
    <div style="position: absolute; left: 0; bottom: 0; width: 50px; height: 50px; background-color: blue;"></div>
</div>

3、多层展示

  • opcity: 0.5 透明度
  • z-index: 层级顺序 ,值大在上面
<!--z-index:值大的在上面; display:none;-->
<div style="z-index:10; position:fixed;
top:50%; left:50%;margin-left:-250px; margin-top:-200px;  /*居中*/
background-color:white; height:400px; width:500px;">
    <input type="text" />
</div>

<!--opacity:0.5 透明度;上右下左:0:全遮住-->
<div style="z-index:9; position:fixed; opacity:0.5;
top:0; bottom:0; right:0; left:0; background-color:black;"></div>

<div style="height:5000px; background-color: greenyellow">FGFFGF</div>

二、css 之 overflow 图片显示方式

当图片大小超过了父级标签,就把父级标签撑开了。

  • overflow: auto:图片滚动条显示
  • overflow: hidden:图片只显示父级标签大小
<!--图片出现滚动条-->
<div style="height:200px; width:300px; overflow: auto">
    <img src="1.png" />
</div>
<!--图片只显示父级大小-->
<div style="height:200px; width:300px; overflow: hidden">
    <img src="1.png" />
    <!--<img src="1.png" style="height:200px; width:300px;"/>-->
</div>

三、css 之 hover 鼠标移过去样式

网站上,鼠标移动过去,变颜色

样式后加上hover :当鼠标移动到当前标签上时,才生效的css属性

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .pg-header{
            position: fixed;
            right: 0;  left: 0;  top: 0;  height: 48px;
            background-color: #2459a2;  line-height: 48px;
        }
        .pg-body{
            margin-top: 50px;
        }
        .w{
            width: 980px;
            margin: 0 auto;
        }
        .pg-header .menu{
            display: inline-block;
            padding: 0 10px 0 10px;  /*上 右 下 左 加上内边距*/
            color: white;
        }
        /*当鼠标移动到当前标签上时,以下CSS属性才生效*/
        .pg-header .menu:hover{
            background-color: blue;
        }
    </style>
</head>
<body>
    <div class="pg-header">
        <div class="w">
            <a class="logo">LOGO</a>
            <a class="menu">全部</a>
            <a class="menu">42区</a>
            <a class="menu">段子</a>
            <a class="menu">1024</a>
        </div>
    </div>

    <div class="pg-body">
        <div class="w">a</div>
    </div>
</body>

四、css 之 background 背景

  • background-color :背景颜色
  • background-image:背景图片

设置了背景图片,当图片很小,父级div很大,图片会堆叠重复放。水平垂直方向都会帮你重复增加。

<div style="height:790px; width:980px; border: 1px solid red;">
    <div style="background-image: url(http://www.autohome.com.cn/images/error/bg.png); height: 100%; "></div>
</div>
  • background-repeat(no-repeat;repeat-x;repeat-y):指定方向堆叠

    • no-repeat:不堆叠
    <div style="background-image:url('4.gif'); height: 80px;background-repeat:no-repeat"></div>
    • repeat-x:只水平堆叠

    • repeat-y:只垂直堆叠

  • background-position :指定背景显示位置
    如图:icon.png
    这里写图片描述

<div style="background-image: url(icon.png);
             background-repeat:no-repeat;
             height: 20px;width:20px;
             border: 1px solid red;
             background-position-x: 0;    <!--水平方向位置-->
             background-position-y: -78px;<!--垂直方向位置-->
             "></div>

这样指定位置有点麻烦,下面说下几种指定x、y值的方式

<!--第一种:直接x、y写-->
background-position-x:
background-position-y:
<!--第二种:background-position(直接跟x、y值)-->
background-position: 10px 10px  
<!--第三种: 简写的方式:-->
<div style="background: #f8f8f8 url(icon.png) 0 -78px no-repeat;
        height: 20px;width:20px;
        border: 1px solid red;
        "></div>

五、小练习

实现用户名,密码登录框。要求登录框上有如下小图标
这里写图片描述
i_name.jpg
这里写图片描述
i_pwd.jpg
这里写图片描述

<div style="height: 35px; width: 400px; position: relative;">
    <!--输入框输满之后,会被图标挡住;为避免挡住,添加padding内边距(370+30=400px)-->
    <input type="text" style="height: 35px; width: 370px; padding-right: 30px;" />
    <!--放在父级标签的指定位置。-->
    <span style="position: absolute; right:6px; top:10px; background-image: url(i_name.jpg);
    height:16px; width: 16px; display: inline-block;"></span>
</div>

<!--以下功能重复,看一个练习即可-->
<div style="margin: 10px 0; height: 35px; width: 400px; position: relative;">
    <input type="password" style="height: 35px; width: 370px; padding-right: 30px;" />
    <span style="position: absolute; right:6px; top:10px; background-image: url(i_pwd.jpg);
    height:16px; width: 16px; display: inline-block;"></span>
</div>

转载请务必保留此出处:http://blog.csdn.net/fgf00/article/details/53180724


六、总结

CSS补充
    position:
        a. fiexd => 固定在页面的某个位置

        b. relative + absolute

            <div style='position:relative;'>
                <div style='position:absolute;top:0;left:0;'></div>
            </div>

    opcity: 0.5 透明度
    z-index: 层级顺序   
    overflow: hidden,auto
    hover

    background-image:url('image/4.gif'); # 默认,div大,图片重复访
    background-repeat: repeat-y;
    background-position-x:
    background-position-y:

    示例:输入框右边放置图标
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
div的position属性详细讲解 1.流动模型 (块元素,内联元素) 2.浮动模型 (float:) 块状元素这么霸道都是独占一行,如果现在我们想让两个块状元素并排显示,怎么办呢?不要着急,设置元素浮动就可以实现这一愿望。 3.层模型--绝对定位 如果想为元素设置层模型的绝对定位,需要设置position:absolute(表示绝对定位),这条语句的作用将元素从文档流拖出来,然后使用left、right、top、bottom属性相对于其最接近的一个具有定位属性的父包含块进行绝对定位。如果不存在这样的包含块,则相对于body元素,即相对于浏览器窗口。 如下面代码可以实现div元素相对于浏览器窗口向右移动100px,向下移动50px。 div{ width:200px; height:200px; border:2px red solid; position:absolute; left:100px; top:50px; } 效果如下: 层模型--相对定位 如果想为元素设置层模型的相对定位,需要设置position:relative(表示相对定位),它通过left、right、top、bottom属性确定元素在正常文档流的偏移位置。相对定位完成的过程是首先按static(float)方式生成一个元素(并且元素像层一样浮动了起来),然后相对于以前的位置移动移动的方向和幅度由left、right、top、bottom属性确定,偏移前的位置保留不动。 如下代码实现相对于以前位置向下移动50px,向右移动100px; #div1{ width:200px; height:200px; border:2px red solid; position:relative; left:100px; top:50px; } 效果图: 层模型--固定定位 fixed:表示固定定位,与absolute定位类型类似,但它的相对移动的坐标是视图(屏幕内的网页窗口)本身。由于视图本身是固定的,它不会随浏览器窗口的滚动条滚动而变化,除非你在屏幕移动浏览器窗口的屏幕位置,或改变浏览器窗口的显示大小,因此固定定位的元素会始终位于浏览器窗口内视图的某个位置,不会受文档流动影响,这与background-attachment:fixed;属性功能相同。以下代码可以实现相对于浏览器视图向右移动100px,向下移动50px。并且拖动滚动条时位置固定不变。 效果见代码::: relative样式 #div1{ width:300px; height:200px; border:2px red italic; position:fixed; bottom:0; right:0; 文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本。 文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本。 文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本。 文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本。 文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本。 文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本。 文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本。 文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本。 文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本。 Relative与Absolute组合使用 小伙伴们学习了12-6小节的绝对定位的方法:使用position:absolute可以实现被设置元素相对于浏览器(body)设置定位以后,大家有没有想过可不可以相对于其它元素进行定位呢?答案是肯定的,当然可以。使用position:relative来帮忙,但是必须遵守下面规范: 1、参照定位的元素必须是相对定位元素的前辈元素: 相对参照元素进行定位 从上面代码可以看出box1是box2的父元素(父元素当然也是前辈元素了)。 2、参照定位的元素必须加入position:relative; #box1{ width:200px; height:200px; position:relative; } 3、定位元素加入position:absolute,便可以使用top、bottom、left、right来进行偏移定位了。 #box2{ position:absolute; top:20px; left:30px; } 这样box2就可以相对于父元素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=utf-8" /> <title>慕课网</title> <link href="css/style.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="js/js/myfocus-2.0.1.min.js"></script> <script type="text/javascript"> myFocus.set({ id:'demo', pattern:'mF_liquid'}); </script> </head> <body> <div id="top"> <div id="top_text"> <ul> <li><a href="#">联系我们</a></li> <li><a href="#">加入收藏</a></li> <li><a href="#">设为首页</a></li> </ul> </div> </div> <div id="middle"> <div id="logo"> <div id="logo_left"> <img src="images/logo.jpg" alt="" width="200" height="80" /> </div> <div id="logo_right"> <img src="images/tel.jpg" width="28" height="28" /> 24小时服务热线 <span>123-456-7890</span> </div> <br class="clear_float"> </div> <div id="nav"> <div id="nav_left"> <img src="images/nav_left.jpg" width="10" height="40" /> </div> <div id="nav_middle"> <ul> <li><a href="#">首页</a></li> <li><a href="#">关于幕课</a></li> <li><a href="#">新闻动态</a></li> <li><a href="#">课程心</a></li> <li><a href="#">在线课程</a></li> <li><a href="#">人才招聘</a></li> </ul> <div id="gezi"> <form> <input type="text" /> </form> </div> </div> <div id="nav_right"> <img src="images/nav_right.jpg" width="10" height="40" /> </div> </div> <div id="demo" class="ad"> <div class="pic"> <ul> <li><a href="#"><img src="images/ad2.jpg" thumb="" alt="" text="详细描述2" /></a></li> <li><a href="#"><img src="images/ad3.jpg" thumb="" alt="" text="详细描述3" /></a></li> <li><a href="#"><img src="images/ad4.jpg" thumb="" alt="" text="详细描述4" /></a></li> <li><a href="#"><img src="images/ad3.jpg" thumb="" alt="" text="详细描述5" /></a></li> </ul> </div>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值