make the webpage composition working on every window size css - css

i would like to put the button into the middle of the height of the page
i tried this :
top: 50%;
but it doesnot work i found here that have to push top margin
i found a code : margin-top: -300px;
can i use something like margin-top : -width:50% this would mean that the button will be in the middle , how can i code it ?
how to 100% center it , and then put the button under that so the firt will be 50% and the second 60% even if i resize the window

You can simply do this by positioning it absolute in its parent element and then you can center the button by translating it.
HTML
<body>
<div class="container">
<button>Centered button</button>
</div>
</body>
CSS
html, body {
height: 100%;
margin: 0;
}
.container {
position: relative;
height: 100%;
}
button {
position: absolute;
top: 50%; //50% from top
left: 50%; //50% from left
//translate it so 50% from the width and heigth of the button will be subtracted thus centering the button
transform: translate(-50%, -50%);
}
Working js fiddle here. Don't forget to add vendor prefixes!
Another option would be flexbox. You can find more information about flexbox centering here.

Related

CSS Item in center of page pushed up when virtual keyboard is visible

I have the following style for a div which is positioned in the center of the page
<div class="myStyle">Hi All...</div>
CSS Code:
<style>
.myStyle {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
when the virtual keyboard though opens up, the div is being pushed up. I don't understand why though. Any suggestions ?
This is because your viewport (or the element's closest relative positioned parent) is shrinking vertically. If you don't want it to move, you'll have to make sure the closest parent with a relative position has a fixed height.
Here's an example, the second div will move based on the height of the window (open the snippet Full Screen with Dev Tools opened)
.row {
width: 100vw;
}
.col {
width: 50%;
float: left;
position: relative;
background: #eee;
}
.col > div {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
width: 100%;
background: #0095ee;
color: #fff;
}
<div class="row">
<div class="col" style="height: 200px;"><div>My Y center is always 100px;</div></div>
<div class="col" style="height: 100vh;"><div>My Y xenter varies with Devtools/etc.</div></div>
</div>
It will adjust to top 50% from viewport. Try to fix some height to that div and give css like below. Odd effect will be reduced. For eg if height of the popup is 400px and width is 600px(Width you can give in percentage as well)
.div {
width:600px;
height:400px;
position:fixed;
margin : auto;
top:0;
bottom:0;
left:0;
right:0;
}
But if you do not want to push div at all. Then you have to mention top in pixels. To make it work in all devices, you might have to use javascript for that. Like in javascript get the height of viewport, calculate the top you want specify based on viewport height. Apply calculated top in pixels.
For eg if top you want to specify as 50%. Then pseudo javascript code is below.
var divHeight = document.height;
var desiredTop = viewportHeight/2;
div.style.top = desiredTop;
But disadvantage of giving fixed top is there will be chance that div gets hidden behind the keyboard.

Ionic Modal bottom spacing

I can't manage to set a bottom spacing between my modal and the screen's bottom limit! In my CSS, I've set the "ion-modal-view" elements CSS height and width to some percentage and gave values to left, right, top and bottom accordingly, everything works as expected, my modal is centered, but I can't detach it from the bottom.
Any help?
Finally, I've managed to get it working by wrapping my ion-modal-view with a div that applies a min-height: 0; directive on the modal class, this made my custom margin effective.
<div class="adic-modal">
<ion-modal-view class="adic-popup">
<!--some content-->
</ion-modal-view>
</div>
This is the CSS:
.adic-modal .modal {
min-height: 0 !important;
}
.adic-popup {
width: 90%;
height: 60%;
top: 20%;
bottom: 20%;
right: 5%;
left: 5%;
background-color: #ffffff;
}
I need to know though if what I did is the right approach.

CSS transforms not pixel perfect

I have the following example for an off-canvas menu: http://jsfiddle.net/pwghdvoh/
When you click the button in the top left of the blue header, it moves the main app view to reveal the hidden menu.
It does this using the following CSS:
.showSidebar .app
{
-webkit-transform: translateZ(-20px)
translateX(240px);
transform: translateZ(-20px)
translateX(240px);
}
However I'm finding that on various resolutions that the app is not moved 240px to the right and 20px offset from the the top and bottom... If you look at the screenshot, you can see that it's too close to the top and bottom of the screen, it should have 20px at the top and bottom.
Could this be caused by the perspective of the wrapper being incorrect?
I do this dynamically using jQuery:
$('.wrapper').css({
'perspective': $(window).width(),
'-webkit-perspective': $(window).width()
});
So it's always the perspective of the viewport width. But this doesn't seem to fix the issue.
Any ideas?
Instead of giving width: 100% and height: 100% to the .wrapper class, I added position absolute and stretched it to its parent container which is body element. and when the side bar is viewed, I gave the top and bottom properties as 20px which overrides the already provided 0px value.
.wrapper {
position: absolute;
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
}
.wrapper.showSidebar {
top: 20px;
bottom: 20px;
}
Working Fiddle

Center content with twitter bootstrap

I am creating a login screen where there is a login box that I'd like to appear in the center of the screen (horizontally and vertically) no matter what resolution the user has.
I have looked around and can only find tutorials/articles that center content horizontally, which is half of what I want.
Any idea how I can achieve centralisation in both planes?
The best approach is using CSS and a Javascript callback for older IE versions.
CSS
.center {
width: 300px; // your login div width
height: 300px; // your login box height
position: absolute;
left: 50%;
top: 50%;
margin-left: -150px; // width/2
margin-top: -150px; // height/2
}
HTML
<div class="center">
Hey dude, I'm in the middle!
</div>
Live example
http://jsfiddle.net/86Asb/
Negative margins are exactly half the height and width, which pull the element back into perfect center. Only works with elements of a fixed height/width. This will work in all browsers except IE6,IE7 maybe.
The only way I know to vertically center an item is using javascript. Here is a simple example of how to do it using jquery and absolute positioning through CSS. Link to a jsfiddle and code to follow.
http://jsfiddle.net/AlienHoboken/XCPGe
Javascript:
$(document).ready(function() {
var width = $('#test').css('width');
var height = $('#test').css('height');
width = width.replace('px', '');
height = height.replace('px', '');
$('#test').css('left', ($(window).width()/2) - (width/2));
$('#test').css('top', ($(window).height()/2) - (height/2));
});
CSS:
#test {
position: absolute;
width: 50px;
height: 50px;
background: #000000;
}
I'm using the following solution (no fixed width of dialog), keeps it centered horizontally.
#test {
position: absolute;
max-width: 300px;
left: 1%;
right: 1%;
}
Any feedback / disadvantages appreciated!

