Position relative div overlaps position fixed div [duplicate] - javascript

So I have this markup and inside it there is <div class="mask"></div> which sets the blue overlay ontop of the image.
If I don't make the .container position:relative, the title text gets hidden behind the blue layer... Almost as if it's usage is mimicking z-index
Why is this the case?
Pen: https://codepen.io/anon/pen/OBbbZB
body {
margin: 0;
font-family: arial;
}
section {
position: relative;
background: url(https://preview.webpixels.io/boomerang-v3.6.1/assets/images/backgrounds/slider/img-41.jpg)
no-repeat left center/cover;
height: 70vh;
display: flex;
justify-content: center;
}
.container {
position: relative;
width: 100%;
max-width: 1280px;
display: flex;
justify-content: center;
align-items: center;
color: white;
}
.mask {
position: absolute;
width: 100%;
height: 100%;
background: #3452ff;
opacity: 0.7;
}
<section>
<div class="mask"></div>
<div class="container">
<h1>Hello World</h1>
</div>
</section>

You need to refer to the specification and more precisely the painting order to understand when each layer is painted.
Without position:relative your element is not positioned and will be painted at the step (4):
For all its in-flow, non-positioned, block-level descendants in tree
order: If the element is a block, list-item, or other block
equivalent:
Then we paint the positioned elements (including the .mask) at the step (8)
All positioned, opacity or transform descendants, in tree order that fall into the following categories
Now when you add position:relative you make the container also positioned thus it will fall in the step (8) too and as described there we consider the tree order since both don't have any z-index specified. So the .container will painted later in this case.
If you change the order of the element (you make the container before the mask) you will notice that position:relative won't have any effect because in both cases the painting order will be the same:
body {
margin: 0;
font-family: arial;
}
section {
position: relative;
background: url(https://preview.webpixels.io/boomerang-v3.6.1/assets/images/backgrounds/slider/img-41.jpg)
no-repeat left center/cover;
height: 70vh;
display: flex;
justify-content: center;
}
.container {
position: relative; /* you can remove this*/
width: 100%;
max-width: 1280px;
display: flex;
justify-content: center;
align-items: center;
color: white;
}
.mask {
position: absolute;
width: 100%;
height: 100%;
background: #3452ff;
opacity: 0.7;
}
<section>
<div class="container">
<h1>Hello World</h1>
</div>
<div class="mask"></div>
</section>
If we check the step (8) it also said opacity or transform which means that if you also change the opacity of the container or add a transform, the order will change too.
body {
margin: 0;
font-family: arial;
}
section {
position: relative;
background: url(https://preview.webpixels.io/boomerang-v3.6.1/assets/images/backgrounds/slider/img-41.jpg)
no-repeat left center/cover;
height: 70vh;
display: flex;
justify-content: center;
}
.container {
transform:translate(0); /*added this*/
width: 100%;
max-width: 1280px;
display: flex;
justify-content: center;
align-items: center;
color: white;
}
.mask {
position: absolute;
width: 100%;
height: 100%;
background: #3452ff;
opacity: 0.7;
}
<section>
<div class="mask"></div>
<div class="container">
<h1>Hello World</h1>
</div>
</section>
It's also trivial to notice that if you add z-index (either negative or positive) you will also affect the painting order and in this case the tree order will have no effect.
Stacking contexts formed by positioned descendants with negative z-indices (excluding 0) in z-index order (most negative first) then tree order
....
Stacking contexts formed by positioned descendants with z-indices greater than or equal to 1 in z-index order (smallest first) then tree order.
We paint the element with negative z-index at (3) and the positive ones at (9) and between those steps we have all the cases where z-index is not involved like described initially.

This is a fascinating question. It appears that when the .mask div is made absolute and taken out of the document flow, it's the positions that are affected, but the stacking order of the elements is still in place. So an element placed before the absolute div appears under the div, and an element placed after the absolute div is stacked after.
This isn't really an answer, I just wanted to demo what I looked at:
body {
margin: 0;
font-family: arial;
}
section {
position: relative;
background: url(https://preview.webpixels.io/boomerang-v3.6.1/assets/images/backgrounds/slider/img-41.jpg) no-repeat left center/cover;
height: 70vh;
display: flex;
justify-content: center;
}
.container0 {
width: 100%;
max-width: 1280px;
display: flex;
justify-content: center;
align-items: center;
color: white;
}
.container {
position: relative;
width: 100%;
max-width: 1280px;
display: flex;
justify-content: center;
align-items: center;
color: white;
}
.mask {
position: absolute;
width: 100%;
height: 100%;
background: #3452ff;
opacity: 0.7;
}
<section>
<div class="container0">
<h1>Another Hello World</h1>
</div>
<div class="mask"></div>
<div class="container">
<h1>Hello World</h1>
</div>
</section>

Related

How to delete gap when one element has no content and has height 0?

.parent {
box-sizing: border-box;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
gap: 16px;
background: green;
}
.main {
flex-grow: 2;
background: red;
}
.header {
background: blue;
}
.big {
height: 500px;
width: 60px;
}
<div class="big">
<div class="parent">
<div class="header"></div>
<div class="main"></div>
</div>
</div>
I created a template which is divided into a header and a main content. The header is optional and has an automatic height adjustment according to the content. The rest of the height of the parent has to be filled by the main element.
I tried to do this with display flex and gap. But when the first element is not there the gap still remains. The gap must be 16px when there are two elements. Is it somehow possible to remove gap with styles when there is one element or is there any other way to manage the height of the elements so that the main element always occupies the remaining space?
The gap property is applied to the container. You can not set a specific gap for the children div, because it is a property of its parent.
What I would advise is to set your empty div, with the display:none property. And once you inject content to it with javascript, set its display to "block", "flex", or whatever display property it is using when visible.
Another solution would be to replace "gap" with margin or padding bottom on the children. And since this is a property of the children components, you will be able to make individual exceptions.
.parent {
box-sizing: border-box;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
// gap: 16px;
background: green;
}
.main {
flex-grow: 2;
background: red;
}
.header {
background: blue;
margin-bottom: 16px;
}
.big {
height: 500px;
width: 60px;
}
<div class="big">
<div class="parent">
<!-- <div class="header"></div> -->
<div class="main"></div>
</div>
</div>
You can use
.header {
background: blue;
margin-bottom: 16px;
}
instead of gap: 16px;

Hi, I was stuck on centering the flex items [duplicate]

I want to center a div vertically with CSS. I don't want tables or JavaScript, but only pure CSS. I found some solutions, but all of them are missing Internet Explorer 6 support.
<body>
<div>Div to be aligned vertically</div>
</body>
How can I center a div vertically in all major browsers, including Internet Explorer 6?
Below is the best all-around solution I could build to vertically and horizontally center a fixed-width, flexible height content box. It was tested and worked for recent versions of Firefox, Opera, Chrome, and Safari.
.outer {
display: table;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.inner {
margin-left: auto;
margin-right: auto;
width: 400px;
/* Whatever width you want */
}
<div class="outer">
<div class="middle">
<div class="inner">
<h1>The Content</h1>
<p>Once upon a midnight dreary...</p>
</div>
</div>
</div>
View A Working Example With Dynamic Content
I built in some dynamic content to test the flexibility and would love to know if anyone sees any problems with it. It should work well for centered overlays also -- lightbox, pop-up, etc.
The simplest way would be the following three lines of CSS:
1) position: relative;
2) top: 50%;
3) transform: translateY(-50%);
Following is an example:
div.outer-div {
height: 170px;
width: 300px;
background-color: lightgray;
}
div.middle-div {
position: relative;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
<div class='outer-div'>
<div class='middle-div'>
Test text
</div>
</div>
One more I can't see on the list:
.Center-Container {
position: relative;
height: 100%;
}
.Absolute-Center {
width: 50%;
height: 50%;
overflow: auto;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
border: solid black;
}
Cross-browser (including Internet Explorer 8 - Internet Explorer 10 without hacks!)
Responsive with percentages and min-/max-
Centered regardless of padding (without box-sizing!)
height must be declared (see Variable Height)
Recommended setting overflow: auto to prevent content spillover (see Overflow)
Source: Absolute Horizontal And Vertical Centering In CSS
Now the Flexbox solution is a very easy way for modern browsers, so I recommend this for you:
.container {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
background: green;
}
body,
html {
height: 100%;
}
<div class="container">
<div>Div to be aligned vertically</div>
</div>
Actually, you need two div's for vertical centering. The div containing the content must have a width and height.
#container {
position: absolute;
top: 50%;
margin-top: -200px;
/* Half of #content height */
left: 0;
width: 100%;
}
#content {
width: 624px;
margin-left: auto;
margin-right: auto;
height: 395px;
border: 1px solid #000000;
}
<div id="container">
<div id="content">
<h1>Centered div</h1>
</div>
</div>
Here is the result.
Edit 2020: only use this if you need to support old browsers like Internet Explorer 8 (which you should refuse to do 😉). If not, use Flexbox.
This is the simplest method I found and I use it all the time
(jsFiddle demo here).
Thank Chris Coyier from CSS Tricks for this article.
html, body{
height: 100%;
margin: 0;
}
.v-wrap{
height: 100%;
white-space: nowrap;
text-align: center;
}
.v-wrap:before{
content: "";
display: inline-block;
vertical-align: middle;
width: 0;
/* adjust for white space between pseudo element and next sibling */
margin-right: -.25em;
/* stretch line height */
height: 100%;
}
.v-box{
display: inline-block;
vertical-align: middle;
white-space: normal;
}
<div class="v-wrap">
<article class="v-box">
<p>This is how I've been doing it for some time</p>
</article>
</div>
Support starts with Internet Explorer 8.
After a lot of research I finally found the ultimate solution. It works even for floated elements. View Source
.element {
position: relative;
top: 50%;
transform: translateY(-50%); /* or try 50% */
}
Use the CSS Flexbox align-items property to achieve this.
html, body {
height: 100%;
}
body {
display: flex;
align-items: center;
}
<div>This is centered vertically</div>
To center the div on a page, check the fiddle link.
#vh {
margin: auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
.box{
border-radius: 15px;
box-shadow: 0 0 8px rgba(0, 0, 0, 0.4);
padding: 25px;
width: 100px;
height: 100px;
background: white;
}
<div id="vh" class="box">Div to be aligned vertically</div>
Another option is to use flex box, check the fiddle link.
.vh {
background-color: #ddd;
height: 400px;
align-items: center;
display: flex;
}
.vh > div {
width: 100%;
text-align: center;
vertical-align: middle;
}
<div class="vh">
<div>Div to be aligned vertically</div>
</div>
Another option is to use a CSS 3 transform:
#vh {
position: absolute;
top: 50%;
left: 50%;
/*transform: translateX(-50%) translateY(-50%);*/
transform: translate(-50%, -50%);
}
.box{
border-radius: 15px;
box-shadow: 0 0 8px rgba(0, 0, 0, 0.4);
padding: 25px;
width: 100px;
height: 100px;
background: white;
}
<div id="vh" class="box">Div to be aligned vertically</div>
The easiest solution is below:
.outer-div{
width: 100%;
height: 200px;
display: flex;
border:1px solid #000;
}
.inner-div{
margin: auto;
text-align: center;
border: 1px solid red;
}
<div class="outer-div">
<div class="inner-div">
Hey there!
</div>
</div>
There are multiple ways to achieve this.
Using flex property of CSS.
Solution #1
.parent {
width: 400px;
height:200px;
background: blue;
display: flex;
align-items: center;
justify-content:center;
}
.child {
width: 75px;
height: 75px;
background: yellow;
}
<div class="parent">
<div class="child"></div>
</div>
or by using display: flex; and margin: auto;
Solution #2
.parent {
width: 400px;
height:200px;
background: blue;
display: flex;
}
.child {
width: 75px;
height: 75px;
background: yellow;
margin:auto;
}
<div class="parent">
<div class="child"></div>
</div>
show text center
Solution #3
.parent {
width: 400px;
height: 200px;
background: yellow;
display: flex;
align-items: center;
justify-content:center;
}
<div class="parent">Center</div>
Using percentage(%) height and width.
Solution #4
.parent {
position: absolute;
height:100%;
width:100%;
background: blue;
display: flex;
align-items: center;
justify-content:center;
}
.child {
width: 75px;
height: 75px;
background: yellow;
}
<div class="parent">
<div class="child"></div>
</div>
Unfortunately — but not surprisingly — the solution is more complicated than one would wish it to be. Also unfortunately, you'll need to use additional divs around the div you want vertically centered.
For standards-compliant browsers like Mozilla, Opera, Safari, etc. you need to set the outer div to be displayed as a table and the inner div to be displayed as a table-cell — which can then be vertically centered. For Internet Explorer, you need to position the inner div absolutely within the outer div and then specify the top as 50%. The following pages explain this technique well and provide some code samples too:
Vertical Centering in CSS
Vertical Centering in CSS with Unknown Height (Internet Explorer 7 compatible) (Archived article courtesy of the Wayback Machine)
There is also a technique to do the vertical centering using JavaScript. Vertical alignment of content with JavaScript & CSS demonstrates it.
If someone cares for Internet Explorer 10 (and later) only, use Flexbox:
.parent {
width: 500px;
height: 500px;
background: yellow;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
}
.centered {
width: 100px;
height: 100px;
background: blue;
}
<div class="parent">
<div class="centered"></div>
</div>
Flexbox support: http://caniuse.com/flexbox
A modern way to center an element vertically would be to use flexbox.
You need a parent to decide the height and a child to center.
The example below will center a div to the center within your browser. What's important (in my example) is to set height: 100% to body and html and then min-height: 100% to your container.
body, html {
background: #F5F5F5;
box-sizing: border-box;
height: 100%;
margin: 0;
}
#center_container {
align-items: center;
display: flex;
min-height: 100%;
}
#center {
background: white;
margin: 0 auto;
padding: 10px;
text-align: center;
width: 200px;
}
<div id='center_container'>
<div id='center'>I am center.</div>
</div>
.center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%); /* (x, y) => position */
-ms-transform: translate(-50%, -50%); /* IE 9 */
-webkit-transform: translate(-50%, -50%); /* Chrome, Safari, Opera */
}
.vertical {
position: absolute;
top: 50%;
//left: 0;
transform: translate(0, -50%); /* (x, y) => position */
}
.horizontal {
position: absolute;
//top: 0;
left: 50%;
transform: translate(-50%, 0); /* (x, y) => position */
}
div {
padding: 1em;
background-color: grey;
color: white;
}
<body>
<div class="vertical">Vertically left</div>
<div class="horizontal">Horizontal top</div>
<div class="center">Vertically Horizontal</div>
</body>
Related: Center a Image
Centering only vertically
If you don't care about Internet Explorer 6 and 7, you can use a technique that involves two containers.
The outer container:
should have display: table;
The inner container:
should have display: table-cell;
should have vertical-align: middle;
The content box:
should have display: inline-block;
You can add any content you want to the content box without caring about its width or height!
Demo:
body {
margin: 0;
}
.outer-container {
position: absolute;
display: table;
width: 100%; /* This could be ANY width */
height: 100%; /* This could be ANY height */
background: #ccc;
}
.inner-container {
display: table-cell;
vertical-align: middle;
}
.centered-content {
display: inline-block;
background: #fff;
padding: 20px;
border: 1px solid #000;
}
<div class="outer-container">
<div class="inner-container">
<div class="centered-content">
Malcolm in the Middle
</div>
</div>
</div>
See also this Fiddle!
Centering horizontally and vertically
If you want to center both horizontally and vertically, you also need the following.
The inner container:
should have text-align: center;
The content box:
should re-adjust the horizontal text-alignment to for example text-align: left; or text-align: right;, unless you want text to be centered
Demo:
body {
margin: 0;
}
.outer-container {
position: absolute;
display: table;
width: 100%; /* This could be ANY width */
height: 100%; /* This could be ANY height */
background: #ccc;
}
.inner-container {
display: table-cell;
vertical-align: middle;
text-align: center;
}
.centered-content {
display: inline-block;
text-align: left;
background: #fff;
padding: 20px;
border: 1px solid #000;
}
<div class="outer-container">
<div class="inner-container">
<div class="centered-content">
Malcolm in the Middle
</div>
</div>
</div>
See also this Fiddle!
It can be done in two ways
body{
left: 50%;
top:50%;
transform: translate(-50%, -50%);
height: 100%;
width: 100%;
}
OR
Using flex
body {
height:100%
width:100%
display: flex;
justify-content: center;
align-items: center;
}
align-items:center; makes the content vertically center
justify-content: center;makes the content horizontally center
This is always where I go when I have to come back to this issue.
For those who don't want to make the jump:
Specify the parent container as position:relative or position:absolute.
Specify a fixed height on the child container.
Set position:absolute and top:50% on the child container to move the top down to the middle of the parent.
Set margin-top:-yy where yy is half the height of the child container to offset the item up.
An example of this in code:
<style type="text/css">
#myoutercontainer {position:relative}
#myinnercontainer {position:absolute; top:50%; height:10em; margin-top:-5em}
</style>
...
<div id="myoutercontainer">
<div id="myinnercontainer">
<p>Hey look! I'm vertically centered!</p>
<p>How sweet is this?!</p>
</div>
</div>
I just wrote this CSS and to know more, please go through: This article with vertical align anything with just 3 lines of CSS.
.element {
position: relative;
top: 50%;
transform: perspective(1px) translateY(-50%);
}
For newcomers, please try:
display: flex;
align-items: center;
justify-content: center;
The three lines of code using transform works practically on modern browsers and Internet Explorer:
.element{
position: relative;
top: 50%;
transform: translateY(-50%);
-moz-transform: translateY(-50%);
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
}
I am adding this answer since I found some incompleteness in the previous version of this answer (and Stack Overflow won't allow me to simply comment).
'position' relative messes up the styling if the current div is in the body and has no container div. However 'fixed' seems to work, but it obviously fixes the content in the center of the viewport
Also I used this styling for centering some overlay divs and found that in Mozilla all elements inside this transformed div had lost their bottom borders. Possibly a rendering issue. But adding just the minimal padding to some of them rendered it correctly. Chrome and Internet Explorer (surprisingly) rendered the boxes without any need for padding
CSS Grid
body, html { margin: 0; }
body {
display: grid;
min-height: 100vh;
align-items: center;
}
<div>Div to be aligned vertically</div>
.center{
display: grid;
place-items: center;
}
The answer from Billbad only works with a fixed width of the .inner div.
This solution works for a dynamic width by adding the attribute text-align: center to the .outer div.
.outer {
position: absolute;
display: table;
width: 100%;
height: 100%;
text-align: center;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.inner {
text-align: center;
display: inline-block;
width: auto;
}
<div class="outer">
<div class="middle">
<div class="inner">
Content
</div>
</div>
</div>
Just do it: Add the class at your div:
.modal {
margin: auto;
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
height: 240px;
}
And read this article for an explanation. Note: Height is necessary.
I did it with this (change width, height, margin-top and margin-left accordingly):
.wrapper {
width: 960px;
height: 590px;
position: absolute;
top: 50%;
left: 50%;
margin-top: -295px;
margin-left: -480px;
}
<div class="wrapper"> -- Content -- </div>
Not answering for browser compatibility but to also mention the new Grid and the not so new Flexbox feature.
Grid
From: Mozilla - Grid Documentation - Align Div Vertically
Browser Support: Grid Browser Support
CSS:
.wrapper {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 10px;
grid-auto-rows: 200px;
grid-template-areas:
". a a ."
". a a .";
}
.item1 {
grid-area: a;
align-self: center;
justify-self: center;
}
HTML:
<div class="wrapper">
<div class="item1">Item 1</div>
</div>
Flexbox
Browser Support: Flexbox Browser Support
CSS:
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
align-items: center;
justify-content: center;
I think a solid solution for all browsers without using Flexbox - "align-items: center;" is a combination of display: table and vertical-align: middle;.
CSS
.vertically-center
{
display: table;
width: 100%; /* Optional */
height: 100%; /* Optional */
}
.vertically-center > div
{
display: table-cell;
vertical-align: middle;
}
HTML
<div class="vertically-center">
<div>
<div style="border: 1px solid black;">some text</div>
</div>
</div>
‣Demo: https://jsfiddle.net/6m640rpp/
Especially for parent divs with relative (unknown) height, the centering in the unknown solution works great for me. There are some really nice code examples in the article.
It was tested in Chrome, Firefox, Opera, and Internet Explorer.
/* This parent can be any width and height */
.block {
text-align: center;
}
/* The ghost, nudged to maintain perfect centering */
.block:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em; /* Adjusts for spacing */
}
/* The element to be centered, can
also be of any width and height */
.centered {
display: inline-block;
vertical-align: middle;
width: 300px;
}
<div style="width: 400px; height: 200px;">
<div class="block" style="height: 90%; width: 100%">
<div class="centered">
<h1>Some text</h1>
<p>Any other text..."</p>
</div>
</div>
</div>
There is a trick I found out recently: You need to use top 50%, and then you do a translateY(-50%).
.outer-div {
position: relative;
height: 150px;
width: 150px;
background-color: red;
}
.centered-div {
position: absolute;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
background-color: white;
}
<div class='outer-div'>
<div class='centered-div'>
Test text
</div>
</div>

How can I create these images/col?

I'm new, and I want to learn how can I create these images to be in the center of the page and I want to be a very small gap between them? May you please help me? I was searching on the internet about how can I create this, but unfortunately I wasn't very lucky. What's the code for this container, row, or column?
Image
Is this what you want? You wrap the two images inside a div container and apply flex property to it. Then use a padding of 5px on each image to get that little space between. Also I have used a custom height to the container, you can adjust it according to your need.
.container {
display: flex;
height: 100vh;
width: 100vw;
align-items: center;
justify-content: center;
}
.container > div {
padding: 0px 5px;
max-width: 90%;
height: 325px;
}
.container div img {
width: 100%;
height: 100%;
object-fit: cover;
}
#media screen and (max-width: 780px) {
.container {
flex-direction: column;
}
.container > div {
padding: 10px 5px;
}
}
<div class="container">
<div><img src="https://i.picsum.photos/id/1/5616/3744.jpg?hmac=kKHwwU8s46oNettHKwJ24qOlIAsWN9d2TtsXDoCWWsQ"/></div>
<div><img src="https://i.picsum.photos/id/10/2500/1667.jpg?hmac=J04WWC_ebchx3WwzbM-Z4_KC_LeLBWr5LZMaAkWkF68"/></div>
</div>

