Calculating Breakpoints of `background-size` Property - html

I have a centered viewport who has a large image as its background. Due to other elements on the page, we elected to have some breakpoints depending on the sizes of of these other elements. So far so good, my layout works fine.
The one gotcha is that since the image used isn't tall enough, once it reaches a certain size the background-color shows and there are bars that are introduced on either side. This has to do with the ratio of the image vs. the ratio of the viewport.
Solution with Bars:
http://output.jsbin.com/vecele
Now, the fix here is to find at what point the bars are introduced. Anything larger than that size, down to the cutoff means the background-size needs to be changed to background-size: 100% auto; vs when before when it was set to background-size: auto 100%;
Fixed Solution:
http://output.jsbin.com/fonozagova/
Now my question is how do I mathematically figure out that "magic number" cutoff? In my example, I just played with the window size to find the actual number I should use here, which really isn't optimal.

Why not set the background size to cover? It will scale the the background image proportionally to fill the container regardless of what the size is.
div.container {
background-size: cover;
}

you can use a pseudo-element with a vertical-padding in % that will use elements width as reference. the ratio will be kept no matter width updates:
.hero {
background-color: #0a6ba1;
}
#header {
background-image: url('https://suretybonds.com/img/bonds/giant-header/inspector-1800.png');
background-repeat: no-repeat;
background-size: 100% auto;
overflow: hidden;
margin: 0 auto 40px;
max-width: 1600px;
position: relative;
}
#header:before {
content:'';
display:inline-block;
padding: 26% 0 0;
}
#media screen and (min-width: 699px) and (max-width: 1465px) {
#header {
}
}
#media screen and (min-width: 0) and (max-width: 699px) {
#header {
}
}
#media screen and (min-width: 0) and (max-width: 500px) {
#header {
}
}
/* extra for demo */
#header {
text-align:center;
font-size:4vw;
}
#header:before {
vertical-align:middle;
}
<div id="header" class="hero">resize window's width</div>
http://jsbin.com/lafosakeke/1/edit?output

Related

My logo is great on the desktop, but on mobile it is too big

I know this is a simple fix, but I am clueless on how to actually fix the problem. It's weird. On the desktop version, my logo is smaller than I want it to be because on the mobile it gets way too big.
Here is my css code:
#logo{
height:380%;
margin-top: -35px;
}
And here is my meta tag for other devices:
<meta name="viewport" content="width=device-width, initial-scale=1">
I also researched a bit and saw that I need an #media? Also here is the picture:
Here is my full css code as requested:
.navbar-text pull-left{
color:#000000;
text-decoration:none;
}
.navbar-text p{
color:inherit;
text-decoration:none;
}
.navbar{
border: 0px;
}
#logo{
height:500%;
margin-top: -35px;
}
#media only screen and (min-width: 640px) {
#logo {
height:150%; // Change this value
margin-top: -35px;
}
}
You can implement the equivalent of max-font-size with media queries
The idea is relatively simple.
You set the font size to 20vw - read about vw here - since it's
a logo.
you overwrite the font-size with a fixed size once the viewer size
exceeds a certain point - 450px in my example below.
The end result is that your font will adjust to the screen size and will be responsive.
Working example:
#logo {
font-size: 20vw;
text-align: center;
}
#media screen and (min-width: 450px) {
#logo {
font-size: 83px;
}
}
<div id="logo">"Quotin"</div>
Edit:
I just realized your logo is an image.
I trimmed the extra empty space off your logo in Photoshop because that's a lot easier than messing with negative margins in this case.
This should work on both mobile and desktop screens and be responsive without needing media queries
Working Example:
#logo {
margin: 0 auto;
max-width: 100%;
width: auto;
height: auto;
display: block;
}
<a class="navbar-brand" href="http://www.quotin.co"><img class="img-responsive" id="logo" src="https://image.ibb.co/dTt4xv/8DL1n.png"></a>
I would replace 380% by a size in either pixel or rem.
Mobile screens have nowadays very high pixel density. Here is a good example on how to make a responsive logo https://getflywheel.com/layout/css-sprites-make-your-logo-responsive/
CSS3 Media Query is what you should be looking for. Assuming that your desired mobile width is 640px, put the following in your CSS to display it differently on mobile view.
#media (min-width: 640px) {
#logo {
height:150%; // Change this value to adjust logo size
margin-top: -35px; // Change this value to vertically move your logo.
}
}
Your code is all good. Just change min-width to max-width, and you're all set.
#media only screen and (max-width: 640px) {
#logo { height: 150%; };
}
Right now, what you are saying is:
#logo { height: 500%; } - Give the logo a height of 500%.
#media ..(min-width: 640px) {#logo {height: 150%;} } - If the screen size is at minimum 640px wide (or wider...), let the height be 150%.
I'm surprised nobody noticed this before.

