admin管理员组

文章数量:1310043

I want a div to be positioned below the screen, but when you scroll it to appears. I can do this using margin-top but this requires a specific number whereas I want it to be relative to the screen it is on, so that it appears when you just start to scroll down on all screens.

How can I do this? Thanks.

I want a div to be positioned below the screen, but when you scroll it to appears. I can do this using margin-top but this requires a specific number whereas I want it to be relative to the screen it is on, so that it appears when you just start to scroll down on all screens.

How can I do this? Thanks.

Share Improve this question edited Jun 21, 2015 at 0:24 HaveNoDisplayName 8,517106 gold badges40 silver badges50 bronze badges asked Jun 17, 2015 at 6:22 BMJ1260BMJ1260 351 gold badge1 silver badge8 bronze badges 3
  • 2 post what you tryed.. – Sarath Kumar Commented Jun 17, 2015 at 6:24
  • Use position:fixed property to fix the elements on the page. – stanze Commented Jun 17, 2015 at 6:29
  • Don't use margin-top, set position: absolute and bottom to the negative height. – Rory McCrossan Commented Jun 17, 2015 at 6:53
Add a ment  | 

4 Answers 4

Reset to default 5

Use vh:

iewport-percentage lengths defined a length relatively to the size of viewport, that is the visible portion of the document. Only Gecko-based browsers are updating the viewport values dynamically, when the size of the viewport is modified (by modifying the size of the window on a desktop puter or by turning the device on a phone or a tablet).

#myEl {
    position: absolute;
    top: 100vh;
}

Docs: https://developer.mozilla/en-US/docs/Web/CSS/length

Demo: http://jsfiddle/tusharj/g8pfow8r/

This can be handled using position:fixed and use top instead of margin-top

div {
    position:fixed;
    top:100px;
}

if you are using jquery, you can use it like that in your javascript, also in window.resize

        var windowHeight = window.innerHeight;
        $("#box").css('position','absolute');
        $("#box").css('top',windowHeight+'px');

viewport values help you define attributes relative to screen size:

margin-top:20vw;

JSFiddle

Take a look here

本文标签: javascriptSet the margintop of a divrelative to screen resolutionStack Overflow