center alignment varies from deskop to mobile - html

I am using the following code to center my div and center the text in my div. This works perfect in my desktops browsers but is spaced bad on ipad and iphones.
Jsfiddle : https://jsfiddle.net/ps7t1rnu/
css
.center-box {
position: absolute;
background: #ccc;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 75%;
height: 75%;
padding: 20px;
text-align: center;
box-shadow: 0 0 30px rgba(0, 0, 0, 0.2);
border-radius: 25px;
}
.cb-text-center {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
html
<div class="center-box">
<div class="cb-text-center"> FOOBAR
BAR BAR
</div>
</div>
The div is centered correctly (as visible on jsfiddle on browser - but on iPhone or iPad it move away from the screen with some weird spacing..
seem image:
You can see the issue when you look at the jsfiddle full screen result - https://jsfiddle.net/ps7t1rnu/embedded/result/ on browser and iPad/iPhone.
thanks for the help..

iOS Safari until and including iOS 8.4, as well as Android browser up until and including Android 4.4 require prefixing the transform property.
-webkit-transform: translate(-50%, -50%);
http://caniuse.com/#search=transform

try this
<meta name="viewport" content="initial-scale=1.0; maximum-scale=1.0; width=device-width;">

Related

Chrome v60 rendering bug?

I've come across an issue in the latest version of Chrome (Version 60.0.3112.78) when having a child element of an absolutely positioned parent element being positioned fixed (along with some other CSS attributes).
Here's a CodePen to demonstrate the behaviour: https://codepen.io/anon/pen/GvJeGL
<div class="outer">
<div class="inner"></div>
</div>
.outer {
position: absolute;
left: calc(50% - 100px);
top: calc(50% - 100px);
z-index: 1; /* auto fixes rendering */
border-radius: 5px; /* 0 fixes rendering, other values give strange results */
overflow: hidden; /* visible fixes rendering */
width: 100px;
height: 100px;
}
.inner {
position: fixed;
width: 80%;
height: 75%;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
background: salmon;
}
This issuse appeared when I updated Chrome to major version 60 (it works in 59). Safari also seem to have issues.
If you play with the CSS values in the Codepen of the ".outer" element you will notice that the rendering behaviour changes. Interesting that border-radius has an effect.
Is this a bug or can anyone explain this behaviour?

Place image between 2 divs (centered on screen, responsive and not over text)

I'm trying to place an image between 2 divs on my page. I have currently been able to get an image between the two divs, but it isn't responsive (only in the correct position at 1920 width) and it overlaps the text of both divs:
screenshot from my website
css
.btwimg {
width: 90%;
height: auto;
position: absolute;
transform: translate3d(-20%, -50%, 0);
z-index: 1;
left: 50%;
background: url("../img/lara2.png");
background-repeat: no-repeat;
box-shadow: 0 5px 7px rgba(0,0,0,0.1);
}
html
<div class="btwimg">
<img src="img/lara2.png">
</div>
what I am trying to achieve
Is it possible to achieve what I'm after?
Thanks in advance.
First you have to add the same amount of padding-bottom to the upper DIV and padding-top to the subsequent DIV to create enogh space for your image. (trial and error to find the right amount)
Your btwing DIV should be a child element of the subsequent DIV. Then this CSS should work:
.btwimg {
width: 90%;
height: 250px /* Just a random guess - Needs a fixed height! */
position: absolute;
z-index: 1;
left: 50%;
top: -50%;
transform: translate(-50%, -50%);
background: url("../img/lara2.png");
background-repeat: no-repeat;
box-shadow: 0 5px 7px rgba(0,0,0,0.1);
}
Actually the height setting should be a calc value which is derived from the original width/height proportion and the 90% width you set, like height: calc(9/16 * 90%);if the proportion is 16/9
I took #Johannes answer and tweaked a little to get the result I wanted:
.btwimg {
max-width: 800px;
min-width: 300px;
height: 16vw;
position: absolute;
z-index: 1;
left: 50%;
top: calc(2vw - 38px); /* keeps div roughly centred at all target resolutions */
transform: translate(-50%, -50%);
}
I then used an image rather than a background to make the re-sizing easier.
.btwimg img {
height: auto;
width: 100%;
}
btwimg was put as a child of the 2nd div as recommended
result at mobile resolutions
result at desktop resolutions

fractional coordinates, in particular to vertically center an object, in chrome [duplicate]

I have a centered form on my page positioned using top and left values and css3 transformations.
<div class="middle">
<h1>This is blurry, or should be.</h1>
</div>
.middle {
position: absolute;
top: 50%;
left: 50%;
min-width: 390px;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
/** backface-visibility: hidden; **/
}
h1 {
padding-bottom: 5px;
border-bottom: 3px solid blue
}
Notice backface-visibility. When set to hidden, all problems are solved for me using chrome 42. It doesn't render blurry. For others however using the same chrome version, it renders blurry with it.
Here's what it looks like without BV: http://jsfiddle.net/mzws2fnp/
To you it may be blurry, to others it may not.
Here's what it looks like with BV: http://jsfiddle.net/mzws2fnp/2/
For some reason people see the border blurry however I do not. I know backface-visibility: hidden is meant to fix that, and it does for me, just not for others using the same browser as I. Strange.
Try -50.1%
transform: translateY(-50%) translateX(-50.1%);
EDIT:
I have found out, they are blurred when chrome dev tools are opened, try to close them and refresh
This is a bug in Google Chrome. I reported this issue to Google:
Rendering bug in css transform: it blurrs borders
<div class="middle">
<input type="text" />
</div>
.middle {
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translateY(-50%) translateX(-50%);
transform: translateY(-50%) translateX(-50%);
}
input {
border: 1px solid black;
border-radius: 4px;
}
var middle = document.querySelector('.middle');
setInterval(function(){
middle.style.paddingTop = middle.style.paddingTop === "0px" ? "1px" : "0px";
}, 1000);
Animated bug demonstration
When you use percentage, will play an odd number. will blurry borders,
using parseInt to assign the value is integer.
$(document).ready(function(){
$('.middle').css({
'top':parseInt($('.middle').position().top)+ 'px',
'left': parseInt($('.middle').position().left)+'px',
'transform':'none',
'-webkit-transform':'none'
});
});
.middle {
position: absolute;
top: 30%;
left: 50%;
min-width: 390px;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);}
h1 {
padding-bottom: 5px;
border-bottom: 4px solid blue}
.middle2 {
position: absolute;
top: 70%;
left: 50%;
min-width: 390px;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);}
h1 {
padding-bottom: 5px;
border-bottom: 4px solid blue}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="middle">
<h1>This is blurry, or should be.</h1>
</div>
<div class="middle2">
<h1>This is blurry, or should be.</h1>
</div>
In this specific case where you're using a solid border, you can try using a box-shadow instead of a border as a workaround. For example, replace: border-bottom: 3px solid blue; with box-shadow: 0px 3px 0px blue;
Use even number (2px or 4px) for the border. Odd number (3px or 5px) is giving that blur.
border-bottom: 4px solid blue;
there is little hack that can help to get any block as center middle.
in parent <div> where we add position: relative add below properties,
display: flex;
justify-content: center;
now add align-self: center; property with the block which we want to make center middle make sure that this block is absolute position.
Because translated element height is odd number. This will not occur when element height is even number.
This problem occurs when we add
transform: translateY(-50%) translateX(-50%);
OR
transform: translate(-50%, -50%);
it is still as an open issue in chromium bugs list.

