Search code examples
csshtmlcss-grid

When using display grid main content section is moved down leaving a large gap


This is my first post on here as I'm well and truly stumped. After using a main container to centralise all content i decided to move to a grid layout to allow for future additions such as sidebars. However, when I did this my main section has been moved down considerably leaving a large gap that seemingly isn't even a margin just empty space. This is my first custom css, html website attempt so any help is greatly appreciated. The codepen: https://codepen.io/weirdflex/pen/MzxqdY Thanks again!

Html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>MayMays</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="main.js"></script>
</head>
<body>
<nav class = "main-nav">
    <a href = "#" class="nav-link"><img class="logo" src="/Images/logo.png" alt="Logo"></a>
    <a href = "#" class="nav-link">Home</a>
    <a href = "#" class="nav-link">Revision</a>
    <a href = "#" class="nav-link">Quiz</a>
    <a href = "#" class="nav-link right">Forums</a>
    <a href = "#" class="nav-link right">Contact Us</a>
</nav>

<section class="main-content">
    <div class="container">
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quis animi aliquam consequuntur et illum, nisi porro eaque sint alias accusantium voluptates fugit, rerum nihil voluptas consectetur molestiae accusamus excepturi cupiditate?</p>
    </div>  
</section>

</body>
</html>

CSS:

@import url('https://fonts.googleapis.com/css?family=Roboto:300,400,500,700');

body {
    background-color: #583a7a;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100%25' height='100%25' viewBox='0 0 2 1'%3E%3Cdefs%3E%3ClinearGradient id='a' gradientUnits='userSpaceOnUse' x1='0' x2='0' y1='0' y2='1' gradientTransform='rotate(178,0.5,0.5)'%3E%3Cstop offset='0' stop-color='%23583a7a'/%3E%3Cstop offset='1' stop-color='%230691b0'/%3E%3C/linearGradient%3E%3ClinearGradient id='b' gradientUnits='userSpaceOnUse' x1='0' y1='0' x2='0' y2='1' gradientTransform='rotate(166,0.5,0.5)'%3E%3Cstop offset='0' stop-color='%23fd9bff' stop-opacity='0'/%3E%3Cstop offset='1' stop-color='%23fd9bff' stop-opacity='1'/%3E%3C/linearGradient%3E%3ClinearGradient id='c' gradientUnits='userSpaceOnUse' x1='0' y1='0' x2='2' y2='2' gradientTransform='rotate(0,0.5,0.5)'%3E%3Cstop offset='0' stop-color='%23fd9bff' stop-opacity='0'/%3E%3Cstop offset='1' stop-color='%23fd9bff' stop-opacity='1'/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect x='0' y='0' fill='url(%23a)' width='2' height='1'/%3E%3Cg fill-opacity='0'%3E%3Cpolygon fill='url(%23b)' points='0 1 0 0 2 0'/%3E%3Cpolygon fill='url(%23c)' points='2 1 2 0 0 0'/%3E%3C/g%3E%3C/svg%3E");
    background-attachment: fixed;
    background-size: cover;
    font-family: 'Roboto', sans-serif;
    height: 200vh;
    margin: 0;
    display: grid;
    grid-template-columns: 1fr 960px 1fr;
}


body * {
    text-decoration: none;
    color: white;
}

.container {
    width: 900px;
    margin: 0 auto;
}

.logo {
    height: 50px;
}

.main-nav {
    height: 80px;
    width: 100%;
    grid-column: 2;
}

.main-content {
    height: 100vh;
    background: #ffffff57;
    box-shadow: 0px 0px 100px #000000;
    grid-column: 2;
}

.nav-link {
    line-height: 50px;
    vertical-align: middle;
    text-transform: uppercase;
    font-weight: 700;
    padding: 1rem 2rem;
    float: left;
}

.right {
   float: right;
}

PS. Excuse the awful formatting idk how people properly paste code into this


Solution

  • You need to remove height: 200vh; from your body CSS in order to remove the new space that has appeared.

    Looking at your design I assume you want your main content to 'float' because of the box-shadow. I've added a margin-bottom: 50px; to your .main-content CSS to keep this effect.

    @import url('https://fonts.googleapis.com/css?family=Roboto:300,400,500,700');
    
    body {
        background-color: #583a7a;
        background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100%25' height='100%25' viewBox='0 0 2 1'%3E%3Cdefs%3E%3ClinearGradient id='a' gradientUnits='userSpaceOnUse' x1='0' x2='0' y1='0' y2='1' gradientTransform='rotate(178,0.5,0.5)'%3E%3Cstop offset='0' stop-color='%23583a7a'/%3E%3Cstop offset='1' stop-color='%230691b0'/%3E%3C/linearGradient%3E%3ClinearGradient id='b' gradientUnits='userSpaceOnUse' x1='0' y1='0' x2='0' y2='1' gradientTransform='rotate(166,0.5,0.5)'%3E%3Cstop offset='0' stop-color='%23fd9bff' stop-opacity='0'/%3E%3Cstop offset='1' stop-color='%23fd9bff' stop-opacity='1'/%3E%3C/linearGradient%3E%3ClinearGradient id='c' gradientUnits='userSpaceOnUse' x1='0' y1='0' x2='2' y2='2' gradientTransform='rotate(0,0.5,0.5)'%3E%3Cstop offset='0' stop-color='%23fd9bff' stop-opacity='0'/%3E%3Cstop offset='1' stop-color='%23fd9bff' stop-opacity='1'/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect x='0' y='0' fill='url(%23a)' width='2' height='1'/%3E%3Cg fill-opacity='0'%3E%3Cpolygon fill='url(%23b)' points='0 1 0 0 2 0'/%3E%3Cpolygon fill='url(%23c)' points='2 1 2 0 0 0'/%3E%3C/g%3E%3C/svg%3E");
        background-attachment: fixed;
        background-size: cover;
        font-family: 'Roboto', sans-serif;
        margin: 0;
        display: grid;
        grid-template-columns: 1fr 960px 1fr;
    }
    
    
    body * {
        text-decoration: none;
        color: white;
    }
    
    .container {
        width: 900px;
        margin: 0 auto;
    }
    
    .logo {
        height: 50px;
    }
    
    .main-nav {
        height: 80px;
        width: 100%;
        grid-column: 2;
    }
    
    .main-content {
        height: 100vh;
        background: #ffffff57;
        box-shadow: 0px 0px 100px #000000;
        grid-column: 2;
        margin-bottom: 50px;
    }
    
    .nav-link {
        line-height: 50px;
        vertical-align: middle;
        text-transform: uppercase;
        font-weight: 700;
        padding: 1rem 2rem;
        float: left;
    }
    
    .right {
       float: right;
    }
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>MayMays</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" media="screen" href="main.css" />
    <script src="main.js"></script>
    </head>
    <body>
    <nav class = "main-nav">
        <a href = "#" class="nav-link"><img class="logo" src="/Images/logo.png" alt="Logo"></a>
        <a href = "#" class="nav-link">Home</a>
        <a href = "#" class="nav-link">Revision</a>
        <a href = "#" class="nav-link">Quiz</a>
        <a href = "#" class="nav-link right">Forums</a>
        <a href = "#" class="nav-link right">Contact Us</a>
    </nav>
    
    <section class="main-content">
        <div class="container">
            <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quis animi aliquam consequuntur et illum, nisi porro eaque sint alias accusantium voluptates fugit, rerum nihil voluptas consectetur molestiae accusamus excepturi cupiditate?</p>
        </div>  
    </section>
    
    </body>
    </html>