Center align grid elements [duplicate]












3
















This question already has an answer here:




  • Aligning grid items across the entire row/column (like flex items can)

    2 answers




I have been trying to center a very simple CSS grid, but it doesn't seem to work out - I would like the grid elements to stay centered when the viewport becomes too small, but one of them is going to the next line (they are centered before and after).



Can anyone tell me what I am doing wrong?






.Listing {
list-style: none;
display: grid;
grid-gap: 10px;
padding: 10px 40px;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
padding-bottom: 30px;
background-color: #71F;
justify-content: center;
}

.Listing li {
background-color: #F7F;
border: solid 1px #dddddd;
height: 150px;
}

<div id="Text">
<ul class="Listing">
<li>
<div></div>
</li>
<li>
<div></div>
</li>
<li>
<div></div>
</li>
</ul>
</div>












share|improve this question















marked as duplicate by Michael_B css
Users with the  css badge can single-handedly close css questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 0:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Try text-align: center; w3schools.com/cssref/pr_text_text-align.asp

    – Dom
    Nov 21 '18 at 20:09











  • @MoHa Did you try the answers out? You can accept an answer to mark this question as closed. (you can upvote multiple answers, but only when you reach 15 more reputation points). Welcome to StackOverflow..

    – Rachel Gallen
    Nov 21 '18 at 22:40













  • CSS Grid doesn't just center things automatically like that. There are column tracks that need to be respected. Simple centering like that works in flex, but not grid.

    – Michael_B
    Nov 22 '18 at 0:47
















3
















This question already has an answer here:




  • Aligning grid items across the entire row/column (like flex items can)

    2 answers




I have been trying to center a very simple CSS grid, but it doesn't seem to work out - I would like the grid elements to stay centered when the viewport becomes too small, but one of them is going to the next line (they are centered before and after).



Can anyone tell me what I am doing wrong?






.Listing {
list-style: none;
display: grid;
grid-gap: 10px;
padding: 10px 40px;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
padding-bottom: 30px;
background-color: #71F;
justify-content: center;
}

.Listing li {
background-color: #F7F;
border: solid 1px #dddddd;
height: 150px;
}

<div id="Text">
<ul class="Listing">
<li>
<div></div>
</li>
<li>
<div></div>
</li>
<li>
<div></div>
</li>
</ul>
</div>












share|improve this question















marked as duplicate by Michael_B css
Users with the  css badge can single-handedly close css questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 0:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Try text-align: center; w3schools.com/cssref/pr_text_text-align.asp

    – Dom
    Nov 21 '18 at 20:09











  • @MoHa Did you try the answers out? You can accept an answer to mark this question as closed. (you can upvote multiple answers, but only when you reach 15 more reputation points). Welcome to StackOverflow..

    – Rachel Gallen
    Nov 21 '18 at 22:40













  • CSS Grid doesn't just center things automatically like that. There are column tracks that need to be respected. Simple centering like that works in flex, but not grid.

    – Michael_B
    Nov 22 '18 at 0:47














3












3








3


0







This question already has an answer here:




  • Aligning grid items across the entire row/column (like flex items can)

    2 answers




I have been trying to center a very simple CSS grid, but it doesn't seem to work out - I would like the grid elements to stay centered when the viewport becomes too small, but one of them is going to the next line (they are centered before and after).



Can anyone tell me what I am doing wrong?






.Listing {
list-style: none;
display: grid;
grid-gap: 10px;
padding: 10px 40px;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
padding-bottom: 30px;
background-color: #71F;
justify-content: center;
}

.Listing li {
background-color: #F7F;
border: solid 1px #dddddd;
height: 150px;
}

<div id="Text">
<ul class="Listing">
<li>
<div></div>
</li>
<li>
<div></div>
</li>
<li>
<div></div>
</li>
</ul>
</div>












share|improve this question

















This question already has an answer here:




  • Aligning grid items across the entire row/column (like flex items can)

    2 answers




I have been trying to center a very simple CSS grid, but it doesn't seem to work out - I would like the grid elements to stay centered when the viewport becomes too small, but one of them is going to the next line (they are centered before and after).



