Positioning inline css elements. Positioning content

Positioning is one of the key concepts in block layout. Having dealt with it, a lot will become clear to you, and layout from shamanism will turn into a meaningful process. So, this article will focus on CSS properties. position and float.

1.position: static

By default, all elements on the page are statically positioned (position: static), which means that the element is not positioned and appears in the document in its usual place, that is, in the same order as in the html markup.

There is no need to specifically assign this property to any element, unless you need to change a previously set positioning to the default.

#content (position: static;)

2. position: relative

Position: relative allows you to use top, bottom, left, and right properties to position an element relative to where it would appear in normal positioning.

Let's move #content 20 pixels down and 40 pixels to the left:

#content (position: relative; top: 20px; left: -40px;)

Notice that there is now empty space where our #content block should have been. Following the #content block, the #footer block did not move down, because #content still takes place in the document, even though we moved it.

At this stage, it may seem that relative positioning is not so useful, but do not rush to conclusions, later in the article, you will find out what it can be used for.

3.position: absolute

With absolute positioning (position: absolute), the element is removed from the document, and appears where you tell it to.

Let's move the # div-1a block to the top, right corner of the page as an example:

# div-1a (position: absolute; top: 0; right: 0; width: 200px;)

Note that this time, because the # div-1a block has been removed from the document, the remaining elements on the page have been positioned differently: # div-1b, # div-1c, and #footer have moved up to where the deleted block was. And the block # div-1a itself is located exactly in the upper right corner of the page.

Thus, we can position any element relative to the page, but this is not enough. In fact, we need to position # div-1a relative to the parent #content block. And at this point, relative positioning comes into play again.

4.position: fixed

Fixed positioning (position: fixed) is a subsection of absolute positioning. Its only difference is that it is always in the visible area of \u200b\u200bthe screen, and does not move when scrolling the page. In this respect, it looks a bit like a fixed background image.

# div-1a (position: fixed; top: 0; right: 0; width: 200px;)

In IE with position: fixed, not everything is as smooth as we would like, but there is many ways get around these restrictions.

5.position: relative + position: absolute

By assigning a relative positioning (position: relative) to the #content block, we can position any child elements relative to its borders. Let's place the # div-1a block in the upper right corner of the #content block.

#content (position: relative;) # div-1a (position: absolute; top: 0; right: 0; width: 200px;)

6. Two columns

Armed with the knowledge from the previous steps, you can try making two columns using relative and absolute positioning.

#content (position: relative;) # div-1a (position: absolute; top: 0; right: 0; width: 200px;) # div-1b (position: absolute; top: 0; left: 0; width: 200px ;)

One of the advantages of absolute positioning is the ability to place elements in any order, regardless of how they are positioned in the markup. In the above example, the # div-1b block is positioned before the # div-1a block.

And now you should have a question: "Where did the rest of the elements from our example go?" They hid under absolutely located blocks. Fortunately, there is a way to fix this.

7. Two columns with fixed height

One solution is to set the container containing the columns to a fixed height.

#content (position: relative; height: 450px;) # div-1a (position: absolute; top: 0; right: 0; width: 200px;) # div-1b (position: absolute; top: 0; left: 0 ; width: 200px;)

The solution is not very suitable, since we never know in advance what size the text will be in the columns, and what font will be used.

8. Float

For columns with variable heights, absolute positioning is not suitable, so let's look at another option.

By assigning a float to the block, we will push it to the right (or left) edge as much as possible, and the text following the block will wrap around it. This is usually used for pictures, but we will use it for a more complex task, since it is the only tool at our disposal.

# div-1a (float: left; width: 200px;)

9. "Floating" speakers

If we assign float: left to the first block, and then float: left to the second, each of the blocks will snuggle to the left, and we get two columns with variable heights.

# div-1a (float: left; width: 150px;) # div-1b (float: left; width: 150px;)

Also, you can assign the opposite float value to the columns, in this case, they will be distributed along the edges of the container.

# div-1a (float: right; width: 150px;) # div-1b (float: left; width: 150px;)

But now we have another problem - the columns go beyond the parent container, thereby breaking the entire layout. This is the most common problem for novice layout designers, although it can be solved quite simply.

10. Clearing float

Float cleaning can be done in two ways. If there is another block after the columns, just assign it clear: both.

