【问题标题】:Grid: Two areas are not coming side by side网格:两个区域不会并排出现
【发布时间】:2019-05-14 05:50:46
【问题描述】:

我只是想像这样将两个区域并排放置在一起:

但我得到了这个:

我通过查看 w3schools 来了解网格,我尝试做他们的示例,但我仍然无法理解,我不知道为什么。有人可以帮我解决这个问题,也可以粗略地解释一下网格。我尝试观看 Youtube 视频,我几乎明白了,例如,我知道它的概念与 html 中的表格相同。

下面还有代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <style>
        body {
        display:grid;
        grid-template: 1fr 2fr;
        grid-gap: 20px 20px 20px 20px;
        }
        table,td,th {
            border: 1px solid black;
            border-collapse:collapse;
        }
        th {
            background-color:black;
            color:white;
            font-family:verdana;
        }
        .time {
            background-color:#D3D3D3
        }
        .button {
            border:0;
            background:transparent;
            text-decoration: underline;
            font-weight:bold;
            cursor:pointer;
        }
        td {
            font-family:Calibri;
        }
        table {
            display:inline-block;
        }
        form {
            display:inline-block;
            float:right;
        }
        </style>
    </head>
<body>
    <header>
        <h1> Soccer Camp Registration</h1>
    </header>
<div class="gridcontainer">
    <colgroup>
        <col class="times">
        <colspan = "7">
    </colgroup>
    <div class="tables">
    <table>
        <tr class="days">
            <th></th>
            <th>Sun</th>
            <th>Mon</th>
            <th>Tue</th>
            <th>Wed</th>
            <th>Thu</th>
            <th>Fri</th>
            <th>Sat</th>
        </tr>
        <tr>
            <td class="time">9:00 am-10:30am</td>
            <td>Free Time</td>
            <td rowspan="3">Technical Skills</td>
            <td colspan="4">Training Session</td>
            <td>Free Time</td>
        </tr>
        <tr>
            <td class="time">10:30 am-12:00pm</td>
            <td rowspan="3">Round Robin Games</td>
            <td colspan="3" rowspan="2">Dribbling</td>
            <td rowspan="2">Game Analysis</td>
            <td> Agility</td>
        </tr>
        <tr>
            <td class="time">1:00pm-2:30pm</td>
            <td>Strength Training</td>
        </tr>
        <tr>
            <td class="time">2:30pm-5:00pm</td>
            <td colspan="3">Nutrition</td>
            <td>Passing and Receiving</td>
            <td colspan="2" rowspan="2">Campers Choice</td>
        </tr>
        <tr>
            <td class="time">6:00pm-8:00pm</td>
            <td colspan="5"> Night Games</td>
        </tr>
    </table>
    </div>
    <caption>July 2017 Weekly Schedule</caption>
    <div class="forms">
    <form method="POST" action="https://csunix.mohawkcollege.ca/tooltime/showit.pl">
        <h2> Request Camp Information</h2>
        <fieldset class="camplc">
            <legend for="camplc"> Camper Location</legend>
            <label for="province"> Province</label>
            <select name="province" id="province">
                <option value="BC"> British Columbia</option>
                <option value="ON" >Ontario</option>
                <option value="AB" >ALberta</option>
                <option value="QC" >Quebec</option>
                <option value="YK" > Yukon</option>
            </select>
        </fieldset>
        <fieldset class="campid">
            <legend for="campid"> Camper Identification</legend>
            <label for="firstname"> First Name</label>
            <input type="text" name="firstname" id="fn">
            <br>
            <label for="lastname">Last Name</label>
            <input type="text" name="lastname" id="ln"> 
            <br>
            <label for="Gender">Gender</label>
            <input type="radio" name="Gender" value="male" id="male">
                <label for="male"> Male</label>
            <input type="radio" name="Gender" value="female" id="female">
                <label for="female">Female</label>
            <br>
            <label for="email">Email</label>
            <input type="text" name="email" id="email" placeholder="name@example.com">
            <br>
            <label for="age">Camper Age</label>
            <select name="age" form="age">
                <option value="8">8-9</option>
                <option value="10" >10-12</option>
                <option value="13" >13-15</option>
                <option value="16" >16-19</option>
            </select>
        </fieldset>
        <fieldset class="info">
            <legend for="info"> Please Send me information about</legend>
            <input type="checkbox" name="infoa" value="Camp Dates" id="cd">
            <label for="campdates"> Camp Dates</label>
            <br>
            <input type="checkbox" name="infoa" value="Camp Fees" id="cf">
            <label for="campdates"> Camp Fees</label>
            <br>
            <input type="checkbox" name="infoa" value="Transportation" id="tn">
            <label for="campdates">Transportation</label>
        </fieldset>
        <fieldset class="questions">
            <legend for="questions"> Question?</legend>
            <textarea placeholder="Please enter any questions or comments here" rows ="5" cols="50"></textarea>
        </fieldset>
        <input type="submit" value="Request Info" class="button">
        <input type="reset" value="Clear Form" class="button">
    </form>
    </div>
