LynxLead
LynxLead

Reputation: 5

How to make CSS Grid resize the content

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 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, 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

Upvotes: 0

Views: 4072

Answers (2)

JTinkers
JTinkers

Reputation: 1789

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/

Upvotes: 0

G-Cyrillus
G-Cyrillus

Reputation: 105863

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>

Upvotes: 1

Related Questions