/    Sign up×
Community /Pin to ProfileBookmark

Element with position absolute stretches the parent div, why?

Hello! I have a problem with position absolute.
Here is a link for a quick demo of the problem. https://jsfiddle.net/zvs4f9hd/#&togetherjs=DijFma547G
The square that’s positioned with absolute positioning stretches the body of the page. But from what I know – it shouldn’t.
What am I doing wrong?
I want the element with position absolute to be outside of the page. Like somewhere where I can never see it!

Thank you!

to post a comment
CSSHTML

6 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinJul 20.2021 — you don't have a parent div in your code

your css works as expected

the red square is positioned at 0,0

the blue square is also position at 0,0

because absolute positioned elements are not in the flow

transform: translate(0px, 100vh);

the translate directive causes the blue square

position to be moved along a vector

0px to the right and 100vh down

https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/translate()

so it is positioned at the view height

just out of sight

https://www.kirupa.com/html5/make_body_take_up_full_browser_height.htm#:~:text=By%20default%2C%20both%20the%20html%20element%20and%20body,height%20of%20the%20content%20that%20is%20inside%20them.

By default, both the html element and body element have their height CSS property set to auto . This means they don’t have an explicit height out of the box. They’ll either take up whatever height they are told to be, or they will take up whatever height of the content that is inside them.

.absolute-container {

position: absolute;

width: 100px;

height: 100px;

background-color: #111BBB;

left: -100px

}

you won't the blue square with the above css
Copy linkTweet thisAlerts:
@SempervivumJul 20.2021 — @weareandrei#1634399
>The square that's positioned with absolute positioning stretches the body of the page. But from what I know - it shouldn't.

It does not stretch. Apply some border to body and it will get visible.

However, obviously the browser makes that element accessible by showing the vertical scrollbar.

You can hide that element by applying `overflow: hidden;` to body; then the scrollbar will disappear:

https://jsfiddle.net/Sempervivum/ohdbr2uz/1/
Copy linkTweet thisAlerts:
@weareandreiauthorJul 20.2021 — @Sempervivum#1634401 thanks for your answer, this helped me to overcome the issue.

But now I am facing another issue. The solution that you are suggesting leads to the situation where I cannot scroll the page. And what if I need to because the re are more then 1 element. Here is my jsfiddle again :)

https://jsfiddle.net/h2wqvf7s/#&togetherjs=QnPnMldi9d
Copy linkTweet thisAlerts:
@SempervivumJul 20.2021 — Is your requirement just to shift the blue box out of view? If so, you can shift it to the left or to the top, then it will not be reachable by scrolling.
Copy linkTweet thisAlerts:
@weareandreiauthorJul 20.2021 — @Sempervivum#1634448 Well, sorry for not explaining it earlier. What I am doing is the navigation menu for mobile devices. When the burger is pressed - the menu moves up from below. Basically it is like an animation of menu appearing on the screen. And I want it to be out of sight, but below and so that I cannot scroll there. But I still need the scroll ))
Copy linkTweet thisAlerts:
@SempervivumJul 20.2021 — 😃 That's always helpful if one knows about the background of a requirement.

I hope this fiddle is what you require. I had to add an additional wrapper, `main`, and make that one scrollable:

https://jsfiddle.net/Sempervivum/2byv06d1/3/
×

Success!

Help @weareandrei spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...