I'm currently trying to figure out if it is at all possible to show and hide the background image on a div with a hover state.
Here's some code:
HTML
<div class="thumb">
<div class="text">
Header<br>
Sub-header<br><br>
Date
</div>
</div>
<div class="thumb">
<div class="text">
Header<br>
Sub-header<br><br>
Date
</div>
</div>
<div class="thumb">
<div class="text">
Header<br>
Sub-header<br><br>
Date
</div>
</div>
CSS
.thumb {
width: 400px;
height: 250px;
background-color: silver;
font-weight: bold;
background: url("http://www.imageurlhere.com") no-repeat 50% 50%;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
.text {
padding: 15px;
}
So essentially I want to alternate between a background image and the background color, so when you hover over the div the background image is show, and on mouseout the background image is hidden and you revert to the background colour (silver).
I found this was do-able via pseudo classes but as this is for a CMS site the images will be changed on the fly, therefore they'll have to be inserted via html:
style="background-image: url('insert url here')"
Is this at all possible to hide and show the background-image?
Is there any reason you cannot use the :hover pseudo-class to make the image appear/disappear when hovered?
For example:
div {
background-image: none;
}
div:hover {
background-image: url('insert url here');
}
Are you against using Javascript (or jQuery)?
If not, I have a solution using the .hover() function in jQuery. You said you didn't want to add new classes for new images. So I'm proposing you store the urls in the .thumb elements.
HTML:
<div class="thumb" data-hoverimage="http://placekitten.com/250/400">
<div class="text">Header
<br>Sub-header
<br>
<br>Date</div>
</div>
<div class="thumb" data-hoverimage="http://placekitten.com/250/300">
<div class="text">Header
<br>Sub-header
<br>
<br>Date</div>
</div>
<div class="thumb" data-hoverimage="http://placekitten.com/400/250">
<div class="text">Header
<br>Sub-header
<br>
<br>Date</div>
</div>
I stored each thumbnail's url in the data-hoverimage attribute (it can be whatever you want).
Then, using jQuery, we can use the hover function. The first function is for when the cursor is over, the second for when the cursor is out.
$(".thumb").hover(function(){
var imgurl = $(this).data("hoverimage");
$(this).css("background-image", "url(" + imgurl + ")")
}, function(){
$(this).css("background-image", "");
});
I have a working model here: http://jsfiddle.net/auURQ/
use this css
.thumb:hover{
background: url("http://www.imageurlhere.com") no-repeat 50% 50%;
}
.thumb {
width: 400px;
height: 250px;
background-color: silver;
font-weight: bold;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
.text {
padding: 15px;
}
if you have different image per Div You have two way for this
1)make a share css for them and dedicated css for on hover image
#thumb1:hover{
background: url("http://www.imageurlhere.com") no-repeat 50% 50%;
}
#thumb2:hover{
background: url("http://www.imageurlhere.com") no-repeat 50% 50%;
}
.thumb {
width: 400px;
height: 250px;
background-color: silver;
font-weight: bold;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
.text {
padding: 15px;
}
another way is use JS or JQuery Lib for set hover Event ,...
Related
I've divided my web page to left and right container(it's working fine). I made new class in div called "user-cover-container" and it only needs to put cover image on the top left side of the page.
Image is in the same folder as css and HTML file. HTML and css files are merged correctly.
.user-cover-container{
height: 200px;
width: 100px;
background-image: url("bck.jpg");
background-size: cover;
}
<div class=”user-cover-container”> </div>
Please check the inverted commas for class attribute in HTML. Change them to - " "
.user-cover-container{
height: 200px;
width: 100px;
background-image: url("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTlhiyjToS4vgqPDf_ip_MdI8B7RKbfOlCCLsb9qH8d1PPN-rj5TA");
background-size: cover;
}
<div class="user-cover-container"> </div>
you have missed the "". add this to your code.
<div class="user-cover-container">test </div>
.user-cover-container{
height: 200px;
width: 100px;
background-image: url("https://www.w3schools.com/w3css/img_forest.jpg");
background-size: cover;
color: white;
}
<div class="user-cover-container">test </div>
I'm working on a website and in a particular section, I want a blue background image to appear for half the section but no matter what I do in terms of CSS, absolutely nothing works.
I've made so many attempts that it's too many to list. What am I doing wrong and how can I fix it?
Here's my html code:
<div class="row secOne secT">
<div class="myImage col-md-6"></div>
</div>
Here's my css code:
.row.secOne.secT {
padding-top: 20px;
}
.myImage {
background-image: url("https://images.pexels.com/photos/281260/pexels-photo-281260.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500");
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
I've tried creating a simple html file using your code and by applying
background-size: 50% 100%;
background-repeat: no-repeat;
on the item having the desired image background I got something that seems your desired result.
Full code sample
<html>
<head>
<style>
.col-md-6 { /* Added this definition in order to get a visible div */
width: 800px;
height: 100px;
border: 1px solid black;
}
.row.secOne.secT {
padding-top: 20px;
}
.myImage {
background-image: url("https://images.pexels.com/photos/281260/pexels-photo-281260.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500");
-webkit-background-size: 50% 100%;
-moz-background-size: 50% 100%;
-o-background-size: 50% 100%;;
background-size: 50% 100%; /* Force background size to only fill half the div */
background-repeat: no-repeat; /* Avoid background repetitions */
}
</style>
</head>
<body>
<div class="row secOne secT">
<div class="myImage col-md-6">
AAAAA
</div>
</div>
</body>
I think you can try this : https://i.stack.imgur.com/fdk00.png
Try this too - https://i.stack.imgur.com/meURR.png
I hope it helps you - sorry if it does not.
You can make appear the blue background of your URL, setting the height CSS property
.row.secOne.secT {
padding-top: 20px;
}
.myImage {
background-image: url("https://images.pexels.com/photos/281260/pexels-photo-281260.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500");
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
height: 22em;
}
<div class="row secOne secT">
<div class="myImage col-md-6"></div>
</div>
Hope it helps!
I'm trying to add a background image to one and colour to another div class using css style. However the image is not showing at all, not sure what the problem is..
HTML:
#{
ViewData["Title"] = "Home Page";
Layout = "~/Views/Shared/_DefaultLayout.cshtml";
}
<div class="parralax">
</div>
<div class="home-parralax-sub-section">
</div>
Here is my CSS:
.parralax {
background: url(../../../../images/Resources/MainImage2.jpg);
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
}
.home-parralax-sub-section{
height: 100px;
background-color: red;
}
Now if I add the image or colour directly to the div like that:
<div class="parralax" style="height: 500px; color: red;">
</div>
<div class="home-parralax-sub-section" style="height: 1000px;">
<img src="~/images/Resources/MainImage.jpg" />
</div>
It will work!
Any help would be appricated, thanks :).
Try this
.parralax {
background: url(http://via.placeholder.com/350x150);
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
height: 150px;
/*Add to this div height*/
}
.home-parralax-sub-section {
height: 100px;
background-color: red;
}
<div class="parralax">
</div>
<div class="home-parralax-sub-section">
</div>
Add height to div containing the background-image
You have to add height to .parallax class then it will work.
.parralax {
background: url('../../../../images/Resources/MainImage2.jpg');
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
height:500px;
}
.home-parralax-sub-section{
height: 100px;
background-color: red;
}
I want put the background image of my second Bootstrap column in the center of the column. However, right now, it is in the center of the whole view and not at the center of the column.
Here are my codes:
HTML:
<div class="container-fluid cust-main-container">
<div class="row">
<div class="col-sm-2">
<!-- [..] -->
</div>
<div class="col-sm-10 bg">
<!-- [..] -->
</div>
</div>
</div>
CSS:
* {
height: 100%;
} //In my actual code, * is html, body
.cust-main-container,
.cust-main-container > .row {
height: 100%;
}
.bg{
background: url('http://placehold.it/150x350');
background-repeat: no-repeat;
background-position: center center;
background-attachment: fixed;
-webkit-background-size: contain;
-moz-background-size: contain;
-o-background-size: contain;
background-size: contain;
}
.col-sm-2 {
border: 1px solid blue;
}
.col-sm-10 {
border: 1px solid green;
}
Here's a jsfiddle: https://jsfiddle.net/1m8ua78z/
The background image is fixed (background-attachment: fixed;) so it's centering in the viewport instead of the column.
The width of col-*-2 is 16.6666%, and the width col-*-10 is 83.3333%. Therefore, the position of the .bg needs to be: 16.6666%+(83.3333%/2) or...
background-position: 58.33325% center;
Because of responsiveness, you'd only want to apply this before the cols stack vertically on smaller screens..
#media (min-width: 768px) {
.bg{
background-position: 58.33325% center;
}
}
http://www.codeply.com/go/5GG6v3rkZ3
You have set background-attachment: fixed; - remove that and set it as background-attachment: scroll;
With fixed "the background is fixed with regard to the viewport", see https://www.w3schools.com/cssref/pr_background-attachment.asp - hence the effect that you got.
A working sample fiddle is here: https://jsfiddle.net/robertjakobson/4ya7pyr5/4/ (I set the background on a div inside the column as a best practice)
Try declaring background-position: 50% 50% or just declare background-position:center
So I'm working on my first project - my own personal webpage. I have a background animation that is essentially a looping image that is pulsing. I want to be able to have the user click the "enter" part of the image and it redirect to my homepage, or click anywhere on the entire page for that matter.
My background annimation is overlapping everything and no matter what I try and do it lays over any kind of additional image or text box i try to create that clicks and links to my home page. Anyone have any suggestions?
html:
<!DOCTYPE html>
{% load staticfiles %}
<html lang="en">
<head>
<title>Test</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="{% static 'personal/css/frontpagebackground.css' %}" type = "text/css"/>
</head>
<body>
<div class="animation"></div>
<div class="body"></div>
<a class="element" href="website.net/link" title="photo" id="element">photo</a>
</body>
</html>
css:
/* hide scroll bar. Y is vertical, x is horizontal*/
body {
margin:0;
padding:0;
overflow-y: hidden;
overflow-x: hidden;
}
/*pulsing background img */
.animation {
width: 100%;
height: 100%;
animation: pulse 6s infinite;
overflow:hidden;
background-color: black;
}
#keyframes pulse {
0% {
background-image: url('img/home2.jpg');
background-repeat: no-repeat;
background-position: center;
background-size: cover;
background-color: black;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
opacity: 1;
}
25% {
background-image: url('img/home.jpg');
background-repeat: no-repeat;
background-position: center;
background-size: cover;
background-color: black;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
opacity: 1;
}
50% {
background-image: url('img/home2.jpg');
background-repeat: no-repeat;
background-position: center;
background-size: cover;
background-color: black;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
opacity: 1;
}
75% {
background-image: url('img/home.jpg');
background-repeat: no-repeat;
background-position: center;
background-size: cover;
background-color: black;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
opacity: 1;
}
100% {
background-image: url('img/home2.jpg');
background-repeat: no-repeat;
background-position: center;
background-size: cover;
background-color: black;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
opacity: 1;
}
}
html,
body {
height: 100%;
}
Make the .animation element
.animation {
width: 100%;
height: 100%;
animation: pulse 6s infinite;
overflow:hidden;
background-color: black;
}
so all other elements will be displayed on top of it instead of after it. That is, if the order in the markup stays in that way that all other elements come after it. If not, you'll have to set the other elements (or a wrapper element around them) to position: relative; and give them a higher z-index value than the background element.
How about a JavaScript onClick listener on the body tag?
You stated you wish to be able to "click anywhere on the entire page". Attaching a bubble up click listener is an easy way to achieve this if you don't mind or are already using Javascript.
<body onclick="window.location.href='website.net/link'">
<div class="animation"></div>
<div class="body"></div>
<a class="element" href="website.net/link" title="photo" id="element">photo</a>
</body>
Since in comments you indicated a preference for avoiding Javascript and you use html5, you can opt for wrapping the other elements in an <a> tag for a similar effect as long as the content fills the space.
However there is one caveat in that nested <a> links are not supported in html5. Therefore, since the button link is the same as the page link you can change the button to a <button> tag
<a id="link-wrap" href="website.net/link">
<div class="animation"></div>
<div class="body"></div>
<button class="element" title="photo" id="element">photo</button>
</a>
And then remove the styling in case you have text
#link-wrap {
text-decoration: none;
color: black
}
By the way I don't see the opening body tag in the snippet.