On Chromium-based browsers, downscaled images used as background-image are pixelated while they look more blurred when displayed with an <img> tag. Is there a way to change the render style of a background image so it look like the display in the tag? I tried the image-rendering property but it doesn't seem to work on background-image. It looks fine on Firefox.
Example of render on Brave, left is background-image and right is with the <img> tag:
#backgroundImage, img {
width: 80px;
min-height: 80px;
margin: 10px 5px;
display: inline-block;
}
#backgroundImage {
background-repeat: no-repeat;
background-position: center;
background-size: cover;
background-image: url("https://i.stack.imgur.com/X5EfB.png");
}
<div id="backgroundImage"></div>
<img src="https://i.stack.imgur.com/X5EfB.png" />
This seems to be happening only when both the size:cover and position:center rules are applying. You can have the same result in the <img> by changing its object-fit to cover:
#backgroundImage, img {
width: 80px;
min-height: 80px;
margin: 10px 5px;
display: inline-block;
object-fit: cover;
}
#backgroundImage {
background-repeat: no-repeat;
background-position: center;
background-size: cover;
background-image: url("https://i.stack.imgur.com/X5EfB.png");
}
<div id="backgroundImage"></div>
<img src="https://i.stack.imgur.com/X5EfB.png" />
So to avoid it, you can replace the background-size:cover rule with 100% 100%:
#backgroundImage, img {
width: 80px;
min-height: 80px;
margin: 10px 5px;
display: inline-block;
}
#backgroundImage {
background-repeat: no-repeat;
background-position: center;
background-size: 100% 100%;
background-image: url("https://i.stack.imgur.com/X5EfB.png");
}
<div id="backgroundImage"></div>
<img src="https://i.stack.imgur.com/X5EfB.png" />
Related
I've ran out of hope for this for the past few days,what I'm basically trying to do is to do this:
CSS:
.div1{
/* background-image code */
}
HTML:
<div class="div1">
<!--Image here-->
</div>
Is it even possible to have a background image larger than the image in the div itself?
See the following example to achieve what you are looking for. Basically you can combine a color and an image by using both the background-color and background-image props at the same time. Position and scale the image with background-size and background-position. background-repeat: no-repeat; is important to be able to see the area that is the simple color background.
.div1 {
background-color: blue;
background-image: url(https://thumbs.dreamstime.com/b/forrest-27720334.jpg);
background-size: 50%;
background-position: center;
background-repeat: no-repeat;
width: 400px;
height: 300px;
}
<div class="div1">
</div>
For two images layered in this way:
.div1 {
background-image: url(https://www.realtree.com/sites/default/files/styles/site_xl/public/content/inserts/2022/imagebybarriebird-ducklings.jpg);
width: 400px;
height: 300px;
position: relative;
color: white;
background-size: 50%;
background-repeat: no-repeat;
background-position: center;
/*to center the text */
display: flex;
align-items: center;
justify-content: center;
}
.div1::before {
content: "";
position: absolute;
width: 100%;
height: 100%;
background-image: url(https://thumbs.dreamstime.com/b/forrest-27720334.jpg);
background-size: cover;
/*to set this image layer behind the duck one */
z-index: -1;
}
<div class="div1">
Example content text
</div>
you can add width and height in each img and background-image
.div1{
width: 100vw;
height: 500px;
/* background-image code */
}
img {
width : 200px;
height : 200px;
}
<div class="div1">
<img src="code.png" alt="">
<!--Image here-->
</div>
Give the img some padding and put the background image on it.
div {
width: fit-content;
}
img {
background-image: url(https://picsum.photos/id/1015/300/300);
background-size: cover;
padding: 30px;
}
<div>
<img src="https://picsum.photos/id/1016/200/300">
</div>
I'm trying to make the image fill the size of my div (300x300px) but it sits in the corner of it. Any help would be great. Thank you
Here is my HTML
<script id="arty-template" type="text/x-handlebars-template">
<section class="myboard">
{{#each items}}
<a href={{external_urls.spotify}} target="_blank">
<div class="boardimage" style="background: url({{images.2.url}}) no-repeat 100% 100%; ;" title="{{name}}"></div>
</a>{{/each}}
</section>
</script>
Here is my CSS
.boardimage {
position: relative;
margin: 39px auto;
width: 300px; height: 300px;
border-radius: 100%;
background-size: cover;
background-position: center center;
float: left;
}
This is what's happening here
This is without no-repeat
I've tried adding height and width to the HTML but it just makes the image disappear.
I have copied your text and removed the handlebar code, and used an image from unspash.com. It seems to be working fine, although the code can be improved a lot.
Maybe there's an issue with the image itself. Check if the image renders correctly.
.boardimage {
position: relative;
margin: 39px auto;
width: 300px; height: 300px;
border-radius: 100%;
background-size: cover;
background-position: center center;
float: left;
}
<a href="" target="_blank">
<div class="boardimage" style="background: url(https://images.unsplash.com/photo-1626246914215-4046df064c1f?ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHwyfHx8ZW58MHx8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=600&q=60) no-repeat 100% 100%; ;" title=""></div>
</a>
I tried like this and seems working:
.boardimage {
width: 200px;
border-radius: 100%;
background-size: cover;
background-position: center center;
float: left;
}
A PNG image cuts off from the bottom.
Picture of the problem
.loadMoreBtn-label {
background-image: url(picture);
background-repeat: no-repeat;
padding-left: 30px;
background-size: 23px 23px;
background-position: ;
display: inline-flex;
}
Try creating a div that has the size you want, setting its background image and applying the background-size: contain; property.
Plus you can manipulate the div when your loading is done without doing CSS stuff.
Something like this:
.loadMoreBtn-label {
display: inline-flex;
}
.loadMoreBtn-label #loading {
display: block;
height: 23px;
width: 23px;
margin-right: 7px;
background-image: url(picture);
background-repeat: no-repeat;
background-size: contain;
}
I can't use background image because I'm using Django variables into CSS.
I looked at these
Image position equivalent to background position and
CSS background position 50% 50% does not work (the suggested answer here uses background-position, where I just want to use an <img> tag).
I want to replicate this into just an <img> tag. everything looks good except background-position: 50% 50%. What's the equivalent for that?
.main-header {
min-height: 40%;
background: url('../img/parallax11.jpeg') no-repeat center;
background-size: cover;
text-align: center;
background-position: 50% 50%;
}
So far this has worked:
.cover-img {
display: flex;
min-height: 30%;
overflow: hidden;
height:30vh;
width: 100vw;
object-fit: cover;
}
The equivalent of background-size: cover; would be:
img {
width: 100%;
height: 100%;
object-fit: cover;
}
The equivalent of background-position: 50% 50%; would be:
object-position: 50% 50%;
See the spec on MDN.
The object-position property determines the alignment of the replaced
element inside its box.
Initial value 50% 50%
.cover-inline-image,
.cover-background-image {
width: 200px;
height: 200px;
margin-bottom: 10px;
}
.cover-inline-image img {
width: 100%;
height: 100%;
object-fit: cover;
}
.cover-background-image {
background-image: url("//unsplash.it/400/300?image=0");
background-size: cover;
background-position: 50% 50%;
}
Inline image:<br>
<div class="cover-inline-image">
<img src="//unsplash.it/400/300?image=0">
</div>
Background image:<br>
<div class="cover-background-image"></div>
How the image looks be default:<br>
<img src="//unsplash.it/400/300?image=0">
object-fit can also 'clip' the image from coordonates via object-position
It would be here : object-position: 50% 50%; or object-position: center center;
see https://developer.mozilla.org/en-US/docs/Web/CSS/object-position
I'm trying to make an image change when a user hovers over it, but the method I used seems to cut off the image, not showing all of it. How can I have it scale the image to the size I specify, rather than just making the div that size and cutting off the rest?
HTML:
<div class="navbar-image" id="navbar-image-ID2Games">
</div>
CSS:
#navbar-image-ID2Games {
width: 5rem;
height: 5rem;
background-repeat: no-repeat;
background: url(http://i.imgur.com/Ou5cX4D.png);
}
#navbar-image-ID2Games:hover {
width: 5rem;
height: 5rem;
background: url(http://i.imgur.com/Tx0SVZr.png) no-repeat;
}
See here: https://jsfiddle.net/7a7ghfw4/
As you can see from the imgur url, the image is supposed to be a save icon, but it cuts off everything but the top left.
Add "background-size: contain" to both of your css rules.
#navbar-image-ID2Games {
width: 5rem;
height: 5rem;
background-repeat: no-repeat;
background: url(http://i.imgur.com/Ou5cX4D.png);
background-size: contain;
}
#navbar-image-ID2Games:hover {
width: 5rem;
height: 5rem;
background: url(http://i.imgur.com/Tx0SVZr.png) no-repeat;
background-size: contain;
}
This works:
#navbar-image-ID2Games {
width: 265px;
height: 265px;
background-repeat: no-repeat;
background: url(http://i.imgur.com/Ou5cX4D.png);
}
#navbar-image-ID2Games:hover {
width: 265px;
height: 265px;
background: url(http://i.imgur.com/Tx0SVZr.png) no-repeat;
}
I only changed the width and height for both classes