但是,我很难理解如何实现这一点。我有一个带有标题和英雄横幅部分的容器,以及一个内部容器,该容器具有侧边栏和主要内容嵌套,以红色和蓝色为边框。
我以为我可以添加一个负的上边距来提升主侧边栏图像,但这在CSS网格中不起作用。我非常困惑,因为我尝试了一些不同的想法,但无济于事。
目前,我有以下内容:
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>