# div-1a (float: left; width: 190px;) # div-1b (float: left; width: 190px;) # div-1c (clear: both;)

Or, assign the overflow: hidden property to the parent container

#content (overflow: hidden;)

Either way, the result will be the same.

Conclusion

Today, only basic positioning techniques have been considered, and the simplest examples. Also, to help novice layout designers, I always recommend a series of articles by Ivan Sagalaev, which at one time helped me a lot.

Last update: 28.04.2016

CSS provides the ability to position the element, that is, we can place the element at a specific place on the page

The main property that controls positioning in CSS is the position property. This property can take one of the following values:

    static: standard item positioning, default value

    absolute: the element is positioned relative to the bounds of the container element if that element's position property is not static

    relative: The element is positioned relative to its default position. Typically, the main purpose of relative positioning is not to move an element, but to set a new anchor point for the absolute positioning of nested elements

    fixed: the element is positioned relative to the browser window, this allows you to create fixed elements that do not change position when scrolling

You should not simultaneously apply the float property and any positioning type other than static (that is, the default type) to an element.

Absolute positioning

The browser viewport has top, bottom, right and left edges. For each of these four edges, there is a corresponding CSS property: left (left margin), right (right margin), top (top margin from container edge), and bottom (bottom margin). The values \u200b\u200bof these properties are specified in pixels, em, or percentage. You do not need to specify values \u200b\u200bfor all four sides. Typically, only two values \u200b\u200bare set - the top margin from the top and the left margin from the left.

Block layout in HTML5

HELLO WORLD



Here the absolutely positioned div will be 100 pixels to the left of the viewport border and 50 pixels from the bottom.

It doesn't really matter that there are other elements after this div. This div block will be positioned relative to the borders of the browser viewport anyway.

If an element with absolute positioning is located in another container, in which, in turn, the value of the position property is not equal to static, then the element is positioned relative to the boundaries of the container:

Positioning in HTML5



Relative positioning

Relative positioning is also specified using the relative value. The same top, left, right, bottom properties are applied to indicate the specific position to which the element is shifted:

Positioning in HTML5



Z-index property

By default, when two border elements match, the element that was last defined in the html markup is displayed on top of the other. However, the z-index property allows you to change the order of elements when they overlap. The property takes a number as its value. Items with a higher z-index value will appear on top of items with a lower z-index.

For example:

Positioning in HTML5



Now let's add a new rule to the redBlock style:

RedBlock (z-index: 100; position: absolute; top: 20px; left: 50px; width: 80px; height: 80px; background-color: red;)

Here the z-index is 100. But it doesn't have to be 100. Since the second block's z-index is undefined and is actually zero, we can set the z-index property to any value greater than zero for the redBlock.

And now the first block will be superimposed on the second, and not vice versa, as it was in the beginning.

Imagine that a web developer doesn't need to think about how a visually finished page will look like. He just writes the code, the elements themselves line up on the page, from top to bottom (block) and left to right (inline). The higher the code is written in the document, the higher it will be displayed on the page.

Basically, we will see the natural behavior of the elements, and the order of their display on the page - by the flow of the document. But we are not at all satisfied with this order, we want to place the logo in the upper left corner, and the button a little to the right. We need complete control over all the elements, we decide where and what will be located. And for this you need a tool (property) that changes the usual behavior of elements in the stream. What is this property?

IN CSS styles this is called positioning (position)which can be ordinary (static), relative (relative) and absolute (absolute).

Relative positioning in CSS

When relative positioning, the block moves relative to its original position. However, by writing to the block:

Position: relative;

nothing happens, it stays in place. And the coordinate system moves the block (top, bottom, left, right), with both positive and negative values.

How it works?

Inside a yellow block with a class block-yellow there is a red block with a class block-relative... In normal flow, the red block overlaps the yellow one, since it is the last one written in the code.

starting position
relative position

We need to move the red box down 10 pixels. This can be done only by forcibly changing the position of the red block. This is how we write top: 10px, but it is necessary to indicate from what count these 10 pixels?

Having registered position: relative, we make it clear what to count relative to its current position.

Block-yellow (
background-color: # ffe70f;
}

Block-relative (
position: relative;
top: 10px;
background-color: # ed5b77;
}