How to prevent images from moving

I have this example where I wanted to demonstrate my problem: https://codepen.io/Kapaak/pen/oNBbgad
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.wrapper {
position: relative;
min-height: 100vh;
height: 100vh;
background-color: #01383a;
display: flex;
align-items: center;
justify-content: center;
color: #f5c1a0;
font-size: 3rem;
}
.wrapper h2 {
z-index: 1;
}
.wrapper .image {
width: 10rem;
position: absolute;
}
.wrapper .image.bottom-left {
bottom: 4rem;
left: 4rem;
}
.wrapper .image.top-right {
top: 4rem;
right: 4rem;
}
.wrapper .image img {
width: 100%;
}
<div class="wrapper">
<h2>Some random headline text</h2>
<div class="image top-right">
<img src="https://images.unsplash.com/photo-1611412570995-ed61b6e2f098?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYxNjY4NTk1Nw&ixlib=rb-1.2.1&q=85" alt="">
</div>
<div class="image bottom-left">
<img src="https://images.unsplash.com/photo-1611412570995-ed61b6e2f098?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYxNjY4NTk1Nw&ixlib=rb-1.2.1&q=85" alt="">
</div>
</div>
I am trying to make some page, where will be some interactive images, that will be positioned somewhere on the page, just like the cats in the codepen. The problem is that when you resize window, they will always retain the same position.
What I want is to give them lets say max-width:900 and when I have smaller width they wont move, they will just overflow the page and I want the headline to be centered, thats why I gave the images position:absolute
The reason your right image is moving when you resize the browser is because you made it absolute compared to the right edge with right: 4rem;. If you change that value to say left: 104rem; then it will always be positioned 104 rem from the left edge of the window, regardless of the window width.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.wrapper {
position: relative;
min-height: 100vh;
height: 100vh;
background-color: #01383a;
display: flex;
align-items: center;
justify-content: center;
color: #f5c1a0;
font-size: 3rem;
}
.wrapper h2 {
z-index: 1;
}
.wrapper .image {
width: 10rem;
position: absolute;
}
.wrapper .image.bottom-left {
bottom: 4rem;
left: 4rem;
}
.wrapper .image.top-right {
top: 4rem;
left: 104rem;
}
.wrapper .image img {
width: 100%;
}
<div class="wrapper">
<h2>Some random headline text</h2>
<div class="image top-right">
<img src="https://images.unsplash.com/photo-1611412570995-ed61b6e2f098?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYxNjY4NTk1Nw&ixlib=rb-1.2.1&q=85" alt="">
</div>
<div class="image bottom-left">
<img src="https://images.unsplash.com/photo-1611412570995-ed61b6e2f098?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTYxNjY4NTk1Nw&ixlib=rb-1.2.1&q=85" alt="">
</div>
</div>
Your best bet would be to either use Bootstrap columns (https://getbootstrap.com/), as these dynamically resize images/text in columns. Or you can use the following css to keep the same position.
#image {
width:100%;
height:100%;
margin-left:50%;
margin-right:50%;
position:absolute;
}
If you need to resize images when the page size is decreased you can use media queries in CSS. These can also be used to change the properties of any element for any screen size.
if you want your title always be centered use
text-align: center;
on your h2 element

