Fixed element gets pushed down

akinuri

This is a part of the application I'm building. There are two columns. First column contains a song lyrics, and the second column contains some buttons that control some of the properties of lyrics column.

Fiddle: default

<div id="content">
    <div class="column board">
        <div id="lyricsBox">
            <div id="song">Kryptonite - 3 Doors Down</div>
                <div id="lyrics">
                    <br><span style="color: DodgerBlue;">[Verse 1]</span>
                    <br>I took a walk around the world to ease my troubled mind
                    <!-- on and on...  see fiddle -->
                </div>
        </div>
    </div>
    <div class="column board" id="controls">
        <div id="lyrics_controls">
            <img src="images/ts_dark.png" id="textSelect" />
            <img src="images/center.png" id="align" />
            <div id="fontSize">
                <span class="up_down" id="increase">&#9650;</span>
                <span id="currentSize">15px</span>
                <span class="up_down" id="decrease">&#9660;</span>
            </div>
        </div>
    </div>
</div>

I decided not to post CSS and JS here, because it'll just spam the question, make it longer and scare you. That's why I isolated the code and created fiddles with different states. If anyone were to suggest that I should also include them, I will.

Since the second column is shorter than the first column, it'll disappear if I scroll down. I don't want that. So, I change the position of the second column to fixed. That solves the problem, but causes another one.

If I change text-align or font-size of lyrics (first) column through control buttons, the second columns gets pushed down.

Fiddle: gets pushed down

To fix the problem, I added another line of code to the control buttons:

controls.style.position = "fixed";

So, after changing text-align or font-size properties of the first column, I set the position of the second column to fixed, though it's already set to fixed with CSS.

Unchecking and checking the position property of the second column in Developers Tools also solves the problem. That gave me the idea of adding controls.style.position = "fixed"; to the end of the control button functions.

Fiddle: js fix

Even though I managed to find a way to make it work, I want to know what causes the problem. Why the second column gets pushed down? And why unchecking and checking it's position property back fixes it?

Joseph Marikle

I agree with @CBroe's comment. Such behavior (fixed position without top, left, right or bottom) is undefined by the spec so there's no telling why it's doing that. If I go into dev tools and remove and add again the fixed positioning, it goes back to where it should be, so I don't think it's intentional (webkit bug perhaps), but in any case, you should be assigning direction attributes along with the fixed positioning. It gets a bit hairy here because it's hard to place it in that exact spot, but considering your first column is more or less a fixed width, you can add a right or left attribute of 50% and adjust is with a negative margin. For example:

position: fixed;
top: 48px;
right: 50%;
margin-right: -388px; /* about half of the left
                         columns ultimate width
                         including padding, margin,
                         etc. */

If your code needs to be more dynamic (is responsive or the code must be reusable with containers of other sizes) you will need to add different classes, use media queries, use javascript to calculate it, or use a combination of these methods.

Demo: fork of your fiddle demonstrating the above CSS

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Line element gets pushed down when text is wrapped

From Dev

Nav being pushed down inside fixed div

From Dev

Element in sticky menu getting pushed down on scroll

From Dev

Bootstrap Navbar Brand image gets pushed down upon collapse

From Dev

2 Divs with fixed position, one gets pushed away as the other gets shown

From Dev

CSS - list element gets pushed out of position by :before directive

From Dev

Fixed element with links in it gets overlapped by div with text in it

From Dev

Comment in fixed line in Excel gets truncated when I scroll down

From Dev

div elements gets pushed down when it contains text. A peculiar behavior of inline-block

From Dev

How to position an element at a fixed position from the top until pushed by another element

From Dev

How to position an element at a fixed position from the top until pushed by another element

From Dev

Content is pushed down

From Dev

Background being pushed down

From Dev

Content is pushed down

From Dev

Sidebar getting pushed down

From Dev

json key gets assigned a number instead of array when second element is supposed to be pushed

From Dev

The First Option in the Drop Down gets Fixed all though I set it to display None using javascript

From Dev

Floating divs being pushed down

From Dev

Html div container pushed down

From Dev

Button being pushed down by a transition

From Dev

JS, link to pushed element

From Dev

SwitchCompat right of TextView gets pushed out of sight

From Dev

Mac Safari Font Rendering changes thinner when any Element on the page gets position:fixed;

From Dev

JQueryUI - draggable element gets dropped onto multiple droppable areas if the droppable areas are scaled down

From Dev

Float in css causing element to move down But when innerHTML changed by JS, it gets up

From Dev

JQueryUI - draggable element gets dropped onto multiple droppable areas if the droppable areas are scaled down

From Dev

SAS: Display SQL pushed down to the database

From Dev

Div float: right being pushed down to bottom

From Dev

Container View getting pushed down as if it had a UINavigationBar?

Related Related

  1. 1

    Line element gets pushed down when text is wrapped

  2. 2

    Nav being pushed down inside fixed div

  3. 3

    Element in sticky menu getting pushed down on scroll

  4. 4

    Bootstrap Navbar Brand image gets pushed down upon collapse

  5. 5

    2 Divs with fixed position, one gets pushed away as the other gets shown

  6. 6

    CSS - list element gets pushed out of position by :before directive

  7. 7

    Fixed element with links in it gets overlapped by div with text in it

  8. 8

    Comment in fixed line in Excel gets truncated when I scroll down

  9. 9

    div elements gets pushed down when it contains text. A peculiar behavior of inline-block

  10. 10

    How to position an element at a fixed position from the top until pushed by another element

  11. 11

    How to position an element at a fixed position from the top until pushed by another element

  12. 12

    Content is pushed down

  13. 13

    Background being pushed down

  14. 14

    Content is pushed down

  15. 15

    Sidebar getting pushed down

  16. 16

    json key gets assigned a number instead of array when second element is supposed to be pushed

  17. 17

    The First Option in the Drop Down gets Fixed all though I set it to display None using javascript

  18. 18

    Floating divs being pushed down

  19. 19

    Html div container pushed down

  20. 20

    Button being pushed down by a transition

  21. 21

    JS, link to pushed element

  22. 22

    SwitchCompat right of TextView gets pushed out of sight

  23. 23

    Mac Safari Font Rendering changes thinner when any Element on the page gets position:fixed;

  24. 24

    JQueryUI - draggable element gets dropped onto multiple droppable areas if the droppable areas are scaled down

  25. 25

    Float in css causing element to move down But when innerHTML changed by JS, it gets up

  26. 26

    JQueryUI - draggable element gets dropped onto multiple droppable areas if the droppable areas are scaled down

  27. 27

    SAS: Display SQL pushed down to the database

  28. 28

    Div float: right being pushed down to bottom

  29. 29

    Container View getting pushed down as if it had a UINavigationBar?

HotTag

Archive