/    Sign up×
Community /Pin to ProfileBookmark

Grid (overflow) and margin

Hello everyone,

I have a question regarding CSS grid : I’ve created a grid with some texts and images … However, if my text is too big to be inside one of my column/row, it will make it bigger and the text spread accross two lines. How do I correct that ? Is there a way to make this text overflow somehow, or separated from the grid column in order to prevent these effects ?

Also, I’ve created a container inside the body, my body has a height, and so is my container … But when I set a margin (auto auto), the top doesn’t work. I’ve read somewhere this is because of margin collapses, and I tried to rectify this by setting some padding on body or container, make it overflow, but nothing works. Do you have any idea how to correct this ?

Thank you, and have a great day.

  • Frederic
  • to post a comment
    CSS

    18 Comments(s)

    Copy linkTweet thisAlerts:
    @SempervivumFeb 02.2022 — It would be helpful if one could view the code in question, HTML and CSS. In case the forum software doesn't allow you posting it you can use https://jsfiddle.net or https://pastebin.com to make it available.
    Copy linkTweet thisAlerts:
    @FredericauthorFeb 02.2022 — The HTML <body>
    <section class="pop-up">
    <div class="container">
    <h1>Bienvenue sur le site <strong>enuhe.fr</strong></h1>
    <div class="fred">
    <img src="fred.jpg" alt="Fred" class="photo">
    <h2>Frédéric Ferreira</h2>
    </div>
    <div class="dvp">
    <img src="code.png" class="png">
    <h3 class="skills">Développeur Web</h3>
    </div>
    <div class="mind">
    <img src="meditation.png" class="png">
    <h3 class="skills">Praticien Mindfulness</h3>
    </div>
    </div>
    </section>
    Copy linkTweet thisAlerts:
    @FredericauthorFeb 02.2022 — the CSS @import url('https://fonts.googleapis.com/css2?family=Nunito:wght@200;400&display=swap');

    *:not(strong) {
    margin: 0;
    padding: 0;
    box-sizing: inherit;
    font-weight: 200;
    }

    body {
    height : 100vh;
    background-color: white;
    }

    .pop-up {
    display: flex;
    justify-content: center;
    align-items: center;
    }

    .container {
    font-family: 'Nunito', sans-serif;
    font-weight: 200;
    color: white;
    font-size: 20px;
    height: 600px;
    width: 900px;
    background: linear-gradient(to top right, #e8cbc0 , #636fa4 );
    border-radius: 15px;
    box-shadow: 0 3rem 5rem rgba(0, 0, 0, 0.25);
    display: grid;
    justify-items: center;
    align-items: center;
    grid-template-columns: repeat(8, minmax(100px, 1fr));
    grid-template-rows: auto;
    grid-template-areas:
    " . title title title title title title ."
    ". . . header header . . ."
    ". . . header header . . ."
    ". footer footer . . foter foter ."
    ". footer footer . . foter foter .";
    }


    .container h1 {
    font-size: 40px;
    grid-area: title;
    }

    .fred {
    grid-area: header;
    text-align: center;
    }

    .photo{
    height: 160px;
    width: 160px;
    border-radius: 50%;
    border: solid 1px white;
    }

    .photo:hover {
    transform: scale(1.1);
    transition-duration: 0.5s;
    }

    .fred h2 {
    margin-top: 15px;
    }

    .dvp {
    grid-area: footer;
    text-align: center;
    }

    .mind {
    grid-area: foter;
    text-align: center;
    }

    .png {
    height: 130px;
    width: 130px;
    padding: 5px 40px;
    background-color: rgba(255, 255, 255, 0.25);
    border-radius: 15px;
    }

    .png:hover{
    background-color: rgba(255, 255, 255, 0.5);
    }

    .png:active {
    transform: translate(5px, 3px);
    box-shadow: 0 1rem 2rem rgba(0, 0, 0, 0.15);
    }

    .skills {
    color: black;
    font-weight: bold;
    text-transform: uppercase;
    font-size: 20px;
    }

    .container strong:active, .skills:active {
    text-decoration: underline;
    color:slateblue
    }

    .fred img, .png, .skills, .container strong {
    cursor: pointer;
    }
    Copy linkTweet thisAlerts:
    @FredericauthorFeb 02.2022 — Also if you see some beginner errors inside my code, and think it should be written in another way I would be glad to hear it 😃
    Copy linkTweet thisAlerts:
    @FredericauthorFeb 02.2022 — @import url('https://fonts.googleapis.com/css2?family=Nunito:wght@200;400&display=swap');

    *:not(strong) {
    margin: 0;
    padding: 0;
    box-sizing: inherit;
    font-weight: 200;
    }

    body {
    height : 100vh;
    background-color: white;
    }

    /* .pop-up {
    display: flex;
    justify-content: center;
    align-items: center;
    } */

    .container {
    font-family: 'Nunito', sans-serif;
    margin: auto auto;
    font-weight: 200;
    color: white;
    font-size: 20px;
    height: 600px;
    width: 900px;
    background: linear-gradient(to top right, #e8cbc0 , #636fa4 );
    border-radius: 15px;
    box-shadow: 0 3rem 5rem rgba(0, 0, 0, 0.25);
    display: grid;
    justify-items: center;
    align-items: center;
    grid-template-columns: repeat(8, minmax(100px, 1fr));
    grid-template-rows: auto;
    grid-template-areas:
    " . title title title title title title ."
    ". . . header header . . ."
    ". . . header header . . ."
    ". footer footer . . foter foter ."
    ". footer footer . . foter foter .";
    }
    Copy linkTweet thisAlerts:
    @FredericauthorFeb 02.2022 — I put the two tryouts I made : the first by creating a flex box with the parent div of my .container, and this other case, just putting margin (auto auto) without the flexbox.
    Copy linkTweet thisAlerts:
    @SempervivumFeb 02.2022 — As a first step I focused on the grid and applied several modifications marked by arrows `---&gt;</C>.<br/>
    This is not really an error but I do not recommend using empty grid items for the purpose of adding spaces. Use margin or padding instead.<br/>
    Modified CSS:
    <CODE>
    `<i>
    </i> @import url('https://fonts.googleapis.com/css2?family=Nunito:wght@200;400&amp;display=swap');

    *:not(strong) {
    margin: 0;
    padding: 0;
    box-sizing: inherit;
    font-weight: 200;
    }

    html,
    body {
    /* ---&gt; minimum height is 100% of viewport,
    allow to expand at the bottom if content
    requires this */
    min-height: 100vh;
    background-color: white;
    }

    .pop-up {
    display: flex;
    justify-content: center;
    align-items: center;
    }

    .container {
    font-family: 'Nunito', sans-serif;
    font-weight: 200;
    color: white;
    font-size: 20px;
    /* ---&gt; you better allow for expanding in vertical
    direction if the content requires it: */
    /* height: 600px; */
    /* ---&gt; allow for shrinking the width in order
    to make the layout responsive: */
    max-width: 900px;
    background: linear-gradient(to top right, #e8cbc0, #636fa4);
    border-radius: 15px;
    box-shadow: 0 3rem 5rem rgba(0, 0, 0, 0.25);
    display: grid;
    justify-items: center;
    /* ---&gt; IMO, when the height of the items differs,
    it's looking nicer when they are top aligned: */
    align-items: start;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto;
    /* ---&gt; use a minimum of template areas,
    add spacing by margin or padding */
    grid-template-areas:
    "title title"
    "header header"
    "dvp mind";
    }


    .container h1 {
    font-size: 40px;
    grid-area: title;
    }

    .dvp,
    .mind {
    /* ---&gt; here we are adding the spacing: */
    margin: 2em 25% 0 25%;
    }

    .fred {
    grid-area: header;
    text-align: center;
    }

    .photo {
    height: 160px;
    width: 160px;
    border-radius: 50%;
    border: solid 1px white;
    }

    .photo:hover {
    transform: scale(1.1);
    transition-duration: 0.5s;
    }

    .fred h2 {
    margin-top: 15px;
    }

    .dvp {
    grid-area: dvp;
    text-align: center;
    }

    .mind {
    grid-area: mind;
    text-align: center;
    }

    .png {
    height: 130px;
    width: 130px;
    padding: 5px 40px;
    background-color: rgba(255, 255, 255, 0.25);
    border-radius: 15px;
    }

    .png:hover {
    background-color: rgba(255, 255, 255, 0.5);
    }

    .png:active {
    transform: translate(5px, 3px);
    box-shadow: 0 1rem 2rem rgba(0, 0, 0, 0.15);
    }

    .skills {
    color: black;
    font-weight: bold;
    text-transform: uppercase;
    font-size: 20px;
    }

    .container strong:active,
    .skills:active {
    text-decoration: underline;
    color: slateblue
    }

    .fred img,
    .png,
    .skills,
    .container strong {
    cursor: pointer;
    }<i>
    </i>
    ``
    Copy linkTweet thisAlerts:
    @SempervivumFeb 02.2022 — >However, if my text is too big to be inside one of my column/row, it will make it bigger and the text spread accross two lines.

    In order to make this issue visible I added some extra text in one of the grid items:
    ``<i>
    </i>&lt;body&gt;
    &lt;section class="pop-up"&gt;
    &lt;div class="container"&gt;
    &lt;h1&gt;Bienvenue sur le site &lt;strong&gt;enuhe.fr&lt;/strong&gt;&lt;/h1&gt;
    &lt;div class="fred"&gt;
    &lt;img src="images/01.png" alt="Fred" class="photo"&gt;
    &lt;h2&gt;Frédéric Ferreira&lt;/h2&gt;
    &lt;/div&gt;
    &lt;div class="dvp"&gt;
    &lt;img src="images/02.png" class="png"&gt;
    &lt;h3 class="skills"&gt;Développeur Web&lt;/h3&gt;
    &lt;/div&gt;
    &lt;div class="mind"&gt;
    &lt;img src="images/03.png" class="png"&gt;
    &lt;h3 class="skills"&gt;Praticien Mindfulness&lt;/h3&gt;
    &lt;p&gt;Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut
    labore et dolore
    magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
    Stet clita kasd
    gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
    &lt;/p&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;/section&gt;
    &lt;/body&gt;<i>
    </i>
    ``

    In order to make the layout look nice I top aligned the items. Not sure if this is what you wish.
    Copy linkTweet thisAlerts:
    @FredericauthorFeb 02.2022 — Thank you, I'm gonna work it through to make it look like you suggested, with padding and margin.

    But about the margin top, it is still not working ... Do you have any suggestion for that ?
    Copy linkTweet thisAlerts:
    @SempervivumFeb 02.2022 — Regarding the margin top I'm not sure if I understand your intention correctly. I added `margin-top: 2em;</C> to <C>.container` and there is a white space on top of that container as required.
    Copy linkTweet thisAlerts:
    @FredericauthorFeb 02.2022 — @Sempervivum#1642138 I wanted to make the container responsive by putting margin (auto auto) to the container, or with the flexbox align-items center of the parent div, but neither of these solutions works regarding the margin-top.
    Copy linkTweet thisAlerts:
    @SempervivumFeb 02.2022 — Do you want to center the container vertically?
    Copy linkTweet thisAlerts:
    @FredericauthorFeb 02.2022 — @Sempervivum#1642140 yes
    Copy linkTweet thisAlerts:
    @SempervivumFeb 02.2022 — In this case flex layout and `justify-content: center;</C> does the magic:
    <CODE>
    `<i>
    </i> html,
    body {
    /* ---&gt; minimum height is 100% of viewport,
    allow to expand at the bottom if content
    requires this */
    min-height: 100vh;
    }

    body {
    background-color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    }<i>
    </i>
    ``
    Copy linkTweet thisAlerts:
    @FredericauthorFeb 02.2022 — @Sempervivum#1642142 Yes that's what I did previously, with the body but in the future I will also use the body for other contents so I thought I should find another way to do this (that's why I created the section .pop-up outside the container and put the flex box with justify content but it didn't work).
    Copy linkTweet thisAlerts:
    @FredericauthorFeb 02.2022 — @Frederic#1642145 Sorry I found how to make it work, I just forgot to put an height on the Pop-up container. Thank you very much for your help, and have a great day :)
    Copy linkTweet thisAlerts:
    @SempervivumFeb 02.2022 — `justify-content` will work only when there is empty space remaining. I achieved this by deleting the extra text inside the p tag. Do you want to have space above even when the content's height is larger than the height of the viewport?
    Copy linkTweet thisAlerts:
    @SempervivumFeb 02.2022 — Our postings crossed, fine that you found a solution. Have a nice day too.
    ×

    Success!

    Help @Frederic spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.10,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...