In the picture, we can see how the red block has moved down, 10 pixels down, from its original position.

If you remove from the code position: relativethen the block will remain in place. And if instead of relative, write absolute, then by default these 10 pixels will be counted from the edge of the browser window and, as a result, our block, on the contrary, will rise up, pulling up to the bottom edge of the browser. And this was not part of our plans.

Absolute positioning in CSS

In the picture block positioned absolutely, this is when the coordinate system is counted from the edge of the browser, replacing only one word, we changed the position of the block.

Position: absolute;

There is one peculiarity. Absolute positioning also affects inline elements. If an inline element is set position: absolute, then it will behave like a block. This is analogous to the property - display: block.

That's not all, the "absolute" element inside the "relative" parent changes its origin and starts positioning from the parent.


position: relative

position: absolute;

Block with class block-absolute be inside the parent block-yellow.

Block-yellow (
position: relative;
padding: 10px;
background-color: # ffe70f;
}

Since the yellow block has relative positioning, then the red block with absolute positioning, is shifted relative to red by the specified pixels.

Block-absolute (
position: absolute;
bottom: 10px;
left: 10px;
background-color: # ed5b77;
}

Fixed positioning

When scrolling the page, the block with position: fixed, will stay in place, usually used to anchor the navigation bar.

z-index

Resolves the overlapping order of "absolute" blocks. We need the red block to be on top of the yellow one, then we indicate z-index: 2 for red and z-index: 1 for yellow.

Red

Yellow

Block-red (
position: relative;
z-index: 2;
background-color: # ffe70f;
}

Block-yelow (
position: absolute;
top: 20px;
z-index: 1;
background-color: # ed5b77;
}

As you probably already know, you can make up a site using tables, in this case the page is split into cells. And you can use blocks for these purposes when the site page consists of separate elements.

Probably I won't be mistaken if I say that at the same time, a serious problem for many novice webmasters is the task of placing blocks in a given place on a web page.

One of the main difficulties in layout with blocks is the positioning (alignment) of these very blocks.

A little about layers

I think many of you have heard of such a concept as a layer (layer). And, as a rule, a layer means a block specified by a tag

... In fact, everything is a little different.

There are no layers in HTML. This is just a metaphor. When it comes to layers, they mean a certain html container (element) that can be placed in a specific place on a web page.

The second misconception is to attribute to layers only the block specified by the tag

