[html] How to horizontally center an element

Here is another way to center horizontally using Flexbox and without specifying any width to the inner container. The idea is to use pseudo elements that will push the inner content from the right and the left.

Using flex:1 on pseudo element will make them fill the remaining spaces and take equal size and the inner container will get centered.

_x000D_
_x000D_
.container {_x000D_
  display: flex;_x000D_
  border: 1px solid;_x000D_
}_x000D_
_x000D_
.container:before,_x000D_
.container:after {_x000D_
  content: "";_x000D_
  flex: 1;_x000D_
}_x000D_
_x000D_
.inner {_x000D_
  border: 1px solid red;_x000D_
  padding: 5px;_x000D_
}
_x000D_
<div class="container">_x000D_
  <div class="inner">_x000D_
    Foo content_x000D_
  </div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

We can also consider the same situation for vertical alignment by simply changing the direction of flex to column:

_x000D_
_x000D_
.container {_x000D_
  display: flex;_x000D_
  flex-direction: column;_x000D_
  border: 1px solid;_x000D_
  min-height: 200px;_x000D_
}_x000D_
_x000D_
.container:before,_x000D_
.container:after {_x000D_
  content: "";_x000D_
  flex: 1;_x000D_
}_x000D_
_x000D_
.inner {_x000D_
  border: 1px solid red;_x000D_
  padding: 5px;_x000D_
}
_x000D_
<div class="container">_x000D_
  <div class="inner">_x000D_
    Foo content_x000D_
  </div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width

Examples related to alignment

How do I center text vertically and horizontally in Flutter? CSS align one item right with flexbox What's the difference between align-content and align-items? align images side by side in html How to align iframe always in the center How to make popup look at the centre of the screen? Responsive image align center bootstrap 3 How to align title at center of ActionBar in default theme(Theme.Holo.Light) Center align a column in twitter bootstrap How do I position an image at the bottom of div?

Examples related to centering

Flutter: Trying to bottom-center an item in a Column, but it keeps left-aligning Centering in CSS Grid Bootstrap 4 Center Vertical and Horizontal Alignment Center a column using Twitter Bootstrap 3 How to horizontally align ul to center of div? How to center a <p> element inside a <div> container? Using margin:auto to vertically-align a div How to center body on a page? How to center a "position: absolute" element How to center a button within a div?