0

I want a column of 4 boxes, that are responsive and also stay in the center of the page, the rest is explained in the comments of the code

I found a solution but it prevents it from being in the center, I included everything in the code i tried.

  • This resizes it but it prevents it from being in the center for some reason.

/* THIS RESIZES IT, BUT PREVENTS IT FROM BEING IN THE CENTER FOR SOME REASON */

.wrapper {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr 1fr 1fr 1fr;
  grid-template-areas: "1" "2" "3" "4";
  position: relative;
  top: 25rem;
  left: 50%;
  transform: translate(-50%, -50%);
}

.wrap {
  min-width: 30rem;
  max-width: 60rem;
  padding: 2rem;
  margin: 1rem;
  border: 3px solid #fff;
  background-color: #c8e6c9;
}
<div class="wrapper">
  <div class="wrap">1</div>
  <div class="wrap">2</div>
  <div class="wrap">3</div>
  <div class="wrap">4</div>
</div>

  • this is perfect, but doesn't resize it

/*  THIS IS PERFECT, EXCEPT IT DOESN'T RESIZE IT */

.wrapper {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr 1fr 1fr 1fr;
  grid-template-areas: "1" "2" "3" "4";
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.wrap {
  min-width: 30rem;
  max-width: 60rem;
  padding: 2rem;
  margin: 1rem;
  border: 3px solid #fff;
  background-color: #c8e6c9;
}
<div class="wrapper">
  <div class="wrap">1</div>
  <div class="wrap">2</div>
  <div class="wrap">3</div>
  <div class="wrap">4</div>
</div>

Thanks a lot.

How I would like to have it

8
  • Just center the container. Commented Jul 31, 2019 at 17:22
  • can you also share your HTMl. You want your grid in the middle of the window ? is there anything else ? ..wrap is your four children ? Commented Jul 31, 2019 at 17:22
  • i added the html, Commented Jul 31, 2019 at 17:24
  • when i use align-items: center; or justify-content: center; nothing happends Commented Jul 31, 2019 at 17:25
  • @JCode , wait, i want to use your solution instead, is there a way to disable the horizontal scrolling and keep it in the center? Commented Jul 31, 2019 at 18:54

2 Answers 2

1

You can use html and body to center your content via flex :

body {
  min-height: 100vh;
  display: flex;
  margin: 0;
}

.wrapper {
  margin: auto;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr 1fr 1fr 1fr;
  grid-template-areas: "1" "2" "3" "4";
}

.wrap {
  min-width: 30rem;
  max-width: 60rem;
  padding: 2rem;
  margin: 1rem;
  border: 3px solid #fff;
  background-color: #c8e6c9;
}
<div class="wrapper">
  <div class="wrap">1</div>
  <div class="wrap">2</div>
  <div class="wrap">3</div>
  <div class="wrap">4</div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

You just need to create a container around it:

<body>
    <div class="cntri">
        <div class="wrapper">
                <div class="wrap">1</div>
                <div class="wrap">2</div>
                <div class="wrap">3</div>
                <div class="wrap">4</div>
        </div>
    </div>
</body>

.wrapper{
    flex-grow: 1;
    max-width: 30rem;
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 1fr 1fr 1fr;
    grid-template-areas: "1" "2" "3" "4";
}

.wrap{
    padding: 2rem;
    margin: 1rem;
    border: 3px solid #fff;
    background-color: #c8e6c9;
}

.cntri
{
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    align-content: center;
}

Try this fiddle, I think it fits your image:

https://jsfiddle.net/bf7Lp6ug/7/

15 Comments

thanks, but I meant column and not row, i misspelled that in my post, sorry
This still works for centering in column really - let me edit it. @LynxLead
@LynxLead Okay, maybe show us how it looks now and how you want it to look.
sure, i photoshopped an image that should explain it
this is great, however, this doesn't resize the box with that space like in the screenshot from Solution1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.