iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >css背景图代码怎么写
  • 198
分享到

css背景图代码怎么写

2023-06-28 23:06:35 198人浏览 八月长安
摘要

这篇文章主要讲解了“CSS背景图代码怎么写”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“css背景图代码怎么写”吧!1. css背景图1.1 背景属性<!DOCTYPE ht

这篇文章主要讲解了“CSS背景图代码怎么写”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“css背景图代码怎么写”吧!

1. css背景图

1.1 背景属性

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>css 背景属性</title>    <style>        .c1        {                        width:600px;            height:600px;            border:solid 1px red;            background-color: yellow;                        background-image:url("./images/xiao.jpg");                        background-repeat:no-repeat;                                                background-attachment: fixed;        }        .c2        {                        width:600px;            height:600px;            margin:10px 20px;            border:solid 1px red;                                background: url("./images/xiao.jpg") no-repeat 50% 50%;        }    </style></head><body>    <div class="c1"></div>    <div class="c2"></div></body></html>

css背景图代码怎么写

1.2 背景图片引入

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>背景图片的引入</title>    <style>                div.c1        {width:60px;height:60px;border:solid 1px gray;background: url("./images/tag.jpg") no-repeat;}        div.c1:hover        {            background: url("./images/tag.jpg") no-repeat;            background-position: -312px -3.5px;        }        .gg        {            width:400px;            height:400px;            border:solid 1px red;        }                div.c2        {            background: url("./images/xiao.jpg") no-repeat;                                    background-size: 100% auto;        }                div.c3        {            background:                 url("./images/game/map_19.gif") no-repeat 30px 80px,                url("./images/game/map_20.gif") no-repeat 50px 50px,                url("./images/game/map_18.gif") no-repeat 100px 50px,                url("./images/game/map_14.gif") no-repeat 180px 100px,                url("./images/game/map_03.gif");        }    </style></head><body>    <div class="c1"></div>    <div class="c2 gg"></div>    <div class="c3 gg"></div></body></html>

css背景图代码怎么写

css背景图代码怎么写

css背景图代码怎么写

css背景图代码怎么写

css背景图代码怎么写

css背景图代码怎么写

2. 相对_绝对_固定

2.1 相对定位

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>定位:相对定位 relative</title>    <style>        .gg        {            width:200px;            height:200px;            border:solid 1px red;        }        .c1        {background:violet;}        .c2        {            background:tan;            position:relative;            top:10px;            left:100px;            z-index:2;        }        .c3        {background:blue;}        .c4        {background:green;}    </style></head><body>        <!--             相对定位: 参考点是他自己本身,相对于原始的位置进行定位;            如果添加了定位:无论是添加(相对,绝对,固定)属性,添加之后会增加额外的其他属性:            z-index 控制层叠关系: 值越大越在上层,值越小越在下层                left                top                right                bottom                 z-index                -->        <div class="c1 gg"></div>        <div class="c2 gg"></div>        <div class="c3 gg"></div>        <div class="c4 gg"></div></body></html>

2.2 绝对定位

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>定位:绝对定位 absolute</title>    <style>        .gg        {width:200px;height:200px;border:solid 1px red;}        .big        {            width:1000px;            height:1000px;            border:solid 1px red;            margin:100px 220px;        }        .c1        {            background:violet;                        position: relative;         }        .c2        {            background:tan;            position: absolute;            top:0px;            left:0px;                                }        .c3        {background:blue;}        .c4        {background:green;}    </style></head><body>    <!--         绝对定位:参考点默认参考的是body         效果:脱离文档流,后面的内容自动补位        使用:绝对定位会参照父级的相对定位进行移动,如果父级中没有relative,相对于body进行定位;            (1)把想要的父级元素变成relative            (2)把要定位的元素变成 absolute    -->    <div class="big">        <div class="c1 gg"></div>        <div class="c2 gg"></div>        <div class="c3 gg"></div>        <div class="c4 gg"></div>    </div></body></html><!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>定位:绝对定位 absolute</title>    <style>        .gg        {width:200px;height:200px;border:solid 1px red;}        .big        {            width:1000px;            height:1000px;            border:solid 1px red;            margin:100px 220px;        }        .c1        {            background:violet;                        position: relative;         }        .c2        {            background:tan;            position: absolute;            top:0px;            left:0px;                                }        .c3        {background:blue;}        .c4        {background:green;}    </style></head><body>    <!--         绝对定位:参考点默认参考的是body         效果:脱离文档流,后面的内容自动补位        使用:绝对定位会参照父级的相对定位进行移动,如果父级中没有relative,相对于body进行定位;            (1)把想要的父级元素变成relative            (2)把要定位的元素变成 absolute    -->    <div class="big">        <div class="c1 gg"></div>        <div class="c2 gg"></div>        <div class="c3 gg"></div>        <div class="c4 gg"></div>    </div></body></html>