How do I centre absolutely positioned content of unknown width?

Before someone asks me why the hell I would want to do this let me come straight out and tell you. That way one of you clever peeps out there can tell me a far better way...
I'm making a viewer for paintings which should stretch to fill the page, well 90% of the height of the screen to be precise. I want to fade the paintings in one over the other and want to center each of them in the middle of the screen.
To fade the paintings in over each other I need to position them 'absolute' to stop them from stacking. Here's where the trouble comes. Ever since I've set them to absolute, every method I use to center the containing div hasn't worked.
Part of the problem is that I'm not setting any width for the paintings as I want them to dynamically size themselves to fill 90% of the user's screen.
I've found a hundreds of methods for centering absolute content and believe I might need to shrink wrap the containing div. However I've not had any success as of yet.
HTML-
<div id="viewer_div">
<img src="" id="first" />
<img id="second" class="hidden"/>
</div>
Style Sheet
#viewer_div {
width:1264px;
}
img {
height:90%;
display:block;
margin-left: auto;
margin-right:auto;
}
The above gives me the desired effect, but doesn't allow me to position the images absolute. Can anyone suggest a way of centering the images but also allows me to fade one over the other?
Pushing the element left by 50% of its width and then translating it horizontally by 50% has worked for me.
.element {
position: absolute;
left: 50%;
transform: translateX(-50%);
}
I found the concept in the following link, and then I translated to fit my horizontal align needs: https://gist.github.com/colintoh/62c78414443e758c9991#file-douchebag-vertical-align-css
Either use JavaScript to calculate the width and move it,
use a CSS hack to move the element right and left by 50%,
or don't absolutely position it.
This answer is incredibly short, but it is to the point. If you require something to be centralised (meaning you would like the browser to determine where the centre point is, and position it there), then you can't use absolute positioning - because that takes away control from the browser.
To fade the paintings in over each other I need to position them
'absolute' to stop them from stacking.
This is where your problem lies. You have assumed that you need absolute positioning for the wrong reason.
If you are experiencing problems placing elements on top of each other, wrap the images in an absolutely positioned container which is 100% width and has text-align: center
If you do feel that absolute positioning is necessary, the following hack can be used to achieve your desired results:
div {
position: absolute;
/* move the element half way across the screen */
left: 50%;
/* allow the width to be calculated dynamically */
width: auto;
/* then move the element back again using a transform */
transform: translateX(-50%);
}
Obviously the above hack has a terrible code smell, but it works on some browsers. Be aware: this hack is not necessarily obvious to other developers, or other browsers (especially IE or mobile).
To go off of Samec's answer, you can also use this to center an absolute position element vertically and horizontally of unknown dimensions:
#viewer_div {
position: relative;
}
#viewer_div img {
position: absolute;
left: 50%;
top: 0%;
transform: translate(-50%, 50%);
}
Centering div with position: absolute and width: unknown:
HTML:
<div class="center">
<span></span>
<div>
content...
</div>
</div>
CSS:
.center{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: grid;
grid-template-columns: auto auto;
/* if you need the overflow */
overflow: auto;
}
You can use this solution even if the content is wider than the screen width, and don't need to transform: translate (which can blur elements)
How it works:
Grid will insert a span before the first auto, and insert a div right before the second auto, and you get:
span(0px) - auto - div(...px) - auto
auto will be equal to each other.
Same for vertical centering, but write grid-template-rows instead of a grid-template-columns
2021
A modern approach to absolutely centered content with grid and inset
The inset CSS property is a shorthand that corresponds to the top, right, bottom, and/or left. MDN
#viewer_div {
display: grid;
place-items: center;
position: absolute;
inset: auto 0;
}
*,
::after,
::before {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
body {
width: 100vw;
height: 100vh;
background-color: hsl(201, 27%, 10%);
position: relative;
}
#viewer_div {
display: grid;
place-items: center;
position: absolute;
inset: auto 0;
background-color: hsl(197, 7%, 21%);
color: white;
}
<div id="viewer_div">
<h1>Title 2021</h1>
<img src="https://via.placeholder.com/180x100.png/09f/fff" id="first" />
</div>

Resources