I have a button fixed inside of outer div. The problem is when I set position: fixed(to keep the button stay while the page is scrolling) it didn't work properly.
The button still scrolled and moved out of the screen.
here is my code
.rotate {
transform: rotate(30deg);
background: blue;
width: 300px;
height: 300px;
margin: 0 auto;
}
.fixed {
position: fixed;
background: red;
padding: 10px;
color: white;
top: 50px;
}
<div class="rotate">
<button class="fixed"> I am fixed inside a rotated div.</button>
</div>
<!-- <div class="fixed"> I am fixed outside a rotated div.</div> -->
How can I fix it to keep button always display on the screen while scrolling the page?
This is either a buggy or by design behavior by the browsers: basically, and "position: fixed" fixed element won't be fixed if any parent element has "transform" set. The following thread has some reference on it:
Positions fixed doesn't work when using -webkit-transform
As for a workaround, you might use a as a wrapper and nest the colored and the rotated inside it, then adjust the positioning with "margin". It's kinda hacky, but it might work depending on the situation. Here's a demo:
[http://codepen.io/jean-andreghetto/pen/OxEaVN?editors=1100][2]
You have the fixed element inside of the static box, which means you made the red link fixed to the blue box, and not to the outside. What you need to do is remove the red link of inside the blue box.
This should be what you want.
https://codepen.io/dawsonhudson17/pen/jGKeRy
.rotate {
transform: rotate(30deg);
background: blue;
width: 300px;
height: 300px;
margin: 0 auto;
}
.fixed {
position: fixed;
background: red;
padding: 10px;
color: white;
top: 50px;
left: 50%;
z-index: 2;
transform: translate(-50%) rotate(30deg);
display: block;
}
<button class="fixed"> I am fixed inside a rotated div.</button>
<div class="rotate">
</div>
Related
I have tried a lot of things and searched online but I cannot figure out the solution to this problem.
I have a div container which has a max-height, min-height and also overflow: auto. When the inner content is larger than the max-height, a scrollbar appears as expected. But, inside the content there is a dropdown, which when clicked, the menu expands, and instead of being displayed outside the parent container, it is like changing the inner content height.
The only solution I found online and made sense to me, is to wrap the container to div with relative positioning and make the dropdown absolute, but there is a big drawback now, the dropdown stays fixed on scroll, as it is absolute positioned relative to the wrapper and not the content. Is there any common way to fix this or any other solution ?
I didn't post any code because I do not want the answer to rely on my code.
I just want a minimal example if possible with these properties:
Container has a max-height
If content is larger than the container's max-height then the container should display a scrollbar.
The content has a dropdown which should scroll with every other element of the content.
The menu options of the dropdown element are escaping the container / are displayed outside the boundaries of the container.
To illustrate on my comments on the question, here's an MCVE:
.scroll-container {
border: 3px dashed #eee;
height: 400px;
padding: 10px;
overflow: auto;
width: 400px;
}
.content {
background-color: #f0f0f0;
height: 600px;
position: relative;
}
.dropdown {
background-color: orange;
position: absolute;
height: 300px;
width: 300px;
left: 300px;
}
<div class="scroll-container">
<div class="content">
<div class="dropdown"></div>
</div>
</div>
As you can see, with absolute positioning based on the relative position of div.content the orange div.dropdown creates a horizontal overflow, which is what you don't want. To fix this scenario, you need to remove position: relative from div.content and use transform: translateX(300px); instead of left: 300px;:
.scroll-container {
border: 3px dashed #eee;
height: 400px;
padding: 10px;
overflow: auto;
width: 400px;
}
.content {
background-color: #f0f0f0;
height: 600px;
}
.dropdown {
background-color: orange;
position: absolute;
height: 300px;
width: 300px;
transform: translateX(300px);
}
<div class="scroll-container">
<div class="content">
<div class="dropdown"></div>
</div>
</div>
I'm trying to place fixed element and keep it relative to its container, not the port view.
I made it in chrome.
On Safari however, the fixed element is placed at the bottom of the page, disregarding its parent position and place. For some reason it gets the right place when clicking the container.
I tried to add translate property to the fixed element, it didn't help.
I tried to create the fixed behaviour with absolute position instead of fix, but couldn't make it to work. It moved with the scroll.
Container CSS
.Container {
display: flex;
flex-direction: column;
position: relative;
}
Fixed Element CSS
.Fixed {
font-weight: 300 !important;
width: fit-content;
font-size: 14px !important;
position: fixed;
background: value(CalendarBackground);
bottom: 0;
left: 3px;
width: 100%;
padding-left: 32px;
border-radius: 3px;
height: 68px;
}
EDIT 1 - React Component JSX (HTML TO BE)
<div className={classes.ExpandedEvent}>
// CONTAINER
<div className={classes.Container}>
<div className={classes.TimeContainer}>
<Text className={classes.Time}>{time}</Text>
{recurrenceJsx}
</div>
{locationJsx}
{summaryJsx}
{attachmentsJsx}
</div>
// FIXED
<TextButton onClick={_onCopyClick} className={classes.Fixed}>{t('Google_Calendar_Copy')}</TextButton>
</div>
EDIT 2 - LIVE EXAMPLE
https://itaytur.github.io/wix-calendar/?path=/story/calendar--desktop-agenda
I deployed the component so it could be seen live. not all the css was loaded sorry in advance, but for reproduce the bug it works.
click the first event from the top, called: CLICK TO SEE FIXED 'COPY TO CALENDAR' BTN IN THE POPUP BOTTOM - NOT SHOWING ON SAFARI.
in chrome the copy button is shown and sticks to the bottom of the popup even when scrolling, in safari it doesn't shown at all.
Because fixed item doesn't care about relative container
You can use absolute position inside a fixed element
But there is already a lot of post about it:
Juste take a look here:
Fixed position but relative to container
Can I position an element fixed relative to parent?
You can also take a look to sticky property: https://www.w3schools.com/howto/howto_css_sticky_element.asp
.wrapper{
width: 100%;
padding: 40px;
background: yellow;
}
.relative-item{
width: 200px;
height:100vh;
background: green;
}
.fixed-item-wrap{
position: fixed;
width: 200px;
height:100vh;
}
.fixed-item{
background: red;
color: white;
position: absolute;
top: 20px;
}
<div class="wrapper">
<div class="relative-item">
<div class="fixed-item-wrap">
<div class="fixed-item">
I'm fixed but relative !
</div>
</div>
</div>
</div>
Here's an example of what I think it is that you're trying to achieve.
If you want the child position to be relative to it's initial position, you should set it's position as relative.
.Container {
background: red;
padding: 50px;
}
.Relative {
background: white;
font-weight: 300 !important;
font-size: 14px !important;
position: relative;
bottom: 20px;
border: 1px solid black;
left: 55px;
padding-left: 32px;
border-radius: 3px;
height: 68px;
}
<div class="Container">
<div class="Relative">
My position is relative to my initial position
</div>
</div>
I am beginner in web development, and i am trying to develop a responsive website. While developing i made one div element that has an image, then inside that i have one box with content "BUILD THAT MATTERS", which is inside a div with a class "firstBlock", the div "firstBlock" moves down when i click the hover button of navigation bar, but the box inside "BUILD THAT MATTERS", doesn't move.
I tried to make the image as a background image of the the div firstBlock, but that made the fitting of image in such a way that it got cropped, so is there any way so that the box inside the div moves with the navigation bar drop down menu, as of now only the firstBlock, that is the image moves, but inner box "BUILD THAT MATTERS" remains fixed in its position.
<-- html-->
<div id="firstBlock">
<img id="img1" src="image1.jpg">
<div class="box1">
<h1 class="text1">BUILD THAT MATTERS</h1>
</div>
</div>
/*CSS*/
#firstBlock {
display: flex;
height: 90%;
width: 100%;
}
#img1 {
width: 100%;
height: 100%;
object-fit: cover;
}
.box1 {
display: flex;
border: 5px solid black;
position: absolute;
top: 70%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
So, the box with content "BUILD THAT MATTERS" should move down with the first block when navigation drop down scrolls down.
fiddle link: https://jsfiddle.net/gj7v8huc/
git link: https://ayush-flash.github.io/StartupLandingPage/StartUpLandingPage/
Very simple rule:
make the parent of the absolute element relative
Your class:
.box1 {
display: flex;
border: 5px solid black;
position: absolute; # parent of this should be relative
top: 40%; # I set this to 40%
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
the parent:
#firstBlock {
display: flex;
height: 90%;
width: 100%;
position: relative; # take care of child with absolute position
}
What is this rule for?
When we have an element that has position: absolute it is removed from current flow of document. For bringing it back to the document's flow but meanwhile keeping it according to the need of absolute position, we can make the parent's position relative
It is all about parent and child element for using position:absolute. Parent element should have relative position so that the absolute child element will do the calculation of top and left from its expected parent. So apply position:relative to your firstBlock div.
#firstBlock {
display: flex;
height: 90%;
width: 100%;
position:relative;
}
Also change your box1 div top and left position as per your parent alignment. I have applied 50% like below.
.box1 {
display: flex;
border: 5px solid black;
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
As I have already posted in the comment, below is the JSFiddle link.
DEMO
z-index is hard, too hard for me right now.
I have a page (#s01) with a fixed sidenav (#s03) and when called a modal (#s02) should cover the whole layer including the sidenav.
Right now the sidenav lays on top of it all despite having a lower z-index than the modal. Altering the z-index (on click) for the sidenav could fix the problem but I'm sure there's a solution without any JavaScript.
This codepen should demonstrate my problem quite well.
What CSS trickery is needed to actually cover the sidenav?
HTML
<div id="s01">
<div id="s02"></div>
</div>
<div id="s03"></div>
CSS
// main
#s01 {
position: relative;
width: 50vw;
height: 100vh;
background: red;
z-index: 1;
}
// reveal
#s02 {
display: none;
position: absolute;
left: 0;
top: 0;
width: 50vw;
height: 100vh;
background: darkblue;
z-index: 3;
}
//sidenav
#s03 {
position: fixed;
top: 50%;
transform: translateY(-50%);
width: 30px;
height: 200px;
background: green;
z-index: 2;
}
Because fixed position element doesn't share the same parent element as the rest elements:
html
<div id="s01">
<div id="s02">Modal!</div>
<button id="mybutton">Click me!</button>
<div id="s03"></div>
</div>
codepen
The z-index property specifies the z-order of an element and its
descendants. When elements overlap, z-order determines which one
covers the other. An element with a larger z-index generally covers an
element with a lower one.
z-index
it's a known 'bug' that elements with fixed position loose their position if the container is translated. For example, if i've got a structure like this:
<div class="container">
<div class="fixed"></div>
</div>
and, say, the container is scrolled, when the conteiner gets transformed (say, translate(x,y), rotate(), or so..), then the fixed element behaves like it was positioned relative and it scrolls with the container. I can see it on the latest firefox, for example.
How can one fix this kind of problem? Is there any way?
This behaviour is not a bug. It's actually the specs recommended behaviour.
(See this post by Eric Meyer, or this question here on SO which accepted solution only provides a link to the same meyer's post)
For those who don't know this issue, and because you didn't provide a snippet into your question, here's one.
document.addEventListener('click', function() {
document.getElementById('container').classList.toggle('transformed')
}, false);
#bg {
border: 1px solid #AFA;
height: 100%;
width: 100%;
position: fixed;
top: 0;
left: 0;
}
#container {
border: 1px solid #FAF;
height: 50%;
width: 75%;
position: relative;
margin: 0 auto;
overflow: auto;
}
#content {
background: rgba(125, 175, 0, .7);
position: fixed;
width: 100%;
top: 0;
left: 0;
}
.transformed {
transform: translate(0, 5em);
}
<div id="bg">
<div id="container" class="transformed">
.<br>.<br>.<br>.<br>.<br>.<br>.
this is a scrollable paragraph
<br>.<br>the "fixed" content does scroll with the paragraph
<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.
you can click to toggle the transformation On/Off
<br>.<br>.<br>.<br>.<br>.
<span id="content">relatively fixed content</span>
</div>
</div>
However, I did find something that may help others facing the same issue.
It's not really a solution, since the "fixed" element will be only inside the container, (except for IE browsers where it will really be fixed to the document). But in my case, it's actually what I wanted and maybe it'll be fine for others too.
If you add a wrapper, set its height:100%; width:100%; and overflow:auto, then your "fixed" content won't scroll with the container.
Actually it's not you container which scrolls anymore, but the wrapper. So you might want to set the container's overflow:visible or hidden to avoid unwanted scrolling of the not so well "fixed" element.
Also, note that you need your wrapper be a block or inline-block element.
#bg {
border: 1px solid #AFA;
height: 100%;
width: 100%;
position: fixed;
top: 0;
left: 0;
}
#container {
border: 1px solid #FAF;
height: 50%;
width: 75%;
position: relative;
margin: 0 auto;
overflow: visible;
}
#wrapper {
height: 100%;
width: 100%;
overflow: auto;
}
#content {
background: rgba(125, 175, 0, .7);
position: fixed;
width: 100%;
top: 0;
left: 0;
}
.transformed {
transform: translate(0, 50%);
}
<div id="bg">
<div id="container" class="transformed">
<div id="wrapper">
.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.
<span id="content">relatively fixed content</span>
</div>
</div>
</div>
I am not familiar with this bug, but when you use positioned: fixed; the element is positioned relative to the browser window, so it doesn't really make any sense to put it inside a container.
This markup would be my recommendation:
<div class="fixed"></div>
<div class="container"></div>
Once you use position: fixed; on any element it is positioned relative to the view-port. Directly from page in MDN about position property.
fixed
Do not leave space for the element. Instead, position it at a specified position relative to the screen's viewport and don't move it when scrolled.
So what you are experiencing is a what it is actually supposed to work like and not a 'bug'.
Now if what you want is something that is positioned with relation to the .container div and translate with it than you will have to use absolute positioning here. Take a look at this fiddle. The important CSS is-
.container {
width: 200px;
height: 100px;
position: relative;
}
.absolute {
position: absolute;
width: 20px;
height: 10px;
top: 50px;
left: 50px;
}
Notice that with positioning the inner div as absolute I have also positioned the outer div as relative as the inner div takes its position in reference to the closest parent div positioned as anything different from static.