右列具有三行的两列布局

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

我正在学习CSS网格,但是我发现网格模板区域,列和行很难理解。我试图设计聊天屏幕布局,其中将有两列。左列将包含会话列表,右列将包含三行。一个是聊天标题,将被固定,另一行将是消息列表,第三行是一种发送消息的形式,该消息也将被固定。我尝试通过以下方式进行操作,但无法正常工作

.chat-container {
  display: grid;
  grid-template-areas: "chat-list chat-title chat-title" "chat-list message-list message-list" "chat-list chat-form chat-form";
  grid-template-columns: 200px 1fr;
  border-radius: 10px;
  height: 95vh;
  width: 100%;
}

.chat-title {
  padding: 10px;
  background: blue;
}

.chat-list {
  padding: 10px;
  background: red;
}

.message-list {
  padding: 10px;
  background: green;
}

.chat-form {
  padding: 10px;
  background: yellow;
}
<div class="chat-container">
  <section class="chat-list">
    Chat List
  </section>
  <section class="chat-title">Chat title</section>
  <section class="message-list">
    Message List
  </section>
  <section class="chat-form">Chat form</section>
</div>

我每行都提到过聊天列表3次,但仍然看不到聊天列表阻止聊天列表。我如何用右列包含3行来布局2列?

html css css-grid
1个回答
1
投票

您需要将区域分配给类别:

.chat-container {
  display: grid;
  grid-template-areas: "chat-list chat-title chat-title" "chat-list message-list message-list" "chat-list chat-form chat-form";
  grid-template-columns: 200px 1fr;
  border-radius: 10px;
  height: 95vh;
  width: 100%;
}

.chat-title {
  padding: 10px;
  background: blue;
}

.chat-list {
  padding: 10px;
  background: red;
  grid-area: chat-list;   /*  added */
}

.message-list {
  padding: 10px;
  background: green;
}

.chat-form {
  padding: 10px;
  background: yellow;
}
<div class="chat-container">
  <section class="chat-list">
    Chat List
  </section>
  <section class="chat-title">Chat title</section>
  <section class="message-list">
    Message List
  </section>
  <section class="chat-form">Chat form</section>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.