Can anyone tell me what I am doing wrong?






.Listing {
list-style: none;
display: grid;
grid-gap: 10px;
padding: 10px 40px;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
padding-bottom: 30px;
background-color: #71F;
justify-content: center;
}

.Listing li {
background-color: #F7F;
border: solid 1px #dddddd;
height: 150px;
}

<div id="Text">
<ul class="Listing">
<li>
<div></div>
</li>
<li>
<div></div>
</li>
<li>
<div></div>
</li>
</ul>
</div>







This question already has an answer here:




  • Aligning grid items across the entire row/column (like flex items can)

    2 answers







.Listing {
list-style: none;
display: grid;
grid-gap: 10px;
padding: 10px 40px;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
padding-bottom: 30px;
background-color: #71F;
justify-content: center;
}

.Listing li {
background-color: #F7F;
border: solid 1px #dddddd;
height: 150px;
}

<div id="Text">
<ul class="Listing">
<li>
<div></div>
</li>
<li>
<div></div>
</li>
<li>
<div></div>
</li>
</ul>
</div>





.Listing {
list-style: none;
display: grid;
grid-gap: 10px;
padding: 10px 40px;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
padding-bottom: 30px;
background-color: #71F;
justify-content: center;
}

.Listing li {
background-color: #F7F;
border: solid 1px #dddddd;
height: 150px;
}

<div id="Text">
<ul class="Listing">
<li>
<div></div>
</li>
<li>
<div></div>
</li>
<li>
<div></div>
</li>
</ul>
</div>






css grid alignment






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 11:37









Rachel Gallen

20.3k135266




20.3k135266










asked Nov 21 '18 at 20:08









Mo HaMo Ha

213




213




marked as duplicate by Michael_B css
Users with the  css badge can single-handedly close css questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 0:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Michael_B css
Users with the  css badge can single-handedly close css questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 0:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Try text-align: center; w3schools.com/cssref/pr_text_text-align.asp

    – Dom
    Nov 21 '18 at 20:09











  • @MoHa Did you try the answers out? You can accept an answer to mark this question as closed. (you can upvote multiple answers, but only when you reach 15 more reputation points). Welcome to StackOverflow..

    – Rachel Gallen
    Nov 21 '18 at 22:40













  • CSS Grid doesn't just center things automatically like that. There are column tracks that need to be respected. Simple centering like that works in flex, but not grid.

    – Michael_B
    Nov 22 '18 at 0:47



















  • Try text-align: center; w3schools.com/cssref/pr_text_text-align.asp

    – Dom
    Nov 21 '18 at 20:09











  • @MoHa Did you try the answers out? You can accept an answer to mark this question as closed. (you can upvote multiple answers, but only when you reach 15 more reputation points). Welcome to StackOverflow..

    – Rachel Gallen
    Nov 21 '18 at 22:40













  • CSS Grid doesn't just center things automatically like that. There are column tracks that need to be respected. Simple centering like that works in flex, but not grid.

    – Michael_B
    Nov 22 '18 at 0:47

















Try text-align: center; w3schools.com/cssref/pr_text_text-align.asp

– Dom
Nov 21 '18 at 20:09





Try text-align: center; w3schools.com/cssref/pr_text_text-align.asp

– Dom
Nov 21 '18 at 20:09













@MoHa Did you try the answers out? You can accept an answer to mark this question as closed. (you can upvote multiple answers, but only when you reach 15 more reputation points). Welcome to StackOverflow..

– Rachel Gallen
Nov 21 '18 at 22:40







@MoHa Did you try the answers out? You can accept an answer to mark this question as closed. (you can upvote multiple answers, but only when you reach 15 more reputation points). Welcome to StackOverflow..

– Rachel Gallen
Nov 21 '18 at 22:40















CSS Grid doesn't just center things automatically like that. There are column tracks that need to be respected. Simple centering like that works in flex, but not grid.

– Michael_B
Nov 22 '18 at 0:47





CSS Grid doesn't just center things automatically like that. There are column tracks that need to be respected. Simple centering like that works in flex, but not grid.

