setting a responsive background image - css

I have a navbar and footer than I grabbed and customized from Bootstrap's site. I now want to have a background imagine, but I'm unsure how to do this. I know if I wanted to have just an imagine, I could put it in a div between the navbar and footer and set play around with height:100%; width:auto; so that it resizes with the browser (although this doesn't work perfectly without some modifications) but I don't know how to do this if I have body { background-image: url('...'); } I have a large resolution picture in there right now, and on a 1080 screen it doesn't scale down, it just shows 1080px of the original imagine. Is there anything in Bootstrap or CSS tricks I can use? This might be trivial to some but I am new to this, just finished Codecademy courses which introduced me to Bootstrap and now I'm trying some stuff on my own.
So to recap I want to have 16:9 ratio of a large picture, if the browser becomes to narrow, I want the height to stay the same and start "cutting off" the left/right sides of the image so that the center of the image is still in the center of the browser. Likewise for vice versa. the height:100%; width:auto; doesn't quite work because if the ratio is wrong, it stretches the image.
I also want to have a different picture if the website is accessed from a screen-reader, but that's a project for another day. Let me know if I need to clarify anything, and thanks in advance

Use your CSS this way to make the background responsive.
body {
background-image: url("myimage.jpg");
background-size: cover;
}
Then when the page becomes "too narrow" use media queries to switch the background-size properties to actual width and height that work the way you have in mind.

Related

Need to set up a responsive (fluid) background image with set height

I am helping a friend setup a web site for his company. At the top of the page, below the navigation bar, I have a background image of a house that is set to 100% width, image size is 2400px x 1602px. My problem is how to control the image so that the house is always front and center. I also need to keep the height at (or around) 75vh.
I have tried using background-size: cover and contain, as well as setting background-positions. But with the way cover works, the house is not always centered. Especially when browser width is larger--then only the roof of the house is visible. Next I tried using the aspect ratio of the image for padding. This works well, but does not allow me to set height (as far as I can tell) so with larger screens I end up with the height being way to big.
Was hoping someone might have a suggestion that would help me out and point me in the right direction. What I would like to have in the end is an image of a house where the house is always viewable and also be able to keep the height # 75vh. I have a feeling that media queries may be my answer, but wanted some advice before I continue on that path. Also wasn't sure if I need to crop my image to limit the height? I have tried so many different things that I am not sure how to proceed. Thank you for any suggestion, I really appreciate it.
I think the background cover image is the right approach, you might just have something wron gin your code that is not centering the image and that is why you only see the roof (top part of the image I assume) instead of the middle part in very panoramic screens.
Here is the code I would use:
This is the HTML
<div class="header">Your menu and other header stuff goes here</div>
<div class="bg_image" style="background: url('https://www.marriedwithmoney.com/wp-content/uploads/2017/06/house-1024x698.jpeg') center center / cover no-repeat;"></div>
And this is the CSS
.header {
width: 100%;
height: 200px;
background-color: #ccc;
}
.bg_image {
width: 100%;
height: 75vh;
}
and here is a working example which does what you inted if I understood your situation correctly https://codepen.io/anon/pen/REerRR

Cropping a background image through CSS

So, as a newbie I'm just getting lost in what I'm trying to do:
On the webshop I need to set up for my work, we are working with a 3rd party template for Lightspeed (e-commerce platform). In this theme, we've got 3 "highlight" items. These are background images + overlapping text and button to display products.
I've set the minimum height for these ID's to 800px:
#highlight-1,#highlight-2,#highlight-3 {
min-height: 800px;
}
So far so good, everything works.
But now, on a mobile platform (yeah, you can laugh already if you want because you know whats coming don't ya?), you've guessed it: the images are WAY too big.
I would love to crop the image to (maybe about) half the size for mobile platforms in portrait mode, so the essence of the image is still retained and users don't have to swipe about a kilometer before they reach the end of the page.
The only thing I can find within the .rain and .css files I have access to is:
#highlight-1{ background: url('highlight_1_image.jpg') no-repeat;}
#highlight-2{ background: url('highlight_2_image.jpg') no-repeat;}
#highlight-3{ background: url('highlight_3_image.jpg') no-repeat;}
I can edit this ánd I have access to a custom .css editor for the template
Anyone who can help a newbie out? Thanks a bunch in advance! :)
The ideal solution is to use a media query to serve a smaller image. This cuts down on bandwidth for mobile visitors who may be using limited data, and I believe has an impact on your page ranking within Google and the like.
#highlight-1{ background: url('highlight_1_image.jpg') no-repeat;}
#media screen and (max-width: 767px) {
#highlight-1{ background: url('highlight_1_mobile_image.jpg') no-repeat;}
}
If that's not an option, you can explicitly specify size with the background-size property, or set it to cover (scaled up or down so a single instance of the image will completley cover the element with cropping if required) or contain (scaled up or down so that the element is completley filled horizontally or vertically)

