Make element scroll without fixed position setting

前端 未结 1 1775
执笔经年
执笔经年 2020-12-22 14:17

Is there a way to make an element scroll with the background without giving it a fixed position? So it won\'t cause problems when resizing the window? the element I am talki

相关标签:
1条回答
  • 2020-12-22 14:23

    This cannot be accomplished without using position fixed, in order to fix your problem with the resize you'll need to use screen query for it. Here's an example of how to use it on your style.css file and a list of default screen sizes

    === UPDATED FOR YOUR NEEDS ===

    In order to align your logo on the middle of the screen and not display it on smaller screens as you requested on the comments you'll need to use a position absolute for your #logo, to make it fixed while you scroll the page, wrap it inside of a fixed div. Here's the trick

    /* Your normal css goes here */
    .logoContainer{
        position:fixed;
    }
    #logo {
        left:50%; /* this puts your logo on the middle of the screen */
        width:38px;
        margin-left:-19px; /* this needs to be half of the width */
        position:absolute;
    }
    
    /* Smartphones (portrait) ----------- */
    @media only screen and (max-width : 320px) {
        /* Your fix for smaller screen sizes example*/
        #logo {
            display:none; /* this tells your div to not display */
        }
    }
    

    Here's an online example

    0 讨论(0)
提交回复
热议问题