如何将两个网格视图放在彼此附近?

时间:2017-09-29 16:02:35

标签: html

我正在从头开始创建一个网站。我在w3school看到了网格视图,并考虑使用它。无论如何,我使用了我的.whole元素中剩余的浮点数,但它不起作用。我已经尝试将其更改为清除左侧,但它也不起作用。有人可以帮我把两个.sections元素放在一起吗?



* {
  box-sizing: border-box;
}

.row::after {
  content: "";
  clear: both;
  display: table;
}

[class*="col-"] {
  float: left;
  padding: 15px;
}

body {
  font-family: "Lucida Sans", sans-serif;
}

.header {
  width: 500px;
  background-color: #9933cc;
  color: #ffffff;
  padding: 15px;
}

.menu ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

.menu li {
  padding: 8px;
  margin-bottom: 7px;
  background-color: #33b5e5;
  color: #ffffff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
}

.menu li:hover {
  background-color: #0099cc;
}

@media only screen and (min-width: 600px) {
  /* For desktop: */
  .col-3 {
    width: 15%;
  }
  .col-6 {
    width: 20%;
  }
}

.sections {
  width: 1300px;
}

.whole {
  float: left;
}

<div class="whole">
  <div class="sections">
    <div class="header">
      <h1>Chania</h1>
    </div>

    <div class="row">

      <div class="col-3 menu">
        <ul>
          <li>The Flight</li>
          <li>The City</li>
          <li>The Island</li>
          <li>The Food</li>
        </ul>
      </div>

      <div class="col-6">
        <h1>The City</h1>
        <p>Chania is the capital of the Chania region on the island of Crete. The city can be divided in two parts, the old town and the modern city.</p>
      </div>
    </div>
  </div>

  <div class="sections">
    <div class="header">
      <h1>Chania</h1>
    </div>

    <div class="row">

      <div class="col-3 menu">
        <ul>
          <li>The Flight</li>
          <li>The City</li>
          <li>The Island</li>
          <li>The Food</li>
        </ul>
      </div>

      <div class="col-6">
        <h1>The City</h1>
        <p>Chania is the capital of the Chania region on the island of Crete. The city can be divided in two parts, the old town and the modern city.</p>
      </div>
    </div>

  </div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

Theres有多个选项,但其中一个是您可以将position: relative;放在.whole上,然后position: absolute;width: 50%;放在.section上(某种类型的也指定了最小宽度)。然后在第一个.section div上添加left: 0;,在另一个.section上添加right: 0;

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* {
    box-sizing: border-box;
}
.row::after {
    content: "";
    clear: both;
    display: table;
}
[class*="col-"] {
    float: left;
    padding: 15px;
}
body {
    font-family: "Lucida Sans", sans-serif;
}
.header {
    width: 500px;
    background-color: #9933cc;
    color: #ffffff;
    padding: 15px;
}
.menu ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
}
.menu li {
    padding: 8px;
    margin-bottom: 7px;
    background-color: #33b5e5;
    color: #ffffff;
    box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}
.menu li:hover {
    background-color: #0099cc;
}
@media only screen and (min-width: 600px) {
    /* For desktop: */
    .col-3 {width: 15%;}
    .col-6 {width: 20%;}
}
.sections {
width: 50%;
min-width: 500px;
position: absolute;
}

.sections.left {
  left: 0;
}

.sections.right {
  right: 0;
}

.whole {
position: relative;
top: 0;
}
</style>
</head>
<body>

<div class="whole">
<div class="sections left">
<div class="header">
  <h1>Chania</h1>
</div>

<div class="row">

<div class="col-3 menu">
  <ul>
    <li>The Flight</li>
    <li>The City</li>
    <li>The Island</li>
    <li>The Food</li>
  </ul>
</div>

<div class="col-6">
  <h1>The City</h1>
  <p>Chania is the capital of the Chania region on the island of Crete. The city can be divided in two parts, the old town and the modern city.</p>
</div>
</div>
</div>

<div class="sections right">
<div class="header">
  <h1>Chania</h1>
</div>

<div class="row">

<div class="col-3 menu">
  <ul>
    <li>The Flight</li>
    <li>The City</li>
    <li>The Island</li>
    <li>The Food</li>
  </ul>
</div>

<div class="col-6">
  <h1>The City</h1>
  <p>Chania is the capital of the Chania region on the island of Crete. The city can be divided in two parts, the old town and the modern city.</p>
</div>
</div>

</div>
</div>

</body>
</html>

然后只调整.section的某些子元素的宽度。如果您希望子元素浮动,则不会将浮点数应用于父级,但此外,子元素上的宽度为1300px,这也会导致问题。

答案 1 :(得分:0)

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* {
    box-sizing: border-box;
}
.row::after {
    content: "";
    clear: both;
    display: table;
}
[class*="col-"] { 
    float: left;
    padding: 15px;
}
body {
    font-family: "Lucida Sans", sans-serif;
}
.header {
    width: auto;
    background-color: #9933cc;
    color: #ffffff;
    padding: 15px;
}
.menu ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
}
.menu li { 
    padding: 8px;
    margin-bottom: 7px;
    background-color: #33b5e5;
    color: #ffffff;
    box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}
.menu li:hover {
    background-color: #0099cc;
}
@media only screen and (min-width: 600px) {
    /* For desktop: */
    .col-3 {width: 15%;} 
    .col-6 {width: 20%;} 
}
.sections {
width: 50%;
float:left;
}
.whole {
float: left;
}
</style>
</head>
<body>

<div class="whole">
<div class="sections">
<div class="header">
  <h1>Chania</h1>
</div>

<div class="row">

<div class="col-3 menu">
  <ul>
    <li>The Flight</li>
    <li>The City</li>
    <li>The Island</li>
    <li>The Food</li>
  </ul>
</div>

<div class="col-6">
  <h1>The City</h1>
  <p>Chania is the capital of the Chania region on the island of Crete. The city can be divided in two parts, the old town and the modern city.</p>
</div>
</div>
</div>

<div class="sections">
<div class="header">
  <h1>Chania</h1>
</div>

<div class="row">

<div class="col-3 menu">
  <ul>
    <li>The Flight</li>
    <li>The City</li>
    <li>The Island</li>
    <li>The Food</li>
  </ul>
</div>

<div class="col-6">
  <h1>The City</h1>
  <p>Chania is the capital of the Chania region on the island of Crete. The city can be divided in two parts, the old town and the modern city.</p>
</div>
</div>

</div>
</div>

</body>
</html>