Div with background image spilling out of parent div in IE11

I understand there's a gazillion questions on this issue but none of the solutions on any of them seem to work for me. This is what my markup looks like:
<div class="immersion-div">
<div class="immersion-div-image"></div>
</div>
As you see, it's a fairly straightforward setup with one div containing another. The parent div is styled to adapt its height to the device screen resolution using media queries. Here's how the two divs are styled:
Parent div:
#media (min-width: 2000px) { .immersion-div { height: 1307px; } }
#media (max-width: 1999px) and (min-width: 1401px) { .immersion-div { height: 1000px; } }
#media (max-width: 1400px) and (min-width: 750px) { .immersion-div { height: 500px; } }
#media (max-width: 749px) and (min-width: 300px) { .immersion-div { height: 300px; } }
#media (max-width: 299px) { .immersion-div { height: 136px; } }
Child div (with the image background):
.immersion-div-image {
background: url(../../bootstrap/img/homepage/spanish_immersion.jpg) no-repeat center center;
background-size: cover;
height: 100%;
}
The image serving as the background is 2880px by 900px (although that should be inconsequential in this scenario) and the resolution of the screen on which I tested this is 1366px wide. Per the defined media queries, the height of parent div should evaluate to 500px since the screen falls in the 750px-1400px category. However, on Internet Explorer, the div seems to render with a height of 1000px! This issue is only affecting IE whereas all other browsers are rendering the divs fine. What could be the problem? Before anyone suggests using background-size: contain, I must admit I tried it and it messes up the aspect ratio leaving a blank band at the bottom of the div which is why I don't want to go that route. Besides, I want to understand why cover wouldn't work on IE when it does just fine on all other browsers.
Just in case it helps, the site in question is peppyburro.com and the affected divs are on the last green image on the home page.

Images in Smaller Browser

http://graduateland.com/
How do i prevent the images from compression. When I reduce the size of my browser window, the image get compressed side way, it's like the human head being compressed.
Looking at that website as an example, the image size isnt affected when screen size changes, only the position of the image changes. How do i do that?
Current CSS
position: absolute;
top: 0;
left: 0;
min-width: 100%;
height: 500px;
If you want images to be resized when the window shrinks, just change height: 500px to height: auto in the CSS you posted. This will force images to keep their original ratio as the width changes. The way your code works right now is that it resizes the image horizontally so it is never wider than its container, but has a fixed height, which messes up the aspect ratio once it begins to shrink horizontally.
If you want the image to stay the same size and just move position as the browser window shrinks you need to apply them as a background-image. Try this CSS code on the container div you want to apply the image background to:
#container {
background: url(path/to/image.jpg) no-repeat center top;
}
On the site you linked they are appyling this CSS
display: table;
width: 100%;
height: 100%;
margin-top: 0;
padding-bottom: 0;
background-image: url("a/image.jpg");
background-repeat: no-repeat;
background-position: center center;
background-size: 100%;
onto a div. But there are great inspector tools which can inspect that for you, so don't ask if you have a 'living' example.
You should particularly have a look at the background properties.
Here's the answer:
Responsive Images with CSS
CSS:
max-width:100% !important;
height:auto;
display:block;
Use #media, like:
#media screen and (max-width: 1280px) and (max-height: 1024px) {
.splash {
background-image: url('../img/splash-1280.jpg');
}
}
#media screen and (min-width: 1281px) and (max-width: 1920px) and (max-height: 960px) {
.splash {
background-image: url('../img/splash-1920.jpg');
}
}
In their CSS:
#media (max-width: 1280px)
[id="get-started"] {
background-size: auto;
background-position: center top;
}
Which overrides:
background-position: center center;
background-size: 100%;

