简体   繁体   English

更改背景颜色和背景图像

[英]Change background color and background image

I want to change my div background color and image on hover. 我想在悬停时更改div背景颜色和图像。 Just like this. 像这样。

在此处输入图片说明

I tried with this way. 我尝试过这种方式。 But it changed only background color. 但是它只改变了背景色。 Is there any way to achieve this kind of situation? 有什么办法可以解决这种情况?

<div class="col-md-4">
  <div class="icon-wrapper"></div>
     <p>Products</p>
</div>

.icon-wrapper {
    background: url("/assets/human-resources.png");
    display: inline-block;
    background-position: center;
    background-repeat: no-repeat;
    border: 1px solid #ddd;
    height: 120px;
    position: relative;
    width: 120px;
    border-radius: 50%;
    transition: all 0.2s;

    img {
        top: 22%;
        left: 24%;
        position: absolute;
    }

    &:hover {
        background: blue;
        background-position: center;
        background-repeat: no-repeat;
        background: url("/assets/car-white.png");
    }
}
background-color: #6DB3F2;
background-image: url('images/checked.png');

you could try to use this instead 您可以尝试使用它来代替

background: #6DB3F2 url('images/checked.png');

I don't know all layout of your website but I can tell you a short introduction to popups - how to create them in right way. 我不知道您网站的所有布局,但是我可以告诉您有关弹出式窗口的简短介绍-如何以正确的方式创建弹出式窗口。

At first, outside all other div blocks create another one for background mask (between last div block element in html and closing body tag) - it will be used only as background layer (with absolute position), style as you wish and set display none to hide it. 首先,在所有其他div块之外,为背景蒙版创建另一个(在html中的最后一个div块元素与body标记之间)-它仅用作背景层(具有绝对位置),根据需要设置样式并设置为不显示隐藏它。

Then create another div block for popup (not inside background block but under it, position fixed to scroll with your page), create popup and style how you want, hide it as well (display none). 然后为弹出窗口创建另一个div块(不在背景块内部,而是在其下,固定位置以与页面一起滚动),创建弹出窗口并按需要设置样式,也将其隐藏(不显示)。

This will give you reusable popup structure that you will be able to use how many times you need afterwards just adding new popup block under first one etc. 这将为您提供可重复使用的弹出结构,之后您只需使用第一个添加新的弹出块等即可使用所需的次数。

I know it is just a theory without real life examples, so you can study a bit here (about structure, jQuery can be used in more easy way): 我知道这只是一个没有实际例子的理论,因此您可以在这里进行一些研究(关于结构,可以更简单地使用jQuery):

Reusable modal popups 可重复使用的模式弹出窗口

PS Answer to your question: PS回答您的问题:

You need to change background-color and background-image (on hover state). 您需要更改背景颜色和背景图像(处于悬停状态)。 :) :)

In my understanding you are trying to change the background image and the background color on hover, right? 以我的理解,您正在尝试更改悬停时的背景图片和背景颜色,对吗? Try this: 尝试这个:

yourElement:hover{
   background-color: yellow;
   background-image: url(../images/bg.gif);
}

Example 1 : Table row background color change 示例1:表格行背景颜色的变化

<table>
    <tr  bgcolor="#777777">
        <td>Content Write here...</td>
    </tr>
</table>

Example 2 : If change background image then use this code 示例2:如果更改背景图像,则使用此代码

<div style="background-image: url('bg.jpg');"></div>

Because you are overriding background on hover selector thats normal situation. 因为您在悬停选择器上覆盖了背景,这是正常情况。 Try background-color:blue; 试试background-color:blue; on hover selector 悬停选择器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM