Centering Div in IE9 without fixed Height/Width - html

Can I borrow someone's eyes before I drop kick this damn thing out the window?
SHORT VERSION: What's the right way to x,y center a div reliably in IE9 when it can not be fixed height/width?
Scenario: I'm customizing the templates of ping federate server that we pass through a windows store app for auth. I mention this because windows store apps (Not to be confused with UWP) uses a jank version of IE9.
My problem...which I can't even go make a damn codepen for since they don't support IE9 anyway....is I'm just trying to center the first child div both vertically and horizontally.
Now then, IE9 doesn't support flexbox, so no love there. I am however able to do;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
which does indeed center it nicely, looks great on everything EXCEPT IE as it displays a large white space at the bottom of the screen. Which in turn, also causes scrollbars to appear which aren't even necessary...
Since the div can not be of a fixed width/height I have not gotten other fixes to work. It also doesn't help I've been doing .NET stuff so long my css is rusty.
So can someone start my weekend off right and enlighten me to some IE Kung Fu fix so I may praise your name and toast beer to you this evening? :)
Hopefully the snippet below (ran in IE as IE9) will help visualize my issue with this stupid whitespace at the bottom that has become my nemesis...
html, body {
height: 100%;
width: 100%;
}
body {
background-image: url("https://static.pexels.com/photos/1350/blur-blurred-background.jpg");
background-size: cover;
background-repeat: no-repeat;
background-position: fixed;
}
div {
min-width: 250px;
min-height: 250px;
background: green;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
<div></div>

the old way was with an extra element, but a pseudo will do for IE9 :
html, body, body:before {
height: 100%;
text-align: center;
}
body:before {
content: '';
display: inline-block;
margin-left: -0.25em;
width: 0;
vertical-align: middle;
}
div {
min-width: 250px;
max-width: 99.5%; /* or white-space on body for security to avoid div wrap under pseudo,
do not forget to reset to normal if you choose so */
min-height: 250px;
background: green;
display: inline-block; /* i'll be against top or left without disappearing if window is too small */
vertical-align: middle;
/* text-align: left; */
}
/* not IE 9 , bg-cover ? */
body {
background-image: url("https://static.pexels.com/photos/1350/blur-blurred-background.jpg");
background-size: cover;
background-repeat: no-repeat;
background-position: fixed;
}
<div></div>

Related

Transform/Scale doesn't appropriately scale background attachment fixed

I have two images which get overlayed on top of each other in my application, these are represented as foreground and background. For both of these I'm using background-attachment: fixed to make sure the images are always the exact same size as each other. This allows me to add an edited version on the foreground, but still keep the two images consistent so they both look like one.
You can see an example of this below;
html,
body {
height: 100%;
width: 100%;
}
.background_container,
.foreground_container {
height: 100%;
width: 100%;
position: relative
}
.background,
.foreground {
background-image: url("https://i.redd.it/uojoraeqr4c31.png");
background-size: cover;
background-attachment: fixed;
height: 100%;
}
.foreground {
max-height: 50%;
margin: 5rem 0;
}
<div class="background_container">
<div class="background"></div>
</div>
<div class="foreground_container">
<div class="foreground"></div>
</div>
The issue I'm having is that I have a need to zoom these images in on an animation. To do this I'm using transform: scale (1.5) on a keyframe, but the more it scales, the more out of sync the two images get. I expect foreground to be scaled the exact same as the background as they are on the same plane due to background-attachment: fixed, but I'm guessing the required height and margin properties cause some issues.
html,
body {
height: 100%;
width: 100%;
}
.background_container,
.foreground_container {
height: 100%;
width: 100%;
position: relative
}
.background,
.foreground {
background-image: url("https://i.redd.it/uojoraeqr4c31.png");
background-size: cover;
background-attachment: fixed;
height: 100%;
transform: scale(1.5);
}
.foreground {
max-height: 50%;
margin: 5rem 0;
}
<div class="background_container">
<div class="background"></div>
</div>
<div class="foreground_container">
<div class="foreground"></div>
</div>
Is there any sort of solution to this? I want example 2 to look like example 1, just more zoomed in.
https://jsbin.com/nesekuxuyu/1/edit?html,css,output
See my jsbin.
Remove your foreground specific styling and add overflow: hidden; to the parent container. It was scaling properly however it was exceeding the bounds of it's parent container and by hiding the overflow you prevent it from distorting the bits you can see.
Edit In outside discussion with James I see the actual issue and am working on an appropriate solution. Scale is overridng the fixed behavior inherent in background-attachment

background-size: cover stretching on certain phones

The background image of the header of my site is stretching and warping in weird ways on certain mobile phones.
Here's the link to the site - https://jaels-creations-stage.callgage.co/
Here's the code for the background image, it s a child of the header container:
.header .background-image {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-position: center 92%;
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
z-index: -1;
/* On Load Animation */
opacity: 0;
}
I've been trying to figure this out for a long while now, I can't replicate it on my macbook, and with devtools remote devices I can't figure out a proper way to fix it. Any help would be greatly appreciated.
Try height:auto; that will cause css to retain the image's natural aspect ratio
I think is because you need to set the eight to auto
bg {
position: absolute;
top: 0;
left: 0;
min-height: 100%;
/* set your own */
min-width: 1440px;
/* Set up proportionate scaling */
width: 100%;
height: auto;
/* Use this if you have your images set to fixed. It will promote the layer */
transform: translate3d(0, 0, 0);
}

A repeat-y background-image disappears/misaligns when zooming out

The image disappears when zoomed out to 40% on Firefox. Up until zoom 50%, it is fine. However at zoom 40%, it just vanishes:
Whereas in Chrome the image is still visible but slightly misaligned, this happens at different levels of zoom:
For once Internet Explorer is actually displaying the expected result regardless of zoom!
What is trident doing differently to webkit and gecko, and how can I patch it?
Here is all the relevant code:
body {
background-color: rgba(31, 59, 8, 1);
}
#main {
z-index: 1;
position: absolute;
top: 113px;
left: 50%;
width: 900px;
height: 100%;
margin-top: 160px;
background-image: url('http://i.stack.imgur.com/zZCB2.png');
background-repeat: repeat-y;
margin-right: -50%;
text-align: center;
transform: translateX(-50%);
}
#main:before {
content: " ";
position: absolute;
left: 0px;
top: -113px;
background-image: url('http://i.stack.imgur.com/7DE7i.png');
background-repeat: no-repeat;
width: 900px;
height: 113px;
}
#main:after {
content: " ";
position: absolute;
left: 0px;
bottom: -200px;
background-image: url('http://i.stack.imgur.com/DVJAq.png');
background-repeat: no-repeat;
width: 900px;
height: 200px;
}
<div id="main"></div>
Making your body.png image 20px tall or so will fix the issue. 1px repeating images act strange sometimes. Should help with redraw/flickering while the element is loaded as well.
Also, setting the "#main:before" to have a top of -112px instead of -113px got rid of the funky line in Chrome when you zoom in (at least for me).
Hope that helps.
I am not sure, but i belive if you split the images and make 3 parts:
::before (white top, with bottom shadow)
::header (the red box)
::after (White bottom, with top shadow from the red box)
I hope that this answer helps!