– Michael_B
Nov 22 '18 at 0:47












3 Answers
3






active

oldest

votes


















1














You have a fixed minimum size.
grid-template-columns: repeat(auto-fill,minmax(300px, 1fr));



When you ask the grid to resize smaller than 300px it will wrap.



Change your min size to a relative unit like % or vh to make it scale.






share|improve this answer































    1














    I would recommend using percentages, as Bryce recommended above. Do bear in mind however, that when setting a width, to accommodate padding and margins. For example, in this instance, in this example, you have 3 list items which you wish to display on one line, as I understand it. It may seem logical to set a percentage dynamic width of 33%, however I would /have set it to 31%, as the padding will knock the width out a bit.
    Give this a whirl:






    .Listing {
    list-style: none;
    display: grid;
    grid-gap: 10px;
    padding: 10px 30px 30px 20px;
    grid-template-columns: repeat(auto-fill, minmax(31%, 1fr));
    background-color: #71F;
    justify-content: center;
    }

    .Listing li {
    background-color: #F7F;
    border: solid 1px #dddddd;
    height: 150px;
    }

    <div id="Text">
    <ul class="Listing">
    <li>
    <div>One</div>
    </li>
    <li>
    <div>Two</div>
    </li>
    <li>
    <div>Three</div>
    </li>
    </ul>
    </div>








    share|improve this answer



















    • 1





      Use css calc(min% - padding) to solve that instead of creating arbitrary percents

      – Bryce Howitson
      Nov 21 '18 at 22:12











    • @BryceHowitson Yes that's the most accurate solution. My answer is intended as a guide though, to explain why not to set it to exactly a third. SO, after all, is not meant as a code writing service. Thanks for the addendum though.

      – Rachel Gallen
      Nov 21 '18 at 22:17



















    0














    Well, I ended up finding a solution, pretty simple but really not elegant ... :
    I just used margin-left:50%; on the third element.



    So it works, but if anyone has a more elegant solution I will be gled to learn it.



    Thanks again






    share|improve this answer






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You have a fixed minimum size.
      grid-template-columns: repeat(auto-fill,minmax(300px, 1fr));



      When you ask the grid to resize smaller than 300px it will wrap.



      Change your min size to a relative unit like % or vh to make it scale.






      share|improve this answer




























        1














        You have a fixed minimum size.
        grid-template-columns: repeat(auto-fill,minmax(300px, 1fr));



        When you ask the grid to resize smaller than 300px it will wrap.



        Change your min size to a relative unit like % or vh to make it scale.






        share|improve this answer


























          1












          1








          1







          You have a fixed minimum size.
          grid-template-columns: repeat(auto-fill,minmax(300px, 1fr));



          When you ask the grid to resize smaller than 300px it will wrap.



          Change your min size to a relative unit like % or vh to make it scale.






          share|improve this answer













          You have a fixed minimum size.
          grid-template-columns: repeat(auto-fill,minmax(300px, 1fr));



          When you ask the grid to resize smaller than 300px it will wrap.



          Change your min size to a relative unit like % or vh to make it scale.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 20:11









          Bryce HowitsonBryce Howitson

          716212




          716212

























              1














              I would recommend using percentages, as Bryce recommended above. Do bear in mind however, that when setting a width, to accommodate padding and margins. For example, in this instance, in this example, you have 3 list items which you wish to display on one line, as I understand it. It may seem logical to set a percentage dynamic width of 33%, however I would /have set it to 31%, as the padding will knock the width out a bit.
              Give this a whirl:






              .Listing {
              list-style: none;
              display: grid;
              grid-gap: 10px;
              padding: 10px 30px 30px 20px;
              grid-template-columns: repeat(auto-fill, minmax(31%, 1fr));
              background-color: #71F;
              justify-content: center;
              }

              .Listing li {
              background-color: #F7F;
              border: solid 1px #dddddd;
              height: 150px;
              }

              <div id="Text">
              <ul class="Listing">
              <li>
              <div>One</div>
              </li>
              <li>
              <div>Two</div>
              </li>
              <li>
              <div>Three</div>
              </li>
              </ul>
              </div>








              share|improve this answer



















              • 1





                Use css calc(min% - padding) to solve that instead of creating arbitrary percents

                – Bryce Howitson
                Nov 21 '18 at 22:12











              • @BryceHowitson Yes that's the most accurate solution. My answer is intended as a guide though, to explain why not to set it to exactly a third. SO, after all, is not meant as a code writing service. Thanks for the addendum though.

                – Rachel Gallen
                Nov 21 '18 at 22:17
















              1














              I would recommend using percentages, as Bryce recommended above. Do bear in mind however, that when setting a width, to accommodate padding and margins. For example, in this instance, in this example, you have 3 list items which you wish to display on one line, as I understand it. It may seem logical to set a percentage dynamic width of 33%, however I would /have set it to 31%, as the padding will knock the width out a bit.
              Give this a whirl:






              .Listing {
              list-style: none;
              display: grid;
              grid-gap: 10px;
              padding: 10px 30px 30px 20px;
              grid-template-columns: repeat(auto-fill, minmax(31%, 1fr));
              background-color: #71F;
              justify-content: center;
              }

              .Listing li {
              background-color: #F7F;
              border: solid 1px #dddddd;
              height: 150px;
              }

              <div id="Text">
              <ul class="Listing">
              <li>
              <div>One</div>
              </li>
              <li>
              <div>Two</div>
              </li>
              <li>
              <div>Three</div>
              </li>
              </ul>
              </div>








              share|improve this answer



















              • 1





                Use css calc(min% - padding) to solve that instead of creating arbitrary percents

                – Bryce Howitson
                Nov 21 '18 at 22:12











              • @BryceHowitson Yes that's the most accurate solution. My answer is intended as a guide though, to explain why not to set it to exactly a third. SO, after all, is not meant as a code writing service. Thanks for the addendum though.

                – Rachel Gallen
                Nov 21 '18 at 22:17














              1












              1








              1







              I would recommend using percentages, as Bryce recommended above. Do bear in mind however, that when setting a width, to accommodate padding and margins. For example, in this instance, in this example, you have 3 list items which you wish to display on one line, as I understand it. It may seem logical to set a percentage dynamic width of 33%, however I would /have set it to 31%, as the padding will knock the width out a bit.
              Give this a whirl:






              .Listing {
              list-style: none;
              display: grid;
              grid-gap: 10px;
              padding: 10px 30px 30px 20px;
              grid-template-columns: repeat(auto-fill, minmax(31%, 1fr));
              background-color: #71F;
              justify-content: center;
              }

              .Listing li {
              background-color: #F7F;
              border: solid 1px #dddddd;
              height: 150px;
              }

              <div id="Text">
              <ul class="Listing">
              <li>
              <div>One</div>
              </li>
              <li>
              <div>Two</div>
              </li>
              <li>
              <div>Three</div>
              </li>
              </ul>
              </div>








              share|improve this answer













              I would recommend using percentages, as Bryce recommended above. Do bear in mind however, that when setting a width, to accommodate padding and margins. For example, in this instance, in this example, you have 3 list items which you wish to display on one line, as I understand it. It may seem logical to set a percentage dynamic width of 33%, however I would /have set it to 31%, as the padding will knock the width out a bit.
              Give this a whirl:






              .Listing {
              list-style: none;
              display: grid;
              grid-gap: 10px;
              padding: 10px 30px 30px 20px;
              grid-template-columns: repeat(auto-fill, minmax(31%, 1fr));
              background-color: #71F;
              justify-content: center;
              }

              .Listing li {
              background-color: #F7F;
              border: solid 1px #dddddd;
              height: 150px;
              }

              <div id="Text">
              <ul class="Listing">
              <li>
              <div>One</div>
              </li>
              <li>
              <div>Two</div>
              </li>
              <li>
              <div>Three</div>
              </li>
              </ul>
              </div>








              .Listing {
              list-style: none;
              display: grid;
              grid-gap: 10px;
              padding: 10px 30px 30px 20px;
              grid-template-columns: repeat(auto-fill, minmax(31%, 1fr));
              background-color: #71F;
              justify-content: center;
              }

              .Listing li {
              background-color: #F7F;
              border: solid 1px #dddddd;
              height: 150px;
              }

              <div id="Text">
              <ul class="Listing">
              <li>
              <div>One</div>
              </li>
              <li>
              <div>Two</div>
              </li>
              <li>
              <div>Three</div>
              </li>
              </ul>
              </div>





              .Listing {
              list-style: none;
              display: grid;
              grid-gap: 10px;
              padding: 10px 30px 30px 20px;
              grid-template-columns: repeat(auto-fill, minmax(31%, 1fr));
              background-color: #71F;
              justify-content: center;
              }

              .Listing li {
              background-color: #F7F;
              border: solid 1px #dddddd;
              height: 150px;
              }

              <div id="Text">
              <ul class="Listing">
              <li>
              <div>One</div>
              </li>
              <li>
              <div>Two</div>
              </li>
              <li>
              <div>Three</div>
              </li>
              </ul>
              </div>






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 21 '18 at 20:40









              Rachel GallenRachel Gallen

              20.3k135266




              20.3k135266








              • 1





                Use css calc(min% - padding) to solve that instead of creating arbitrary percents

                – Bryce Howitson
                Nov 21 '18 at 22:12











              • @BryceHowitson Yes that's the most accurate solution. My answer is intended as a guide though, to explain why not to set it to exactly a third. SO, after all, is not meant as a code writing service. Thanks for the addendum though.

                – Rachel Gallen
                Nov 21 '18 at 22:17














              • 1





                Use css calc(min% - padding) to solve that instead of creating arbitrary percents

                – Bryce Howitson
                Nov 21 '18 at 22:12











              • @BryceHowitson Yes that's the most accurate solution. My answer is intended as a guide though, to explain why not to set it to exactly a third. SO, after all, is not meant as a code writing service. Thanks for the addendum though.

                – Rachel Gallen
                Nov 21 '18 at 22:17








              1




              1





              Use css calc(min% - padding) to solve that instead of creating arbitrary percents

              – Bryce Howitson
              Nov 21 '18 at 22:12





              Use css calc(min% - padding) to solve that instead of creating arbitrary percents

              – Bryce Howitson
              Nov 21 '18 at 22:12













              @BryceHowitson Yes that's the most accurate solution. My answer is intended as a guide though, to explain why not to set it to exactly a third. SO, after all, is not meant as a code writing service. Thanks for the addendum though.

              – Rachel Gallen
              Nov 21 '18 at 22:17





              @BryceHowitson Yes that's the most accurate solution. My answer is intended as a guide though, to explain why not to set it to exactly a third. SO, after all, is not meant as a code writing service. Thanks for the addendum though.

              – Rachel Gallen
              Nov 21 '18 at 22:17











              0














              Well, I ended up finding a solution, pretty simple but really not elegant ... :
              I just used margin-left:50%; on the third element.



              So it works, but if anyone has a more elegant solution I will be gled to learn it.



              Thanks again






              share|improve this answer




























                0














                Well, I ended up finding a solution, pretty simple but really not elegant ... :
                I just used margin-left:50%; on the third element.



                So it works, but if anyone has a more elegant solution I will be gled to learn it.



                Thanks again






                share|improve this answer


























                  0












                  0








                  0







                  Well, I ended up finding a solution, pretty simple but really not elegant ... :
                  I just used margin-left:50%; on the third element.



                  So it works, but if anyone has a more elegant solution I will be gled to learn it.



                  Thanks again






                  share|improve this answer













                  Well, I ended up finding a solution, pretty simple but really not elegant ... :
                  I just used margin-left:50%; on the third element.



                  So it works, but if anyone has a more elegant solution I will be gled to learn it.



                  Thanks again







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 0:45









                  Mo HaMo Ha

                  213




                  213















                      Popular posts from this blog

                      If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                      Alcedinidae

                      Can an atomic nucleus contain both particles and antiparticles? [duplicate]