I am struggling with a problem, where I want to set the background image for one component differently. This is how it is written in the index.css:
body,
html {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
background-position: center;
background-size: cover;
background-repeat: no-repeat;
font-family: "EB Garamond", serif !important;
background-image: url("views/graphics/CatanBG.png");
overflow-x: hidden;
}
/* set background image per page */
.game-bg {
background: url('views/graphics/sea.gif');
background-size: 30px;
;
}
This produces the following picture:
As you can see, the actual background image is still ("views/graphics/CatanBG.png") and when zooming out it looks like this:
I use game-bg in the div for the component where I want to have it as background image like that:
render() {
console.log("state", this.state);
return (
<div className={"game-bg"}>
more code
</div>
}
Anyone with an idea on what the problem is?
Thanks in advance!
so I know you're using CSS instead but if you wanted to consider using styled-components (which is great by the way) then it's super simple.
You can use this:
const GlobalStyle = createGlobalStyle`
html {
background: ${props => (props.something ? url(imageone) : url(imagetwo))};
}
`
that way you can pass a prop in and then render something different based off that. I do this to render a different background image for my app
the docs are here: https://styled-components.com/docs/api
let me know if you need any more details
Related
I have a problem with replacing the jenkins logo on the login screen. Uses the jenkins login-theme add-on:
https://plugins.jenkins.io/login-theme/
In head I define the following code:
<style>
.simple-page .logo {
background-image: url('https://www.jenkins.io/images/logos/snow/snow.png');
background-repeat: no-repeat;
background-position: 50% 0;
height: 350px;
background-size: contain;
}
.simple-page form {
width: unset;
max-width: unset;
}
</style>
However, in the header like this:
<script>
document.getElementsByTagName('h1')[0].textContent = 'Welcome to Balajee\'s Jenkins!'
</script>
I get something like this:
The old logo is automatically added to the code all the time
</head><body><div class="simple-page" role="main"><div class="modal login"><div id="loginIntroDefault"><div class="logo"><img src="/static/9e651e0d/images/svgs/logo.svg" alt="Jenkins logo"></div><h1>Witamy w Jenkinsie!</h1></div>
How to block the appearance of the old logo and keep only the new one ???
I managed to solve the problem myself, more Work Around because it seems to me that the plugin responsible for changing the look of the home page is not working properly. As I wrote above, there was a problem with the fact that apart from the logo that I define, also the default logo appears. The workaround for this porble based on the generated HTML code was very easy to just resize the gernered default logo to zero with the following code:
In Head fieled :
<style>
.simple-page .logo {
background-image: url('https://www.jenkins.io/images/logos/snow/snow.png');
background-repeat: no-repeat;
background-position: 50% 0;
height: 200px;
background-size: contain;
}
.simple-page .logo > img {
height: 0;
}
</style>
I have an img tag like this (and I can't use a container)
<img src="https://example.com/first-image.jpg" class="my-image">
And I would like to display a second image instead of the first one on mouseover using only CSS. Maybe doing something like this:
.my-image:hover {
background-image: url("https://example.com/second-image.jpg");
}
I'm not sure if it that's possible. Any ideas?
Solution 1: HTML can be edited
You can do it by not using the src attribute and set the default image in your CSS, then change it on hover like so:
.my-image {
width: 200px;
height: 200px;
background-image: url(https://....jpg);
background-size: cover;
background-position: center;
}
.my-image:hover {
background-image: url(https://....jpg);
}
See working example here: https://jsfiddle.net/86ujL1dv/
Note that modern browsers don't load images until they're needed, so there will be a slight delay on the first hover, as the user's browser will load the second picture.
Solution 2: HTML cannot be edited
It's a bit of a hack, but you can set the size of the img to zero, and use padding to make it look the same, then use the background-image property, like so:
.my-image {
width: 200px;
height: 200px;
}
.my-image:hover {
width: 0;
height: 0;
padding: 100px;
background-size: cover;
background-position: center;
background-image: url(https://images.pexels.com/photos/4017166/pexels-photo-4017166.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260);
}
See live example here: https://jsfiddle.net/1bo7L89w/
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 />
How do I write the right adress, the code is:
.slide1 {
background: url(http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371566801_p17tbs0rrjqdt1u4dnk94fe4b63.jpg)no-repeat center;
}
How do I put a image from my resource file? I tried background-image: url("pic1.jpg") -that's the right address- and it doesn't work.
What I do is I put the images from a slideshow in css and put a simple cod afterwards in html to display it.
You have to pass a width and a height in order to make it appear. Try it like this:
.slide1 {
background: url('http://media.dunkedcdn.com/assets/prod/40946/580x0-9_cropped_1371566801_p17tbs0rrjqdt1u4dnk94fe4b63.jpg') no-repeat center;
width: 200px;
height: 200px;
}
Edit:
If the image is in the same directory where your css file is, simply use:
background: url('nameOftheFile.jpg') no-repeat center;
Also this thread may be interesting for you: How to go up a level in the src path of a URL in HTML?
Details : Using Atom File Editor with Mac OS X, Making a new site on localhost and after saving it I check changes on chrome browser. Right now i have come across a problem which causes the image to not show up on the website while i was trying to set background image of a section tag in CSS.
Problem : I am trying to use the stellar.js parallax plugin and for that i need to set the background property of the section tag to an image of my choice. The problem is that when i use "background : url(parallax1.png);" the image does not show up on the website and only a white blank space is shown.
HTML File :
<!-- Attempting Parallax Here -->
<section class="parallax1 pic1">
</section>
<!-- Ending Attempt Here -->
CSS File :
.pic1
{
background : url(parallax1.png);
}
.parallaxcontent
{
background-attachment: fixed;
background-repeat: no-repeat;
position: relative;
background-size : cover;
width: 100%;
height: auto;
}
Points You Should Know :
the image parallax1.png is in the same directory and i have checked the spelling, which is correct.
I have also tried using background-image: instead of background: .
If i use < img src="parallax.png" /> the image shows up perfectly
i have tried to encase parallax.png in '' as well as "".
Any help would be appreciated,
Thanks.
Generally, you should have some content within the section tag or you can set in .pic1 class
min-height:200px;
Try background : url("parallax1.png");
Read more here
CSS background Property
take a look
html
<section class="parallax1 pic1">
</section>
css
.pic1
{
background: blue url("https://static.xx.fbcdn.net/rsrc.php/v2/y6/r/QDLPxJ5bMSg.png");
width: 100px;
height: 32px;
border:1px solid #000;
}
.parallaxcontent
{
background-attachment: fixed;
background-repeat: no-repeat;
position: relative;
background-size : cover;
width: 100%;
height: auto;
}
working Fiddle