Locking/fixing an image to an exact spot on a background image

I am trying to set images as links to an exact spot on a background image. I need the image links to stay in the same space on the background image and be responsive.
I am using html and css, and have little knowledge of any other programming language. I am trying to use a background image that is fixed and four images as links with absolute position values, but the image links move around when I change the browser window. Any suggestions? Thanks!
I can give you 2 ways to solve this as i used them before:
1: Use all measurements in %:
That is actually not responsive, but falls into the concept of fluid layout.. wich means it always fills the whole that it can on whatever size.
2: Use media queries:
that is considered responsive, because at each key sizing it will change your positionning:
#media (max-width: 1200px) {
.element {
/*INSERT POSITIONING STYLES HERE (POSITIONING[top, right, bottom, left])*/
}
}
.element {
/*INSERT DEFAULT STYLING HERE (COLORS, DISPLAY MODE, VISIBILITY, OPACITY)*/
}
REMEMBER: in order to position to work, you have to either set the element to position: fixed/absolute/relative.
Hope i helped out. In case of doubt, just ask me!

Fixed background image until bottom of page

I'm trying to get a background image to start and stay in a fixed position, but only until the rest of the 'content' of the page is finished, at which point the full image is displayed.
I'm working on a purely CSS solution. I should note that the image is larger than most (laptop) screens.
Specifically, here's the code that I've been using:
body {
background:$bgcolor;
background-image:url('http://i.imgur.com/cIGSehG.jpg');
background-repeat:no-repeat;
background-position:0px 72px;
background-attachment:fixed;
margin:0;
...
}
The image that I'm using is given in the url():
The effect that I'm looking for is basically the image will display only about the top 10% of the grass hill while you're looking at most of the page, but if you finally scroll all the way down past all the page content, the remaining 90% of the grass hill will be shown.
I couldn't find this anywhere, but I may have just been using poor search terms since I'm not so familiar with the lingo.
Well, this was one jiggy nut! I did come up with a not so stable trick to achieve this. I don't have time to develop it any more right now, but perhaps it might be a working concept.
Main contept
By providing a large and empty footer area that the user is likely to hover when reaching the bottom of the page, we use a sibling selector to change the position of its sibling element containing the background:
#footer:hover ~ #background {
background-position: center bottom;
}
Along with a few quirks (which ought to be improved) we can achieve a parallax effect.
Go Fiddle
Check out this JFiddle (in Chrome) to see and play with it.

Change width coverage of a background-image URL in CSS?

If I have a background image that is, let's say, 20px in width, and I want it to rather be 40px in width (but adjusting in photoshop would not work as I want it).
How can I change the width?
body {
background-color:#5b7c8a;
background-image:url('images/diagnol.png');
background-repeat:repeat-y;
margin:0;
}
It's diagonal lines as you can see from the image,
screenshot http://img62.imageshack.us/img62/4263/testzjr.png
and the spacing is just as I want them. Editing the image may mess up the spacing. So I'm trying to get the background coverage area of the body to be a little bit more? Right now the image is 6px width. I want it to appear as 20px width, as if the image were in repeat-x-y in a 20px div? (so not stretching the image, just gaining more ground with the image)?
I hope this makes sense > <
"adjusting in photoshop would not work as I want it"
CSS is not image editing software. In photoshop you just need to go to Image > Image Size and scale as you wish. Or take your time and learn to use the basics of Photoshop to get the effect you want. You just need to not be scared and experiment with each tool till you get what you want, as you can obviously undo the undesired effects.
The CSS way is possible but it's not meant for these kind of situations and will not be cross browser friendly. It's rarely used on typical sites so fortunately I'm not going to encourage it. Good luck.
I believe there's been misunderstandings with what the OP wants.
Incase i got it right, check out this example:
http://jsfiddle.net/vYxza/
Think of the #text as body and the .this_div as the element you need to create inside it.
Basically you create new element where you repeat the background in all directions but you just restrict the width of that element.
Another way of doing it:
http://jsfiddle.net/vYxza/1/ - This is actually the way i would do it mostlikely.
Note that again.. if you wanted to use it in body... think of .this_div as html and #text as body

Resources