</div>  
</body>
</html>

【问题讨论】:

    标签: html css html-table flexbox


    【解决方案1】:

    试试这个代码,这与你给你的图片完全一样,希望它对你有用。

       body {
    /*     display: grid;
        grid-template-columns: 2fr 1fr;
        grid-template-rows: 1fr; */
    }
    header { text-align: center;}
    table,td,th {
      border: 1px solid black;
      border-collapse: collapse;
    }
    th {
      background-color: black;
      color: white;
      font-family: verdana;
      padding: 25px 67px;
    }
    .time { background-color:#D3D3D3 }
    .button {
      border: 0;
      background:transparent;
      text-decoration: underline;
      font-weight:bold;
      cursor: pointer;
    }
    td {
      font-family: Calibri;
      border: 1px solid #ccc;
      padding: 20px;
    }
    table { display: inline-block; }
    form {
      display: inline-block;
      border: 2px solid #262626;
      /* float:right; */
    }
    .gridcontainer{
        display: grid;
        grid-template-columns: 2fr 1fr;
        grid-template-rows: 1fr;
        border: 2px solid #262626;
    }
    fieldset.camplc { padding: 20px 100px; }
    fieldset.campid { padding: 10px 90px; }
    fieldset.info { padding: 10px 85px; }
    fieldset.questions { padding: 10px 90px; }
       <header>
            <h1> Soccer Camp Registration</h1>
        </header>
    <div class="gridcontainer">
        <colgroup>
            <col class="times">
            <colspan = "6">
    <!--     </colgroup> -->
        <div class="tables">
        <table>
            <tr class="days">
                <th></th>
                <th>Sun</th>
                <th>Mon</th>
                <th>Tue</th>
                <th>Wed</th>
                <th>Thu</th>
                <th>Fri</th>
                <th>Sat</th>
            </tr>
            <tr>
                <td class="time">9:00 am-10:30am</td>
                <td>Free Time</td>
                <td rowspan="3">Technical Skills</td>
                <td colspan="4">Training Session</td>
                <td>Free Time</td>
            </tr>
            <tr>
                <td class="time">10:30 am-12:00pm</td>
                <td rowspan="3">Round Robin Games</td>
                <td colspan="3" rowspan="2">Dribbling</td>
                <td rowspan="2">Game Analysis</td>
                <td> Agility</td>
            </tr>
            <tr>
                <td class="time">1:00pm-2:30pm</td>
                <td>Strength Training</td>
            </tr>
            <tr>
                <td class="time">2:30pm-5:00pm</td>
                <td colspan="3">Nutrition</td>
                <td>Passing and Receiving</td>
                <td colspan="2" rowspan="2">Campers Choice</td>
            </tr>
            <tr>
                <td class="time">6:00pm-8:00pm</td>
                <td colspan="5"> Night Games</td>
            </tr>
        </table>
        </div>
          
        <caption>July 2017 Weekly Schedule</caption>
             </colspan>
         </colgroup>
       
    <!-- <!--     <colgroup> -->
            <col class="times"> 
            <colspan = "4">
        </colgroup>
        <div class="forms">
        <form method="POST" action="https://csunix.mohawkcollege.ca/tooltime/showit.pl">
            <h2> Request Camp Information</h2>
            <fieldset class="camplc">
                <legend for="camplc"> Camper Location</legend>
                <label for="province"> Province</label>
                <select name="province" id="province">
                    <option value="BC"> British Columbia</option>
                    <option value="ON" >Ontario</option>
                    <option value="AB" >ALberta</option>
                    <option value="QC" >Quebec</option>
                    <option value="YK" > Yukon</option>
                </select>
            </fieldset>
            <fieldset class="campid">
                <legend for="campid"> Camper Identification</legend>
                <label for="firstname"> First Name</label>
                <input type="text" name="firstname" id="fn">
                <br>
                <label for="lastname">Last Name</label>
                <input type="text" name="lastname" id="ln"> 
                <br>
                <label for="Gender">Gender</label>
                <input type="radio" name="Gender" value="male" id="male">
                    <label for="male"> Male</label>
                <input type="radio" name="Gender" value="female" id="female">
                    <label for="female">Female</label>
                <br>
                <label for="email">Email</label>
                <input type="text" name="email" id="email" placeholder="name@example.com">
                <br>
                <label for="age">Camper Age</label>
                <select name="age" form="age">
                    <option value="8">8-9</option>
                    <option value="10" >10-12</option>
                    <option value="13" >13-15</option>
                    <option value="16" >16-19</option>
                </select>
            </fieldset>
            <fieldset class="info">
                <legend for="info"> Please Send me information about</legend>
                <input type="checkbox" name="infoa" value="Camp Dates" id="cd">
                <label for="campdates"> Camp Dates</label>
                <br>
                <input type="checkbox" name="infoa" value="Camp Fees" id="cf">
                <label for="campdates"> Camp Fees</label>
                <br>
                <input type="checkbox" name="infoa" value="Transportation" id="tn">
                <label for="campdates">Transportation</label>
            </fieldset>
            <fieldset class="questions">
                <legend for="questions"> Question?</legend>
                <textarea placeholder="Please enter any questions or comments here" rows ="5" cols="50"></textarea>
            </fieldset>
            <input type="submit" value="Request Info" class="button">
            <input type="reset" value="Clear Form" class="button">
        </form>
        </div>
       </div>

    【讨论】:

      【解决方案2】:

      仔细阅读文档并按照教程进行操作。甚至 W3schools 也有很好的 CSS 网格教程。当我刚刚学习 CSS 网格时,这也是我的起点。因此,您似乎没有按照应有的方式遵循他们的教程或其他教程,因为您做了我相信教程会教您不要做的事情。现在让我们分解一下我的意思。

      首先,CSS 网格存在的原因并不是让您仍然在您已经定位或想要使用display: grid; 定位的元素上使用display: inline-block 或float。我说的是这一行:

          table {
              display:inline-block;
          }
          form {
              display:inline-block;
              float:right;
          }
      

      您在表单和表格上使用了inline-block 和float,这是您尝试使用display-grid 定位的元素。那不是目标。你需要坚持一个。要么仅使用显示网格定位它们,要么使用显示内联块。尽管您并没有真正在它们上提供 display-grid,但这些元素与您在将 display: grid 提供给其父级后最终将分配 grid-area 属性的元素相同。

      话虽如此,以下是变化: 首先,我将它们注释掉,而是使用它们的包装 div 来定位它们。包装 div 是指您使用 class ="forms" 和 class="tables" 创建的 div。此外,我没有将display:grid 提供给身体,而是将其提供给您gridcontainer div,您创建了它并且再次没有使用它。 最后,我还将表 div 之外的标题放在其中,因为标题标签必须立即插入到表标签之后。简而言之,将下面的代码与您必须了解我所做的更改的代码进行比较,然后再次观看和阅读教程。甚至您提到的作为您从中学习 css 网格的来源的 w3schools 也足以说明您能够完成您想要的任务。不要停在那里。继续学习其他教程并快乐编码。这是工作代码:

      <!DOCTYPE html> 
      <html>
      
      <head>
      <meta charset="UTF-8">
      <style>
          .gridcontainer {
          display:grid;
          grid-template-areas: 'schedule info-table';
          /*
          grid-template: 1fr 2fr;
          grid-gap: 20px 20px 20px 20px;
          */
          }
      
          table,td,th {
              border: 1px solid black;
              border-collapse:collapse;
          }
          th {
              background-color:black;
              color:white;
              font-family:verdana;
          }
          .time {
              background-color:#D3D3D3
          }
          .button {
              border:0;
              background:transparent;
              text-decoration: underline;
              font-weight:bold;
              cursor:pointer;
          }
          td {
              font-family:Calibri;
          }
          /*
          table {
              display:inline-block;
          }
         */
         /*
          form {
              display:inline-block;
              float:right;
          }
          */
          .tables {
              grid-area: schedule;
          }
          .forms {
               grid-area: info-table;   
              border: 2px solid black;
              height: 80vh;
      
          }
      
          table {
             height: 70vh; 
             width: 100%;
          }
          h1 {
              display: flex;
              flex-direction: column;
              justify-content: center;
              text-align: center;
          }
      
          caption {
              display: block;
              border-bottom: 1px solid black;
              height: 20px;
              text-align: center;
          }
      
          </style>
       </head>
       <body>
      
      <header>
          <h1> Soccer Camp Registration</h1>
      </header>
      
      <div class="gridcontainer">
          <!--Did not understand why you need this, so I just commented it out. Also, 
          the colspan was throwing sintax errors. Check it!!!!-->
          <!--
      <colgroup>
      <col class="times">
      <colspan="7">
      </colgroup>
      -->
          <div class="tables">
              <table>
                  <tr class="days">
                      <th></th>
                      <th>Sun</th>
                      <th>Mon</th>
                      <th>Tue</th>
                      <th>Wed</th>
                      <th>Thu</th>
                      <th>Fri</th>
                      <th>Sat</th>
                  </tr>
                  <tr>
                      <td class="time">9:00 am-10:30am</td>
                      <td>Free Time</td>
                      <td rowspan="3">Technical Skills</td>
                      <td colspan="4">Training Session</td>
                      <td>Free Time</td>
                  </tr>
                  <tr>
                      <td class="time">10:30 am-12:00pm</td>
                      <td rowspan="3">Round Robin Games</td>
                      <td colspan="3" rowspan="2">Dribbling</td>
                      <td rowspan="2">Game Analysis</td>
                      <td> Agility</td>
                  </tr>
                  <tr>
                      <td class="time">1:00pm-2:30pm</td>
                      <td>Strength Training</td>
                  </tr>
                  <tr>
                      <td class="time">2:30pm-5:00pm</td>
                      <td colspan="3">Nutrition</td>
                      <td>Passing and Receiving</td>
                      <td colspan="2" rowspan="2">Campers Choice</td>
                  </tr>
                  <tr>
                      <td class="time">6:00pm-8:00pm</td>
                      <td colspan="5"> Night Games</td>
                  </tr>
              </table>
              <caption>July 2017 Weekly Schedule</caption>
          </div>
          <div class="forms">
              <form method="POST" 
          action="https://csunix.mohawkcollege.ca/tooltime/showit.pl">
                  <h2> Request Camp Information</h2>
                  <fieldset class="camplc">
                      <legend for="camplc"> Camper Location</legend>
                      <label for="province"> Province</label>
                      <select name="province" id="province">
                          <option value="BC"> British Columbia</option>
                          <option value="ON">Ontario</option>
                          <option value="AB">ALberta</option>
                          <option value="QC">Quebec</option>
                          <option value="YK"> Yukon</option>
                      </select>
                  </fieldset>
                  <fieldset class="campid">
                      <legend for="campid"> Camper Identification</legend>
                      <label for="firstname"> First Name</label>
                      <input type="text" name="firstname" id="fn">
                      <br>
                      <label for="lastname">Last Name</label>
                      <input type="text" name="lastname" id="ln">
                      <br>
                      <label for="Gender">Gender</label>
                      <input type="radio" name="Gender" value="male" id="male">
                      <label for="male"> Male</label>
                      <input type="radio" name="Gender" value="female" id="female">
                      <label for="female">Female</label>
                      <br>
                      <label for="email">Email</label>
                      <input type="text" name="email" id="email" 
                     placeholder="name@example.com">
                      <br>
                      <label for="age">Camper Age</label>
                      <select name="age" form="age">
                          <option value="8">8-9</option>
                          <option value="10">10-12</option>
                          <option value="13">13-15</option>
                          <option value="16">16-19</option>
                      </select>
                  </fieldset>
                  <fieldset class="info">
                      <legend for="info"> Please Send me information about</legend>
                      <input type="checkbox" name="infoa" value="Camp Dates" id="cd">
                      <label for="campdates"> Camp Dates</label>
                      <br>
                      <input type="checkbox" name="infoa" value="Camp Fees" id="cf">
                      <label for="campdates"> Camp Fees</label>
                      <br>
                      <input type="checkbox" name="infoa" value="Transportation" 
                     id="tn">
                      <label for="campdates">Transportation</label>
                  </fieldset>
                  <fieldset class="questions">
                      <legend for="questions"> Question?</legend>
                      <textarea placeholder="Please enter any questions or comments 
                  here" rows="5" cols="50"></textarea>
                  </fieldset>
                  <input type="submit" value="Request Info" class="button">
                  <input type="reset" value="Clear Form" class="button">
              </form>
          </div>
          </div>
          </body>
      
         </html>
      

      这里是codepen上代码的工作链接:https://codepen.io/anon/pen/GPpXWM

      【讨论】:

        猜你喜欢
        • 2019-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 2010-12-09
        相关资源
        最近更新 更多