Positioning a large number of blocks in html. Displaying an element above the rest! Relative positioning in CSS
Imagine that a web developer doesn't have to think at all about how it will look visually finished page. He simply writes the code, the elements themselves line up on the page, from top to bottom (block) and from left to right (inline). The higher up in the document the code is written, the higher it will appear on the page.
In general, we will see the natural behavior of the elements, and the order in which they are displayed on the page - the flow of the document. But we are not at all happy with this order; we want to place the logo on the left top corner, and the button is slightly to the right. We need full control over all the elements, we decide where and what will be located. And for this you need a tool (property) that changes the normal behavior of elements in the flow. What kind of property is this?
IN CSS styles this is called positioning (position), which may be ordinary (static), relative (relative) and absolute (absolute).
Relative positioning in CSS
At relative positioning, the block moves relative to its starting position. However, having written to the block:
Position: relative;
nothing will happen, it will remain 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 a normal flow, the red block overlaps the yellow one, since it is written last in the code.
We need to move the red block down 10 pixels. This can only be done by forcibly changing the position of the red block. That's how we write it top: 10px, but you need to indicate where these 10 pixels are to be counted from?
Having registered position: relative, we make it clear that we are counting relative to its current position.
Block-yellow (
background-color: #ffe70f;
}
Block-relative (
position: relative;
top: 10px;
background-color: #ed5b77;
}
In the picture we see how the red block has moved down, 10 pixels down, from its original position.
If you remove it from the code position: relative, then the block will remain in place. What if instead 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, reaching the bottom edge of the browser. And this was not part of our plans.
Absolute Positioning in CSS
There is a block in the picture absolutely positioned, this is when the coordinate system is counted from the edge of the browser, by replacing just one word, we changed the position of the block.
Position: absolute;
There is one peculiarity. Absolute positioning also affects inline elements. If the inline element is given position: absolute, then it will behave like a block one. This is analogous to the property - display: block.
That's not all, the "absolute" element located inside the "relative" parent changes its coordinate reference point and begins to be positioned away 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 , shifts relative to red by specified pixels.
Block-absolute (
position: absolute;
bottom: 10px;
left: 10px;
background-color: #ed5b77;
}
Fixed positioning
When scrolling a page, a block with position: fixed, will remain in place, this method is usually used to fix the navigation bar.
z-index
Resolves the order of overlapping "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.
Block-red (
position: relative;
z-index: 2;
background-color: #ffe70f;
}
Block-yellow (
position: absolute;
top: 20px;
z-index: 1;
background-color: #ed5b77;
}
When working with text, the user may need to swap sentences or entire paragraphs or arrange fragments in some non-standard way. Move a block of text in an application Microsoft Office Word can be done in several ways.
Instructions
Ctrl, Shift and right/left arrow highlight a word, and if you use the up or down arrows, you can select an entire paragraph.
Last update: 04/28/2016
CSS provides element positioning capabilities, meaning we can place an element at a specific location on the page.
The main properties that control positioning in CSS are position property. This property can take one of the following values:
static : standard element positioning, default value
absolute : the element is positioned relative to the bounds of the container element if its 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 position new point bindings for 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 scrolled
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's viewing area has a top, bottom, right, and left edge. Each of these four edges has a corresponding CSS property: left, right, top, and bottom. The values for these properties are specified in pixels, ems, or percentages. It is not necessary to set values for all four sides. As a rule, only two values are set - the indent from the top edge top and the indent from the left edge left.
HELLO WORLD
Here the absolute positioned div will be 100 pixels to the left of the viewport border and 50 pixels from the bottom.
It is not so important that after this div element some other elements are coming. The div block in any case, it will be positioned relative to the boundaries of the browser viewport.
If an element with absolute positioning is located in another container, which, in turn, has a position property value not equal to static , then the element is positioned relative to the boundaries of the container:
Relative positioning
Relative positioning also specified using the relative value. To specify the specific position to which the element is shifted, the same top, left, right, bottom properties are used:
z-index property
By default, if two border elements match, the element defined in html markup the last one. However, the z-index property allows you to change the order of elements when they are overlaid. The property takes a number as its value. Elements with great value This property will appear on top of elements with a lower z-index value.
For example:
Now let’s add a new rule to the redBlock block 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 does not have to be the number 100. Since the second block has no z-index defined and in fact equal to zero, then for redBlock we can set y z-index properties any value greater than zero.
And now the first block will overlap the second, and not vice versa, as was the case in the beginning.
They work with all positioned elements except static ones.
Positioning example.
Elements may overlap each other!
Displaying an element above the rest!
The position property has 4 values: static, fixed, relative and absolute. Each of these meanings will be demonstrated below with an example of use.
Before we examine in detail all types of positioning of elements on a page, we will have to consider what document flow is.
Document flow
By default, elements on a web page are displayed in the order in which they appear in the HTML document, i.e. block elements occupy the entire width available to them and are stacked vertically one below the other. Inline elements are lined up horizontally until the entire available width is occupied, after the entire width is occupied, a line break is made and everything starts again. This arrangement of elements is called normal flow(also called document flow or general flow).
You can use the float or position property to remove an element from the normal flow. If an element "falls out" of the normal flow, then elements that are located in the code below that element will be shifted into its place on the web page.
Static positioning
Static is the default positioning for all elements on a web page. If you don't apply the position property to an element, it will be static and will appear on the web page according to the general flow of elements.
At using CSS properties top , left , right or bottom to a statically positioned element, they will be ignored.
If necessary, you can set static positioning in the style sheet using the static value:
First paragraph.
Second paragraph.
Try »Fixed positioning
Elements with fixed positioning are located on the page relative to the browser window. Such elements are removed from the overall flow; elements following the fixed element in the flow will ignore it, moving and taking its place on the web page.
It is worth paying attention to the fact that elements with fixed positioning can overlap other elements, hiding them completely or partially. While scrolling long pages, they create the effect of motionless objects, remaining in the same place:
![](https://i2.wp.com/donald.gif)
Text Text Text Text Text Text Text Some text Text Text Text Text TextTry »
Relative positioning
Relatively positioned elements, like static elements, remain in the general flow. When applying the top , left , right or bottom properties to relatively positioned elements, they will move relative to their position, leaving empty space where the element was originally located.
Such elements do not affect the position of the elements surrounding them; the remaining elements remain in place and can be overlapped by a relatively positioned element:
First level title.
Relatively positioned title.
Third level heading.
Try »Note: Elements with relative positioning (relative) are usually used as a parent for elements with absolute positioning (absolute).
Absolute positioning
Absolutely positioned elements are completely removed from the overall flow, the remaining elements will take up the free space, completely ignoring absolutely positioned elements. You can then position the element wherever you want on the web page using the top , left , right , or bottom properties.
All absolutely positioned elements are placed relative to the browser window or relative to the nearest positioned ancestor (if there is one) that has a position property of absolute , fixed , or relative .
![](https://i1.wp.com/image.jpg)
Let's change the location of the logo using absolute positioning.
The logo will now be located in the upper right corner of the page.
Overlapping elements
When elements are outside the overall flow of the page, they can overlap each other. Typically, the order of elements corresponds to their order in the HTML code of the page, however, it is possible to control the order of overlap using the CSS property z-index , the higher its value, the higher the element will be.