0

I am trying to create this particular layout with CSS Grid: enter image description here

However I am having a really hard time trying to understand how to implement this. I have a container with a header and a hero banner section, and an inner container that has the sidebar and the main content nested, as bordered in red and blue.

I thought I could just add a negative top margin to lift up the main sidebar image, but that does not work in css grid. I am super confused, as I have tried a few different ideas, but to no avail.

Currently, I do have the following:

html * {
  box-sizing: border-box;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-smoothing: antialiased;
}

html,
body {
  font-family: verdana, sans-serif;
  color: #000;
  font-size: 12px;
  height: 100%;
  line-height: 1.5;
}

.container {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-areas: "header header" "hero hero";
}

.inner-container {
  display: grid;
  grid-template-columns: 260px 1fr;
  grid-template-areas: "sidebar main";
}

header {
  grid-area: header;
  width: 100%;
  padding: 0;
}

header .navbar>div {
  border-bottom: 1px solid #ddd;
}

header .navbar p {
  padding: 0 15px;
}

.hero {
  grid-area: hero;
  height: 300px;
  background-color: #bbb;
  padding: 0;
  display: flex;
}

.inner-container .sidebar {
  grid-area: sidebar;
  grid-row: span/3;
  border: 1px solid red;
}

.inner-content .sidebar {
  margin-top: -180px;
}

.inner-container .main {
  grid-area: main;
  border: 1px solid blue;
}


/* Media Queries */

@media (max-width: 991px) {
  .profile-meta-sidebar {
    display: none;
  }
  .container {
    grid-template-columns: 1fr;
    grid-template-areas: "header" "hero";
  }
  .inner-container {
    grid-template-columns: 1fr;
    grid-template-areas: "sidebar" "main";
  }
}

@media (min-width: 992px) and (max-width: 1239px) {
  .inner-container {
    grid-template-columns: 260px 1fr;
    grid-template-areas: "sidebar main";
    width: 970px;
    margin: 0 auto;
  }
}

@media (min-width: 1260px) {
  .inner-container {
    grid-template-columns: 260px 1fr;
    grid-template-areas: "sidebar main";
    width: 1240px;
    margin: 0 auto;
  }
}
<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="">

  <title>CSS Grid Version</title>

  <!-- Bootstrap core CSS -->
  <link href="normalize.css" rel="stylesheet">

  <!-- Custom styles for this template -->
  <link href="main.css" rel="stylesheet">
</head>

<body>
  <div class="container">
    <header>
      <div class="navbar">
        <div class="top">
          <p>brand</p>
        </div>

        <div class="bottom">
          <p>logo</p>
        </div>
      </div>
    </header>

    <div class="hero">
      <div class="wrapper">
        <p>content</p>
      </div>
    </div>

    <div class="inner-container">
      <div class="sidebar">
        <div class="figure">
          <img src="http://placehold.it/230x320" />
          <div class="caption">***</div>

          <div class="row profile-meta-sidebar">
            <div class="col-lg-12 px-0 d-flex justify-content-center align-items-center">
              <ul class="profile-meta p-0 m-0">
                <li><a href="">tindex</a></li>
                <li><a href="">heroes</a></li>
                <li><a href="">fans</a></li>
              </ul>
            </div>
          </div>
          <ul>
            <li>item one</li>
            <li>item two</li>
            <li>item four</li>
            <li>item five</li>
          </ul>
        </div>
      </div>
      <div class="main">

      </div>
    </div>
  </div>
  <!-- /.container -->

  <!-- JavaScript
    ================================================== -->
  <!-- Placed at the end of the document so the pages load faster -->

</body>

</html>
VXp
  • 11,598
  • 6
  • 31
  • 46
Krys
  • 819
  • 1
  • 21
  • 38
  • Grid is well suited for overlapping elements. Consider creating a container with many small rows. Then just place grid areas along those rows, with your sidebar overlapping the content area by being assigned to the same rows. Here's an example: https://stackoverflow.com/a/43919551/3597276 – Michael Benjamin Sep 20 '18 at 02:29
  • Also see: [Overlapping items in CSS Grid](https://stackoverflow.com/q/49361243/3597276). – Michael Benjamin Sep 20 '18 at 02:30
  • @Michael_B that just seems overly complicated for my layout. – Krys Sep 20 '18 at 03:28
  • Actually, your layout seems overly complicated on its own ;-) The solutions in the answers I linked to are quite simple. – Michael Benjamin Sep 20 '18 at 03:33
  • @Michael_B Right. This layout was quite simple to do in bootstrap and flexbox, so would I need another row above the sidebar and main content for the image to be pulled up higher into? – Krys Sep 20 '18 at 03:35
  • @Michael_B My layout also has a set width on the inner container, so am I dong it correctly with nesting the sidebar and main content inside another container like that? And is that stopping the sidebar from being pushed up into the hero section? – Krys Sep 20 '18 at 03:40
  • Hi @Krys ,I thinks this link help for you ,Link here https://getbootstrap.com/docs/4.0/examples/album/ – core114 Sep 20 '18 at 06:47
  • @core114 I don't want to use bootstrap. I am trying to create it in CSS Grid. – Krys Sep 20 '18 at 08:53

0 Answers0