Centring video in viewport within a sticky footer layout

I'm using Phillip Walton's 'solved-by-flexbox' sticky footer solution for my site.
In addition to this, I have some video pages on which I want to centre the video embed (video-container) in the viewport (both horizontally and vertically)
Below is everything as it is set-up, and Here's a JSFiddle just for good measure.
.Site {
display: flex;
flex-direction: column;
height: 100vh;
/* 1, 3 */
}
.Site-content {
flex: 1 0 auto;
/* 2 */
padding: var(--space) var(--space) 0;
width: 100vw;
}
.Site-header,
.Site-footer {
flex: none;
/* 2 */
}
.Site-header {
position: absolute;
top: 0;
z-index: 600;
flex: none;
}
.video-container {
position: relative;
width: 70vw;
height: calc(70vw * 9 / 16);
margin-left: auto;
margin-right: auto;
}
.video-container iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
<body class="Site">
<div class="Site-header">This is a header</div>
<div class="Site-content">
<div class="video-container">
<iframe src="https://player.vimeo.com/video/100978843" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
</div>
<div class="Site-footer">This is the footer</div>
</body>
I've seen a few different options, but most of them rely on knowing the exact size of the div or object to be centred.
For example, this option:
.video-container
{
width: 70vw;
height: calc(70vw * 9 / 16);
margin:0 auto;
background:#f7f7f7;
position:absolute;
left:50%;
top:50%;
}
Doesn't work, my video-container element still sits at the top of the page.
I've seen another code example which uses Flexbox to centre:
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
but this a no-go for me as it interferes with not only my sticky footer set-up but also the rest of the site content (for obvious reasons).
Is there another way to do this?
edit: I'm not sure why I'm receiving down votes. I've tried to explain the scenario with steps I have taken, and I've now provided full and complete code in both a JS Fiddle and a Stack Snippet. If there is anything else or my question is not clear then please, by all means let me know and I can edit the question.
SO36651747
Centring video in viewport within a sticky footer layout
Changed every style except the iframe. Got rid of the CSS variables (they had no ::root so they were useless). Made .Site-content a flex container because flex containers only affect their children. The elements you wanted to center were the grand-children of .Site, therefore it could not control the centering of the video-container.
Changed the style of the video container so that it's height and width maintains a 9:16 aspect ratio.
See references below.
Changes are marked with 🖉
The red borders are there to show the distance between the header/footer/video.
PLUNKER
README.md
.Site {
display: flex;
flex-direction: column;
height: 100vh;
overflow: hidden; 🖉
align-items: center; 🖉
align-content: center; 🖉
}
.Site-content {
flex: 1 0 auto;
width: 100vw;
display: flex; 🖉
flex-flow: column nowrap; 🖉
justify-content: center; 🖉
align-items: center; 🖉
}
.Site-header,
.Site-footer {
flex: none;
position: absolute;
z-index: 600;
height: 10vh; 🖉
width: 100vw; 🖉
}
.Site-header {
top: 0;
border-bottom: 1px solid red; 🖉
}
.Site-footer {
bottom: 0; 🖉
border-top: 1px solid red; 🖉
}
.video-container {
position: relative;
width: 100%; 🖉
height: 0; 🖉
padding-bottom: 56.25%; 🖉
margin-left: auto;
margin-right: auto;
}
References:
Flexbox - align-items
Responsive Video

Categories