... This is also not true. These also include paragraphs (

), lists (

    ) and other elements.

    And now about
    oh.

    As mentioned above, you can set a location for any HTML element. There is no need to always use the tag for this.

    ... Also, the use of this tag does not oblige you to set it any position on the web page.

    The meaning of using the block specified by the tag

    is reduced to enlargement. After all, other elements (paragraphs, pictures, etc.) can be placed inside it. Thus, a large block with a variety of content is obtained, which is much easier to position on a web page than each of the elements separately.

    Positioning of elements.

    There are four main types of positioning:

    1. Static
    2. Absolute
    3. Fixed
    4. Relative
    )

    Static

    Used as the default location. In this case, the browser looks at the html code, separates it into elements and composes a page from them. It turns out a sequence of a number of elements. They are displayed in the order in which they are specified in the html code.

    Applying parameters left, top, right and bottom does not produce any results.

    Be aware of static positioning when using relative positioning.

    Absolute

    Absolute positioning specifies the coordinates of the upper left corner of the block. In this case, the coordinates are counted relative to the location of the parent element. If the parent is a browser window, then the block is aligned relative to it. If there is another element inside which the block is located, then the alignment is already relative to this element.

    Fixed

    Already from the name it becomes clear that in this case the element is fixed. It is located in a specific place on the web page and does not move anywhere. This alignment is often applied to pop-ups, where they are fixed to the center and do not move when the page is scrolled.

    Relative

    This kind of positioning can be difficult. Its name is not entirely apt. Many people confuse relative and absolute positioning of elements. The alignment might appear to be relative to the parent element. And in the case of absolute positioning - relative to the browser window. But this is not the case.

    It is necessary to understand that the location of the element in this case occurs relative to its place in a static position. This is what was mentioned above.

    In simple terms, you tell the browser to move the element that many pixels from where it is located by default.

    There is another difficult point. What happens if the parent is relatively positioned and the nested element is absolute? In this case, the coordinates of the child element will be counted relative to the parent element, taking into account its offset, if any.

    Summarize.

    So there is a property position... This property can take 4 values Static, Absolute, Fixed and Relative... By default goes Static.

    When you specify coordinates for an element, you also need to tell the browser how it should count these coordinates. We must give him a starting point.

    Don't forget that in the absence of a property position coordinates will not be taken into account, the block will remain in the same place, in its static position.


    Now let's see how the coordinates are set.

    For these purposes, four types of properties are used:

    1. Top
    2. Left
    3. Right
    4. Bottom

    Top - a positive value (for example, 20px) moves the block 20 pixels down. A negative value (-20px) moves the element 20 pixels up. All this happens relative to the upper left corner.

    Left - shift to the left or to the right, depending on the sign. Relative to the upper left corner.

    Right- offset to the right and left, depending on which sign. Relative to the upper right corner.

    Bottom - shift up or down, depending on the sign. Occurs relative to the lower left corner.

    Here is the HTML code:





    And this is the CSS:

    #1 {
    position: relative;
    top: 100px;
    left: 100px;
    width: 500px;
    height: 500px;
    background-color: #CCCCCC;
    }

    #11 {
    background-color: # 003399;
    position: absolute;
    bottom: -30px;
    right: -50px;
    width: 100px;
    height: 100px;
    }

    #2 {
    background-color: # 990066;
    width: 200px;
    height: 300px
    }

    Practice.

    While writing and figured it out myself.

    Positioning css blocks one of the most important stages of layout, because this is what affects the adaptability of the site or the possibility of its implementation in the future (if not required at the moment), and also has a significant impact on the further scaling of the site. It is not uncommon for "unfortunate layout designers" to make such a layout that in the end it is easier to throw it out than to change something, but at the same time it can look completely in accordance with the layout. This situation arises from not understanding where and when to use this or that type of positioning. Today we will try to understand this issue. And so, in css there is a property position. This property can take 5 values, but we will consider 4 main ones:

    • absolute
    • static
    • relative
    • fixed

    Absolute positioning of blocks (absolute)

    The first on our list is the absolute positioning method. When this property is assigned, the block becomes a unit on its own and the rest of the page elements do not affect its location, as well as it does not affect other elements. The size of the box is determined by the width and height properties, and the position on the page is determined by the top properties. left, right and bottom these parameters set the margins from the top, left, right and bottom edges. If the block has no parent element, as well as if the positioning of the parent element is different from static, then the top, left, bottom, right properties set the indents from the beginning of the page, otherwise from the edges of the parent element.

    Most often, this type of positioning is used when the block needs to be pressed against the right or bottom edge. Let's consider such a layout.

    For convenience, the blocks were highlighted with a multi-colored frame. The markup will look like this:

    XHTML

    Relation (position: relative;) .yellow (position: absolute; right: 10px; bottom: 10px;)

    Relation

    position: relative;

    Yellow

    position: absolute;

    right: 10px;

    bottom: 10px;

    In this example, we used the right and bottom properties to set the indentation from the right and bottom edges. These property values \u200b\u200bare equivalent to top: Element height - 10px and left: Element width - 10px.

    Also, absolute positioning is sometimes used when it is necessary to make a "run over" of one element to another.

    Static positioning

    The most common type of positioning that occurs on every page and is usually set for most elements is static, in css it is written as static. For most HTML tags, this value is the default, i.e. if position is not explicitly set, then the value will be static. With this arrangement, the elements are lined up one above the other and the coordinates in the window of each element depend on the nearest elements with position: static or position: relative. The top, left, right, bottom properties will not work with this positioning, the position is changed due to the margin property.

    Relative positioning

    This kind of positioning is very similar to static positioning, except that the position of the element can be changed with the top, left, right, bottom and margin properties.

    Fixed positioning

    Fixed positioning according to the specifics of specifying coordinates is similar to absolute, but the position is calculated not relative to the HTML page, but relative to the browser window, i.e. with top: 10px, you will set the top padding to 10px FROM BROWSER WINDOWand no matter what level of scrolling you are on, this element will always follow with your screen.

    Usually used for navigation elements, so that the user has always in sight of important information or links to interesting pages.

Did you like the article? To share with friends: