Jullian-luc Barber Jullian-luc Barber - 1 month ago 9
CSS Question

Change body background image with css

So I am trying to change my background image, the problem is, as it is on my body, it is also changing on my other web page and I do not want it but I want to keep the same background image. Should I give an id/class to my body on my first page (where I want to hover function)?

<!DOCTYPE Html>
<head>
<meta charset="utf-8" />
<title>MakeHappy, One good thought everyday!</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="randomMessages.js"></script>
<link rel="stylesheet" href="MakeHappy.css" />
</head>
<div id="wink" class="imageSwap">
</div>
<body>
<header>
<nav>
<ul>
<a id="MakeHappy" href="Main.html">Make Happy</a>
<a id="Contact" href="#">Contact us</a>
<a id="Suscribe" href="#">Suscribe</a>
<a id="Project" href="#">Project</a>
</ul>
</nav>
</header>

</body>


\

<!DOCTYPE Html>
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="randomMessages.js"></script>
<title>MakeHappy, One good thought everyday!</title>
<link rel="stylesheet" href="MakeHappy.css" />
</head>
<body class="Main">
<input type="button" onclick="popup()" value="Click to be Happy!">
<div id="demo"></div>
</body>


\

body {
background-image: url("MakeHappy.jpg");
background-repeat: no-repeat;
background-position: 200px 60px;
background-size: 900px;
background-color: black;
}
body:hover{
background-image: url("MakeHappy2.jpg");
background-position: 200px 60px;
}
.Main{
background-size: 100px;
background-position:top;
}
.imageSwap{
position:absolute;
color: yellow;
padding: 150px;
bottom: 130px;
right: 500px
}
#MakeHappy{
color: white;
position: absolute;
right: 660px;
}
#Contact{
color: white;
position: absolute;
right: 660px;
bottom: 10px;
transform: rotate(180deg);
}
#Suscribe{
color: white;
position: absolute;
left: 320px;
bottom: 300px;
transform: rotate(270deg);
}
#Project{
color: white;
position: absolute;
right: 360px;
bottom: 300px;
transform: rotate(90deg);
}
.Main div{
color: white;
position: fixed;
}
input{
position: fixed;
bottom: 150px;
left: 625px;
}

Answer

You should give it a class with that specific background, if you only want the hover on lets say your startpage.

body {
  background-image: url("MakeHappy.jpg");
  background-repeat: no-repeat;
  background-position: 200px 60px;
  background-size: 900px;
  background-color: black;
}

.startpage-background:hover{
  background-image: url("MakeHappy2.jpg");
}

but your first html has errors, you shouldn't declare a div outside the body

Not valid html

<head>
  <link rel="stylesheet" href="MakeHappy.css" />
</head>
<div id="wink" class="imageSwap">
</div>
<body>
</body>

But in general your html and css has not good practises and inconsistent code. I never use id to style a element, only classes. Also keep your classes and ids lowercase like some-class instead of SomeClass. Also I think you can avoid to absolute position your navigation elements.

Comments