Home  >  Article  >  Web Front-end  >  Detailed explanation of examples of using inline-block for centering in CSS

Detailed explanation of examples of using inline-block for centering in CSS

高洛峰
高洛峰Original
2017-03-10 11:11:012424browse

This article mainly explains in detail the example of using inline-block for centering in CSS. When using it, pay attention to the width of the container. Friends in need can refer to the

urgently needed method: inline-block method Centered. The basic approach is to use display: inline-block, vertical-align: middle styles and pseudo-elements to center the content block in the container. My implementation uses several new tricks not seen elsewhere to solve some problems.

The declared width of the content area cannot be greater than 100% of the container minus the width of 0.25em. Like an area with long text. Otherwise, the content area will be pushed to the top, which is why the :after pseudo-class is used. Using the :before pseudo-class will make the element 100% size!
Detailed explanation of examples of using inline-block for centering in CSS

If the content block needs to occupy as much horizontal space as possible, you can add the max-width: 99%; style to the large container, or use max when considering the browser and container width. -width: calc(100% – 0.25em) style.

This method has most of the same benefits as table-cell, but I initially gave up on this method because it was more like a hack. Regardless of this, browser support is great and the method is proving to be very popular.

HTML:

CSS:

.Center-Container.is-Inline {    
  text-align: center;   
  overflow: auto;   
}   

.Center-Container.is-Inline:after,   
.is-Inline .Center-Block {   
  display: inline-block;   
  vertical-align: middle;   
}   

.Center-Container.is-Inline:after {   
  content: '';   
  height: 100%;   
  margin-left: -0.25em; /* To offset spacing. May vary by font */
}   

.is-Inline .Center-Block {   
  max-width: 99%; /* Prevents issues with long content causes the content block to be pushed to the top */
  /* max-width: calc(100% - 0.25em) /* Only for IE9+ */
}

Benefits:

Content is highly variable
If the content overflows, it can automatically expand the height of the parent element.
The browser has good compatibility and can even be adjusted to support IE7

Also note:

Requires additional containers
Depends on margin- left: -0.25em style, horizontally centered, needs to be adjusted for different font sizes
The declared width of the content area cannot be greater than 100% of the container minus the width of 0.25em


The above is the detailed content of Detailed explanation of examples of using inline-block for centering in CSS. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn