Color overlay (hover) on resizing image

2019-09-04 06:48发布

I want to have a color overlay on an image. Now this is pretty simple, but given the fact the image's border changes on hover, it isn't (for me, because I'm fairly new to CSS). I don't know if this is even possible with CSS, or would you have to use JavaScript (or JQuery)?

So far, I've tried this: put the image in a <div>, which dynamically resizes on hover. This works pretty fine, but as I'm going to have a dark background, there is one little problem. There is a minimal 'border' of less than one pixel always around the image:

It's barely visible, but it annoys me. I've already tried to make the outside div smaller, but that leaves me with out-centered lines of not faded image and white border.

Take a look at my code here (nothing special, but just in case): JSFiddle

Is there some way of fixing this? I haven't learnt a lot of Javascript yet, so I would rather do it with css than with js, but if there's no other way...

标签: html css3
1条回答
兄弟一词,经得起流年.
2楼-- · 2019-09-04 07:45

I've updated some code; see http://jsfiddle.net/GR8Vq/:

* {
    margin: 0;
    padding: 0;
}

body {
    background: #000;
}

.imgholder {
    position: relative; // added
    margin: 5px;
    // background: #fff;
    float: left;
    height: 200px;
    // border-radius: 200px / 110px;
    transition: border-radius 0.3s ease-out;
}

.imgholder img,
.imgholder:after {
    border-radius: 200px / 110px;
    transition: 0.3s ease-out;
}

.imgholder:after {
    content: '';
    display: block;
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background: rgba(255,255,255,.3);
    opacity: 0;
}

.imgholder:hover:after { opacity: 1; }

.imgholder:hover{
    border-radius: 20px;
}

.imgholder:hover img,
.imgholder:hover:after {
    border-radius: 20px;
}

Instead of a border-radius on the image holder, I used a :after selector to add a layer on top of the image. Should be better like this?

查看更多
登录 后发表回答