There are several ways to access data and experiments in the center of CSS elements:
Center horizontally
1. The simplest thing to do is to add text-align:center to the parent element. This statement does not care whether the child element has a fixed size.
But only the image, button, text and other inline elements (display for inline or inline-block, etc.) are horizontally centered.
However, in IE6, 7, these two wonderful browsers, it can be any element in the horizontal center.
2. The child element width is determined, can use margin:0 Auto or margin-left:auto; Margin-right:auto
This method can only be centered horizontally and is not valid for floating or absolutely positioned elements.
3. The child element width is indeterminate, you can place the child element in the table and set the table to margin:0 Auto.
4. Use floating Center, do not need to know the width of the element
Box setting for wrapping child elements: position:relative;float:left;left:50% (this box should not be set to size)
Child element: position:relative;left:-50%
Center vertically
1. The parent element is highly indeterminate, and the Padding-top and Padding-bottom implementations are centered in the parent element.
Note: Using Margin-top and margin-bottom in child elements does not actually achieve the effect of padding:
in a regular document flow, the vertical margin adjacent to 2 or more block-level box models is collapsed.
The final margin value is calculated as follows:
A, all are positive, take the largest person;
B, not all positive values, then the absolute value is taken, and then minus the maximum with a positive number;
C, without a positive value, take absolute values and subtract the maximum by 0.
Note: Adjacent box models may be dynamically generated by DOM elements and do not have an adjacent or inheritance relationship.
in the adjacent box model, if one of them is floating, the vertical margin is not folded, even between a floating box model and its child elements.
The margin between the element that sets the overflow property and its child elements is not collapsed (except for the overflow value).
The box model with absolute positioning (Position:absolute) is set, and the vertical margin is not folded, even between their child elements.
The display:inline-block elements are set, and the vertical margin is not collapsed, even between their child elements.
Another: If you set Padding-top padding-bottom in a child element, you can visually achieve the same effect, but the div is not actually centered vertically .
2. If it is a single line of text that is highly determined by the parent layer, set the line-height of the text to the height of the text parent container
3. Vertical centering of multi-line text, picture, block-level elements with height determination of the parent layer
There is a property vertical-align for vertical centering in CSS, but this property only takes effect when the parent layer is TD or TH, which is not supported by default for other block-level elements such as Div, p, and so on.
Under Firefox and IE8, you can set the display value of the block-level element to Table-cell to activate the Vertical-align property, but ie6,7 does not support it, so there is no way to cross-browser compatibility. But we can use table.
Omni-Directional Centering
1. Child element dimensions are known.
Parent element settings: position:relative
pair Element Set position:absolute;top:50%;margin-top:-heigt/2 px; LEFT:50%;MARGIN-LEFT:-WIDTH/2 px;
Attention:
This is a non-responsive method, cannot use a percentage size, and cannot set the maximum minimum value for min-/max-.
Content may go beyond the container
You need to reserve space for padding, or you need to use the Box-sizing:border-box style.
Can also be written as: Position:absolute;top:0;bottom:0;left:0;rght:0;margin:auto;
2.transform
child element settings
{Margin:auto; Position:absolute; top:50%; left:50%; -webkit-transform:translate ( -50%,-50%); -ms-transform:translate ( -50%,-50%); Transform:translate ( -50%,-50%);}
3.table-cell
4.inline-block
5.flexbox
Resources
Http://www.igooda.cn/jzjl/20131013387.html
CSS element Centering implementation method