Flexbox: center horizontally and verticallyCentering content horizontally using flexboxHow to center flex block in center page?Vertically align input when using FlexboxCentering elements vertically in a fixed-height containerText doesn't align in the middleHow to center a image in a div horizontally and verticallyvertically center elements in divtext centering on CSS with flexboxCenter image vertically and horizontally in flexboxCSS Flex stretch not working together with vertical alignHow to horizontally center a <div>?How to vertically center a div for all browsers?Vertically align text next to an image?How to set the margin or padding as percentage of height of parent container?CSS center text (horizontally and vertically) inside a div blockVertically centering a div inside another divHow do I vertically center text with CSS?Flex-box: Align last row to gridvertical-align with Bootstrap 3In CSS Flexbox, why are there no “justify-items” and “justify-self” properties?

When did J.K. Rowling decide to make Ron and Hermione a couple?

Why didn't General Martok receive discommendation in Star Trek: Deep Space Nine?

How is char processed in math mode?

Would people understand me speaking German all over Europe?

Reducing the time for rolling hash

Scam? Checks via Email

What Marvel character has this 'W' symbol?

Were there any unmanned expeditions to the moon that returned to Earth prior to Apollo?

What do the novel titles of The Expanse series refer to?

What is the term for completing a climbing route uncleanly?

Why are prop blades not shaped like household fan blades?

Word for giving preference to the oldest child

What kind of horizontal stabilizer does a Boeing 737 have?

Balancing Humanoid fantasy races: Elves

How to structure presentation to avoid getting questions that will be answered later in the presentation?

Why do we need a voltage divider when we get the same voltage at the output as the input?

Translate the beginning of the blessing "Asher Yatzar"

Just how much information should you share with a former client?

What is the range of a Drunken Monk's Redirect attack?

How can a class have multiple methods without breaking the single responsibility principle

Why don't short runways use ramps for takeoff?

"Fewer errors means better products" or fewer errors mean better products."

Music Theory: Facts or Hierarchy of Opinions?

How does the barbarian's bonus damage from Rage interact with two-weapon fighting?



Flexbox: center horizontally and vertically


Centering content horizontally using flexboxHow to center flex block in center page?Vertically align input when using FlexboxCentering elements vertically in a fixed-height containerText doesn't align in the middleHow to center a image in a div horizontally and verticallyvertically center elements in divtext centering on CSS with flexboxCenter image vertically and horizontally in flexboxCSS Flex stretch not working together with vertical alignHow to horizontally center a <div>?How to vertically center a div for all browsers?Vertically align text next to an image?How to set the margin or padding as percentage of height of parent container?CSS center text (horizontally and vertically) inside a div blockVertically centering a div inside another divHow do I vertically center text with CSS?Flex-box: Align last row to gridvertical-align with Bootstrap 3In CSS Flexbox, why are there no “justify-items” and “justify-self” properties?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








540















How to center div horizontally, and vertically within the container using flexbox. In below example, I want each number below each other (in rows), which are centered horizontally.






.flex-container 
padding: 0;
margin: 0;
list-style: none;
display: flex;
align-items: center;
justify-content: center;

row
width: 100%;

.flex-item
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin: 10px;
line-height: 150px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;

<div class="flex-container">
<div class="row">
<span class="flex-item">1</span>
</div>
<div class="row">
<span class="flex-item">2</span>
</div>
<div class="row">
<span class="flex-item">3</span>
</div>
<div class="row">
<span class="flex-item">4</span>
</div>
</div>





http://codepen.io/anon/pen/zLxBo










share|improve this question


























  • Possible duplicate of Best way to center a <div> on a page vertically and horizontally?

    – Donald Duck
    May 1 '18 at 8:49

















540















How to center div horizontally, and vertically within the container using flexbox. In below example, I want each number below each other (in rows), which are centered horizontally.






.flex-container 
padding: 0;
margin: 0;
list-style: none;
display: flex;
align-items: center;
justify-content: center;

row
width: 100%;

.flex-item
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin: 10px;
line-height: 150px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;

<div class="flex-container">
<div class="row">
<span class="flex-item">1</span>
</div>
<div class="row">
<span class="flex-item">2</span>
</div>
<div class="row">
<span class="flex-item">3</span>
</div>
<div class="row">
<span class="flex-item">4</span>
</div>
</div>





http://codepen.io/anon/pen/zLxBo










share|improve this question


























  • Possible duplicate of Best way to center a <div> on a page vertically and horizontally?

    – Donald Duck
    May 1 '18 at 8:49













540












540








540


127






How to center div horizontally, and vertically within the container using flexbox. In below example, I want each number below each other (in rows), which are centered horizontally.






.flex-container 
padding: 0;
margin: 0;
list-style: none;
display: flex;
align-items: center;
justify-content: center;

row
width: 100%;

.flex-item
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin: 10px;
line-height: 150px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;

<div class="flex-container">
<div class="row">
<span class="flex-item">1</span>
</div>
<div class="row">
<span class="flex-item">2</span>
</div>
<div class="row">
<span class="flex-item">3</span>
</div>
<div class="row">
<span class="flex-item">4</span>
</div>
</div>





http://codepen.io/anon/pen/zLxBo










share|improve this question
















How to center div horizontally, and vertically within the container using flexbox. In below example, I want each number below each other (in rows), which are centered horizontally.






.flex-container 
padding: 0;
margin: 0;
list-style: none;
display: flex;
align-items: center;
justify-content: center;

row
width: 100%;

.flex-item
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin: 10px;
line-height: 150px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;

<div class="flex-container">
<div class="row">
<span class="flex-item">1</span>
</div>
<div class="row">
<span class="flex-item">2</span>
</div>
<div class="row">
<span class="flex-item">3</span>
</div>
<div class="row">
<span class="flex-item">4</span>
</div>
</div>





http://codepen.io/anon/pen/zLxBo






.flex-container 
padding: 0;
margin: 0;
list-style: none;
display: flex;
align-items: center;
justify-content: center;

row
width: 100%;

.flex-item
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin: 10px;
line-height: 150px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;

<div class="flex-container">
<div class="row">
<span class="flex-item">1</span>
</div>
<div class="row">
<span class="flex-item">2</span>
</div>
<div class="row">
<span class="flex-item">3</span>
</div>
<div class="row">
<span class="flex-item">4</span>
</div>
</div>





.flex-container 
padding: 0;
margin: 0;
list-style: none;
display: flex;
align-items: center;
justify-content: center;

row
width: 100%;

.flex-item
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin: 10px;
line-height: 150px;
color: white;
font-weight: bold;
font-size: 3em;
text-align: center;

<div class="flex-container">
<div class="row">
<span class="flex-item">1</span>
</div>
<div class="row">
<span class="flex-item">2</span>
</div>
<div class="row">
<span class="flex-item">3</span>
</div>
<div class="row">
<span class="flex-item">4</span>
</div>
</div>






html css html5 css3 flexbox






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 22 '16 at 21:38









Michael_B

172k51 gold badges284 silver badges396 bronze badges




172k51 gold badges284 silver badges396 bronze badges










asked Sep 26 '13 at 11:22









bsrbsr

22.8k67 gold badges184 silver badges279 bronze badges




22.8k67 gold badges184 silver badges279 bronze badges















  • Possible duplicate of Best way to center a <div> on a page vertically and horizontally?

    – Donald Duck
    May 1 '18 at 8:49

















  • Possible duplicate of Best way to center a <div> on a page vertically and horizontally?

    – Donald Duck
    May 1 '18 at 8:49
















Possible duplicate of Best way to center a <div> on a page vertically and horizontally?

– Donald Duck
May 1 '18 at 8:49





Possible duplicate of Best way to center a <div> on a page vertically and horizontally?

– Donald Duck
May 1 '18 at 8:49












10 Answers
10






active

oldest

votes


















609














I think you want something like the following.






html, body 
height: 100%;

body
margin: 0;

.flex-container
height: 100%;
padding: 0;
margin: 0;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
align-items: center;
justify-content: center;

.row
width: auto;
border: 1px solid blue;

.flex-item
background-color: tomato;
padding: 5px;
width: 20px;
height: 20px;
margin: 10px;
line-height: 20px;
color: white;
font-weight: bold;
font-size: 2em;
text-align: center;

<div class="flex-container">
<div class="row">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
<div class="flex-item">4</div>
</div>
</div>





See demo at: http://jsfiddle.net/audetwebdesign/tFscL/



Your .flex-item elements should be block level (div instead of span) if you want the height and top/bottom padding to work properly.



Also, on .row, set the width to auto instead of 100%.



Your .flex-container properties are fine.



If you want the .row to be centered vertically in the view port, assign 100% height to html and body, and also zero out the body margins.



Note that .flex-container needs a height to see the vertical alignment effect, otherwise, the container computes the minimum height needed to enclose the content, which is less than the view port height in this example.



Footnote:

The flex-flow, flex-direction, flex-wrap properties could have made this design easier to implement. I think that the .row container is not needed unless you want to add some styling around the elements (background image, borders and so on).



A useful resource is: http://demo.agektmr.com/flexbox/






share|improve this answer






















  • 5





    Flex items do not need to be block level unless the content they contain requires it. Also, you've prefixed all of the display properties, but didn't prefix any of the other Flexbox properties (which have different names in the other drafts).

    – cimmanon
    Sep 26 '13 at 14:32






  • 1





    @cimmanon I agree about with you about block level, and I edited my post accordingly. Block level is not required for alignment but may be needed if the user wants to specify height and so on. I took liberty about the browser prefixes, I just assumed a perfect browser for the sake of arriving at a working demo. Thank you again for your comment, appreciate the feedback.

    – Marc Audet
    Sep 26 '13 at 14:57







  • 1





    If it overflows, it crops the top. i.imgur.com/3dgFfQK.png Any way to avoid this?

    – Brian Gates
    Sep 4 '14 at 20:00






  • 1





    @BrianGates If the height of the window is too short, how do you want the 4 elements to be displays, 2x2, 1x4?

    – Marc Audet
    Sep 5 '14 at 11:38






  • 2





    Solution is here: stackoverflow.com/questions/24538100/…

    – Brian Gates
    Sep 5 '14 at 22:19


















