整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

HTML如何设置div背景图片的的透明度

这样一个需求,就是在一个DIV中包含有一个Image标签,但是在Div标签中包含有一张背景图片,设计图上的样子是这张背景图片是有一个透明度的,但是如果直接使用opacity属性设置的的话就会连Div中的内容的透明度也会受到影响,那么我们如何在HTML中设置div背景图片的透明度呢?,可以通过以下几种方法实现。

方法一:使用伪元素

这是在日常开发中被推荐使用的方法,通过这种方式实现不会影响到div中的其他内容的透明度只会影响它自己背景的透明度,详细实现如下。

<!DOCTYPE html>
<html>
<head>
    <style>
        .container {
            position: relative;
            width: 300px;
            height: 200px;
            overflow: hidden;
        }

        .container::before {
            content: "";
            background-image: url('your-image.jpg');
            background-size: cover;
            background-position: center;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            opacity: 0.5; /* 调整透明度 */
            z-index: 1;
        }

        .content {
            position: relative;
            z-index: 2;
            color: white;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="content">
            这里是内容
        </div>
    </div>
</body>
</html>

方法二:使用RGBA颜色叠加

这种方式比较适合那种需要给背景图片上添加蒙版的情况,但是笔者尝试的时候,结果实在是不尽人意。所以还是选择了上面的推荐方法,不过这种方式要比上面的那种方式实现起来要简单很多。如下所示。

<!DOCTYPE html>
<html>
<head>
    <style>
        .container {
            width: 300px;
            height: 200px;
            background: rgba(255, 255, 255, 0.5) url('your-image.jpg') no-repeat center center;
            background-size: cover;
        }
    </style>
</head>
<body>
    <div class="container">
        这里是内容
    </div>
</body>
</html>

方法三:使用CSS滤镜

这种方式实现会影响到整个的div的样式,也就是说页面中的内容的透明度也会受到影响,并且这种影响不会被其他样式所改变。如下所示。

<!DOCTYPE html>
<html>
<head>
    <style>
        .container {
            width: 300px;
            height: 200px;
            background: url('your-image.jpg') no-repeat center center;
            background-size: cover;
            filter: opacity(0.5); /* 调整透明度 */
        }
    </style>
</head>
<body>
    <div class="container">
        这里是内容
    </div>
</body>
</html>

以上就是实现如何调整div的背景透明度,在一些特殊场景中我们还可以通过JS的方式来实现。上面的方法中,推荐使用的是伪元素方法,因为它在修改了div背景透明度之后,并不会影响到其他的元素,RGBA色彩添加则是局限于一些色彩华丽的地方使用,而对于一些单色调的内容来讲这种方式实现效果不是太好。通过CSS过滤样式,虽然是最直接的方式,但是如果在div内部有内容的情况下会影响到整个组件体系的样式。

在实际开发中,我们可以选择合适的方式来实现这个需求。当然还有其他的实现方式,有兴趣的读者可以留言我们一起讨论。

目中有时候会遇到这个问题:一行有3个div,希望这3个div平分屏幕宽度,并且高度等于宽度。

第一个问题:平分屏幕宽度

可以对div设置百分比宽度,而不是直接用px宽度,这里用到了响应式设计的思想,可以参考这篇文章:自适应网页设计(Responsive Web Design)

第二个问题:动态设置高度和宽度一致

有两种方法,一种是用js动态设置,一种是直接用CSS设置

先看下html代码

<ul>

<li class="container">

<div class="dummy">

</div>

<div class="element">

some text

</div>

</li>

<li class="container">

<div class="dummy">

</div>

<div class="element">

some text

</div>

</li>

<li class="container">

<div class="dummy">

</div>

<div class="element">

some text

</div>

</li>

</ul>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

公用的CSS

ul,li{

list-style: none;

}

* {

margin: 0;

padding: 0;

outline: 0

}

body {

margin: 0;

padding: 0;

-webkit-appearance: none;

font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;

font-size: 16px;

}

ul{

margin:10px;

}

.container {

display: inline-block;

position: relative;

width: 32%;

text-align: center;

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

用js动态设置

var cw = $('.dummy').width();

$('.dummy').css({'height':cw+'px'});

$(window).resize(function() {

var cw = $('.dummy').width();

$('.dummy').css({'height':cw+'px'});

});

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

用CSS设置

.dummy {

padding-top: 100%; /* 1:1 aspect ratio */

width: 100%;

background: #333333;

}

  • 1
  • 2
  • 3
  • 4
  • 5

CSS设置padding-top的原理:Use CSS to Specify the Aspect Ratio of a Fluid Element

/———————————————————

然后尝试对图片设置高度等于动态宽度

js方法很简单,跟上面的方法基本相同

<ul>

<li class="container">

<img src="images/test_1.jpg"/>

<div class="element">

some text

</div>

</li>

<li class="container">

<img src="images/test_2.jpg"/>

<div class="element">

some text

</div>

</li>

<li class="container">

<img src="images/test_3.jpg"/>

<div class="element">

some text

</div>

</li>

</ul>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

.container {

display: inline-block;

position: relative;

width: 32%;

text-align: center;

}

.container img{

width: 100%;

height:100%;

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

var cw = $('.dummy').width();

$('.dummy').css({'height':cw+'px'});

$(window).resize(function() {

var cw = $('.dummy').width();

$('.dummy').css({'height':cw+'px'});

});

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

CSS方法

<ul>

<li class="container">

<div class="dummy">

</div>

<div class="element">

some text

</div>

</li>

<li class="container">

<div class="dummy">

</div>

<div class="element">

some text

</div>

</li>

<li class="container">

<div class="dummy">

</div>

<div class="element">

some text

</div>

</li>

</ul>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

.container {

display: inline-block;

position: relative;

width: 32%;

text-align: center;

}

.dummy{

padding-top: 100%; /* 1:1 aspect ratio */

width: 100%;

background:url(images/test_3.jpg) no-repeat;

-webkit-background-size: 100%;

background-size: 100%;

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

通过设置background可以实现。

div包含img的方法没有试验成功,以后继续尝试

/————————————-

还有一个问题,怎么设置div和img之间的padding,又能保证div宽度是屏幕宽度的三分之一?

果图:



核心点:

  • div宽度自适应
宽度自适应 width
默认情况下, 块级元素不设置宽度, 默认为整个屏幕宽度或者和父级同宽
如果元素脱离了文档流(浮动或者定位), 那么元素的宽度由元素的内容决定
  • css resize属性
相关介绍: MDN
resize 生效的条件: 不支持内联元素; 块级元素,需要overflow属性的计算值不是visible。

整体布局

<div class="container">
  <!-- resizable 用于拉伸的工具 -->
  <div class="resizable"></div>
  <!-- content 要展示的内容区域 -->
  <div class="content">content</div>
</div>
  • container 父容器, 用于控制脱离文档流, 然其宽度随着拉伸大小而决定
  • resizable 可以拉伸的容器, 从而可以控制父容器的宽度
  • content 真正展示的容器, 其宽度随着父级而定

基础知识, 拉伸区域的实现, 右下角出现可拉伸图标

.content {
  width: 200px;
  height: 200px;
  resize: horizontal;
  cursor: ew-resize;
  overflow: hidden; // 必须要配合overflow来使用resize, 否则拉伸图标无法显示
  border: 1px solid red;
}
<div class="content">content</div>


限制拉伸尺寸

通过设置min-width、min-height、max-width和max-height可以限制拉伸尺寸。

所有代码

<!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>Document</title>
    <style>
      .container {
        position: relative;
        /* 脱离文档流 */
        float: left; 
      }

      .resizable {
        width: 200px;
        height: 200px;
        overflow: scroll;
        resize: horizontal;
        cursor: ew-resize;
        opacity: 0;
        min-width: 200px; // 盒子宽度最小为200px
      }
        /* 更改拖拽图标的大小和父容器一样大 */
      .resizable::-webkit-scrollbar {
        width: 20px;
        height: 200px;
      }

      /* 使用定位, 将容器定位到父容器的正中间, 跟着父容器的大小改变而改变 */
      .content {
        margin: 0;
        height: 200px;
        position: absolute;
        top: 0;
        /* 留出5px为了鼠标放上去可以显示拖拽 */
        right: 5px;
        bottom: 0;
        left: 0;
        border: 1px solid;
      }
    </style>
  </head>

  <body>
    <div class="container">
      <!-- resizable 用于拖拽的工具 -->
      <div class="resizable"></div>
      <!-- content 要展示的内容区域 -->
      <div class="content">content</div>
    </div>
  </body>
</html>

层级图