{"id":14121,"date":"2011-09-06T18:53:22","date_gmt":"2011-09-07T01:53:22","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=14121"},"modified":"2019-11-03T15:47:49","modified_gmt":"2019-11-03T22:47:49","slug":"top-right-bottom-left","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/properties\/t\/top-right-bottom-left\/","title":{"rendered":"top \/ bottom \/ left \/ right"},"content":{"rendered":"
The You might use it, for example, to nudge an icon into place:<\/p>\n Or position a special element inside a container.<\/p>\n The value for The element will will generally move away from a given side when its value is positive, and towards it when the value is negative. In the example below, a positive length for \n See the Pen The placement of an element with a value for The When \n See the Pen When In this demo, the pink box on the left is positioned 50px down from the top of the page because it has no positioned ancestor elements. The pink box on the right is positioned 50px down from the top of its parent, because the parent has a \n See the Pen When \n See the Pen At first glance, it may seem like there isn’t a difference between \n See the Pen When Setting \n See the Pen You can set a value for each of In most cases, In this example, we set \n See the Pen Elements with If you’ve positioned an element and found that there’s now an empty space or not enough space where you expected, it might have to do with whether the element is in or out of the document’s flow.<\/p>\n When an element is taken out of the document’s flow, it means that the space it originally took up on the page disappears. This is the case when an element is positioned \n See the Pen top<\/code>,
bottom<\/code>,
left<\/code>, and
right<\/code> properties are used with position<\/a> to set the placement of an element. They only have an effect on positioned elements, which are elements with the
position<\/code> property set to anything other than
static<\/code>. For example:
relative<\/code>,
absolute<\/code>,
fixed<\/code>, or
sticky<\/code>.<\/p>\n
div {\r\n <top || bottom || left || right>: <length> || <percentage> || auto || inherit;\r\n}<\/code><\/pre>\n
button .icon {\r\n position: relative;\r\n top: 1px;\r\n}<\/code><\/pre>\n
.container {\r\n position: relative;\r\n}\r\n.container header {\r\n position: absolute;\r\n top: 0;\r\n}<\/code><\/pre>\n
top<\/code>,
bottom<\/code>,
left<\/code>, and
right<\/code> can be any of the following:<\/p>\n
\n
top<\/code> and
bottom<\/code>) or width (for
left<\/code> and
right<\/code>)<\/li>\n
auto<\/code><\/li>\n
inherit<\/code><\/li>\n<\/ul>\n
top<\/code> moves the element down (away from the top) and a negative length for
top<\/code> will move the element up (towards the top):<\/p>\n
\n Top: positive and negative values<\/a> by Matsuko Friedland (@missmatsuko<\/a>)
\n on CodePen<\/a>.<\/span>\n<\/p>\nPosition<\/h3>\n
top<\/code>,
bottom<\/code>,
left<\/code>, or
right<\/code> depends on its value for
position<\/code>. Let’s take a look at what happens when we set the same value for
top<\/code> on elements with different values for
position<\/code>.<\/p>\n
static<\/code><\/h4>\n
top<\/code> property has no effect on unpositioned elements (elements with
position<\/code> set to
static<\/code>). This is how elements are positioned by default. You could use
position: static;<\/code> as one method to undo the effect of
top<\/code> on an element.<\/p>\n
relative<\/code><\/h4>\n
top<\/code> is set on an element with
position<\/code> set to
relative<\/code>, the element will move up or down in relation to its original placement in the document.<\/p>\n
\n Top: relative<\/a> by Matsuko Friedland (@missmatsuko<\/a>)
\n on CodePen<\/a>.<\/span>\n<\/p>\nabsolute<\/code><\/h4>\n
top<\/code> is set on an element with
position<\/code> set to
absolute<\/code>, the element will move up or down in relation to its closest positioned ancestor (or the document, if there are no positioned ancestors).<\/p>\n
position<\/code> of
relative<\/code>.<\/p>\n
\n Top: absolute<\/a> by Matsuko Friedland (@missmatsuko<\/a>)
\n on CodePen<\/a>.<\/span>\n<\/p>\nfixed<\/code><\/h4>\n
top<\/code> is set on an element with
position<\/code> set to
fixed<\/code>, the element will move up or down in relation to the browser’s viewport.<\/p>\n
\n Top: fixed<\/a> by CSS-Tricks (@css-tricks<\/a>)
\n on CodePen<\/a>.<\/span>\n<\/p>\nabsolute<\/code> and
fixed<\/code>. The difference can be seen when you compare them on a page that has enough content to scroll. As you scroll down, the
fixed<\/code> position element is always in view, while the
absolute<\/code> position element scrolls away.<\/p>\n
\n Scrolling: fixed vs. absolute<\/a> by CSS-Tricks (@css-tricks<\/a>)
\n on CodePen<\/a>.<\/span>\n<\/p>\nsticky<\/code><\/h4>\n
top<\/code> is set on an element with
position<\/code> set to
sticky<\/code>, the element will move up or down in relation to the nearest ancestor with a scrolling box (or the viewport, if no ancestor has a scrolling box), limited to the bounds of its containing element.<\/p>\n
top<\/code> on a
sticky<\/code> positioned element doesn’t do much unless its container is taller than it is, and you have enough content to scroll. Like with
fixed<\/code>, the element will stay in view as you scroll. Unlike
fixed<\/code>, the element will fall out of view once it reaches the edges of its containing element.<\/p>\n
\n Scrolling: fixed vs. sticky<\/a> by CSS-Tricks (@css-tricks<\/a>)
\n on CodePen<\/a>.<\/span>\n<\/p>\nGotchas<\/h3>\n
Setting opposite sides<\/h4>\n
top<\/code>,
bottom<\/code>,
left<\/code>, and
right<\/code> on a single element. When you set values for opposite sides (
top<\/code> and
bottom<\/code>, or
left<\/code> and
right<\/code>), the result might not always be what you expect.<\/p>\n
bottom<\/code> is ignored if
top<\/code> is already set, and
right<\/code> is ignored if
left<\/code> is already set. When direction<\/a> is set to
rtl<\/code> (right to left),
left<\/code> is ignored instead of
right<\/code>. In order for each value to have an effect, the element must have a
position<\/code> set to
absolute<\/code> or
fixed<\/code> and
height<\/code> set to
auto<\/code> (default).<\/p>\n
top<\/code>,
bottom<\/code>,
left<\/code>, and
right<\/code> to `20px`, and expect each side of the inner box to be 20px away from the sides of the outer box:<\/p>\n
\n Setting top, bottom, left, and right<\/a> by CSS-Tricks (@css-tricks<\/a>)
\n on CodePen<\/a>.<\/span>\n<\/p>\nWhen fixed isn’t relative to the viewport<\/h4>\n
position<\/code> set to
fixed<\/code> aren’t always positioned in relation to the viewport. It will be positioned relative to its closest ancestor with a
transform<\/code>,
perspective<\/code>, or
filter<\/code> property set to anything other than
none<\/code>, if one exists.<\/p>\n
Adding or removing space<\/h4>\n
absolute<\/code> or
fixed<\/code>. In this example, the containing box of the absolutely positioned element has collapsed because the absolutely positioned element was removed from the document’s flow:<\/p>\n
\n Document flow<\/a> by Matsuko Friedland (@missmatsuko<\/a>)
\n on CodePen<\/a>.<\/span>\n<\/p>\nBrowser Support<\/h3>\n