Home  >  Q&A  >  body text

Applying position: fixed to an element breaks the alignment of content controlled by display: flex.

<p>I need it. word and. Line elements are anchored to the top of the viewport when the page is scrolled - in their original position, where the lines are below the words. <br /><br />I managed to fix it. word and position: sticky, but facing two problems:<br /></p><p><code></code><code></code></p> When I apply sticky to . line, it "jumps" above the word and I don't know how to fix it to the top of the viewport but force it to stay there. The stickyness below the word element was not the correct value initially - it would "shake" the text when you started scrolling. The correct value is fixed - it works great and I'd rather use it! However, it breaks. The location of warning class (red font): it should be in. Opposite (horizontally) of the transcription text, but a fixed value that collapses the text of these two classes together. <br /><p><br /></p><p>Here is also the JS Bin with this "situation". </p><ol> </ol> <p><br /></p> <pre class="brush:css;toolbar:false;">.word { position: sticky; top: 0; z-index: 1; background-color: white; } .word { padding: 1vw 3vw 2% 3vw; } .word-details { display: flex; justify-content: space-between; align-items: baseline; } .transcription { font-weight: normal; } .warning { color: red; margin-left: auto; } .line { border-top: 2px solid #fdb239; } .meaning { list-style-type: none; counter-reset: item; hyphens: auto; font-size: calc(0.7em 1.5vw); } .meaning > li { position: relative; } .meaning > li::before { content: counter(item); counter-increment: item; position: absolute; top: 0; text-align: center; margin-left: calc(-0.7em - 2.5vw); } .meaning-word { margin-top: 50px; } .sentences { list-style-type: none; padding-left: 0; font-size: calc(0.7em 1.5vw); margin-top: 30px; } .sentences > li.sentence-ru { margin-top: 15px; }</pre> <pre class="brush:html;toolbar:false;"><!DOCTYPE html> <html> <body> <div class="word"> <span>Word</span> <div class="word-details"> <div class="transcription">/ transcription /</div> <div class="warning">COMMENT</div> </div> </div> <hr class="line"> <ol class="meaning"> <li class="meaning-word">Meaning1</li> <ul class="sentences"> <li class="sentence-en">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</li> <li class="sentence-ru">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</li> </ul> <li class="meaning-word">Meaning2</li> <ul class="sentences"> <li class="sentence-en">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</li> </ul> </ol> </body> </html></pre> <p><br /></p>
P粉131455722P粉131455722422 days ago415

reply all(1)I'll reply

  • P粉539055526

    P粉5390555262023-08-03 11:07:47

    Keeping several elements separated at the top of the page while scrolling is difficult, so I'd suggest a simpler solution. The "jumping" of your sticky bar is because the initial position of the sticky bar is different from where it is at the top of the page when scrolling

    1. To avoid your hr line disappearing while scrolling, I would put all the components you want to keep at the top of the page in a container, such as a div, and then make that container sticky.

      The second problem is that your sticky bar is a bit low at the beginning because of the default body margin (10px) setting. Then we scroll the page - the body margins have already been scrolled, and you set the sticky bar to be absolutely at the top (top: 0;), so it has to jump the extra 10px to the top. A quick fix is ​​to set the body top margin to 0 and then your pastebar will always be in the same position at the top of the page.

      The following is the updated code snippet.



    body {
        margin-top: 0;
    }
    
    .sticky-container {
        position: sticky;
        top: 0;
        z-index: 1;
        background-color: white;
    }
    
    .word {
        padding: 1vw 3vw 2% 3vw;
    }
    
    .word-details {
        display: flex;
        justify-content: space-between;
        align-items: baseline;
    }
    
    .transcription {
        font-weight: normal;
    }
    
    .warning {
        color: red;
        margin-left: auto;
    }
    
    .line {
        border-top: 2px solid #fdb239;
    }
    
    .meaning {
        list-style-type: none;
        counter-reset: item;
        hyphens: auto;
        font-size: calc(0.7em + 1.5vw);
    }
    
    .meaning > li {
        position: relative;
    }
    
    .meaning > li::before {
        content: counter(item);
        counter-increment: item;
        position: absolute;
        top: 0;
        text-align: center;
        margin-left: calc(-0.7em - 2.5vw);          
    }
    
    .meaning-word {
        margin-top: 50px;
    }
    
    .sentences {
        list-style-type: none;
        padding-left: 0;
        font-size: calc(0.7em + 1.5vw);
        margin-top: 30px;                   
    }
    
    .sentences > li.sentence-ru {
        margin-top: 15px;                   
    }
    <html>
        <body>
    
        <div class="sticky-container">
            <div class="word">
                <span>Word</span>
                <div class="word-details">
                    <div class="transcription">/ transcription /</div>
                    <div class="warning">COMMENT</div>
                </div>
            </div>
            <hr class="line">
        </div>
    
          <ol class="meaning">
                <li class="meaning-word">Meaning1</li>
                <ul class="sentences">
                    <li class="sentence-en">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</li>
                    <li class="sentence-ru">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</li>
                </ul>
            <li class="meaning-word">Meaning2</li>
                <ul class="sentences">
                    <li class="sentence-en">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</li>
                </ul>
          </ol>
    
        </body>
    </html>

    reply
    0
  • Cancelreply