2.3 固定定位

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>定位:固定定位 fixed</title>    <style>                *        {margin:0px;padding:0px;}        body        {height:2000px;}        .c1        {            width:500px;            height: 600px;            border:solid 1px red;            background-color: green;            position: fixed;            left:50%;            margin-left:-250px;            top:50%;            margin-top:-300px;        }                img        {            position:fixed;            bottom:20px;            left:-100px;             transition: all 1s ease 0.1s;                   }                img:hover        {            left:0px;            background-color: red;        }    </style></head><body>     <img src="images/xiao.jpg"/>     <div class="c1">我没动</div>     <p>1111222333444</p></body></html>

3. float浮动

3.1 display转换元素

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>display 转换元素</title>    <style>        div                {display:inline;border:solid 1px red;width:1000px;height:20px;}        span                {display:block;width:100px;height:50px;border:solid 1px red;}        a                {display:inline-block;width:500px;height:30px;border:solid 1px red;}                p                {display:none;}    </style></head><body>    <!-- 元素的分类:        块状元素 : block        行内元素 : inline        行内块状元素  : inline-block    -->    <div>第一个div</div>    <div>第二个div</div>    <span>我是span1</span>    <span>我是span2</span>    <a href="#">我是链接1</a>    <a href="#">我是链接2</a>    <p>12345</p></body></html>

3.2 float浮动

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>float 浮动</title>    <style>        .content        {width:700px;clear:both;}        .content .c1        {background: red;width:100px;height:100px;float:left;}        .content .c2        {background: tan;width:100px;height:100px;float:left;}        .content .c3        {background:blue;width:100px;height:100px;float:left;}        .content .c4        {background:green;width:100px;height:100px;float:left;}        .content2        {width:700px;height:500px;border:solid 1px red;clear:both;}        #a1        {float:left;width:100px;height:100px;border:solid 1px red;}        #a2        {display:block;width:100px;height:100px;border:solid 1px red;background: teal;clear: both;}    </style></head><body>    <!--     # ###块状元素浮动:    float:left  向左浮动  ,元素脱离原始文档流,后面的内容自动补齐;    float:right 向右浮动  ,元素脱离原始文档流,后面的内容自动补齐;    目的: 让块状元素在一排显示     clear:both; 清除两边的浮动    -->    <div class="content">        <div class="c1"></div>        <div class="c2"></div>        <div class="c3"></div>        <div class="c4"></div>    </div>    <!--     # ###行内元素浮动:        如果对行内元素进行浮动:        默认会把行内元素升级成行内块状元素,可以设置宽和高         消除浮动产生的影响:clear:both;    -->    <div class="content2">        <a href="#" id="a1">点击我跳转1</a>        <a href="#" id="a2">点击我跳转2</a>    </div></body></html>

4. html里面的bug

4.1 float内容塌陷问题

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>float 浮动</title>    <style>        .content        {width:700px;clear:both;}        .content .c1        {background: red;width:100px;height:100px;float:left;}        .content .c2        {background: tan;width:100px;height:100px;float:left;}        .content .c3        {background:blue;width:100px;height:100px;float:left;}        .content .c4        {background:green;width:100px;height:100px;float:left;}        .content2        {width:700px;height:500px;border:solid 1px red;clear:both;}        #a1        {float:left;width:100px;height:100px;border:solid 1px red;}        #a2        {display:block;width:100px;height:100px;border:solid 1px red;background: teal;clear: both;}    </style></head><body>    <!--     # ###块状元素浮动:    float:left  向左浮动  ,元素脱离原始文档流,后面的内容自动补齐;    float:right 向右浮动  ,元素脱离原始文档流,后面的内容自动补齐;    目的: 让块状元素在一排显示     clear:both; 清除两边的浮动    -->    <div class="content">        <div class="c1"></div>        <div class="c2"></div>        <div class="c3"></div>        <div class="c4"></div>    </div>    <!--     # ###行内元素浮动:        如果对行内元素进行浮动:        默认会把行内元素升级成行内块状元素,可以设置宽和高         消除浮动产生的影响:clear:both;    -->    <div class="content2">        <a href="#" id="a1">点击我跳转1</a>        <a href="#" id="a2">点击我跳转2</a>    </div></body></html>

4.2 margin-top失效问题

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>margin-top失效问题</title>    <style>        *        {margin:0px;padding:0px;}        .box1        {width:100px;height:100px;margin-top:10px;border:solid 1px red;}        .father        {width:300px;height:300px;background: yellow;overflow: hidden;}        .son        {width:150px;height:150px;margin-top:50px;}    </style></head><body>    <!-- overflow: hidden; overflow auto 如果内容超出边框,会以下拉框的形式显示,不会溢出 -->    <div class="box1">        sdfsf    </div>    <div class="father">        <div class="son">12</div>            </div></body></html>

4.3 overflow

