CSS背景

背景颜色

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>背景颜色</title>
    <style>
      div {
        width: 200px;
        height: 200px;
        /* 背景颜色 */
        /* background-color: blue; 
         transparent是默认值 透明的 */

        /* 背景图片 */
        /* background-image: url(); 
          url() 括号里面填写图片地址
          默认none没有背景图片*/

        /* 背景不平铺 */
        /* background-repeat: no-repeat; */
        /* 默认背景平铺 */
        /* 沿着X轴/Y轴平铺 */
        /* background-repeat: repeat-x;
        background-repeat: repeat-y; */
      }
    </style>
  </head>
  <body>
    <div></div>
  </body>
</html>

背景图片位置

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>背景图片位置</title>
    <style>
      h3 {
        width: 200px;
        height: 50px;
        background-image: url(./csdn.png);
        background-repeat: no-repeat;
        /* 背景图片位置 */
        /* 背景位置方位名词 */
        background-position: center left;
        /* 方位名词部分顺序,只写一个另一个默认居中 */
        /* background-position: 20px 30px; */
        /* 精确位置第一个是X轴第二个是Y轴 */

        /* 背景图像固固定 */
        /* background-attachment: fixed; */
        /* 默认scroll 背景图片跟随对象内容滚动 */

        /* 背景复合性写法 */
        /* background: transparent url() repeat fixed top;
        color image repeat fixed position
        颜色    图片  平铺    滚动   位置 */

        text-indent: 10em;
        line-height: 50px;
        font-size: 20px;
      }
    </style>
  </head>
  <body>
    <h3>csdn</h3>
  </body>
</html>

背景色半透明

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>背景色半透明</title>
    <style>
      div {
        width: 300px;
        height: 300px;
        background: rgba(0, 0, 0, 0.5);
        /* 0.5代表透明度是50% */
      }
    </style>
  </head>
  <body>
    <div></div>
  </body>
</html>

背景图片大小

(1)宽高百分比充满容器(图片很可能变形)

background-size: 100% 100%;

(2)给图片设置固定的宽高(也有可能会变形)

background-size: 100px 100px;

(3)等比缩放图片,全部填满容器为止,宽或高可能会溢出容器

background-size: cover;

(4)等比缩放图片,当宽高其中任意一个填满容器为止,可能会填充不满容器

background-size: contain;

在这里插入图片描述
ps:图片来自黑马程序员

评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值