Nivo Slider custom height/width problems

I have searched for a resolution to my problem, but have not yet been successful.
I have images of different sizes in Nivo Slider, but I need to create a viewport that displays the image centered in a div. It's hard to explain, but I have included a diagram below.
The image must be centered in a div, while the div must also be responsive. I don't want the div to change its size and would like the image to create an overflow that is hidden on the div.
I have tried different methods of CSS and HTML, but neither are my greatest strengths.
If I understand correctly what you want to achieve is something like this (uncommenting /*overflow: hidden;*/): DEMO
HTML:
<div>
<img src="http://i.imgur.com/cjgKmvp.jpg"/>
</div>
CSS:
div{
position: relative;
margin: 100px auto;
width: 400px;
height: 300px;
border: 3px solid red;
/*overflow: hidden;*/
}
img {
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translateX(-50%) translateY(-50%);
-ms-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
min-width: 100%;
min-height: 100%;
width: auto;
height: auto;
z-index: -1;
}
Note: I comment overflow: hidden; so you can see how the image is positioned.

Trying to get vertical text to bottom align

The layout I'm trying to achieve is shown in this image:
The HTML below is one of many attempts which haven't worked. The project this is for is targeting HTML5 in the latest browsers only, so there is no need for it to work in anything except the latest Chrome, Safari, Firefox and (with a following wind) IE9 beta.
<!DOCTYPE html>
<html>
<body>
<div style="border: solid 1px red; width:600px; height: 600px">
<span style="-webkit-transform:rotate(-
90deg);display:block;position:absolute;bottom:600px">My Vertical Text</span>
<img src="http://www.gizmodo.com/assets/resources/2007/01/Bill-gates-mugshot.jpg"
style="position:absolute;bottom:600px" />
</div>
</body>
</html>
I suppose you might want something like this: http://jsfiddle.net/aNscn/3/
bottom: 600px is going to get you nowhere - that's just going to put the elements 600px away from the bottom of the user's screen. Instead, give the outer div a position: relative and let the two elements align to it's bottom with a suitably low bottom value. Also check out the transform-origin property to get the positioning of the span correct after rotation.
#outer {
border: solid 1px red;
width:600px;
height: 600px;
position: relative;
}
#text {
-webkit-transform: rotate(-90deg);
-webkit-transform-origin: left top;
-moz-transform: rotate(-90deg);
-moz-transform-origin: left top;
-o-transform: rotate(-90deg);
-o-transform-origin: left top;
transform: rotate(-90deg);
transform-origin: left top;
position: absolute;
bottom: 0;
left: 5px;
}
#img {
position:absolute;
bottom: 15px;
left: 30px;
}