crop image when overlap in responsive web design

i am creating a responsive web design containing images....i want to know if there is any way i can crop images when they overlap i.e if i have two images in one line image 1 and image 2
image 1 is at the left and image 2 is at right and i start lessening width of my browser, and when image 2 reaches image 1, image 2 starts cropping or hiding or whatever....how m i going to do that?
here is my code for what i am trying:
#logo{
float:right;
margin:88px 0 0 70px;
position:absolute;
}
#header-add{
float:right;
margin:35px -10% 0 0;
cursor:pointer;
}
Logo is image 1 and header-add is image 2
Rather than crop the image, I'd suggest simply setting your CSS to set the width of the images appropriately when the browser width is decreased. This way you don't have to worry about cropping.
For example (values arbitrary, but percentage-based, which I find best for responsive design):
#media screen and (max-width: 768px) {
#header-add {
width: 40%;
}
}
#media screen and (max-width: 480px) {
#header-add {
width: 25%;
}
}
If you don't want to set the width of the images via CSS, you can essentially "crop" the images if you enclose each of them in a div and you can set overflow:hidden on the div, and then set the width of the div in the CSS (like the aforementioned image width example).
Hope it helps!
Addition:
In answer to your comment about cropping from the left, here's how I would recommend doing it. The downside is that you have to add an explicit height on the div that crops the image, but it should work for you.
The HTML:
<div id="crop_div">
<img src="foo.jpg" alt="bar" />
</div>
The CSS:
#crop_div {
float: right;
height: 100px; /* Needed since contents use absolute position */
overflow: hidden; /* To crop the img inside of it */
position: relative; /* Set for img position below */
width: 400px;
}
#crop_div img {
position: absolute; /* To anchor it on the right */
right: 0;
}
#media screen and (max-width: 768px) {
#crop_div {
width: 40%;
}
}
clip() and overflow: hidden for masking for sure your content.
min-width and/or max-width to manage the width of each div when the sum of both would be too large for the width of the container.

Need Empty Div With Background Image To Force Height and Must Be Responsive

I need the following:
emtpy div with no content
background image set to the div the
background image to be fluid/responsive on re-size I cannot set fixed
dimensions on the div
Everything I try fails to force the div open to support the size of the background image. Any help is greatly appreciated...
http://www.everymountain.us/
<header id="header">
<div class="wrapper">
<div class="inner">
<div class="top_banner"></div>
</div>
<div class="clear"></div>
</div>
</header>
.front #header .top_banner { background: url('images/bg_front.jpg') no-repeat; background-size: cover; }
The way to lock a height's aspect ratio to it's fluid width is to use padding-top or padding-bottom percentage. This is because all padding percentages are of the the element container's width. Your background image is 960 x 520, so the height is 54.166666666667%.
html
<div class="top_banner"></div>
css
.top_banner {
background-image: url('images/bg_front.jpg');
background-size: 100%;
width: 100%;
padding-top: 54.166666666667%;
height: 0;
}
Example: http://jsfiddle.net/SsTZe/156/
Essentially the same question: CSS fluid image replacement?
You can handle it after applying CSS
#DivName{
background-size: auto auto;
}
here first auto is for width and second is for height
Since this is a top google result for creating fluid-height divs in general (not just empty ones like the question specifies), I wanted to leave a CSS Calc solution that lets you put content into the div (the padding trick forces it to be empty):
.my-div {
background: #ccc url(https://link-to-image/img.jpg) no-repeat 50% 0;
background-size: 100% auto;
width: calc(100vw - 350px);
height: calc((100vw - 350px) * 0.468795); /* replace the decimal with your height / width aspect ratio */
}
Try to use medie queries in your CSS for different screen sizes to handle different fixed heights.
For example:
#media (min-width: 1200px) {
div { height: 3em; }
}
#media (min-width: 768px) and (max-width: 979px) {
div { height: 2em; }
}
#media (max-width: 767px) {
div { height: 1.2em; }
}
#media (max-width: 480px) {
div { height: 1em; }
}
etc. what you need to customize. You can leave the div width 100% to fit for all screen and the background-size:cover. You can also make different size backgrounds (diff. files) for each screen sizes to give less size to your website for mobile or tablet devices.