<!DOCTYPE html><html>    <head>    <meta charset="utf-8" />    <style>        .test {            overflow: hidden;            width: 200px;            height: 100px;            background: #eee;        }    </style>    </head>    <body>        <!-- overflow:hidden 对超出部分内容进行隐藏 -->        <div class="test">            <p>苏打速度</p>            <p>苏打速度</p>            <p>苏打速度</p>            <p>苏打速度</p>            <p>苏打速度</p>        </div>    </body></html>

感谢各位的阅读,以上就是“css背景图代码怎么写”的内容了,经过本文的学习后,相信大家对css背景图代码怎么写这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

--结束END--

本文标题: css背景图代码怎么写

本文链接: https://www.lsjlt.com/news/321245.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • css背景图代码怎么写
    这篇文章主要讲解了“css背景图代码怎么写”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“css背景图代码怎么写”吧!1. css背景图1.1 背景属性<!DOCTYPE ht...
    99+
    2023-06-28
  • html设置背景图片的代码怎么写
    这篇文章主要介绍html设置背景图片的代码怎么写,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!html设置背景图片的代码是:“<body background="图片地址"></...
    99+
    2023-06-14
  • html背景颜色代码怎么写
    非常抱歉,由于您没有提供文章标题,我无法为您生成一篇高质量的文章。请您提供文章标题,我将尽快为您生成一篇优质的文章。...
    99+
    2024-05-16
  • PNG图片在IE6中背景不透明的CSS与JS代码怎么写
    本篇内容主要讲解“PNG图片在IE6中背景不透明的CSS与JS代码怎么写”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“PNG图片在IE6中背景不透明的CSS与J...
    99+
    2024-04-02
  • css怎么设置背景图片和背景颜色
    这篇文章主要讲解了“css怎么设置背景图片和背景颜色”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“css怎么设置背景图片和背景颜色”吧!一、设置背景颜色:b...
    99+
    2024-04-02
  • css怎么设置背景图片
    css设置背景图片的方法:可以通过background-image属性设置背景图片。background-image属性用法格式。background-image:url(1.jpg);//url里是图片的路径示例:在html文件中css设...
    99+
    2024-04-02
  • css怎么将背景图居中
    这篇文章主要介绍css怎么将背景图居中,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完! css中,可利用“background-position”属性让背景...
    99+
    2024-04-02
  • CSS怎么设置div背景图
    这篇文章主要介绍了CSS怎么设置div背景图,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。给组件添加背景图控制只需要两步:<View   ...
    99+
    2023-06-08
  • css怎么改变图片的背景
    这篇文章主要介绍css怎么改变图片的背景,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!css改变图片的背景的方法是,添加background-image属性,并且将属性值设置为你想要的图片的url地址,例如【back...
    99+
    2023-06-15
  • css中怎么设置背景图片
    这篇文章主要为大家展示了“css中怎么设置背景图片”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“css中怎么设置背景图片”这篇文章吧。   &nbs...
    99+
    2024-04-02
  • css怎么设置div背景图片
    这篇文章主要介绍了css怎么设置div背景图片的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇css怎么设置div背景图片文章都会有所收获,下面我们一起来看看吧。一、背景基本语法...
    99+
    2024-04-02
  • css怎么在背景图片上加图片
    在 css 中可以叠加图片到背景图片上,方法包括:指定图片 url(1)、调整位置(2)、设置大小(3)、控制透明度(4)、使用 css 滤镜(5)。 如何在 CSS 中在背景图片上叠...
    99+
    2024-04-25
    css
  • css图片居中代码怎么写
    css图片居中方法:使用margin属性设置图片上下左右边距,居中图片。使用text-align属性设置容器文本居中,并使用display: block;和margin: 0 auto;...
    99+
    2024-04-25
    css
  • Css边框和背景代码有哪些
    今天小编给大家分享一下Css边框和背景代码有哪些的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下...
    99+
    2024-04-02
  • CSS怎么实现背景图像透明
    这篇文章主要为大家展示了“CSS怎么实现背景图像透明”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“CSS怎么实现背景图像透明”这篇文章吧。   首先,我们来看...
    99+
    2024-04-02
  • css怎么实现背景图片平铺
    这篇文章主要介绍了css怎么实现背景图片平铺,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。   我们首先来看一下css设置背景图片平铺方式...
    99+
    2024-04-02
  • css怎么设置背景图片居中
    在 css 中,可通过 background-position 属性设置背景图片居中:水平值:center(居中)、left(左对齐)、right(右对齐)垂直值:center(居中)、...
    99+
    2024-04-25
    css 垂直居中
  • CSS背景色渐变写法
    这篇文章主要介绍“CSS背景色渐变写法”,在日常操作中,相信很多人在CSS背景色渐变写法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”CSS背景色渐变写法”的疑惑有所帮助!接...
    99+
    2024-04-02
  • 怎么设置css背景图片颜色
    这篇文章主要讲解了“怎么设置css背景图片颜色”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么设置css背景图片颜色”吧! c...
    99+
    2024-04-02
  • css怎么改变背景图片大小
    这篇文章主要介绍css怎么改变背景图片大小,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完! 在css中,可以使用background-size属性来改变背景...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作