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;
}
Related
The Problem
I have a user image, which I want to scale up and down with the window so that the height is always 100% and the image stays centered.
Example 1
This example scales as the window is resized, but the height doesn't stay at 100% and therefore gets cut off at the bottom.
.user {
width: 100%;
height: 100%;
object-fit: cover;
object-position: 50% 0%;
}
CodePen Example 1
Example 2
This example works perfectly, apart from when the width of the browser window is smaller than the width of the image, the right-hand side is cut off.
I do want the image to be cropped, but I want the right and left sides to be cropped equally.
.user {
object-position: center;
display: block;
height: 100%;
margin-left: auto;
margin-right: auto;
}
CodePen Example 2
Visual Example
Here is an example of how I want the images to appear when the browser is scaled horizontally/vertically.
An idea is to use multiple background like this:
I used multiple div to illustrate with different sizes
body,
html {
height: 100vh;
margin: 0;
}
.bg-shine {
background-position: center;
background-repeat: no-repeat;
background-size: auto 100%, cover;
background-image: url("https://icons.iconarchive.com/icons/paomedia/small-n-flat/512/user-male-icon.png"), url("https://t.motionelements.com/stock-video/design-elements/me1656952-blue-sunrise-background-hd-a0120-poster.jpg");
}
<div style="display: inline-block;">
<div class="bg-shine" style="height:100px;width:400px;">
</div>
<div class="bg-shine" style="height:100px;width:200px;">
</div>
</div>
<div style="display: inline-block;">
<div class="bg-shine" style="height:200px;width:100px;">
</div>
</div>
Update
To avoid using the image within CSS you can consider the inline style and a separate div for the user image so that you have almost the same markup as using an image tag:
body,
html {
height: 100vh;
margin: 0;
}
.bg-shine {
background-position: center;
background-repeat: no-repeat;
background-size: cover;
background-image: url("https://t.motionelements.com/stock-video/design-elements/me1656952-blue-sunrise-background-hd-a0120-poster.jpg");
}
.bg-shine>div {
background-size: auto 100%;
background-position: center;
background-repeat: no-repeat;
height:100%;
}
<div style="display: inline-block;">
<div class="bg-shine" style="height:100px;width:400px;">
<div style="background-image:url('https://icons.iconarchive.com/icons/paomedia/small-n-flat/512/user-male-icon.png')"></div>
</div>
<div class="bg-shine" style="height:100px;width:200px;">
<div style="background-image:url('https://icons.iconarchive.com/icons/paomedia/small-n-flat/512/user-male-icon.png')"></div>
</div>
</div>
<div style="display: inline-block;">
<div class="bg-shine" style="height:200px;width:100px;">
<div style="background-image:url('https://icons.iconarchive.com/icons/paomedia/small-n-flat/512/user-male-icon.png')"></div>
</div>
</div>
I like your question! I approached it from a different angle, and tried to use background rather than img element. Please see the results here:
https://codepen.io/Varin/pen/xYqXQe
body, html {
height: 100vh;
margin: 0;
}
.bg-shine {
height: 100vh;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
background-image: url("http://t.motionelements.com/stock-video/design-elements/me1656952-blue-sunrise-background-hd-a0120-poster.jpg");
position: relative;
}
.image {
padding:0;
margin:0;
width: 100%;
height:100%;
position:absolute;
top:50%;
left:50%;
transform: translate(-50%, -50%);
background-image:url('http://icons.iconarchive.com/icons/paomedia/small-n-flat/512/user-male-icon.png');
background-repeat: no-repeat;
background-position: center center;
background-size: auto 100%;
}
<div class="bg-shine">
<div class="image">
</div>
</div>
I am currently positioning a background image that is small in height but large in width to stretch all the way across the browser. I am only able to achieve this when I do background size cover, but not when I set a certain size to the image other than cover. I tried background repeat-x but that does not seem to work either.
<html>
<body>
<div class="background">
<div class=“header”></div>
//some content
</div>
<footer><footer/>
</body>
</html>
CSS
.background {
background-image: url(some image);
background-size: //tried cover and it works but not when I set it to width 100% or something like 2800px
background-repeat: repeat-x;
background-position-y: bottom;
}
html, body, .background {
height: 100%;
}
Just add background-size: cover code in css will resolve the issue.
.background {
background-image: url(some image);
background-size: cover;
background-repeat: repeat-x;
background-position-y: bottom;
}
html, body, .background {
height: 100%;
}
It is working with background-size:100%;
.background {
background-image: url("marakele-elephant1.jpg");
background-size: 100%;
background-repeat: no-repeat;
background-position-y: bottom;
}
html, body{
height: 100%;
}
body {
background-image: url(http://ppcdn.500px.org/75319705/1991f76c0c6a91ae1d23eb94ac5c7a9f7e79c480/2048.jpg) ;
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
background-color: #999;
}
div, body{
margin: 0;
padding: 0;
}
.wrapper {
height: 100%;
width: 100%;
}
<div class="wrapper">
<div class="message"></div>
</div>
Not very related to this question but I hope this answer will save someone's time
For the people who are using bootstrap. Keep the image inside a container, check again if it is inside class="container", I had a typo, I wrote classs instead of class and the background image wouldn't fit.
Second, close previous divs.
Third, if you don't use container and start with just <div class='row'></div>, background image won't fit.
Working Example:
<div class="container" style="background-image: url('img'); background-size: cover;">
<div class="row">
</div>
</div>
I'm curious if the CSS unit vw (view width) will accomplish what you are trying to do with width: 100%
Instead of width: 100%, try width: 100vw
https://www.w3schools.com/cssref/css_units.asp
I have image (width = 1920px) and I need to show the whole image at Full HD resolution (width=1920px) the middle of the image at smaller resolutions (see screenshot):
screenshot
Could you help me add CSS style to display center of image at smaller resoultions?
//html:
<body>
<div class="header">
</div>
</body>
//css:
html, body {
margin: 0;
padding: 0;
}
.header{
background-image: url(Header.png);
width: 100%;
height: 292px;
}
Try this one:
background-image: url('path/to/img.png');
background-size: cover;
background-attachment: fixed;
background-repeat: no-repeat;
And optional:
background-position: center;
Use background-position: center; to center the image, so when the width is less then the image size, the center will be remain visible.
.header {
height: 1080px;
background: url(http://www.cats.club/wp-content/uploads/2017/03/image_header_option.jpg);
background-size: cover;
background-position: center;
}
<header class="header"></header>
Just use background-position: center; and background-size: cover;.
You can find documentation on these attributes and property values here.
See the snippet. The original image is of three pirate skulls. Here you can see it is centered on the parent div.
.header {
width: 200px;
height: 200px;
background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/f/f4/Flag_of_Christopher_Condent.svg/1000px-Flag_of_Christopher_Condent.svg.png");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
<div class="header">
</div>
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
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 ,...