如何在不使用浮动的情况下将CSS中的元素相邻定位,即主内容在侧栏旁边

问题描述 投票:0回答:1

我有一个侧栏。我有一个主内容。我想把我的main content div放在我的左侧栏旁边。我的主内容div是以CSS网格布局的形式。我确定网格将是最好的包含我需要显示的信息,由我得到的一个模拟图来描述。我正在探索将整个HTML页面做成网格的选项,但我不确定如何解决这个问题。所以,我想我会为我的侧栏创建一个div,并让它停留在那里,而我的主要内容,网格,将围绕它移动。但是没有用。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Grid</title>
  <link rel="stylesheet" type="text/css" href="styles/normalize.css">
  <link rel="stylesheet" type="text/css" href="scratch.css">
</head> 
  <style>
    body {
      background: darkgrey;
      height: 1000px;
      position: relative;
    }

    .side-bar {
      background: red;
      padding: 0;
      margin: 0;
      width: 202px;
      height: 100%;
      position: relative;
    }

    .side-bar p{
      padding: 0;
      margin: 0;

    }

    .grid-container {
      display: grid;
      background: #E1E1E1;
      height: 500px;
      width: 1100px;
      grid-template-columns: 250px 250px 250px 250px;
      grid-template-rows: 200px 200px;
      grid-gap: 30px 20px;
      padding: 0;
      margin: 0 25px 0 30px;
      justify-content: right;
      align-content: center;
      position: relative;
      left: 400px;
    }

    .grid-items {
      font-weight: bold;
    }

    .grid-item1 {
      background: greenyellow;
    }

    .grid-item2 {
      background: rosybrown;
    }

    .grid-item3 {
      background: cadetblue;
    }

    .grid-item4 {
      background: darkturquoise;
    }

    .grid-item5 {
      background: darkkhaki;
      grid-column: span 2;
    }

    .grid-item6 {
      background: lightblue;
      grid-column: span 2;
    }
  </style>
  <body>

    <div class="side-bar">
      <p>Side Bar</p>
    </div>

    <div class="grid-container">
      <div class="grid-items grid-item1">
        <p>Grid Item 1</p>
      </div>

      <div class="grid-items grid-item2">
        <p>Grid Item 2</p>
      </div>

      <div class="grid-items grid-item3">
        <p>Grid Item 3</p>
      </div>

      <div class="grid-items grid-item4">
        <p>Grid Item 4</p>
      </div>

      <div class="grid-items grid-item5">
        <p>Grid Item 5</p>
      </div>

      <div class="grid-items grid-item6">
        <p>Grid Item 6</p>
      </div>
    </div>
  </body>
</html>
html css web frontend css-grid
1个回答
1
投票

你最好的选择是有一个包含侧栏的主网格和一个包含主要内容的网格在里面。

body,
html {
  background: darkgrey;
  height: 100%;
  margin: 0;
}

#horizontal-menu {
  background: lightblue;
  padding: 8px;
  margin-bottom: 15px;
}

#horizontal-menu p {
  margin: 0;
}

.grid-container {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  grid-gap: 15px;
  height: 100%;
}

.side-bar {
  background: red;
  padding: 0;
  margin: 0;
  height: 100%;
  grid-column: 1 / 2;
}

.grid-items-container {
  grid-column: 2 / 3;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(2, 1fr);
  grid-gap: 30px 20px;
  background: #E1E1E1;
  padding: 15px;
}

.side-bar p {
  padding: 0;
  margin: 0;
}

.grid-items {
  font-weight: bold;
}

.grid-item1 {
  background: greenyellow;
}

.grid-item2 {
  background: rosybrown;
}

.grid-item3 {
  background: cadetblue;
}

.grid-item4 {
  background: darkturquoise;
}

.grid-item5 {
  background: darkkhaki;
  grid-column: span 2;
}

.grid-item6 {
  background: lightblue;
  grid-column: span 2;
}
<div id="horizontal-menu">
  <p>Menu</p>
</div>

<div class="grid-container">

  <div class="side-bar">
    <p>Side Bar</p>
  </div>

  <div class="grid-items-container">

    <div class="grid-items grid-item1">
      <p>Grid Item 1</p>
    </div>

    <div class="grid-items grid-item2">
      <p>Grid Item 2</p>
    </div>

    <div class="grid-items grid-item3">
      <p>Grid Item 3</p>
    </div>

    <div class="grid-items grid-item4">
      <p>Grid Item 4</p>
    </div>

    <div class="grid-items grid-item5">
      <p>Grid Item 5</p>
    </div>

    <div class="grid-items grid-item6">
      <p>Grid Item 6</p>
    </div>
  </div>