Positioning scrolling divs

I come to you once more for a question. I've got a logo consisting of three elements: TEXT | LOGO | TEXT. The logo part is static. It should always be horizontal + vertical centered. I've accomplished this with a background. The two text parts need to be able to scroll up and down and when it leaves the top of the page a new version should come up from the bottom of the page and stop at the point that it's exactly lined up with the logo again.
I've got this perfectly working on my own desktop which is a 1920 * 1080 resolution. However, i've done this trough padding and pixels.
#logo {
background:none;
padding-top:411px;
}
#logo2 {
background:none;
padding-top:958px;
}
#logo3 {
background:none;
padding-top:962px;
}
Obviously this is not the way to go since on any other resolution the alignment is totally off.
I've created a JSFiddle: http://jsfiddle.net/thedonny/mzr91b4f/
Since the Fiddle shows the output in a smaller screen you already see my problem. I pretty sure im missing something here. Is there anyone that could point me in a right direction on how to pull this of?
Kind regards,
Arjen.
Try this one on for size: http://jsfiddle.net/mzr91b4f/2/
I made each of the #sections 100% the height of the viewport (after setting html, body, and .container-fluid to 100% height). I then made each of the #logos offset from the top by 50% with a margin-top of (approximately) half the height of the image. I did some funky adjustment to the last #logo to get it to be exactly the height of the viewport so the scrollbar stops exactly at the bottom of the window on Section Three. Here is the CSS (a bit reorganized):
html {
height: 100%;
}
body {
background: url(http://i.imgur.com/5rFLWpS.png) repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
height: 100%;
}
.container-fluid {
height: 100%;
width: 100%;
}
#sectionone, #sectiontwo, #sectionthree {
height: 100%;
width: 100%;
}
#logo {
background:none;
position: absolute;
top: 50%;
margin-top: -72px;
}
#logo2 {
background:none;
position: absolute;
top: 150%;
margin-top: -72px;
}
#logo3 {
background:none;
position: absolute;
top: 250%;
margin-top: -72px;
bottom: 300%;
margin-bottom: -72px;
}

