I am new to JS and I'm making a game in HTML5/JS, and I need to set a background image for a div that is nested in another div, without overflow. I put the background-image property on the element, but the background is not appearing.
I haven't been able to find any other examples of this online, or how to fix it. The background-image property is showing up in the browser, and the file paths are correct.
HTML:
<div>
<div id="DivId" class="DivClass">
</div>
</div>
CSS:
.DivClass {
background-color: rgba(20, 20, 20, 0.75);
background-repeat:no-repeat;
background-size:47%;
background-position: center;
background-repeat: no-repeat;
}
#DivId {
background-image: url(../InventoryAssets/Key-trimmy.png);
}
In my opinion you should keep the file in the same folder in which you html file is there and then try it for example
#DivId {
background-image: url(Key-trimmy.png);
}
I hope this helps you
or try
document.body.style.backgroundImage = "url('Key-trimmy.png')";
you can also add image in body tag in html
<body style="background-image: Key-trimmy.png;">
I suggest giving a height value to the div. Background images are not HTML content themselves.
For example:
#DivId {
background-image: url(../InventoryAssets/Key-trimmy.png);
height: 500px;
}
Related
So here's the problem - i need an image to slightly change when the cursor is hovering on it. However, simply writing something like this in CSS styles:
img {src="";} img:hover {src="";}
seems to do nothing. Is there a solution to this problem using only HTML and CSS?
Thank you for your time!
You can use background-image property and change the url on hover
img {
height: 200px;
width: 200px;
background-image: url("https://pixy.org/src/477/4774988.jpg");
background-size: 200px 200px;
;
}
img:hover {
background-image: url("https://pixy.org/src/19/193722.jpg");
}
<img />
I've tried multiple different ways but none displayed the image as background.
CSS
.spec{
background: url('white_logo_red_font.jpg');
height: 100px;
width: 150px;
}
HTML
<div class="spec">
hi
</div>
The image is in the same folder so the url is correct. I've also tried background-image instead of background.
Edit: image location
index.js is where the code is.
That is probably caused by the wrong image path.
Try using link from the web, for example: background: url('https://via.placeholder.com/350x150') the image should be displayed - that means that your css syntax is fine.
Then fix your path, you probably have to add './', '/images/', '../images/' before "white_logo..." the image path have to be relative to the css file.
try,
CSS
.spec{
background: url('../white_logo_red_font.jpg');
height: 100px;
width: 150px;
}
HTML
<div class="spec">
hi
</div>
I have noticed that there is something wring with the background image url background: url('white_logo_red_font.jpg'); use correct url.
You can try my code.
.spec {
background: url(https://images.unsplash.com/photo-1578163246111-e02c555d00e1?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80);
height: 100px;
width: 150px;
background-size: cover;
background-position: center;
}
<div class="spec">
hi
</div>
I want to override Wordpress's Twenty-Seventeen header image by adding my own background-image via CSS. However, it just displays blank white... Here's the body's html:
<body class="page-template-default page page-id-5 logged-in wp-custom-logo has-header-image page-one-column title-tagline-hidden colors-light page-soins" data-aos-easing="ease" data-aos-duration="400" data-aos-delay="0" cz-shortcut-listen="true">
and here's my CSS to disable wordpress's header image:
.page-soins .wp-custom-header {
display: none;
}
and here's my CSS to add my background image instead:
body.page-soins {
background: url("images/backgrounds/soins-bg-1.jpg") no-repeat center center fixed;
background-size: cover;
}
Finally, here's the link to the page: http://latelierdegaia.ch/soins
Can anyone help?
.site-content-contain
and
.site-mast-head
both have
background-color: #fff;
which covers your background-image.
I found a workaround that works around! But does the trick. I just added the CSS background-image to the "site-header" class instead of the "body".
body.page-soins .site-header {
background: url("images/backgrounds/soins-bg-1.jpg") no-repeat center center fixed;
background-size: cover;
}
You just have to remove background-color: #fafafa; from your .site-header class.
I have a list of image (around 30) and I want to create CSS sprite so that they are colored when hovered. The images all have the same size. The problem is that if I just want to have one css class, I have to put the background-image on the inline html like this :
CSS:
.sprite{
background-position: 0 0;
width: 185px;
height: 100px;
}
.sprite:hover{
background-position: 185px 0;
}
HTML:
<div class="sprite" style="background: url('image1');">
<div class="sprite" style="background: url('image2');">
<div class="sprite" style="background: url('image3');">
...
And it does not work. I only manage to do the sprite if I put the background image on the css but if I do this, I would have to create as many CSS classes as I have images which is not elegant at all. Do you have a better idea? (PS: I'm using Less CSS)
Often sprites are used for icons, then people would create one .icon class and 'extend' it by another class like .icon.facebook. The .icon class contains all the dimensions and other default settings for the icon. The extended class does the customization of the icon.
.icon {
width: ...;
height: ...;
display: inline-block;
background: transparent url(...) no-repeat;
}
.icon.facebook {
background-position: ...px ...px;
}
.icon.facebook:hover {
// color version
background-position: ...px ...px;
}
The Problem is that you overwrite all background attributes inline (incl. -position); use:
style="background-image: url('image1');"
I am using CSS for hover and it works, but the hover image allow the background image (pic_normal) to display like transparent behind the image(pic_hover).
How to display only the hover image when mouse over on it?
HTML
<img id="first" src="images/clients/pic_normal.png" />
CSS
#first img:hover {
background-image: url("/images/clients/pic_hover.png");
background-repeat: no-repeat;
}
Your CSS works just fine as intended - it changes the background of your img element. Think about the pic_normal.png as the content of your element (e.gg. some text). When you changing the background the content doesn't change.
Try this instead - http://jsfiddle.net/WvKye/
<div id="first"></div>
#first {
background: url("images/clients/pic_normal.png") no-repeat;
height: 300px;
width: 300px;
}
#first:hover {
background: url("images/clients/pic_hover.png") no-repeat;
}
Use this:
<img onMouseOver="this.src='images/clients/pic_normal.png'"
onMouseOut="this.src='images/clients/pic_normal.png'"
src="images/clients/pic_normal.png"/>
I think u need some javascript for that
Using Jquery u can do like this
$("#first").hover(function()
{
$(this).attr("src","/images/clients/pic_hover.png");
},function()
{
$(this).attr("src","/images/clients/pic_normal.png");
}
);