Positioning css blocks. Positioning (aligning) block elements in CSS

Positioning css blocks. Positioning (aligning) block elements in CSS

21.03.2022

Positioning is one of the key concepts in block layout. Having dealt with it, a lot will become clear to you, and layout will turn from shamanism 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 positioned statically (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 position to default.

#content( position: static; )

2.position:relative

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

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

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

Notice that there is now an empty space where our #content block should have been. Following the #content block, the #footer block didn't move down because #content still has its place in the document even though we moved it.

At this stage, it may not seem like relative positioning is all that useful, but don't jump to conclusions, later in the article, you will learn 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, for example, move the #div-1a block to the top right corner of the page:

#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 are positioned differently: #div-1b, #div-1c, and #footer have moved up to the position of the removed block. And the #div-1a block itself is located exactly in the right, upper corner of the page.

Thus, we can position any element relative to the page, but this is not enough. Actually, 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 subset of absolute positioning. Its only difference is that it is always in the visible area of ​​the screen, and does not move while scrolling the page. In this respect, it is 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 bypass these restrictions.

5. position:relative + position:absolute

By giving the #content block relative positioning (position: relative), we can position any child elements relative to its borders. Let's place a #div-1a block in the top 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 an arbitrary order, regardless of how they are located in the markup. In the example above, the #div-1b block is placed before the #div-1a block.

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

7. Two fixed height columns

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

#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 placed in columns, and what font will be used.

8.Float

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

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

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

9. Floating columns

If we assign float: left to the first block and then float: left to the second, each of the blocks will be pressed to the left edge, and we will get two columns, with a variable height.

#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 beginner layout designers, although it can be solved quite simply.

10. Clear float

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

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

Or assign the property overflow: hidden to the parent container

#content ( overflow:hidden; )

Either way, the result will be the same.

Conclusion

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

One of the best things about CSS is that styles give us the ability to position content and elements on a page in almost any way imaginable. This brings structure to our design and helps make the content more visual.

There are several different types of positioning in CSS, each of which has its own scope. In this chapter, we're going to take a look at a few different use cases - creating reusable layouts and unique positioning of disposable elements, as well as describing a few techniques for doing so.

Positioning via float

One way to position elements on a page is through the float property. This property is quite versatile and can be applied in many ways.

Essentially, the float property takes an element, removes it from the normal flow of the page, and positions it to the left or right of the parent element. All other elements on the page will wrap around such an element. For example, paragraphs will wrap around an image if the element has the float property is set.

When the float property is applied to multiple elements at the same time, it makes it possible to create a layout with floated elements next to or opposite each other, as shown in a multi-column layout.

The float property takes several values, the two most popular being left and right , which allow an element to be positioned to the left or right of its parent.

Img ( float: left; )

float in practice

Let's create a general page layout with a header at the top, two columns in the center, and a footer at the bottom. Ideally, this page should be marked up with elements

,
,

© 2022 hecc.ru - Computer technology news