Skip to content Skip to sidebar Skip to footer

Fixed Position And Float Not Working Correctly

I am trying to create a section in my site where the is a scrolling section on my site and a section right next to it that doesn't move. Here is my code:

Solution 1:

The problem is you are declaring a float: right; AND a position:fixed; on div.right_wrapper. The layout issues you're describing make complete sense.

First some background information on CSS absolute and fixed positions:

There can in fact be no less than seven layers in one stacking context, and any number of elements in those layers, but don't worry—you are unlikely to have to deal with seven layers in a stacking context. The order in which the elements (all elements, not only the positioned ones) within one stacking context are rendered, from back to front is as follows:

  1. The background and borders of the elements that form the stacking context
  2. Positioned descendants with negative stack levels
  3. Block-level descendants in the normal flow
  4. Floated descendants
  5. Inline-level descendants in the normal flow
  6. Positioned descendants with the stack level set as auto or (zero)
  7. Positioned descendants with positive stack levels

The highlighted entries are the elements whose stack level we can change using the z-index property.

(Taken from http://www.w3.org/wiki/CSS_absolute_and_fixed_positioning)

The reason you're having these issues is because you're assigning a fixed position to an object. This takes it out of the normal object flow. When you remove the fixed position, this leaves only the float: right declaration, which puts it at the right of the main column.

Instead of float: right, position the item using declarations such as right: 0;.


To help illustrate what I'm talking about, below is an example.

#wrapper {
  position: relative;
  width: 90% margin: 1em auto;
  /* Background color to help illustrate areas*/background-color: red;
}
#content {
  position: relative;
  width: 70%;
  padding: 1em;
  background-color: white;
}
#menu {
  position: fixed;
  padding: 10px;
  top: 30px;
  left: calc(70% + 30px);
  background-color: white;
}
<divid="wrapper"><divid="content"><h1><spanclass="mw-headline"id="The_third_dimension.E2.80.94z-index">The third dimension—z-index</span></h1><p>It’s natural to regard a web page as two-dimensional. Technology hasn’t evolved far enough that 3D displays are commonplace, so we have to be content with width and height and fake 3D effects. But CSS rendering actually happens in three dimensions!
      That doesn’t mean you can make an element hover in front of the monitor—yet—but you can do some other useful things with positioned elements.
    </p><p>The two main axes in a web page are the horizontal X axis and the vertical Y axis. The origin of this co-ordinate system is in the upper left-hand corner of the viewport, ie where both the X and Y values are 0. But there is also a Z axis, which we
      can imagine as running perpendicular to the monitor’s surface (or to the paper, when printing). Higher Z values indicate a position “in front of” lower Z values. Z values can also be negative, which indicate a position “behind” some point of reference
      (I’ll explain this point of reference in a minute).
    </p><p>Before we continue, I should warn you that this is one of the most complicated topics within CSS, so don’t get disheartened if you don't understand it on your first read.
    </p><p>Positioned elements (including relatively positioned elements) are rendered within something known as a stacking context. Elements within a stacking context have the same point of reference along the Z axis. I’ll explain more about this below. You
      can change the Z position (also called the stack level) of a positioned element using the <code>z-index</code> property. The value can be an integer number (which may be negative) or one of the keywords <code>auto</code> or <code>inherit</code>. The
      default value is <code>auto</code>, which means the element has the same stack level as its parent.
    </p><p>You should note that you can only specify an <i>index</i> position along the Z axis. You can’t make one element appear 19 pixels behind or 5 centimetres in front of another. Think of it like a deck of cards: you can stack the cards and decide that the
      ace of spades should be on top of the three of diamonds—each card has its stack level, or Z index.
    </p><p>If you specify the <code>z-index</code> as a positive integer, you assign it a stack level “in front of” other elements within the same stacking context that have a lower stack level. A <code>z-index</code> of 0 (zero) means the same as <code>auto</code>,
      but there is a difference to which I will come back in a minute. A negative integer value for <code>z-index</code> assigns a stack level “behind” the parent’s stack level.
    </p><p>When two elements in the same stacking context have the same stack level, the one that occurs later in the source code will appear on top of its preceding siblings.
    </p><p>There can in fact be no less than seven layers in one stacking context, and any number of elements in those layers, but don't worry—you are unlikely to have to deal with seven layers in a stacking context. The order in which the elements (all elements,
      not only the positioned ones) within one stacking context are rendered, from back to front is as follows:
    </p><ol><li>The background and borders of the elements that form the stacking context
      </li><li><b>Positioned descendants with negative stack levels</b></li><li>Block-level descendants in the normal flow
      </li><li>Floated descendants
      </li><li>Inline-level descendants in the normal flow
      </li><li><b>Positioned descendants with the stack level set as <code>auto</code> or  (zero)</b></li><li><b>Positioned descendants with positive stack levels</b></li></ol><p>The highlighted entries are the elements whose stack level we can change using the <code>z-index</code> property.
    </p></div><divid="menu"><h3>This is a fixed menu.</h3><ul><li><ahref="#">Link 1</a></li><li><ahref="#">Link 2</a></li><li><ahref="#">Link 3</a></li><li><ahref="#">Link 4</a></li><li><ahref="#">Link 5</a></li></ul></div></div>

Solution 2:

You've called a div with position:fixed and not specified any anchor points, so it's anchoring to top:0, left:0. If you want to fix an element to something other than top left 0,0 you'd need to specify them in top left bottom right.

my-class{
  position: fixed;
  right: 10px;
  top: 10px;
}

Post a Comment for "Fixed Position And Float Not Working Correctly"