How to remove the background image border when on hover? - html

I have a card with an image header. When on hover of the card, I'd like to change the non-image part background color. Code as follows:
.card {
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
transition: 0.3s;
width: 40%;
height: 500px;
}
.card-image {
display: block;
position: relative;
}
.card:hover {
background-color: grey;
}
img {
display: block;
width: 100%;
height: 200px;
background-size: cover;
}
.container {
padding: 0 16px;
}
<div class="card">
<div class="card-image">
<img style="background-image: url(https://www.w3schools.com/howto/img_avatar.png);">
</div>
<div class="container">
<h4><b>John Doe</b></h4>
<p>Architect & Engineer</p>
</div>
</div>
I use a background image as the image here because I need the background-size: cover effect, but as hovering on the card, you can see a border being added to the image due to it's actually a background image, I wonder how I can get rid of the border?
EDIT: the border isn't that obvious:

You are using background-image attribute, you should use the style on a <div> tag instead (or any other tag you with display:block), since this is an a background image not an image, I think this is caused by default browser behavior for not providing src attribute, which would be considered as 404 error for the image, not sure if this is the case here, here is a working snippet:
.card {
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
transition: 0.3s;
width: 40%;
height: 500px;
}
.card-image {
display: block;
position: relative;
}
.card:hover {
background-color: grey;
}
.img {
display: block;
width: 100%;
height: 200px;
background-size: cover;
background-image: url(https://www.w3schools.com/howto/img_avatar.png);
}
.container {
padding: 0 16px;
}
<div class="card">
<div class="card-image">
<div class="img"></div>
</div>
<div class="container">
<h4><b>John Doe</b></h4>
<p>Architect & Engineer</p>
</div>
</div>

The problem is that you're using the img HTML element with a background-image CSS declaration. Two possible solutions:
1) Keep the img element, add a src attribute, and use the object-fit declaration.
HTML:
<div class="card-image">
<img src="https://www.w3schools.com/howto/img_avatar.png">
</div>
CSS:
img {
display: block;
width: 100%;
height: 200px;
object-fit: cover;
}
2) Remove the img element (and declaration) and apply the background-image declaration to the .card-image element.
HTML:
<div class="card-image"></div>
CSS:
.card-image {
background-image: url("https://www.w3schools.com/howto/img_avatar.png");
background-size: cover;
height: 200px;
}

Related

image background opacity with no affecting borders

How to set background with opacity without affecting border line opacity? Solutions I found does not help.
<div class="selected">
<img
src="../assets/img/image-product-1-thumbnail.jpg"
alt="product-1-thumbnail"
/>
</div>
img {
width: 100%;
max-width: 5rem;
height: auto;
border-radius: 10%;
}
.selected {
background-color: red;
img {
border: 2px solid red;
}
}
let thumbnailImages = document.querySelectorAll('.thumbnail');
thumbnailImages.forEach(image => {
image.addEventListener("mouseover", ()=> {
image.classList.add('blur')
image.parentElement.style.outline = "2px solid hsl(26, 100%, 55%)";
image.parentElement.style.borderRadius = "12px";
})
image.addEventListener("mouseout", ()=> {
image.classList.remove('blur')
image.parentElement.style.outline = "";
})
});
.left-section-bottom{
display: flex;
width: 80%;
margin: 32px auto;
gap: 40px;
}
.left-section-bottom img{
border-radius: 12px;
}
.thumbnail.blur{
opacity: 0.4;
cursor: pointer;
}
.thumbnail-div{
display: flex;
}
<div class="left-section-bottom">
<div class="thumbnail-div"><img class="thumbnail" src="images/image-product-1-thumbnail.jpg" alt=""></div>
<div class="thumbnail-div"><img class="thumbnail" src="images/image-product-1-thumbnail.jpg" alt=""></div>
<div class="thumbnail-div"><img class="thumbnail" src="images/image-product-1-thumbnail.jpg" alt=""></div>
<div class="thumbnail-div"><img class="thumbnail" src="images/image-product-1-thumbnail.jpg" alt=""></div>
</div>
I was looking for an answer to this same question, and finally figured it out. The original question is in regard to this challenge:
https://www.frontendmentor.io/challenges/ecommerce-product-page-UPsZ9MJp6
Since there are 4 images you have to loop through them one by one.
You have to use querySelectorAll, not querySelector.
Just loop through your thumbnail images with a forEach().
Wrap your images in a div (as Kingsly mentioned) so you can apply
border style to the div instead of your blurred images.
This code should work if you add your images.
You should put the image in a div container with the size you want. Add the border and border-radius to the div, add opacity to the img element.
IMAGE CREDIT: https://www.pexels.com/#belle-co-99483
.img-container {
width: 200px;
height: 150px;
border: 2px solid red;
border-radius: 10%;
overflow: hidden;
}
img {
width: 100%;
height: 100%;
opacity: .7;
}
.selected {
//background-color: red;
}
img {
}
<div class="selected">
<div class="img-container">
<img
src="https://images.pexels.com/photos/1000445/pexels-photo-1000445.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"
alt="product-1-thumbnail"
/>
</div>
</div>