223














How to Center Elements Vertically and Horizontally in Flexbox



Below are two general centering solutions.



One for vertically-aligned flex items (flex-direction: column) and the other for horizontally-aligned flex items (flex-direction: row).



In both cases the height of the centered divs can be variable, undefined, unknown, whatever. The height of the centered divs doesn't matter.



Here's the HTML for both:



<div id="container"><!-- flex container -->

<div class="box" id="bluebox"><!-- flex item -->
<p>DIV #1</p>
</div>

<div class="box" id="redbox"><!-- flex item -->
<p>DIV #2</p>
</div>

</div>



CSS (excluding decorative styles)



When flex items are stacked vertically:



#container 
display: flex; /* establish flex container */
flex-direction: column; /* make main axis vertical */
justify-content: center; /* center items vertically, in this case */
align-items: center; /* center items horizontally, in this case */
height: 300px;


.box
width: 300px;
margin: 5px;
text-align: center; /* will center text in <p>, which is not a flex item */



enter image description here



DEMO




When flex items are stacked horizontally:



Adjust the flex-direction rule from the code above.



#container 
display: flex;
flex-direction: row; /* make main axis horizontal (default setting) */
justify-content: center; /* center items horizontally, in this case */
align-items: center; /* center items vertically, in this case */
height: 300px;



enter image description here



DEMO




Centering the content of the flex items



The scope of a flex formatting context is limited to a parent-child relationship. Descendants of a flex container beyond the children do not participate in flex layout and will ignore flex properties. Essentially, flex properties are not inheritable beyond the children.



Hence, you will always need to apply display: flex or display: inline-flex to a parent element in order to apply flex properties to the child.



In order to vertically and/or horizontally center text or other content contained in a flex item, make the item a (nested) flex container, and repeat the centering rules.



.box 
display: flex;
justify-content: center;
align-items: center; /* for single line flex container */
align-content: center; /* for multi-line flex container */



More details here: How to vertically align text inside a flexbox?



Alternatively, you can apply margin: auto to the content element of the flex item.



p margin: auto; 