Repaint bug with background-attachment fixed and background-size cover in Chrome

I have element with:
background-image url('../images/belly.png')
background-position 50% 50%
background-repeat no-repeat
background-attachment fixed
background-size cover
And underlying element with position: fixed;
And if I scroll page background is not redrawing. Problem appear in Chrome. Any solution?
demo: http://silentimp.github.io/90daysofbelly/
video: https://www.youtube.com/watch?v=av6jZciNszo&feature=youtu.be
I have noticed the best way to make sure the page backgound stays fixed no matter what is: place it as the background image of an empty first child of body, with these CSS rules:
.background-holder {
position: fixed;
width: 100%;
height: 100%;
display: block;
z-index: -10;
background-image: url(//link-to-image);
background-size: cover;
}
And here's the page structure:
<html>
<head>
</head>
<body>
<div class="background-holder"></div>
<div class="main-container">
<!-- content goes here -->
</div>
</body>
</html>
I had the same issue you had and struggled with it for almost 3 days. But as of June 2020 and improving on #tao's answer, here is a reliable solution I found for this that works on all devices and has 100% browser compatibility. It allows the desired effect in any place of the page and not just the top or bottom of the page, and you can create as many as you need or want.
The only known issue is with safari. The browser repaints the whole image every scroll movement so it puts a heavy burden on graphics and most of the time makes the image flicker up and down some 10px. There is literally no fix for this, but I think there is also no better response for your inquire.
I hope this works for you. You can check the results live in www.theargw.com, where I have three different fixed background images.
body, .black {
width: 100%;
height: 200px;
background: black;
}
.e-with-fixed-bg {
width: 100%;
height: 300px;
/* Important */
position: relative;
}
.bg-wrap {
clip: rect(0, auto, auto, 0);
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.bg {
position: fixed;
display: block;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-size: cover;
background-position: center center;
background-image: url(https://images.pexels.com/photos/949587/pexels-photo-949587.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500);
transform: translateZ(0);
will-change: transform;
}
.e-container {
z-index: 1;
color: white;
background: transparent;
}
<div class="black"></div>
<div class="e-with-fixed-bg">
<div class="bg-wrap">
<div class="bg"></div>
</div>
<div class="e-container">
<h1>This works well enought</h1>
</div>
</div>
<div class="black"></div>
--------------------- EDIT ---------------------
The code posted was missing the background wrapper that allows the background to not change size and maintain the fixed position. Sorry to post the wrong code this morning guys! But here is the change.