</div>

0
投票
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Grid</title>
    <link rel="stylesheet" type="text/css" href="styles/normalize.css">
    <link rel="stylesheet" type="text/css" href="scratch.css">
</head>

<style>
    html,body {
        background: darkgrey;
        height: 100%;
        position: relative;
    }

    .side-bar {
        background: red;
        padding: 0;
        margin: 0;
        width: 202px;
        height: 100%;
        position: absolute;
        left:0;
        display: block;
    }

    .side-bar p {
        padding: 0;
        margin: 0;

    }

    .main-content {
        display: block;
        height: 500px;
        width: 1100px;
        position: absolute;
        left: 203px;
    }

    .grid-container {
        display: grid;
        height: 500px;
        width: 1100px;
        background: #E1E1E1;
        grid-template-columns: 250px 250px 250px 250px;
        grid-template-rows: 200px 200px;
        grid-gap: 30px 20px;
        padding: 0;
        margin: 0 25px 0 30px;
        justify-content: right;
        align-content: center;
        position: relative;
    }

    .grid-items {
        font-weight: bold;
    }

    .grid-item1 {
        background: greenyellow;
    }

    .grid-item2 {
        background: rosybrown;
    }

    .grid-item3 {
        background: cadetblue;
    }

    .grid-item4 {
        background: darkturquoise;
    }

    .grid-item5 {
        background: darkkhaki;
        grid-column: span 2;
    }

    .grid-item6 {
        background: lightblue;
        grid-column: span 2;
    }
</style>

<body>

    <div class="side-bar">
        <p>Side Bar</p>
    </div>

    <div class="main-content">
        <div class="grid-container">
            <div class="grid-items grid-item1">
                <p>Grid Item 1</p>
            </div>

            <div class="grid-items grid-item2">
                <p>Grid Item 2</p>
            </div>

            <div class="grid-items grid-item3">
                <p>Grid Item 3</p>
            </div>

            <div class="grid-items grid-item4">
                <p>Grid Item 4</p>
            </div>

            <div class="grid-items grid-item5">
                <p>Grid Item 5</p>
            </div>

            <div class="grid-items grid-item6">
                <p>Grid Item 6</p>
            </div>
        </div>
    </div>
</body>

</html>

绝对定位是你可能想尝试的一个选项。不知道你想要在你的页面上有多大的响应速度。


0
投票

用于div display: table-cell;

body {
  background: darkgrey;
  height: 1000px;
}

.right-content,
.side-bar {
  display: table-cell;
}

.side-bar {
  background: red;
  padding: 0;
  margin: 0;
  width: 30%;
  height: 100%;
  position: relative;
}

.side-bar p {
  padding: 0;
  margin: 0;
}

.right-content {
  width: 70%;
  
}

.grid-container {
  display: grid;
  padding:10px;
  background: #E1E1E1;
  height: 500px;
  grid-template-columns: 250px 250px 250px 250px;
  grid-template-rows: 200px 200px;
  grid-gap: 30px 20px; 
  margin: 0 25px 0 30px;
  justify-content: right;
  align-content: center;
  position: relative;
}

.grid-items {
  font-weight: bold;
}

.grid-item1 {
  background: greenyellow;
}

.grid-item2 {
  background: rosybrown;
}

.grid-item3 {
  background: cadetblue;
}

.grid-item4 {
  background: darkturquoise;
}

.grid-item5 {
  background: darkkhaki;
  grid-column: span 2;
}

.grid-item6 {
  background: lightblue;
  grid-column: span 2;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Grid</title>
  <link rel="stylesheet" type="text/css" href="styles/normalize.css">
  <link rel="stylesheet" type="text/css" href="scratch.css">
</head>


<body>

  <div class="container">

    <div class="side-bar">
      <p>Side Bar</p>
    </div>

    <div class="right-content">
      <div class="grid-container">
        <div class="grid-items grid-item1">
          <p>Grid Item 1</p>
        </div>

        <div class="grid-items grid-item2">
          <p>Grid Item 2</p>
        </div>

        <div class="grid-items grid-item3">
          <p>Grid Item 3</p>
        </div>

        <div class="grid-items grid-item4">
          <p>Grid Item 4</p>
        </div>

        <div class="grid-items grid-item5">
          <p>Grid Item 5</p>
        </div>

        <div class="grid-items grid-item6">
          <p>Grid Item 6</p>
        </div>
      </div>
    </div>
  </div>
</body>

</html>
© www.soinside.com 2019 - 2024. All rights reserved.