Learn about flex auto margins here: Methods for Aligning Flex Items (see box#56).




Centering multiple lines of flex items



When a flex container has multiple lines (due to wrapping) the align-content property will be necessary for cross-axis alignment.



From the spec:




8.4. Packing Flex Lines: the align-content
property



The align-content property aligns a flex container’s lines within the
flex container when there is extra space in the cross-axis, similar to
how justify-content aligns individual items within the main-axis.
Note, this property has no effect on a single-line flex container.




More details here: How does flex-wrap work with align-self, align-items and align-content?




Browser support



Flexbox is supported by all major browsers, except IE < 10. Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes. For a quick way to add prefixes use Autoprefixer. More details in this answer.




Centering solution for older browsers



For an alternative centering solution using CSS table and positioning properties see this answer: https://stackoverflow.com/a/31977476/3597276






share|improve this answer



























  • can we do it horizontally right, left and vertically center?

    – kapil
    Feb 28 '17 at 12:28






  • 1





    @kapil, adjust the justify-content property to space-between or space-around... jsfiddle.net/8o29y7pd/105

    – Michael_B
    Feb 28 '17 at 12:54






  • 3





    This answer should be up on top. Using Flex should be the preferred way for doing this, since it scales nicely across devices and screen. Its far more easier than using floats and managing multiple divs adhere to that.

    – SeaWarrior404
    Nov 10 '17 at 9:33











  • Ie 11 is not working properly horizontal and vertical center

    – Hemanth SP
    Mar 14 at 12:02











  • @HemanthSP, post a question with details.

    – Michael_B
    Mar 14 at 16:46


















34














Add



.container 
display: flex;
justify-content: center;
align-items: center;



to the container element of whatever you want to center. Documentation:
justify-content and
align-items.






share|improve this answer




















  • 2





    Thanks for the succinct answer. While all the verbiage above is fine, these 3 lines is exactly what I arrived here looking for (well, display: inline-flex in my case, but that was an easy edit.)

    – Jeff Ward
    May 11 '17 at 21:51



















26


















display: flex;
align-items: center;
justify-content: center;







share|improve this answer






















  • 1





    this properties can perfectly to center .align-items which center vertically and justify-content which center horizontally

    – Asiya Fatima
    Sep 28 '18 at 6:02


















25














Don't forgot to use important browsers specific attributes:



align-items: center; -->



-webkit-box-align: center;
-moz-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;


justify-content: center; -->



-webkit-box-pack: center;
-moz-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;


You could read this two links for better understanding flex:
http://css-tricks.com/almanac/properties/j/justify-content/ and
http://ptb2.me/flexbox/



Good Luck.






share|improve this answer




















  • 1





    this is a good point, for today (to support only the newest browsers) you just need the last two lines -webkit.. for safari and the last one for all the others

    – Pete Kozak
    Jul 17 '14 at 12:24







  • 1





    +1 because old 2009 and March 2012 working drafts still have significant user share (combined about 8% according to caniuse.com).

    – benebun
    Sep 24 '14 at 15:58











  • Flext doesn't work in safari, how could u solve that problem ?

    – user3378649
    Jan 25 '15 at 6:38











  • I checked the last version of safari on windows many days ago and I don't remember it very well, but I'll check and will say you. Just please tell me what version of safari did you mean? and on which OS?

    – QMaster
    Jan 25 '15 at 13:47











  • @user3378649 Latest safari version could support Flex-box, Please see this link: caniuse.com/#search=flexbox

    – QMaster
    Oct 4 '16 at 18:47


















14














1 - Set CSS on parent div to display: flex;



2 - Set CSS on parent div to flex-direction: column;
Note that this will make all content within that div line up top to bottom. This will work best if the parent div only contains the child and nothing else.



3 - Set CSS on parent div to justify-content: center;



Here is an example of what the CSS will look like:






.parentDivClass 
display: flex;
flex-direction: column;
justify-content: center;








share|improve this answer
































    6














    diplay: flex; for it's container and margin:auto; for it's item works perfect.



    NOTE: You have to setup the width and height to see the effect.






    #container
    width: 100%; /*width needs to be setup*/
    height: 150px; /*height needs to be setup*/
    display: flex;


    .item
    margin: auto; /*These will make the item in center*/
    background-color: #CCC;

    <div id="container">
    <div class="item">CENTER</div>
    </div>








    share|improve this answer
































      3














      If you need to center a text in a link this will do the trick:






      div 
      display: flex;

      width: 200px;
      height: 80px;
      background-color: yellow;


      a
      display: flex;
      align-items: center;
      justify-content: center;
      text-align: center; /* only important for multiple lines */

      padding: 0 20px;
      background-color: silver;
      border: 2px solid blue;

      <div>
      <a href="#">text</a>
      <a href="#">text with two lines</a>
      </div>








      share|improve this answer




















      • 1





        Sweet! Sometimes I feel that I'm too dump for Flexbox .) Suppose I need more practice, some of its logic is still unclear. Thanks, Leo!

        – Zoltán
        Mar 19 '17 at 20:11


















      3














      margin: auto works perfect with flexbox. It centralize vertically and horizontally.






      html, body 
      height: 100%;
      max-height: 100%;


      .flex-container
      display: flex;

      height: 100%;
      background-color: green;


      .container
      display: flex;
      margin: auto;

      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS</title>
      </head>
      <body>
      <div class="flex-container">
      <div class="container">
      <div class="row">
      <span class="flex-item">1</span>
      </div>
      <div class="row">
      <span class="flex-item">2</span>
      </div>
      <div class="row">
      <span class="flex-item">3</span>
      </div>
      <div class="row">
      <span class="flex-item">4</span>
      </div>
      </div>
      </div>
      </body>
      </html>








      share|improve this answer
































        -7














        Using CSS+



        <div class="EXTENDER">
        <div class="PADDER-CENTER">
        <div contentEditable="true">Edit this text...</div>
        </div>
        </div>


        take a look HERE






        share|improve this answer




















        • 1





          since this is easily and widely supported by modern browsers with pure flexbox your library solution is surely not necessary. Especially since he asked how to do it using flex box not a css library.

          – bungleofsketches
          Dec 8 '15 at 16:58













        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19026884%2fflexbox-center-horizontally-and-vertically%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        10 Answers
        10






        active

        oldest

        votes








        10 Answers
        10






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        609














        I think you want something like the following.






        html, body 
        height: 100%;

        body
        margin: 0;

        .flex-container
        height: 100%;
        padding: 0;
        margin: 0;
        display: -webkit-box;
        display: -moz-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
        align-items: center;
        justify-content: center;

        .row
        width: auto;
        border: 1px solid blue;

        .flex-item
        background-color: tomato;
        padding: 5px;
        width: 20px;
        height: 20px;
        margin: 10px;
        line-height: 20px;
        color: white;
        font-weight: bold;
        font-size: 2em;
        text-align: center;

        <div class="flex-container">
        <div class="row">
        <div class="flex-item">1</div>
        <div class="flex-item">2</div>
        <div class="flex-item">3</div>
        <div class="flex-item">4</div>
        </div>
        </div>





        See demo at: http://jsfiddle.net/audetwebdesign/tFscL/



        Your .flex-item elements should be block level (div instead of span) if you want the height and top/bottom padding to work properly.



        Also, on .row, set the width to auto instead of 100%.



        Your .flex-container properties are fine.



        If you want the .row to be centered vertically in the view port, assign 100% height to html and body, and also zero out the body margins.



        Note that .flex-container needs a height to see the vertical alignment effect, otherwise, the container computes the minimum height needed to enclose the content, which is less than the view port height in this example.



        Footnote:

        The flex-flow, flex-direction, flex-wrap properties could have made this design easier to implement. I think that the .row container is not needed unless you want to add some styling around the elements (background image, borders and so on).



        A useful resource is: http://demo.agektmr.com/flexbox/






        share|improve this answer






















        • 5





          Flex items do not need to be block level unless the content they contain requires it. Also, you've prefixed all of the display properties, but didn't prefix any of the other Flexbox properties (which have different names in the other drafts).

          – cimmanon
          Sep 26 '13 at 14:32






        • 1





          @cimmanon I agree about with you about block level, and I edited my post accordingly. Block level is not required for alignment but may be needed if the user wants to specify height and so on. I took liberty about the browser prefixes, I just assumed a perfect browser for the sake of arriving at a working demo. Thank you again for your comment, appreciate the feedback.

          – Marc Audet
          Sep 26 '13 at 14:57







        • 1





          If it overflows, it crops the top. i.imgur.com/3dgFfQK.png Any way to avoid this?

          – Brian Gates
          Sep 4 '14 at 20:00






        • 1





          @BrianGates If the height of the window is too short, how do you want the 4 elements to be displays, 2x2, 1x4?

          – Marc Audet
          Sep 5 '14 at 11:38






        • 2





          Solution is here: stackoverflow.com/questions/24538100/…

          – Brian Gates
          Sep 5 '14 at 22:19















        609














        I think you want something like the following.






        html, body 
        height: 100%;

        body
        margin: 0;

        .flex-container
        height: 100%;
        padding: 0;
        margin: 0;
        display: -webkit-box;
        display: -moz-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
        align-items: center;
        justify-content: center;

        .row
        width: auto;
        border: 1px solid blue;

        .flex-item
        background-color: tomato;
        padding: 5px;
        width: 20px;
        height: 20px;
        margin: 10px;
        line-height: 20px;
        color: white;
        font-weight: bold;
        font-size: 2em;
        text-align: center;

        <div class="flex-container">
        <div class="row">
        <div class="flex-item">1</div>
        <div class="flex-item">2</div>
        <div class="flex-item">3</div>
        <div class="flex-item">4</div>
        </div>
        </div>





        See demo at: http://jsfiddle.net/audetwebdesign/tFscL/



        Your .flex-item elements should be block level (div instead of span) if you want the height and top/bottom padding to work properly.



        Also, on .row, set the width to auto instead of 100%.



        Your .flex-container properties are fine.



        If you want the .row to be centered vertically in the view port, assign 100% height to html and body, and also zero out the body margins.



        Note that .flex-container needs a height to see the vertical alignment effect, otherwise, the container computes the minimum height needed to enclose the content, which is less than the view port height in this example.



        Footnote:

        The flex-flow, flex-direction, flex-wrap properties could have made this design easier to implement. I think that the .row container is not needed unless you want to add some styling around the elements (background image, borders and so on).



        A useful resource is: http://demo.agektmr.com/flexbox/






        share|improve this answer






















        • 5





          Flex items do not need to be block level unless the content they contain requires it. Also, you've prefixed all of the display properties, but didn't prefix any of the other Flexbox properties (which have different names in the other drafts).

          – cimmanon
          Sep 26 '13 at 14:32






        • 1





          @cimmanon I agree about with you about block level, and I edited my post accordingly. Block level is not required for alignment but may be needed if the user wants to specify height and so on. I took liberty about the browser prefixes, I just assumed a perfect browser for the sake of arriving at a working demo. Thank you again for your comment, appreciate the feedback.

          – Marc Audet
          Sep 26 '13 at 14:57







        • 1





          If it overflows, it crops the top. i.imgur.com/3dgFfQK.png Any way to avoid this?

          – Brian Gates
          Sep 4 '14 at 20:00






        • 1





          @BrianGates If the height of the window is too short, how do you want the 4 elements to be displays, 2x2, 1x4?

          – Marc Audet
          Sep 5 '14 at 11:38






        • 2





          Solution is here: stackoverflow.com/questions/24538100/…

          – Brian Gates
          Sep 5 '14 at 22:19













        609












        609








        609







        I think you want something like the following.






        html, body 
        height: 100%;

        body
        margin: 0;

        .flex-container
        height: 100%;
        padding: 0;
        margin: 0;
        display: -webkit-box;
        display: -moz-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
        align-items: center;
        justify-content: center;

        .row
        width: auto;
        border: 1px solid blue;

        .flex-item
        background-color: tomato;
        padding: 5px;
        width: 20px;
        height: 20px;
        margin: 10px;
        line-height: 20px;
        color: white;
        font-weight: bold;
        font-size: 2em;
        text-align: center;

        <div class="flex-container">
        <div class="row">
        <div class="flex-item">1</div>
        <div class="flex-item">2</div>
        <div class="flex-item">3</div>
        <div class="flex-item">4</div>
        </div>
        </div>





        See demo at: http://jsfiddle.net/audetwebdesign/tFscL/



        Your .flex-item elements should be block level (div instead of span) if you want the height and top/bottom padding to work properly.



        Also, on .row, set the width to auto instead of 100%.



        Your .flex-container properties are fine.



        If you want the .row to be centered vertically in the view port, assign 100% height to html and body, and also zero out the body margins.



        Note that .flex-container needs a height to see the vertical alignment effect, otherwise, the container computes the minimum height needed to enclose the content, which is less than the view port height in this example.



        Footnote:

        The flex-flow, flex-direction, flex-wrap properties could have made this design easier to implement. I think that the .row container is not needed unless you want to add some styling around the elements (background image, borders and so on).



        A useful resource is: http://demo.agektmr.com/flexbox/






        share|improve this answer















        I think you want something like the following.






        html, body 
        height: 100%;

        body
        margin: 0;

        .flex-container
        height: 100%;
        padding: 0;
        margin: 0;
        display: -webkit-box;
        display: -moz-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
        align-items: center;
        justify-content: center;

        .row
        width: auto;
        border: 1px solid blue;

        .flex-item
        background-color: tomato;
        padding: 5px;
        width: 20px;
        height: 20px;
        margin: 10px;
        line-height: 20px;
        color: white;
        font-weight: bold;
        font-size: 2em;
        text-align: center;

        <div class="flex-container">
        <div class="row">
        <div class="flex-item">1</div>
        <div class="flex-item">2</div>
        <div class="flex-item">3</div>
        <div class="flex-item">4</div>
        </div>
        </div>





        See demo at: http://jsfiddle.net/audetwebdesign/tFscL/



        Your .flex-item elements should be block level (div instead of span) if you want the height and top/bottom padding to work properly.



        Also, on .row, set the width to auto instead of 100%.



        Your .flex-container properties are fine.



        If you want the .row to be centered vertically in the view port, assign 100% height to html and body, and also zero out the body margins.



        Note that .flex-container needs a height to see the vertical alignment effect, otherwise, the container computes the minimum height needed to enclose the content, which is less than the view port height in this example.



        Footnote:

        The flex-flow, flex-direction, flex-wrap properties could have made this design easier to implement. I think that the .row container is not needed unless you want to add some styling around the elements (background image, borders and so on).



        A useful resource is: http://demo.agektmr.com/flexbox/






        html, body 
        height: 100%;

        body
        margin: 0;

        .flex-container
        height: 100%;
        padding: 0;
        margin: 0;
        display: -webkit-box;
        display: -moz-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
        align-items: center;
        justify-content: center;

        .row
        width: auto;
        border: 1px solid blue;

        .flex-item
        background-color: tomato;
        padding: 5px;
        width: 20px;
        height: 20px;
        margin: 10px;
        line-height: 20px;
        color: white;
        font-weight: bold;
        font-size: 2em;
        text-align: center;

        <div class="flex-container">
        <div class="row">
        <div class="flex-item">1</div>
        <div class="flex-item">2</div>
        <div class="flex-item">3</div>
        <div class="flex-item">4</div>
        </div>
        </div>





        html, body 
        height: 100%;

        body
        margin: 0;

        .flex-container
        height: 100%;
        padding: 0;
        margin: 0;
        display: -webkit-box;
        display: -moz-box;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
        align-items: center;
        justify-content: center;

        .row
        width: auto;
        border: 1px solid blue;

        .flex-item
        background-color: tomato;
        padding: 5px;
        width: 20px;
        height: 20px;
        margin: 10px;
        line-height: 20px;
        color: white;
        font-weight: bold;
        font-size: 2em;
        text-align: center;

        <div class="flex-container">
        <div class="row">
        <div class="flex-item">1</div>
        <div class="flex-item">2</div>
        <div class="flex-item">3</div>
        <div class="flex-item">4</div>
        </div>
        </div>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Oct 26 '16 at 9:34









        nalzok

        7,01210 gold badges37 silver badges81 bronze badges




        7,01210 gold badges37 silver badges81 bronze badges










        answered Sep 26 '13 at 12:13









        Marc AudetMarc Audet

        37.9k10 gold badges53 silver badges73 bronze badges




        37.9k10 gold badges53 silver badges73 bronze badges










        • 5





          Flex items do not need to be block level unless the content they contain requires it. Also, you've prefixed all of the display properties, but didn't prefix any of the other Flexbox properties (which have different names in the other drafts).

          – cimmanon
          Sep 26 '13 at 14:32






        • 1





          @cimmanon I agree about with you about block level, and I edited my post accordingly. Block level is not required for alignment but may be needed if the user wants to specify height and so on. I took liberty about the browser prefixes, I just assumed a perfect browser for the sake of arriving at a working demo. Thank you again for your comment, appreciate the feedback.

          – Marc Audet
          Sep 26 '13 at 14:57







        • 1





          If it overflows, it crops the top. i.imgur.com/3dgFfQK.png Any way to avoid this?

          – Brian Gates
          Sep 4 '14 at 20:00






        • 1





          @BrianGates If the height of the window is too short, how do you want the 4 elements to be displays, 2x2, 1x4?

          – Marc Audet
          Sep 5 '14 at 11:38






        • 2





          Solution is here: stackoverflow.com/questions/24538100/…

          – Brian Gates
          Sep 5 '14 at 22:19












        • 5





          Flex items do not need to be block level unless the content they contain requires it. Also, you've prefixed all of the display properties, but didn't prefix any of the other Flexbox properties (which have different names in the other drafts).

          – cimmanon
          Sep 26 '13 at 14:32






        • 1





          @cimmanon I agree about with you about block level, and I edited my post accordingly. Block level is not required for alignment but may be needed if the user wants to specify height and so on. I took liberty about the browser prefixes, I just assumed a perfect browser for the sake of arriving at a working demo. Thank you again for your comment, appreciate the feedback.

          – Marc Audet
          Sep 26 '13 at 14:57







        • 1





          If it overflows, it crops the top. i.imgur.com/3dgFfQK.png Any way to avoid this?

          – Brian Gates
          Sep 4 '14 at 20:00






        • 1





          @BrianGates If the height of the window is too short, how do you want the 4 elements to be displays, 2x2, 1x4?

          – Marc Audet
          Sep 5 '14 at 11:38






        • 2





          Solution is here: stackoverflow.com/questions/24538100/…

          – Brian Gates
          Sep 5 '14 at 22:19







        5




        5





        Flex items do not need to be block level unless the content they contain requires it. Also, you've prefixed all of the display properties, but didn't prefix any of the other Flexbox properties (which have different names in the other drafts).

        – cimmanon
        Sep 26 '13 at 14:32





        Flex items do not need to be block level unless the content they contain requires it. Also, you've prefixed all of the display properties, but didn't prefix any of the other Flexbox properties (which have different names in the other drafts).

        – cimmanon
        Sep 26 '13 at 14:32




        1




        1





        @cimmanon I agree about with you about block level, and I edited my post accordingly. Block level is not required for alignment but may be needed if the user wants to specify height and so on. I took liberty about the browser prefixes, I just assumed a perfect browser for the sake of arriving at a working demo. Thank you again for your comment, appreciate the feedback.

        – Marc Audet
        Sep 26 '13 at 14:57






        @cimmanon I agree about with you about block level, and I edited my post accordingly. Block level is not required for alignment but may be needed if the user wants to specify height and so on. I took liberty about the browser prefixes, I just assumed a perfect browser for the sake of arriving at a working demo. Thank you again for your comment, appreciate the feedback.

        – Marc Audet
        Sep 26 '13 at 14:57





        1




        1





        If it overflows, it crops the top. i.imgur.com/3dgFfQK.png Any way to avoid this?

        – Brian Gates
        Sep 4 '14 at 20:00





        If it overflows, it crops the top. i.imgur.com/3dgFfQK.png Any way to avoid this?

        – Brian Gates
        Sep 4 '14 at 20:00




        1




        1





        @BrianGates If the height of the window is too short, how do you want the 4 elements to be displays, 2x2, 1x4?

        – Marc Audet
        Sep 5 '14 at 11:38





        @BrianGates If the height of the window is too short, how do you want the 4 elements to be displays, 2x2, 1x4?

        – Marc Audet
        Sep 5 '14 at 11:38




        2




        2





        Solution is here: stackoverflow.com/questions/24538100/…

        – Brian Gates
        Sep 5 '14 at 22:19





        Solution is here: stackoverflow.com/questions/24538100/…

        – Brian Gates
        Sep 5 '14 at 22:19













        223














        How to Center Elements Vertically and Horizontally in Flexbox



        Below are two general centering solutions.



        One for vertically-aligned flex items (flex-direction: column) and the other for horizontally-aligned flex items (flex-direction: row).



        In both cases the height of the centered divs can be variable, undefined, unknown, whatever. The height of the centered divs doesn't matter.



        Here's the HTML for both:



        <div id="container"><!-- flex container -->

        <div class="box" id="bluebox"><!-- flex item -->
        <p>DIV #1</p>
        </div>

        <div class="box" id="redbox"><!-- flex item -->
        <p>DIV #2</p>
        </div>

        </div>



        CSS (excluding decorative styles)



        When flex items are stacked vertically:



        #container 
        display: flex; /* establish flex container */
        flex-direction: column; /* make main axis vertical */
        justify-content: center; /* center items vertically, in this case */
        align-items: center; /* center items horizontally, in this case */
        height: 300px;


        .box
        width: 300px;
        margin: 5px;
        text-align: center; /* will center text in <p>, which is not a flex item */



        enter image description here



        DEMO




        When flex items are stacked horizontally:



        Adjust the flex-direction rule from the code above.



        #container 
        display: flex;
        flex-direction: row; /* make main axis horizontal (default setting) */
        justify-content: center; /* center items horizontally, in this case */
        align-items: center; /* center items vertically, in this case */
        height: 300px;



        enter image description here



        DEMO




        Centering the content of the flex items



        The scope of a flex formatting context is limited to a parent-child relationship. Descendants of a flex container beyond the children do not participate in flex layout and will ignore flex properties. Essentially, flex properties are not inheritable beyond the children.



        Hence, you will always need to apply display: flex or display: inline-flex to a parent element in order to apply flex properties to the child.



        In order to vertically and/or horizontally center text or other content contained in a flex item, make the item a (nested) flex container, and repeat the centering rules.



        .box 
        display: flex;
        justify-content: center;
        align-items: center; /* for single line flex container */
        align-content: center; /* for multi-line flex container */



        More details here: How to vertically align text inside a flexbox?



        Alternatively, you can apply margin: auto to the content element of the flex item.



        p margin: auto; 


        Learn about flex auto margins here: Methods for Aligning Flex Items (see box#56).




        Centering multiple lines of flex items



        When a flex container has multiple lines (due to wrapping) the align-content property will be necessary for cross-axis alignment.



        From the spec:




        8.4. Packing Flex Lines: the align-content
        property



        The align-content property aligns a flex container’s lines within the
        flex container when there is extra space in the cross-axis, similar to
        how justify-content aligns individual items within the main-axis.
        Note, this property has no effect on a single-line flex container.




        More details here: How does flex-wrap work with align-self, align-items and align-content?




        Browser support



        Flexbox is supported by all major browsers, except IE < 10. Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes. For a quick way to add prefixes use Autoprefixer. More details in this answer.




        Centering solution for older browsers



        For an alternative centering solution using CSS table and positioning properties see this answer: https://stackoverflow.com/a/31977476/3597276






        share|improve this answer



























        • can we do it horizontally right, left and vertically center?

          – kapil
          Feb 28 '17 at 12:28






        • 1





          @kapil, adjust the justify-content property to space-between or space-around... jsfiddle.net/8o29y7pd/105

          – Michael_B
          Feb 28 '17 at 12:54






        • 3





          This answer should be up on top. Using Flex should be the preferred way for doing this, since it scales nicely across devices and screen. Its far more easier than using floats and managing multiple divs adhere to that.

          – SeaWarrior404
          Nov 10 '17 at 9:33











        • Ie 11 is not working properly horizontal and vertical center

          – Hemanth SP
          Mar 14 at 12:02











        • @HemanthSP, post a question with details.

          – Michael_B
          Mar 14 at 16:46















        223














        How to Center Elements Vertically and Horizontally in Flexbox



        Below are two general centering solutions.



        One for vertically-aligned flex items (flex-direction: column) and the other for horizontally-aligned flex items (flex-direction: row).



        In both cases the height of the centered divs can be variable, undefined, unknown, whatever. The height of the centered divs doesn't matter.



        Here's the HTML for both:



        <div id="container"><!-- flex container -->

        <div class="box" id="bluebox"><!-- flex item -->
        <p>DIV #1</p>
        </div>

        <div class="box" id="redbox"><!-- flex item -->
        <p>DIV #2</p>
        </div>

        </div>



        CSS (excluding decorative styles)



        When flex items are stacked vertically:



        #container 
        display: flex; /* establish flex container */
        flex-direction: column; /* make main axis vertical */
        justify-content: center; /* center items vertically, in this case */
        align-items: center; /* center items horizontally, in this case */
        height: 300px;


        .box
        width: 300px;
        margin: 5px;
        text-align: center; /* will center text in <p>, which is not a flex item */



        enter image description here



        DEMO




        When flex items are stacked horizontally:



        Adjust the flex-direction rule from the code above.



        #container 
        display: flex;
        flex-direction: row; /* make main axis horizontal (default setting) */
        justify-content: center; /* center items horizontally, in this case */
        align-items: center; /* center items vertically, in this case */
        height: 300px;



        enter image description here



        DEMO




        Centering the content of the flex items



        The scope of a flex formatting context is limited to a parent-child relationship. Descendants of a flex container beyond the children do not participate in flex layout and will ignore flex properties. Essentially, flex properties are not inheritable beyond the children.



        Hence, you will always need to apply display: flex or display: inline-flex to a parent element in order to apply flex properties to the child.



        In order to vertically and/or horizontally center text or other content contained in a flex item, make the item a (nested) flex container, and repeat the centering rules.



        .box 
        display: flex;
        justify-content: center;
        align-items: center; /* for single line flex container */
        align-content: center; /* for multi-line flex container */



        More details here: How to vertically align text inside a flexbox?



        Alternatively, you can apply margin: auto to the content element of the flex item.



        p margin: auto; 


        Learn about flex auto margins here: Methods for Aligning Flex Items (see box#56).




        Centering multiple lines of flex items



        When a flex container has multiple lines (due to wrapping) the align-content property will be necessary for cross-axis alignment.



        From the spec:




        8.4. Packing Flex Lines: the align-content
        property



        The align-content property aligns a flex container’s lines within the
        flex container when there is extra space in the cross-axis, similar to
        how justify-content aligns individual items within the main-axis.
        Note, this property has no effect on a single-line flex container.




        More details here: How does flex-wrap work with align-self, align-items and align-content?




        Browser support



        Flexbox is supported by all major browsers, except IE < 10. Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes. For a quick way to add prefixes use Autoprefixer. More details in this answer.




        Centering solution for older browsers



        For an alternative centering solution using CSS table and positioning properties see this answer: https://stackoverflow.com/a/31977476/3597276






        share|improve this answer



























        • can we do it horizontally right, left and vertically center?

          – kapil
          Feb 28 '17 at 12:28






        • 1





          @kapil, adjust the justify-content property to space-between or space-around... jsfiddle.net/8o29y7pd/105

          – Michael_B
          Feb 28 '17 at 12:54






        • 3





          This answer should be up on top. Using Flex should be the preferred way for doing this, since it scales nicely across devices and screen. Its far more easier than using floats and managing multiple divs adhere to that.

          – SeaWarrior404
          Nov 10 '17 at 9:33











        • Ie 11 is not working properly horizontal and vertical center

          – Hemanth SP
          Mar 14 at 12:02











        • @HemanthSP, post a question with details.

          – Michael_B
          Mar 14 at 16:46













        223












        223








        223







        How to Center Elements Vertically and Horizontally in Flexbox



        Below are two general centering solutions.



        One for vertically-aligned flex items (flex-direction: column) and the other for horizontally-aligned flex items (flex-direction: row).



        In both cases the height of the centered divs can be variable, undefined, unknown, whatever. The height of the centered divs doesn't matter.



        Here's the HTML for both:



        <div id="container"><!-- flex container -->

        <div class="box" id="bluebox"><!-- flex item -->
        <p>DIV #1</p>
        </div>

        <div class="box" id="redbox"><!-- flex item -->
        <p>DIV #2</p>
        </div>

        </div>



        CSS (excluding decorative styles)



        When flex items are stacked vertically:



        #container 
        display: flex; /* establish flex container */
        flex-direction: column; /* make main axis vertical */
        justify-content: center; /* center items vertically, in this case */
        align-items: center; /* center items horizontally, in this case */
        height: 300px;


        .box
        width: 300px;
        margin: 5px;
        text-align: center; /* will center text in <p>, which is not a flex item */



        enter image description here



        DEMO




        When flex items are stacked horizontally:



        Adjust the flex-direction rule from the code above.



        #container 
        display: flex;
        flex-direction: row; /* make main axis horizontal (default setting) */
        justify-content: center; /* center items horizontally, in this case */
        align-items: center; /* center items vertically, in this case */
        height: 300px;



        enter image description here



        DEMO




        Centering the content of the flex items



        The scope of a flex formatting context is limited to a parent-child relationship. Descendants of a flex container beyond the children do not participate in flex layout and will ignore flex properties. Essentially, flex properties are not inheritable beyond the children.



        Hence, you will always need to apply display: flex or display: inline-flex to a parent element in order to apply flex properties to the child.



        In order to vertically and/or horizontally center text or other content contained in a flex item, make the item a (nested) flex container, and repeat the centering rules.



        .box 
        display: flex;
        justify-content: center;
        align-items: center; /* for single line flex container */
        align-content: center; /* for multi-line flex container */



        More details here: How to vertically align text inside a flexbox?



        Alternatively, you can apply margin: auto to the content element of the flex item.



        p margin: auto; 


        Learn about flex auto margins here: Methods for Aligning Flex Items (see box#56).




        Centering multiple lines of flex items



        When a flex container has multiple lines (due to wrapping) the align-content property will be necessary for cross-axis alignment.



        From the spec:




        8.4. Packing Flex Lines: the align-content
        property



        The align-content property aligns a flex container’s lines within the
        flex container when there is extra space in the cross-axis, similar to
        how justify-content aligns individual items within the main-axis.
        Note, this property has no effect on a single-line flex container.




        More details here: How does flex-wrap work with align-self, align-items and align-content?




        Browser support



        Flexbox is supported by all major browsers, except IE < 10. Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes. For a quick way to add prefixes use Autoprefixer. More details in this answer.




        Centering solution for older browsers



        For an alternative centering solution using CSS table and positioning properties see this answer: https://stackoverflow.com/a/31977476/3597276






        share|improve this answer















        How to Center Elements Vertically and Horizontally in Flexbox



        Below are two general centering solutions.



        One for vertically-aligned flex items (flex-direction: column) and the other for horizontally-aligned flex items (flex-direction: row).



        In both cases the height of the centered divs can be variable, undefined, unknown, whatever. The height of the centered divs doesn't matter.



        Here's the HTML for both:



        <div id="container"><!-- flex container -->

        <div class="box" id="bluebox"><!-- flex item -->
        <p>DIV #1</p>
        </div>

        <div class="box" id="redbox"><!-- flex item -->
        <p>DIV #2</p>
        </div>

        </div>



        CSS (excluding decorative styles)



        When flex items are stacked vertically:



        #container 
        display: flex; /* establish flex container */
        flex-direction: column; /* make main axis vertical */
        justify-content: center; /* center items vertically, in this case */
        align-items: center; /* center items horizontally, in this case */
        height: 300px;


        .box
        width: 300px;
        margin: 5px;
        text-align: center; /* will center text in <p>, which is not a flex item */



        enter image description here



        DEMO




        When flex items are stacked horizontally:



        Adjust the flex-direction rule from the code above.



        #container 
        display: flex;
        flex-direction: row; /* make main axis horizontal (default setting) */
        justify-content: center; /* center items horizontally, in this case */
        align-items: center; /* center items vertically, in this case */
        height: 300px;



        enter image description here



        DEMO




        Centering the content of the flex items



        The scope of a flex formatting context is limited to a parent-child relationship. Descendants of a flex container beyond the children do not participate in flex layout and will ignore flex properties. Essentially, flex properties are not inheritable beyond the children.



        Hence, you will always need to apply display: flex or display: inline-flex to a parent element in order to apply flex properties to the child.



        In order to vertically and/or horizontally center text or other content contained in a flex item, make the item a (nested) flex container, and repeat the centering rules.



        .box 
        display: flex;
        justify-content: center;
        align-items: center; /* for single line flex container */
        align-content: center; /* for multi-line flex container */



        More details here: How to vertically align text inside a flexbox?



        Alternatively, you can apply margin: auto to the content element of the flex item.



        p margin: auto; 


        Learn about flex auto margins here: Methods for Aligning Flex Items (see box#56).




        Centering multiple lines of flex items



        When a flex container has multiple lines (due to wrapping) the align-content property will be necessary for cross-axis alignment.



        From the spec:




        8.4. Packing Flex Lines: the align-content
        property



        The align-content property aligns a flex container’s lines within the
        flex container when there is extra space in the cross-axis, similar to
        how justify-content aligns individual items within the main-axis.
        Note, this property has no effect on a single-line flex container.




        More details here: How does flex-wrap work with align-self, align-items and align-content?




        Browser support



        Flexbox is supported by all major browsers, except IE < 10. Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes. For a quick way to add prefixes use Autoprefixer. More details in this answer.




        Centering solution for older browsers



        For an alternative centering solution using CSS table and positioning properties see this answer: https://stackoverflow.com/a/31977476/3597276







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Sep 3 '17 at 11:07

























        answered Oct 10 '15 at 1:06









        Michael_BMichael_B

        172k51 gold badges284 silver badges396 bronze badges




        172k51 gold badges284 silver badges396 bronze badges















        • can we do it horizontally right, left and vertically center?

          – kapil
          Feb 28 '17 at 12:28






        • 1





          @kapil, adjust the justify-content property to space-between or space-around... jsfiddle.net/8o29y7pd/105

          – Michael_B
          Feb 28 '17 at 12:54






        • 3





          This answer should be up on top. Using Flex should be the preferred way for doing this, since it scales nicely across devices and screen. Its far more easier than using floats and managing multiple divs adhere to that.

          – SeaWarrior404
          Nov 10 '17 at 9:33











        • Ie 11 is not working properly horizontal and vertical center

          – Hemanth SP
          Mar 14 at 12:02











        • @HemanthSP, post a question with details.

          – Michael_B
          Mar 14 at 16:46

















        • can we do it horizontally right, left and vertically center?

          – kapil
          Feb 28 '17 at 12:28






        • 1





          @kapil, adjust the justify-content property to space-between or space-around... jsfiddle.net/8o29y7pd/105

          – Michael_B
          Feb 28 '17 at 12:54






        • 3





          This answer should be up on top. Using Flex should be the preferred way for doing this, since it scales nicely across devices and screen. Its far more easier than using floats and managing multiple divs adhere to that.

          – SeaWarrior404
          Nov 10 '17 at 9:33











        • Ie 11 is not working properly horizontal and vertical center

          – Hemanth SP
          Mar 14 at 12:02











        • @HemanthSP, post a question with details.

          – Michael_B
          Mar 14 at 16:46
















        can we do it horizontally right, left and vertically center?

        – kapil
        Feb 28 '17 at 12:28





        can we do it horizontally right, left and vertically center?

        – kapil
        Feb 28 '17 at 12:28




        1




        1





        @kapil, adjust the justify-content property to space-between or space-around... jsfiddle.net/8o29y7pd/105

        – Michael_B
        Feb 28 '17 at 12:54





        @kapil, adjust the justify-content property to space-between or space-around... jsfiddle.net/8o29y7pd/105

        – Michael_B
        Feb 28 '17 at 12:54




        3




        3





        This answer should be up on top. Using Flex should be the preferred way for doing this, since it scales nicely across devices and screen. Its far more easier than using floats and managing multiple divs adhere to that.

        – SeaWarrior404
        Nov 10 '17 at 9:33





        This answer should be up on top. Using Flex should be the preferred way for doing this, since it scales nicely across devices and screen. Its far more easier than using floats and managing multiple divs adhere to that.

        – SeaWarrior404
        Nov 10 '17 at 9:33













        Ie 11 is not working properly horizontal and vertical center

        – Hemanth SP
        Mar 14 at 12:02





        Ie 11 is not working properly horizontal and vertical center

        – Hemanth SP
        Mar 14 at 12:02













        @HemanthSP, post a question with details.

        – Michael_B
        Mar 14 at 16:46





        @HemanthSP, post a question with details.

        – Michael_B
        Mar 14 at 16:46











        34














        Add



        .container 
        display: flex;
        justify-content: center;
        align-items: center;



        to the container element of whatever you want to center. Documentation:
        justify-content and
        align-items.






        share|improve this answer




















        • 2





          Thanks for the succinct answer. While all the verbiage above is fine, these 3 lines is exactly what I arrived here looking for (well, display: inline-flex in my case, but that was an easy edit.)

          – Jeff Ward
          May 11 '17 at 21:51
















        34














        Add



        .container 
        display: flex;
        justify-content: center;
        align-items: center;



        to the container element of whatever you want to center. Documentation:
        justify-content and
        align-items.






        share|improve this answer




















        • 2





          Thanks for the succinct answer. While all the verbiage above is fine, these 3 lines is exactly what I arrived here looking for (well, display: inline-flex in my case, but that was an easy edit.)

          – Jeff Ward
          May 11 '17 at 21:51














        34












        34








        34







        Add



        .container 
        display: flex;
        justify-content: center;
        align-items: center;



        to the container element of whatever you want to center. Documentation:
        justify-content and
        align-items.






        share|improve this answer













        Add



        .container 
        display: flex;
        justify-content: center;
        align-items: center;



        to the container element of whatever you want to center. Documentation:
        justify-content and
        align-items.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 12 '17 at 13:06









        benben

        2,4452 gold badges16 silver badges42 bronze badges




        2,4452 gold badges16 silver badges42 bronze badges










        • 2





          Thanks for the succinct answer. While all the verbiage above is fine, these 3 lines is exactly what I arrived here looking for (well, display: inline-flex in my case, but that was an easy edit.)

          – Jeff Ward
          May 11 '17 at 21:51













        • 2





          Thanks for the succinct answer. While all the verbiage above is fine, these 3 lines is exactly what I arrived here looking for (well, display: inline-flex in my case, but that was an easy edit.)

          – Jeff Ward
          May 11 '17 at 21:51








        2




        2





        Thanks for the succinct answer. While all the verbiage above is fine, these 3 lines is exactly what I arrived here looking for (well, display: inline-flex in my case, but that was an easy edit.)

        – Jeff Ward
        May 11 '17 at 21:51






        Thanks for the succinct answer. While all the verbiage above is fine, these 3 lines is exactly what I arrived here looking for (well, display: inline-flex in my case, but that was an easy edit.)

        – Jeff Ward
        May 11 '17 at 21:51












        26


















        display: flex;
        align-items: center;
        justify-content: center;







        share|improve this answer






















        • 1





          this properties can perfectly to center .align-items which center vertically and justify-content which center horizontally

          – Asiya Fatima
          Sep 28 '18 at 6:02















        26


















        display: flex;
        align-items: center;
        justify-content: center;







        share|improve this answer






















        • 1





          this properties can perfectly to center .align-items which center vertically and justify-content which center horizontally

          – Asiya Fatima
          Sep 28 '18 at 6:02













        26












        26








        26











        display: flex;
        align-items: center;
        justify-content: center;







        share|improve this answer



















        display: flex;
        align-items: center;
        justify-content: center;








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 30 '18 at 16:37









        Eureka

        7723 silver badges12 bronze badges




        7723 silver badges12 bronze badges










        answered Sep 28 '18 at 5:58









        Asiya FatimaAsiya Fatima

        1,0213 silver badges14 bronze badges




        1,0213 silver badges14 bronze badges










        • 1





          this properties can perfectly to center .align-items which center vertically and justify-content which center horizontally

          – Asiya Fatima
          Sep 28 '18 at 6:02












        • 1





          this properties can perfectly to center .align-items which center vertically and justify-content which center horizontally

          – Asiya Fatima
          Sep 28 '18 at 6:02







        1




        1





        this properties can perfectly to center .align-items which center vertically and justify-content which center horizontally

        – Asiya Fatima
        Sep 28 '18 at 6:02





        this properties can perfectly to center .align-items which center vertically and justify-content which center horizontally

        – Asiya Fatima
        Sep 28 '18 at 6:02











        25














        Don't forgot to use important browsers specific attributes:



        align-items: center; -->



        -webkit-box-align: center;
        -moz-box-align: center;
        -ms-flex-align: center;
        -webkit-align-items: center;
        align-items: center;


        justify-content: center; -->



        -webkit-box-pack: center;
        -moz-box-pack: center;
        -ms-flex-pack: center;
        -webkit-justify-content: center;
        justify-content: center;


        You could read this two links for better understanding flex:
        http://css-tricks.com/almanac/properties/j/justify-content/ and
        http://ptb2.me/flexbox/



        Good Luck.






        share|improve this answer




















        • 1





          this is a good point, for today (to support only the newest browsers) you just need the last two lines -webkit.. for safari and the last one for all the others

          – Pete Kozak
          Jul 17 '14 at 12:24







        • 1





          +1 because old 2009 and March 2012 working drafts still have significant user share (combined about 8% according to caniuse.com).

          – benebun
          Sep 24 '14 at 15:58











        • Flext doesn't work in safari, how could u solve that problem ?

          – user3378649
          Jan 25 '15 at 6:38











        • I checked the last version of safari on windows many days ago and I don't remember it very well, but I'll check and will say you. Just please tell me what version of safari did you mean? and on which OS?

          – QMaster
          Jan 25 '15 at 13:47











        • @user3378649 Latest safari version could support Flex-box, Please see this link: caniuse.com/#search=flexbox

          – QMaster
          Oct 4 '16 at 18:47















        25














        Don't forgot to use important browsers specific attributes:



        align-items: center; -->



        -webkit-box-align: center;
        -moz-box-align: center;
        -ms-flex-align: center;
        -webkit-align-items: center;
        align-items: center;


        justify-content: center; -->



        -webkit-box-pack: center;
        -moz-box-pack: center;
        -ms-flex-pack: center;
        -webkit-justify-content: center;
        justify-content: center;


        You could read this two links for better understanding flex:
        http://css-tricks.com/almanac/properties/j/justify-content/ and
        http://ptb2.me/flexbox/



        Good Luck.






        share|improve this answer




















        • 1





          this is a good point, for today (to support only the newest browsers) you just need the last two lines -webkit.. for safari and the last one for all the others

          – Pete Kozak
          Jul 17 '14 at 12:24







        • 1





          +1 because old 2009 and March 2012 working drafts still have significant user share (combined about 8% according to caniuse.com).

          – benebun
          Sep 24 '14 at 15:58











        • Flext doesn't work in safari, how could u solve that problem ?

          – user3378649
          Jan 25 '15 at 6:38











        • I checked the last version of safari on windows many days ago and I don't remember it very well, but I'll check and will say you. Just please tell me what version of safari did you mean? and on which OS?

          – QMaster
          Jan 25 '15 at 13:47











        • @user3378649 Latest safari version could support Flex-box, Please see this link: caniuse.com/#search=flexbox

          – QMaster
          Oct 4 '16 at 18:47













        25












        25








        25







        Don't forgot to use important browsers specific attributes:



        align-items: center; -->



        -webkit-box-align: center;
        -moz-box-align: center;
        -ms-flex-align: center;
        -webkit-align-items: center;
        align-items: center;


        justify-content: center; -->



        -webkit-box-pack: center;
        -moz-box-pack: center;
        -ms-flex-pack: center;
        -webkit-justify-content: center;
        justify-content: center;


        You could read this two links for better understanding flex:
        http://css-tricks.com/almanac/properties/j/justify-content/ and
        http://ptb2.me/flexbox/



        Good Luck.






        share|improve this answer













        Don't forgot to use important browsers specific attributes:



        align-items: center; -->



        -webkit-box-align: center;
        -moz-box-align: center;
        -ms-flex-align: center;
        -webkit-align-items: center;
        align-items: center;


        justify-content: center; -->



        -webkit-box-pack: center;
        -moz-box-pack: center;
        -ms-flex-pack: center;
        -webkit-justify-content: center;
        justify-content: center;


        You could read this two links for better understanding flex:
        http://css-tricks.com/almanac/properties/j/justify-content/ and
        http://ptb2.me/flexbox/



        Good Luck.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jun 14 '14 at 12:02









        QMasterQMaster

        2,4511 gold badge27 silver badges48 bronze badges




        2,4511 gold badge27 silver badges48 bronze badges










        • 1





          this is a good point, for today (to support only the newest browsers) you just need the last two lines -webkit.. for safari and the last one for all the others

          – Pete Kozak
          Jul 17 '14 at 12:24







        • 1





          +1 because old 2009 and March 2012 working drafts still have significant user share (combined about 8% according to caniuse.com).

          – benebun
          Sep 24 '14 at 15:58











        • Flext doesn't work in safari, how could u solve that problem ?

          – user3378649
          Jan 25 '15 at 6:38











        • I checked the last version of safari on windows many days ago and I don't remember it very well, but I'll check and will say you. Just please tell me what version of safari did you mean? and on which OS?

          – QMaster
          Jan 25 '15 at 13:47











        • @user3378649 Latest safari version could support Flex-box, Please see this link: caniuse.com/#search=flexbox

          – QMaster
          Oct 4 '16 at 18:47












        • 1





          this is a good point, for today (to support only the newest browsers) you just need the last two lines -webkit.. for safari and the last one for all the others

          – Pete Kozak
          Jul 17 '14 at 12:24







        • 1





          +1 because old 2009 and March 2012 working drafts still have significant user share (combined about 8% according to caniuse.com).

          – benebun
          Sep 24 '14 at 15:58











        • Flext doesn't work in safari, how could u solve that problem ?

          – user3378649
          Jan 25 '15 at 6:38











        • I checked the last version of safari on windows many days ago and I don't remember it very well, but I'll check and will say you. Just please tell me what version of safari did you mean? and on which OS?

          – QMaster
          Jan 25 '15 at 13:47











        • @user3378649 Latest safari version could support Flex-box, Please see this link: caniuse.com/#search=flexbox

          – QMaster
          Oct 4 '16 at 18:47







        1




        1





        this is a good point, for today (to support only the newest browsers) you just need the last two lines -webkit.. for safari and the last one for all the others

        – Pete Kozak
        Jul 17 '14 at 12:24






        this is a good point, for today (to support only the newest browsers) you just need the last two lines -webkit.. for safari and the last one for all the others

        – Pete Kozak
        Jul 17 '14 at 12:24





        1




        1





        +1 because old 2009 and March 2012 working drafts still have significant user share (combined about 8% according to caniuse.com).

        – benebun
        Sep 24 '14 at 15:58





        +1 because old 2009 and March 2012 working drafts still have significant user share (combined about 8% according to caniuse.com).

        – benebun
        Sep 24 '14 at 15:58













        Flext doesn't work in safari, how could u solve that problem ?

        – user3378649
        Jan 25 '15 at 6:38





        Flext doesn't work in safari, how could u solve that problem ?

        – user3378649
        Jan 25 '15 at 6:38













        I checked the last version of safari on windows many days ago and I don't remember it very well, but I'll check and will say you. Just please tell me what version of safari did you mean? and on which OS?

        – QMaster
        Jan 25 '15 at 13:47





        I checked the last version of safari on windows many days ago and I don't remember it very well, but I'll check and will say you. Just please tell me what version of safari did you mean? and on which OS?

        – QMaster
        Jan 25 '15 at 13:47













        @user3378649 Latest safari version could support Flex-box, Please see this link: caniuse.com/#search=flexbox

        – QMaster
        Oct 4 '16 at 18:47





        @user3378649 Latest safari version could support Flex-box, Please see this link: caniuse.com/#search=flexbox

        – QMaster
        Oct 4 '16 at 18:47











        14














        1 - Set CSS on parent div to display: flex;



        2 - Set CSS on parent div to flex-direction: column;
        Note that this will make all content within that div line up top to bottom. This will work best if the parent div only contains the child and nothing else.



        3 - Set CSS on parent div to justify-content: center;



        Here is an example of what the CSS will look like:






        .parentDivClass 
        display: flex;
        flex-direction: column;
        justify-content: center;








        share|improve this answer





























          14














          1 - Set CSS on parent div to display: flex;



          2 - Set CSS on parent div to flex-direction: column;
          Note that this will make all content within that div line up top to bottom. This will work best if the parent div only contains the child and nothing else.



          3 - Set CSS on parent div to justify-content: center;



          Here is an example of what the CSS will look like:






          .parentDivClass 
          display: flex;
          flex-direction: column;
          justify-content: center;








          share|improve this answer



























            14












            14








            14







            1 - Set CSS on parent div to display: flex;



            2 - Set CSS on parent div to flex-direction: column;
            Note that this will make all content within that div line up top to bottom. This will work best if the parent div only contains the child and nothing else.



            3 - Set CSS on parent div to justify-content: center;



            Here is an example of what the CSS will look like:






            .parentDivClass 
            display: flex;
            flex-direction: column;
            justify-content: center;








            share|improve this answer













            1 - Set CSS on parent div to display: flex;



            2 - Set CSS on parent div to flex-direction: column;
            Note that this will make all content within that div line up top to bottom. This will work best if the parent div only contains the child and nothing else.



            3 - Set CSS on parent div to justify-content: center;



            Here is an example of what the CSS will look like:






            .parentDivClass 
            display: flex;
            flex-direction: column;
            justify-content: center;








            .parentDivClass 
            display: flex;
            flex-direction: column;
            justify-content: center;





            .parentDivClass 
            display: flex;
            flex-direction: column;
            justify-content: center;






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 25 '18 at 22:05









            MelaniePMelanieP

            1411 silver badge5 bronze badges




            1411 silver badge5 bronze badges
























                6














                diplay: flex; for it's container and margin:auto; for it's item works perfect.



                NOTE: You have to setup the width and height to see the effect.






                #container
                width: 100%; /*width needs to be setup*/
                height: 150px; /*height needs to be setup*/
                display: flex;


                .item
                margin: auto; /*These will make the item in center*/
                background-color: #CCC;

                <div id="container">
                <div class="item">CENTER</div>
                </div>








                share|improve this answer





























                  6














                  diplay: flex; for it's container and margin:auto; for it's item works perfect.



                  NOTE: You have to setup the width and height to see the effect.






                  #container
                  width: 100%; /*width needs to be setup*/
                  height: 150px; /*height needs to be setup*/
                  display: flex;


                  .item
                  margin: auto; /*These will make the item in center*/
                  background-color: #CCC;

                  <div id="container">
                  <div class="item">CENTER</div>
                  </div>








                  share|improve this answer



























                    6












                    6








                    6







                    diplay: flex; for it's container and margin:auto; for it's item works perfect.



                    NOTE: You have to setup the width and height to see the effect.






                    #container
                    width: 100%; /*width needs to be setup*/
                    height: 150px; /*height needs to be setup*/
                    display: flex;


                    .item
                    margin: auto; /*These will make the item in center*/
                    background-color: #CCC;

                    <div id="container">
                    <div class="item">CENTER</div>
                    </div>








                    share|improve this answer













                    diplay: flex; for it's container and margin:auto; for it's item works perfect.



                    NOTE: You have to setup the width and height to see the effect.






                    #container
                    width: 100%; /*width needs to be setup*/
                    height: 150px; /*height needs to be setup*/
                    display: flex;


                    .item
                    margin: auto; /*These will make the item in center*/
                    background-color: #CCC;

                    <div id="container">
                    <div class="item">CENTER</div>
                    </div>








                    #container
                    width: 100%; /*width needs to be setup*/
                    height: 150px; /*height needs to be setup*/
                    display: flex;


                    .item
                    margin: auto; /*These will make the item in center*/
                    background-color: #CCC;

                    <div id="container">
                    <div class="item">CENTER</div>
                    </div>





                    #container
                    width: 100%; /*width needs to be setup*/
                    height: 150px; /*height needs to be setup*/
                    display: flex;


                    .item
                    margin: auto; /*These will make the item in center*/
                    background-color: #CCC;

                    <div id="container">
                    <div class="item">CENTER</div>
                    </div>






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 13 '18 at 2:27









                    PolarPolar

                    1,2621 gold badge20 silver badges35 bronze badges




                    1,2621 gold badge20 silver badges35 bronze badges
























                        3














                        If you need to center a text in a link this will do the trick:






                        div 
                        display: flex;

                        width: 200px;
                        height: 80px;
                        background-color: yellow;


                        a
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        text-align: center; /* only important for multiple lines */

                        padding: 0 20px;
                        background-color: silver;
                        border: 2px solid blue;

                        <div>
                        <a href="#">text</a>
                        <a href="#">text with two lines</a>
                        </div>








                        share|improve this answer




















                        • 1





                          Sweet! Sometimes I feel that I'm too dump for Flexbox .) Suppose I need more practice, some of its logic is still unclear. Thanks, Leo!

                          – Zoltán
                          Mar 19 '17 at 20:11















                        3














                        If you need to center a text in a link this will do the trick:






                        div 
                        display: flex;

                        width: 200px;
                        height: 80px;
                        background-color: yellow;


                        a
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        text-align: center; /* only important for multiple lines */

                        padding: 0 20px;
                        background-color: silver;
                        border: 2px solid blue;

                        <div>
                        <a href="#">text</a>
                        <a href="#">text with two lines</a>
                        </div>








                        share|improve this answer




















                        • 1





                          Sweet! Sometimes I feel that I'm too dump for Flexbox .) Suppose I need more practice, some of its logic is still unclear. Thanks, Leo!

                          – Zoltán
                          Mar 19 '17 at 20:11













                        3












                        3








                        3







                        If you need to center a text in a link this will do the trick:






                        div 
                        display: flex;

                        width: 200px;
                        height: 80px;
                        background-color: yellow;


                        a
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        text-align: center; /* only important for multiple lines */

                        padding: 0 20px;
                        background-color: silver;
                        border: 2px solid blue;

                        <div>
                        <a href="#">text</a>
                        <a href="#">text with two lines</a>
                        </div>








                        share|improve this answer













                        If you need to center a text in a link this will do the trick:






                        div 
                        display: flex;

                        width: 200px;
                        height: 80px;
                        background-color: yellow;


                        a
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        text-align: center; /* only important for multiple lines */

                        padding: 0 20px;
                        background-color: silver;
                        border: 2px solid blue;

                        <div>
                        <a href="#">text</a>
                        <a href="#">text with two lines</a>
                        </div>








                        div 
                        display: flex;

                        width: 200px;
                        height: 80px;
                        background-color: yellow;


                        a
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        text-align: center; /* only important for multiple lines */

                        padding: 0 20px;
                        background-color: silver;
                        border: 2px solid blue;

                        <div>
                        <a href="#">text</a>
                        <a href="#">text with two lines</a>
                        </div>





                        div 
                        display: flex;

                        width: 200px;
                        height: 80px;
                        background-color: yellow;


                        a
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        text-align: center; /* only important for multiple lines */

                        padding: 0 20px;
                        background-color: silver;
                        border: 2px solid blue;

                        <div>
                        <a href="#">text</a>
                        <a href="#">text with two lines</a>
                        </div>






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Mar 4 '17 at 17:47









                        LeoLeo

                        1,0091 gold badge12 silver badges22 bronze badges




                        1,0091 gold badge12 silver badges22 bronze badges










                        • 1





                          Sweet! Sometimes I feel that I'm too dump for Flexbox .) Suppose I need more practice, some of its logic is still unclear. Thanks, Leo!

                          – Zoltán
                          Mar 19 '17 at 20:11












                        • 1





                          Sweet! Sometimes I feel that I'm too dump for Flexbox .) Suppose I need more practice, some of its logic is still unclear. Thanks, Leo!

                          – Zoltán
                          Mar 19 '17 at 20:11







                        1




                        1





                        Sweet! Sometimes I feel that I'm too dump for Flexbox .) Suppose I need more practice, some of its logic is still unclear. Thanks, Leo!

                        – Zoltán
                        Mar 19 '17 at 20:11





                        Sweet! Sometimes I feel that I'm too dump for Flexbox .) Suppose I need more practice, some of its logic is still unclear. Thanks, Leo!

                        – Zoltán
                        Mar 19 '17 at 20:11











                        3














                        margin: auto works perfect with flexbox. It centralize vertically and horizontally.






                        html, body 
                        height: 100%;
                        max-height: 100%;


                        .flex-container
                        display: flex;

                        height: 100%;
                        background-color: green;


                        .container
                        display: flex;
                        margin: auto;

                        <!DOCTYPE html>
                        <html>
                        <head>
                        <meta charset="utf-8">
                        <meta name="viewport" content="width=device-width">
                        <title>JS</title>
                        </head>
                        <body>
                        <div class="flex-container">
                        <div class="container">
                        <div class="row">
                        <span class="flex-item">1</span>
                        </div>
                        <div class="row">
                        <span class="flex-item">2</span>
                        </div>
                        <div class="row">
                        <span class="flex-item">3</span>
                        </div>
                        <div class="row">
                        <span class="flex-item">4</span>
                        </div>
                        </div>
                        </div>
                        </body>
                        </html>








                        share|improve this answer





























                          3














                          margin: auto works perfect with flexbox. It centralize vertically and horizontally.






                          html, body 
                          height: 100%;
                          max-height: 100%;


                          .flex-container
                          display: flex;

                          height: 100%;
                          background-color: green;


                          .container
                          display: flex;
                          margin: auto;

                          <!DOCTYPE html>
                          <html>
                          <head>
                          <meta charset="utf-8">
                          <meta name="viewport" content="width=device-width">
                          <title>JS</title>
                          </head>
                          <body>
                          <div class="flex-container">
                          <div class="container">
                          <div class="row">
                          <span class="flex-item">1</span>
                          </div>
                          <div class="row">
                          <span class="flex-item">2</span>
                          </div>
                          <div class="row">
                          <span class="flex-item">3</span>
                          </div>
                          <div class="row">
                          <span class="flex-item">4</span>
                          </div>
                          </div>
                          </div>
                          </body>
                          </html>








                          share|improve this answer



























                            3












                            3








                            3







                            margin: auto works perfect with flexbox. It centralize vertically and horizontally.






                            html, body 
                            height: 100%;
                            max-height: 100%;


                            .flex-container
                            display: flex;

                            height: 100%;
                            background-color: green;


                            .container
                            display: flex;
                            margin: auto;

                            <!DOCTYPE html>
                            <html>
                            <head>
                            <meta charset="utf-8">
                            <meta name="viewport" content="width=device-width">
                            <title>JS</title>
                            </head>
                            <body>
                            <div class="flex-container">
                            <div class="container">
                            <div class="row">
                            <span class="flex-item">1</span>
                            </div>
                            <div class="row">
                            <span class="flex-item">2</span>
                            </div>
                            <div class="row">
                            <span class="flex-item">3</span>
                            </div>
                            <div class="row">
                            <span class="flex-item">4</span>
                            </div>
                            </div>
                            </div>
                            </body>
                            </html>








                            share|improve this answer













                            margin: auto works perfect with flexbox. It centralize vertically and horizontally.






                            html, body 
                            height: 100%;
                            max-height: 100%;


                            .flex-container
                            display: flex;

                            height: 100%;
                            background-color: green;


                            .container
                            display: flex;
                            margin: auto;

                            <!DOCTYPE html>
                            <html>
                            <head>
                            <meta charset="utf-8">
                            <meta name="viewport" content="width=device-width">
                            <title>JS</title>
                            </head>
                            <body>
                            <div class="flex-container">
                            <div class="container">
                            <div class="row">
                            <span class="flex-item">1</span>
                            </div>
                            <div class="row">
                            <span class="flex-item">2</span>
                            </div>
                            <div class="row">
                            <span class="flex-item">3</span>
                            </div>
                            <div class="row">
                            <span class="flex-item">4</span>
                            </div>
                            </div>
                            </div>
                            </body>
                            </html>








                            html, body 
                            height: 100%;
                            max-height: 100%;


                            .flex-container
                            display: flex;

                            height: 100%;
                            background-color: green;


                            .container
                            display: flex;
                            margin: auto;

                            <!DOCTYPE html>
                            <html>
                            <head>
                            <meta charset="utf-8">
                            <meta name="viewport" content="width=device-width">
                            <title>JS</title>
                            </head>
                            <body>
                            <div class="flex-container">
                            <div class="container">
                            <div class="row">
                            <span class="flex-item">1</span>
                            </div>
                            <div class="row">
                            <span class="flex-item">2</span>
                            </div>
                            <div class="row">
                            <span class="flex-item">3</span>
                            </div>
                            <div class="row">
                            <span class="flex-item">4</span>
                            </div>
                            </div>
                            </div>
                            </body>
                            </html>





                            html, body 
                            height: 100%;
                            max-height: 100%;


                            .flex-container
                            display: flex;

                            height: 100%;
                            background-color: green;


                            .container
                            display: flex;
                            margin: auto;

                            <!DOCTYPE html>
                            <html>
                            <head>
                            <meta charset="utf-8">
                            <meta name="viewport" content="width=device-width">
                            <title>JS</title>
                            </head>
                            <body>
                            <div class="flex-container">
                            <div class="container">
                            <div class="row">
                            <span class="flex-item">1</span>
                            </div>
                            <div class="row">
                            <span class="flex-item">2</span>
                            </div>
                            <div class="row">
                            <span class="flex-item">3</span>
                            </div>
                            <div class="row">
                            <span class="flex-item">4</span>
                            </div>
                            </div>
                            </div>
                            </body>
                            </html>






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Apr 20 '17 at 18:52









                            Aksana TolstoguzovaAksana Tolstoguzova

                            3212 silver badges11 bronze badges




                            3212 silver badges11 bronze badges
























                                -7














                                Using CSS+



                                <div class="EXTENDER">
                                <div class="PADDER-CENTER">
                                <div contentEditable="true">Edit this text...</div>
                                </div>
                                </div>


                                take a look HERE






                                share|improve this answer




















                                • 1





                                  since this is easily and widely supported by modern browsers with pure flexbox your library solution is surely not necessary. Especially since he asked how to do it using flex box not a css library.

                                  – bungleofsketches
                                  Dec 8 '15 at 16:58















                                -7














                                Using CSS+



                                <div class="EXTENDER">
                                <div class="PADDER-CENTER">
                                <div contentEditable="true">Edit this text...</div>
                                </div>
                                </div>


                                take a look HERE






                                share|improve this answer




















                                • 1





                                  since this is easily and widely supported by modern browsers with pure flexbox your library solution is surely not necessary. Especially since he asked how to do it using flex box not a css library.

                                  – bungleofsketches
                                  Dec 8 '15 at 16:58













                                -7












                                -7








                                -7







                                Using CSS+



                                <div class="EXTENDER">
                                <div class="PADDER-CENTER">
                                <div contentEditable="true">Edit this text...</div>
                                </div>
                                </div>


                                take a look HERE






                                share|improve this answer













                                Using CSS+



                                <div class="EXTENDER">
                                <div class="PADDER-CENTER">
                                <div contentEditable="true">Edit this text...</div>
                                </div>
                                </div>


                                take a look HERE







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Dec 1 '15 at 17:58









                                Marco AlloriMarco Allori

                                2,51422 silver badges21 bronze badges




                                2,51422 silver badges21 bronze badges










                                • 1





                                  since this is easily and widely supported by modern browsers with pure flexbox your library solution is surely not necessary. Especially since he asked how to do it using flex box not a css library.

                                  – bungleofsketches
                                  Dec 8 '15 at 16:58












                                • 1





                                  since this is easily and widely supported by modern browsers with pure flexbox your library solution is surely not necessary. Especially since he asked how to do it using flex box not a css library.

                                  – bungleofsketches
                                  Dec 8 '15 at 16:58







                                1




                                1





                                since this is easily and widely supported by modern browsers with pure flexbox your library solution is surely not necessary. Especially since he asked how to do it using flex box not a css library.

                                – bungleofsketches
                                Dec 8 '15 at 16:58





                                since this is easily and widely supported by modern browsers with pure flexbox your library solution is surely not necessary. Especially since he asked how to do it using flex box not a css library.

                                – bungleofsketches
                                Dec 8 '15 at 16:58

















                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19026884%2fflexbox-center-horizontally-and-vertically%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript