CSS: Get a center-positioned fluid image background - html

This has been annoying me now for a few hours as I keep fiddling around with the code and don't seem to be getting anywhere at all.
I really like the way that this website has a fluid image background:
Here
I have downloaded the code and am not currently trying to make a slideshow, but instead a single image background that when you play around with the size of your browser expands and contracts the same way as in the website.
If i take the code that they use in their CSS and apply it to my own image I keep getting this instead.
https://jsfiddle.net/9cpz4gua/
body{
background-image:url(https://blogs-images.forbes.com/ericsavitz/files/2011/03/smiley-face.jpg?width=960);
background-repeat:no-repeat;
width: 100%;
height: 100%;
position: absolute;
top: 0px;
left: 0px;
color: transparent;
background-size: cover;
background-position: 50% 50%;
background-repeat: none;
opacity: 0;
z-index: 0;
}
Could someone please advise me what to do in order to get my background image to expand and contract about the center as the website does?

Here is my answer, it may not be the easiest or the most complete but it give a very beautiful looking result that answers your question
Basically, you create a new div directly under your body
and style it this way:
.bgimg {
z-index: 1;
background: url(http://dondev.ovh/light_abstract.jpg);
width: 100%;
height: 100%;
position: absolute;
top: 0px;
left: 0px;
background-size: cover;
background-position: 50% 50%;
background-repeat: no-repeat;
}
See it in action
This is a css only answer, you can use JavaScript alongside for better result.
Regards

If all you want is to have your image always fill the page, just remove some things from your own style to achieve the "cover" effect.
Try this:
body{
background-image:url(https://blogs-images.forbes.com/ericsavitz/files/2011/03/smiley-face.jpg?width=960);
background-repeat:no-repeat;
color: transparent;
background-size: cover;
opacity: 0;
z-index: 0;
}
https://jsfiddle.net/9cpz4gua/2/

Related

Using an SVG as a background for a specific portion of a website

I'm trying to use an SVG as a background for a specific section on a website I'm creating, but I'm finding it difficult. The main problem I'm having is the SVG covers the text, but in the image, I'll link below the SVG is behind the text.
This is the image
This is some code I wrote which didn't work.
.icons_and_text {
margin-top: 100px;
position: relative;
}
.icons_and_text::before {
content: '';
background-image: url('./images/bg-curvy-desktop.svg');
background-size: cover;
position: absolute;
top: -600px;
left: 10;
height: 400px;
width: 100%;
}
I'm looking for answers which would enable me to solve the problem on my own next time, thank you.
Hope it works for you
header .container {
position: relative;
z-index: 1;
}
This seems to work, although it's not exactly what I want. What I want is a perfect way to position the image.
header.header {
background-color: hsl(217, 28%, 15%);
background-image: url(../images/bg-curvy-desktop.svg);
background-position: center bottom;
background-repeat: no-repeat;
background-size: 100% 23rem;
}

Full screen centered background responsive

I'm looking to make my homepage a full screen centered background image, where it doesn't matter what screen size the device is, the image always covers the entire page and with the correct aspect ratio. I'd also like it to work across various devices.
I've seen various different ways to do this but i just don't seem to get the effect that i'm looking for. So i'm either doing things incorrectly or i just haven't found/thought about a solution that works.
Thanks
html {
background: url(images/bg.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
Try the above code and switch html with any other element as required. So if you only want it on the body or something for example.
This was taken from this article on CSS Tricks -
CSS Tricks - Perfect Background Image
.bg {
position: fixed;
top: -50%;
left: -50%;
width: 200%;
height: 200%;
}
.bg img {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
min-width: 50%;
min-height: 50%;
}
Like that, you can still use the html image tag.
.div {
background: url('images/bg.jpg');
background-size: cover;
background-position: center;
}
And for responsive layout if you are new to this i suggest using Bootstrap framework.

Website background interfering with website content

I'm sure this is a basic question but I am a complete beginner so sorry ahead of time! I am building a website and I decided to add an image as my background with this code:
#background {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
height: auto !important;
min-height:100%;
background-image: url('/images/background.jpg');
background-repeat: no-repeat;
background-attachment: fixed;
background-size: 100% 100%;
background-size:cover;
opacity: 0.4;
filter:alpha(opacity=40);
}
<div id="background"></div>
However, when I add this, it doesn't allow me to interact with my contact me script or my embedded PDF.
code for pdf:
<center>
<iframe id="iframepdf" src="/documents/Resume.pdf" width="783px" height="1009"></iframe>
</center>
Help would be appreciated
The div you are using as your background is probably getting in the way of other things in the website. I would suggest using the tag for your background image
body{
background-image:url(background.jpg)
}
for a start you have way too much stuff in your background image css. If you want to cover a div, then use the div tag but if you want to cover the whole page
use
body {
background-image: url('/images/background.jpg');
background-size: cover;
opacity: 0.4;
filter:alpha(opacity=40);
}

Background image bigger than window

I need some help with CSS. As you can see here https://jsfiddle.net/88eb92ed/ the scrollbars are enabled. I want to hide them. I've never used CSS before, and I used a template, that's why I don't know how to change it. I would like to disable the scrollbars. I don't really know what's easier: change background image size or disable scrollbars. Some code:
.body {
overflow: hidden;
position: absolute;
top: -20px;
left: -20px;
right: -40px;
bottom: -40px;
width: auto;
height: auto;
background-size: cover;
background-position: center;
background-attachment: fixed;
background-repeat: no-repeat;
background-image: url({{ url_for('static',filename='images/parisbackground.jpg') }});
-webkit-filter: blur(5px);
z-index: 0;
}
I tried using overflow: hidden (from this SO question) and check several webpages trying to fix this. It seems that the image is bigger than the window, so I would like to keep the image center, but adjustable to the window size, with no scrollbars.
Thanks!
If you just want to disable the scroll bars:
body {
overflow: hidden;
}
Notice That's on the <body> tag and not the .body class.
If you want to force the elements to fit in their parent containers, you will need to refactor how they're positioning in relation to one another.
You've got some interesting things going on in regard to your markup. I'm not sure what the purpose of .grad is. Also, would it not be simpler to apply the styles to <body> rather than trying to absolutely position <div class="body"> behind a bunch of stuff?
If you're sticking with .body, you don't need to define all four dimensions for positioning. You only need to orient one position for either X or Y.
So it looks more like:
position: absolute;
top: 0;
left: 0;
Try below :
http://jsfiddle.net/pratyush141/mkzkqdv0/
.body{
width:100%;
overflow: hidden;
position: absolute;
top: -20px;
left: -20px;
right: -40px;
bottom: -40px;
background-size: cover;
background-position: center;
background-attachment: fixed;
background-repeat: no-repeat;
background-image: url(http://www.meezan.tv/themes/default/member_images/example_background.png);
-webkit-filter: blur(5px);
z-index: 0;
}

Hovering over images while having them scale with the browser

I'm trying to make an image that changes into another image when hovered over that also scales based on the size of the browser.
The css code I have so far is this:
img {
max-width: 100%;
height: auto;
width: auto\9; /* ie8 */
}
#a_Cscaleskb{
width:0px;
height:0px;
background:url('../images/a_Cscaleskb_up.gif');
padding:47px 114px;
}
#a_Cscaleskb:hover {
background: url('../images/a_Cscaleskb_over.gif');
}
with the html:
<img id="a_Cscaleskb" src="../../images/a_Cscaleskb_up.gif" alt="" />
This works great for changing the image on hover but when I shrink down the browser it doesn't change.
Another piece of code I've tried is this:
#a_Cscaleskb{
background:url('../images/a_Cscaleskb_up.gif');
top: 0;
left: 0;
right: 0;
bottom: 0;
background-repeat: no-repeat;
background-size: contain;
}
#a_Cscaleskb:hover {
background: url('../images/a_Cscaleskb_over.gif');
top: 0;
left: 0;
right: 0;
bottom: 0;
background-repeat: no-repeat;
background-size: contain;
}
This css scales properly upon hovering, but you can see both images laying over each other. I know it's also possible to use javascript but I'm trying to avoid using it. Any suggestions are greatly appreciated.
If you want to scale your images according to browser size then use this sneaky bit of code:
img{max-width:100%;width:auto;height:auto}
that's what i've used in my gallery page in www.busyfeet.rachelgallen.com - give it a look and see it in action