, is inline, there will be no wrapping of containers to a new line, all containers
stay on the same line.
Container
it is convenient to use when setting a part of the text with special formatting (highlighting with color, in a different font, etc.)
For containers
apply the following CSS properties:
#perviy( vertical-align:sub; ) #vtoroy( vertical-align:3px; ) #tretiy( vertical-align:-3px; )
As a result, the line of text will look like this:
This is nothing more than vertical alignment of inline elements, and the CSS property vertical-align copes with this task perfectly.
We digress a little, now we return to our main task.
Vertical alignment in a div container
No matter what, to align inside the div container, we will use the property vertical-align. As I said, this property can be used in case of alignment of inline elements (we discussed this case in detail above and it does not suit us for alignment in a div container); it remains only to use the fact that vertical-align works for table cells.
How can we use it? We do not have a table, we are working with a div container.
Ha, it turns out to be very simple.
CSS property display allows you to turn our div block into a table cell, this can be done easily and naturally:
Let's have a class div textalign:
Block content
For this block, specify the following CSS property:
Textalign(display: table-cell; )
This CSS instruction will miraculously turn our div block into a table cell without visually changing it in any way. And for a table cell, we can apply the property vertical-align fully and the desired vertical centering will work.
However, everything cannot end so simply. We also have a wonderful IE browser. It does not know how to work with the property display: table-cell(I suggest you familiarize yourself with the table illustrating the performance of this CSS property in different browsers on the site htmlbook.ru). Therefore, we will have to go to various tricks.
There are many ways to achieve alignment in a div container for all browsers:
- Method using an additional helper div container
- Method using expression . It is connected with the cunning calculation of block heights. You can't do without knowledge of JavaScript.
- Using the line-height property. This method is only suitable for vertical alignment in a block of known height, and therefore is not applicable in the general case.
- Using absolute and relative content offset in case of IE browser. This method seems to me the most understandable and simple. Also, it's implementable for a variable height div container. We will dwell on it in more detail.
As you understand, we need to solve the problem of vertical alignment in IE associated with its misunderstanding of the property display: table-cell(neither IE6 nor IE7 nor IE8 are not familiar with this property). Therefore, using conditional comment specifically for browsers of the IE family, we will specify other CSS properties.
Conditional comment
View design:
...
Инструкции, действующие только в случае выполнения условия в квадратных скобках...
is called a conditional comment (be careful, the type of the conditional comment must completely match the given example, up to a space).
The instructions contained in such a conditional comment will only be executed if the browser interpreting the given code belongs to the IE family.
Thus, using a conditional comment, we can hide a piece of code from all browsers except IE.
The solution of the problem
Because of all this parsley, we will need to provide our HTML code with two additional containers. This is how our block with text will look like:
It's some kind of test text.
It consists of two lines.
For class container div textalign CSS properties are set that align its content vertically for all normal browsers (except IE, of course):
Display: table-cell; vertical-align: middle;
And two more properties that set the width and height for the block:
Width:500px; height: 500px;
This is quite enough to align the contents of the container in the center relative to the vertical, in all browsers except IE.
Now we start adding properties related to alignment for IE family browsers(it is for them that we used additional blocks div and span):
Referring to the tag div inside the class block textalign. To do this, you must first specify the name of the class, and then, separated by a space, the tag we are referring to.
Textalign div( position: absolute; top: 50%; )
This construction means: for all div tags inside the block with the class textalign apply the listed properties.
Accordingly, the styles set for the block textalign are modified:
Textalign( display: table-cell; vertical-align: middle; width:500px; height: 500px; position: relative; )
Now the top left point of the text box is shifted down by 50%.
To explain what is happening, I drew an illustration:
As you can see from the picture, we have made some progress. But that is not all! The top left dot of the yellow block has indeed moved 50% down from its parent (purple) block. But what we need is that at fifty percent of the height of the purple block is yellow block center, not its top left point.
Now the tag will go span and its relative positioning:
Textalign span( position: relative; top: -50%; )
Thus, we have shifted the yellow block up by 50% of its height, relative to the initial position. As you understand, the height of the yellow block is equal to the height of the centered content. And the last span operation puts our content in the middle of the purple box. Hooray!
Let's Shaman a Little
First things first, we need to hide the parsley from all normal browsers and open it for IE. This can be done, of course, with the help of a conditional comment, it was not in vain that we got to know him:
There is a small problem. If the content being centered is too high, then this leads to unpleasant consequences: there is an extra vertical scroll height.
Solution: property needs to be added overflow: hidden bloc textalign.
Get to know the property in detail overflow possible in .
The final look of the CSS instructions for the block textalign looks like:
Textalign( display: table-cell; vertical-align: middle; width:500px; height: 500px; position: relative; overflow: hidden; border: 1px solid black; )
Sorry, I forgot to mention one important point. If you keep trying set the height of the class block textalign as a percentage, then you have nothing will come of it.
Centering in a Variable Height Block
Very often there is a need to set the height of the class block textalign not in pixels, but as a percentage of the height of the parent block, and align the contents of the div container in the middle.
The catch is that it is impossible to do this for a table cell (and the class block textalign turns into a table cell, thanks to the property display:table-cell).
In this case, you must use the outer block, for which the CSS property is specified display:table and already for it to set the percentage value of the height. Then the block nested in it, with the CSS directive display:table-cell, will happily inherit the height of the parent block.
In order to implement a variable-height block in our example, we'll edit the CSS a bit:
class textalign we will change the value of the property display with table cell on the table and remove the alignment directive vertical-align: middle. Now we can safely change the height value from 500 pixels to, for example, 100%.
So the CSS properties for the class block textalign will look like this:
Textalign( display: table; width:500px; height: 100%; position: relative; overflow: hidden; border: 1px solid black; )
It remains to implement content centering. To do this, the div container nested in the class block textalign(this is the same yellow block in the figure), you need to set the CSS property display:table-cell, then it will inherit a height of 100% from the parent block textalign(purple block). And nothing will stop us from aligning the contents of the nested div container to the center with the property vertical-align: middle.
Getting another additional list of CSS properties for the div nested in the container textalign.
Textalign div( display: table-cell; vertical-align: middle; )
That's the whole trick. Optionally, you can variable height with centered content.
For more information on the vertical alignment of a variable height block, see .
The problem of vertically centering elements in CSS has a number of ready-made solutions. The choice of alignment method in each individual case depends on the type, size, positioning of elements and other properties specified by them.
Below are the vertical alignment techniques popular with layout designers. It shows how they work, and for which cases each of them is most suitable.
Here are two div elements:
Each method will be used to align the indoor unit to the center of the outdoor unit.
line-height for one line
When the parent spans one line of text and the height of the child is known, the line-height property can be applied. The property value must be equal to the height of the outer box. This only works for one line, so it's good for the child to add overflow: hidden and white-space: nowrap to prevent line wrapping.
You won't be able to use line-height=100% as a percentage, because 100% in this case is the height of the font.
Container(
height: 300px;
line-height: 300px
}
Inner(
white-space: nowrap;
overflow: hidden;
}
The method is applicable only when the height of the outer block is known.
Table with vertical-align
A table is the best way to align elements vertically. In order not to violate the semantics, it is better to create table elements using CSS. The position of the cell content is controlled by vertical-align. Four values of this property work in tables:
baseline - by default;
. bottom - content at the bottom of the cell;
. middle - content in the middle of the cell;
. top - content at the top of the cell.
In the first example, a lone table cell becomes the outer block.
Container(
display:table-cell;
vertical-align: middle;
}
The method is good because it works for elements without a specified height, but in some cases its use is hindered by the fact that the outer block, like any table cell, adapts its width to its content, and you can only stretch it by setting the width for width explicitly. For a cell without a table, percentages do not work adequately.
This shortcoming is corrected by wrapping the cell in the parent with the display:table property. This element can be sized as a percentage. The final code will look like this:
Outer-wrapper (
display:table;
}
Container(
display:table-cell;
vertical-align: middle;
}
Position: absolute + negative margin
The method is used when the height of the inner element is known. For an external block, it may be unknown. The parent is given relative positioning, and the child inside it is absolute.
A top property value of 50% causes the nested element to be positioned top-most in the middle of the outer box. It remains to raise it with a negative margin-top by half of its own height so that it stands exactly in the center of the vertical.
Container(
position: relative;
}
Inner(
height: 140px;
position: absolute;
top: 50%;
margin-top: -70px;
}
The disadvantage of this method is the need to know the height of the child.
Inline alignment with vertical-align
The alignment of inline and inline-block elements, including images and icons, in their surrounding text is done with the vertical-align property. Unlike a table, in this case the entire set of its values specified in the specification works.
Given the height of the parent, this property can be used to center multiline text.
For the outer block, the centering of one line is prescribed.
Container(
height: 140px;
line-height: 140px;
}
The line-height value for the inner block is overridden to normal or whatever value you want. He is also given the alignment vertical-align: middle and conversion to inline-block type - display: inline-block.
Inner(
display: inline-block
line-height: normal
vertical-align: middle;
}
The disadvantage of this method is that you need to know the height of the parent.
Alignment with transform
The translateY function of the transform property allows you to center an inner box with an unknown height. To do this, the parent must be positioned relatively and the child must be positioned absolutely.
The top property with a value of 50% lowers the inner box so that its top edge is in the middle of the parent. The translateY value: -50% raises the element by half of its own height and thus aligns the vertical centers of the boxes.
Container(
position: relative;
}
Inner(
position: absolute;
top: 50%;
transform: translateY(-50%);
}
The disadvantage of the reception is the limited support for transform functions by the IE browser.
Alignment via pseudo-element
The method works when the height is unknown for either the first or the second block. An inline pseudo-element inline-block is added inside the parent using before or after . The height of the added element must be equal to the height of the parent - height: 100%. The vertical alignment of the content is set with vertical-align: middle.
Vertical-align: middle aligns the inner block relative to this pseudo-element. Since the parent and child are the same height, the inner element, while vertically aligned with the pseudo-element, is centered with the outer box as well.
Container:before (
content: "";
height: 100%;
vertical-align: middle;
display: inline-block
}
Inner(
display: inline-block
vertical-align: middle;
}
Universal way. Does not work if the inner block is set to absolute positioning.
Flexbox
The newest and easiest way to align elements vertically. Flexbox allows you to arrange the elements of a Web page in any way you like. To center a block, just set the parent to display: flex and the child to margin: auto.
Container(
display:flex;
width: 320px
height: 140px;
}
Inner(
width: 120px
margin: auto;
}
Flexbox only works in modern browsers.
Choice of method
Which vertical alignment technique to use depends on the task and the limitations that may be present in any particular case.
The height of the elements is unknown
In this situation, you can use one of four universal methods:
1. Table. The parent is a table cell created in HTML or via display-table/display-cell. This parent element is given vertical-align: middle.
2. Pseudo-element. For the outer block, an inline-block pseudo-element is created with width=100% and vertical-align: middle. The child is given display: inline-block and vertical-align: middle. The method does not work only when the inner block is given absolute positioning.
3. Transform. The parent gets position: relative. The child is given position: absolute, top: 50% and transform: translateY(-50%);
4 Flexbox. The outer block is set to display: flex, the inner block is set to margin: auto.
Only the height of the child is known
The outer block is positioned relatively; the inner element is given absolute positioning, top: 50% and a margin-top equal to half of its height.
One line per block with known height
The outer box is set to the line-height property with a value equal to its height.
The height of the outer block is known, but the inner element is not.
The parent is given a line-height equal to its height. The child's line-height is redefined to normal or whatever value you want, and it is given display: inline-block and vertical-align: middle.
When designing a web page layout, you've probably come across a situation where you need to center a div horizontally and vertically using CSS. There are several ways using CSS and jQuery.
But first the basics:
Horizontal alignment with CSS
class-name(
margin:0 auto;
width:200px;
height:200px;
}
To center a div only horizontally, you need to specify a width and an automatic value for the left and right margins (this is a shorthand form of writing CSS properties). This method works on block elements (div, p, h1, etc.). To apply it to inline elements (such as hyperlinks and images), you need to write another rule - display:block .
Horizontal and vertical alignment with CSS
Centering the div horizontally and vertically at the same time is a little more tricky. You need to know the dimensions of the div in advance.
class-name(
width:300px;
height:200px;
position:absolute;
left:50%;
top:50%;
margin:-100px 0 0 -150px;
}
With the absolute positioning of an element, we can pull it out of the flow and set its position relative to the browser window. By setting the div offset to 50% from the left and top of the window, we center the top left corner of the page. The only thing left to do is move the div left and up by half of its width and height, with a negative margin so that it's perfectly centered.
Horizontal and vertical alignment with jQuery
As stated, the above CSS method only works on fixed size divs. jQuery comes to the rescue:
// function declaration:
$(window).resize(function()(
$(".class-name").css((
position:"absolute",
left: ($(window).width() - $(".class-name").outerWidth())/2,
top: ($(window).height() - $(".class-name").outerHeight())/2
});
});
// function call:
$(window).resize();
The function calculates the window width in $(window).resize() whenever the user resizes the window. We use outerWidth() and outerHeight() because, unlike regular width() and height() , they add padding and border width, returning the size. Finally, we adapt on window resize and center the div on page reload.