Images exceeding size

I want to display images in my div. I display different images according to the condition (if moviePoster true).
I moviePoster I display it, but if it is false, I display a image that informed that the file is not found.
I don't know why but when I display these 2 types of image, the moviePoster image, exceeding the size of the dive.
This is a screen from the issue in my application
.list {
border: 1px solid grey;
display: flex;
margin: 30px;
height: 230px;
border-radius: 10px;
box-shadow: 1px 1px 12px #555;
}
.list-img {
// border: 5px solid red;
width: 20%;
}
img {
height: 100%;
width: 100%;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
<div class="list">
<div class="list-img">
<div *ngIf="moviePoster; else notFound">
<img src="https://image.tmdb.org/t/p/w200/{{moviePoster}}" alt="...">
</div>
<ng-template #notFound>
<img src="../../assets/not_found.jpg" alt="...">
</ng-template>
</div>
img is a default display: inline, so height and width refers to inner content.
Anyway set image width and height usually stretches image, and that's orrible.
I suggest on using a div with the image setted as background, so use something like that:
html:
...
<div
*ngif="..."
class="thumbnail"
style="background-image: url(https://...{{ moviePoster }})"
></div>
...
css:
.thumbnail{
background-size: cover;
background-position: center;
}
You should apply this class also to #notFound.
One thing missing: thumbnail div must have height and width expressed
You can do that in multiple ways via css, depending on how have you structured the card box.
Hope it helps.
i think this is what you want :
if the image is null then the default background will show.
UPDATE: i have made it more dynamic
.list {
display: flex;
flex-direction: column;
}
.list-item {
height: 150px;
overflow: hidden;
border-radius: 10px;
box-shadow: 0 0 3px 2px rgba(0,0,0,.5);
}
.list-item + .list-item {
margin-top: 10px;
}
.list-img {
width: 200px;
height: 150px;
transition: background .5s ease;
background-size: cover;
background-repeat: no-repeat;
background-position: center center;
background-image: url(https://www.tourismselangor.my/wp-content/themes/Directory/images/noimage.jpg);
}
<div class="list">
<div class="list-item">
<div class="list-img" style="background-image: url(https://picsum.photos/200)"></div>
</div>
<div class="list-item">
<div class="list-img"></div>
</div>
<div class="list-item">
<div class="list-img" style="background-image: url(https://picsum.photos/200)"></div>
</div>
</div>

How to make a round img using css

I am trying to make a 50% rounded img inside a simple div. I have tried 2 ways to do it, but obviously neither has worked for me.
If it makes something clear, the image, that I'm getting is square
/*HTML*/
<div class="img-container"></div>
/*CSS*/
.img-container {
background: url("../../../../../assets/img/user-photo.svg") no-repeat center top / cover;
height: 125px;
width: 125px;
border-radius: 50%;
}
/*HTML*/
<div class="img-container">
<img src="path_to_img" alt="User photo">
</div>
/*CSS*/
.img-container {
width: 125px;
height: 125px;
img {
width: 100%;
height: 100%;
border-radius: 50%;
}
}
I am getting it quite cropped in the bottom.
Are there any other solutions or what I am doing wrong in 2 examples above? Any help appreciate.
This is some simple markup, a div container and the image is enough.
If the image may come in different sizes or shapes, you can use the object-fit property to make sure it displays correctly regardless. Just make sure you define the explicit size (height and width) you need the image to be, and then you can use object-fit: cover on the img itself so it maintains its aspect ratio and uses all available space for exmaple.
.img-container {
background-color: purple;
padding: 10px 0;
width: 200px;
}
img {
border-radius: 50%;
width: 100px;
height: 100px;
display: block;
margin: 0 auto;
}
img.rect-img {
object-fit: cover;
}
<div class="img-container">
<img src="https://via.placeholder.com/100" alt="User photo">
</div>
<div class="img-container">
<img class="rect-img" src="https://via.placeholder.com/100x50" alt="User photo">
</div>
It works without any problem. I think your image has some transparent parts.
img {
box-shadow: 2px 2px 5px rgba(0, 0, 0, .4);
}
img.a {
width: 64px;
border-radius: 100%;
}
img.b {
width: 64px;
border-radius: 100%;
object-fit: cover;
width: 64px;
height: 64px;
}
<h1>Non-square image:</h1>
<img class="a" src="https://images.unsplash.com/photo-1579075475207-e59cd9d39be8">
<hr>
<h1>Converted to square using CSS:
<h1/>
<img class="b" src="https://images.unsplash.com/photo-1579075475207-e59cd9d39be8">

Text over image CSS Chrome Error

I tried this from this question: Text over image using CSS transitions.
It's working fine in both IE11 and Firefox Quantum and in both sites the animation/transition works perfectly but when I try to visualize it in Chrome the text that should appear beneath the image and eventually hover it goes to the bottom of the page. The console also shows me zero errors.
My question resumes in if it is a CSS absolute attribute problem or something else.
Here's my code:
.size {
height: 150px;
width: 200px;
border: solid 1px orange;
overflow: hidden;
position: relative
}
.pic:hover > .text {
height: 190px;
}
.text {
position: absolute;
width: 200px;
height: 50px;
bottom: -40px;
right: 0;
left: 0px;
transition: height 0.7s ease-out;
background-color: #fed136;
overflow: hidden;
border: solid 1px #fed136;
padding: 10px;
}
.text > h4 {
text-align: center;
}
.block {
margin: 10px 10px 50px 10px;
float: left;
}
<div class="row">
<div class="block">
<div class="pic animated zoomIn">
<img src="someimage.jpg" class="size" />
<div class="text">
<h4>Some Title</h4>
<p>Some text</p>
</div>
</div>
</div>
</div>
I'm using Bootstrap 3.3.5 and jQuery 2.1.4
I'm assuming that what you want is for the rollover text to be inside the image.
In this case I don't even know how it's working on IE11 or Firefox in your computer because here it fails on all browsers I have.
Your problem is basically that you are applying the CSS in the wrong element. Change .size to .pic in your CSS and it will get what you want.
Without position: relative inside .pic your position: absolute in .text is relative to the page itself (or the html element) and not the .pic element.
You also need overflow: hidden to hide any elements that go beyond the borders of the .pic element, thus hiding .text.
Added position relative & overflow as hidden to container div 'pic' and made height of div 'text' to zero. Its working fine in Chrome without any error and in IE too.
.size {
height: 150px;
width: 200px;
border: solid 1px orange;
overflow: hidden;
position:relative
}
.pic {
position:relative;
overflow:hidden;
}
.pic:hover > .text {
height: 190px;
}
.text {
position: absolute;
width: 200px;
height:0;
bottom: -40px;
right: 0;
left:0px;
transition: height 0.7s ease-out;
background-color: #fed136;
overflow: hidden;
border: solid 1px #fed136;
padding: 10px;
}
.text > h4 {
text-align:center;
}
.block {
margin:10px 10px 50px 10px;
float:left;
}
<div class="row">
<div class="block">
<div class="pic animated zoomIn">
<img src="someimage.jpg" class="size" />
<div class="text">
<h4>Some Title</h4>
<p>Some text</p>
</div>
</div>
</div>
</div>
You can add properties for diferent browsers. First clear your cash and see if the problem is realy only i Chrome. Then you can create something like that :
#media screen and (-webkit-min-device-pixel-ratio:0) {
.container {-chrome-:only(;
property:value;
);}

Make an image come out of a div with background-image

I need some help, I need to code this image:
This is what I have so far:
I tried adding a margin-top, padding-top, tried all combinations of position relative and absolute, I just need some ideias on how to do it.
This is how my code is structured:
<div class="background-oficina">
<div class="container">
<div class="col-xs-12 text-center">
<img class="logo" src="logo.png" alt="Oficina de Redação">
</div>
</div>
</div>
This is the css for the two classes that I'm using:
.background-oficina {
background: #fff url("bg-texture.png");
}
.logo {
padding-top: 50px;
margin: 0 auto;
}
You could use an additional absolutely positioned element to which you assign the repeated background pattern and which you put behind the original element by using z-index: -1:
html, body {
margin: 0;
}
.background-oficina {
position: relative;
border: 1px solid #333;
border-bottom: none;
}
.bg-container {
position: absolute;
z-index: -1;
left: 0;
top;
width: 100%;
height: 120px; /* or whatever height is desired */
background: url("http://placehold.it/20x15/cff");
}
.text-center {
text-align: center;
}
.logo {
padding-top: 50px;
margin: 0 auto;
}
<div class="background-oficina">
<div class="bg-container"></div>
<div class="container">
<div class="col-xs-12 text-center">
<img class="logo" src="http://placehold.it/200x150/fb7" alt="Oficina de Redação">
</div>
</div>
</div>
Your trying this, you can set default height and width to parent div that consist of that logo then using position:absolute you can push that out of parent div, but don't add overflow:hidden to parent div or else it hides your image or element that you are trying to push outside parent div as hidden.
.background-oficina {
background: #fff url("https://via.placeholder.com/800x100/000") no-repeat;
box-shadow: inset 0 0 0 1000px rgba(255, 255, 255, 0.2);
background-size: 100% 100%;
width: 100%;
height: 100px;
position: relative; /*Add this*/
}
.logo {
padding-top: 50px;
margin: 0px auto;
position: absolute; /*Add this*/
bottom: -20px; /*Add this*/
}
<div class="background-oficina padding margin-bottom">
<div class="container">
<div class="col-xs-12 text-center margin-bottom">
<img class="logo" src="https://via.placeholder.com/50/ff2" alt="Oficina de Redação">
</div>
</div>
</div>