I'm using bootstrap jumbotron, and including a background image. Resizing the screen makes the image tile and repeat, whereas I want the image to be responsively resized.
<div class="jumbotron" style="background-image: url(http://www.californiafootgolfclub.com/static/img/footgolf-1.jpg); background-size: 100%;">
<div class="container for-about">
<h1>About</h1>
</div>
</div>
How would you go about making the image responsive? The site is HERE. Thanks for your ideas!
The simplest way is to set the background-size CSS property to cover:
.jumbotron {
background-image: url("../img/jumbotron_bg.jpg");
background-size: cover;
}
This is what I did.
First, just override the jumbotron class, and do the following:
.jumbotron{
background: url("bg.jpg") no-repeat center center;
-webkit-background-size: 100% 100%;
-moz-background-size: 100% 100%;
-o-background-size: 100% 100%;
background-size: 100% 100%;
}
So, now you have a jumbotron with responsive background in place.
However, as Irvin Zhan already answered, the height of the background still not showing correctly.
One thing you can do is fill your div with some spaces such as this:
<div class="jumbotron">
<div class="container">
About
<br><br><br> <!--keep filling br until the height is to your liking-->
</div>
</div>
Or, more elegantly, you can set the height of the container. You might want to add another class so that you don't override Bootstrap container class.
<div class="jumbotron">
<div class="container push-spaces">
About
</div>
</div>
.push-spaces
{
height: 100px;
}
I found that this worked perfectly for me:
.jumbotron {
background-image: url(/img/Jumbotron.jpg);
background-size: cover;
height: 100%;}
You can resize your screen and it will always take up 100% of the window.
This is how I do :
<div class="jumbotron" style="background: url(img/bg.jpg) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;">
<h1>Hello</h1>
</div>
You could try this:
Simply place the code in a style tag in the head of the html file
<style>
.jumbotron {
background: url("http://www.californiafootgolfclub.com/static/img/footgolf-1.jpg") center center / cover no-repeat;
}
</style>
or put it in a separate css file as shown below
.jumbotron {
background: url("http://www.californiafootgolfclub.com/static/img/footgolf-1.jpg") center center / cover no-repeat;
}
use center center to center the image horizontally and vertically.
use cover to make the image fill out the jumbotron space and finally no-repeat so that the image is not repeated.
TLDR: Use background-size: 100% 100%;.
background-size: cover; may cut off some parts of the image producing poor results.
Using background-size: 100% 100%; you force the image to take up 100% of the parent element for both height and width.
See W3Schools for more information on this.
Here is a working, responsive jumbotron background image:
<div class="jumbotron" style="background-image: url(http://yourImageUrl.jpg); background-size: 100% 100%;">
<h1>Welcome</h1>
<p class="lead">Your message here</p>
<p>Learn more ยป</p>
</div>
Unfortunately, there is no way to make the div height respond to the background-size. Easiest solution that I have used is adding an img tag within your jumbotron that contains that background image.
The below code works for all the screens :
.jumbotron {
background: url('backgroundimage.jpg') no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
}
The cover property will resize the background image to cover the entire container, even if it has to stretch the image or cut a little bit off one of the edges.
Related
I'm trying to build a website which uses a background image with Bootstrap 4. I want the image to resize but unfortunately it focuses at different points on the image as it hits the breakpoints not really centering the image. Should I use resized images via photoshop or resize images via css resizing/shrinking the image. i.e. How do I get background image to stay at the center of the screen. sample of the media query code below: (initial image size is 2000px x 1333px. Thanks!
.landpage-img {
background: url(../img/bg-neon-01-2000x1333.jpg) no-repeat center center fixed;
}
You can make parent query
.parent {
width: 100%;
position:relative;
}
And
.landpage-img {
position:absolute;
left: 50%;
transform: translateX(-50%);
}
And your image will be in the middle of parrent selector.
You can either use background-size: cover; or background-size: contain;.
Like so:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
.landpage-img {
background: url(https://picsum.photos/2000/1333) no-repeat center center fixed;
background-size: cover;
}
</style>
<div class="container-fluid landpage-img" style="height: 100vh">
<div class="row">
<div class="col">
Some content goes here
</div>
</div>
</div>
background-size: contain; maintains the proportions of the image while background-size: cover; is designed to cover things completely even if the image needs to be cropped a little. So, you'll probably want to use background-size: cover; most of the time.
Right now my wordpress site is set up so the images scale when the screen size changes. I was hoping that, instead, the image can remain at a specific size in the center of the screen and become cropped equally on the left and right when the screen size changes.
I have tried max-width:none but that doesn't keep and crop the image in the center of the page.
Site: Zxndesignco.com
The image in question is the only image on the home page. I only know CSS so I was hoping there is a CSS solution.
Example of what i'm taking about: https://gatewaydemo.wordpress.com/
Thanks for the help.
The general idea is to not use an image, and make that image the background-image of that hero section instead. So delete the img tag and add something like this CSS to .sow-image-container height: 400px; background: url(https://zxndesignco.com/wp-content/uploads/2016/12/HomeImg.jpg) center top; background-size: cover
body{
background-repeat:no-repeat;
background-position:center center fixed;
background-image:url(https://zxndesignco.com/wp-content/uploads/2016/12/HomeImg.jpg);
background-attachment:fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
height: 100%;
}
.white{
font-size: 24px;
color: #fff;
}
<div>
<p class="white">Here are some words</p>
</div>
Usually in the body or in a div. I like this group because it covers all the browser bases.
I'm trying to setup a full page background image using the following in the css file
html {
background: url(images/background.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
Although it does appear nicely, it appears only below of anything appearing in the main body (e.g. footer)
Any help would be appreciated
Regards, loaannis
Perhaps you didn't set your div's right:
Here I have a sample code for you including a "main-content" div and a "footer" div inside the "body" tag:
HTML:
<body>
<div id="main-content">
<p>1234 testing 1234</p>
<p>main content goes here</p>
<p>4321 testing 4321</p>
</div>
<div id="footer">
<p>Contact information here</p>
</div>
</body>
and this is the CSS with an extra line determining the footer color (for better visibility):
body {
background: url("http://www.neyralaw.com/wp-content/uploads/2012/07/tokyo-blue-background-4547.jpg") no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
#main-content {color:yellow;}
#footer {position:absolute; bottom:0; color:white;}
Notice that, for better visibility I have replaced your local image url with an image I found on the internet. Of course any image url will show correctly. I have also replaced the "html" with "body" in the css. Since the "body" is what it is actually "shown" I don't see why you should style the "html". However, even if body is replaced with html in the css, it still works fine!
Demo here: http://jsfiddle.net/Ee74C/4/
Happy coding,
Thodoris
So, I'm trying to get a background-image to work on any device, where it covers the body and stays in the same place no matter where you scroll. Setting background-attachment: fixed; works great for most devices, except for Android 2.X. Now, what I am trying is to have a separate div that fills up the screen with the background image.
I got this idea from This answer. Works perfectly! Except, on occasion, whenever the screen size changes or the content changes, the background becomes white all of a sudden. This happens sporadically and I cannot find a cause for it.
Here's my css for the background:
#background{
position:fixed;
top:0;
left:0;
width:100%;
height:100%;
background: url('../Images/About/about_background#2x.png');
background-size: cover;
background-position: top center;
background-origin: padding-box;
background-attachment: fixed;
}
Here's the HTML:
<body ng-controller="MainCtrl">
<div id="background"></div>
<div id="wrapper" class="">
<div id="scroller" >
<div class="container" style="" ng-view ng-class="slide">
</div>
</div>
</div>
</body>
I have noticed that inspecting the element in developer tools and removing ".container" will remove the white background and the "real" background will show again.
Does anybody have an idea as to why this would happen? Or, if there is a better way to obtain a fixed position background image, please let me know! Thanks in advance!
on the body Can you try with this solution if you like:
background: url(img/aurora.png) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
So I wanted to resize the background image of a div using background-size: contain, and I set the width to 100%. I need this for a mobile site, where the dimension of the screen is variable. However, i have a dilemma when setting the height.
If I set the height in pixels, when the picture becomes smaller, it leaves an empty space below the picture.
If I don't set the height, the picture won't appear at all.
What's the best solution to this?
Here's my code:
<div id="container">
<p>Some text</p>
<div id="my-picture"></div>
</div>
CSS:
#container {
width: 100%;
}
#my-picture {
background:url(somepic.jpg) no-repeat;
background-size: contain;
height: ???
}
According to W3Schools you should use Background-size: cover;
#my-picture {
background:url(somepic.jpg) no-repeat;
background-size: cover;
height: ???
}