gpt4 book ai didi

html - 当用于根据内容改变宽度时,内联 block 不适用于网格

转载 作者:太空宇宙 更新时间:2023-11-04 15:51:06 26 4
gpt4 key购买 nike

我正在使用网格,但我面临的问题是行内 block 无法使用它。即我希望我的 div 的高度和宽度随其中的内容而变化。如果我不使用网格那么它工作正常但是使用网格它会拉伸(stretch)到列的宽度

.BotWrapper {
position: relative;
width: 100%;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 1em;
grid-auto-rows: minmax(20px, auto);
margin-top: 0.8em;
word-wrap: break-word;
}

.BotWrapper > div {
margin-left: 2%;
background: #BEE9E8;
padding: 1.7%;
display: inline-block;
min-height: 40px;
min-width: 6em;
max-width: 20em;
margin-bottom: 5px;
margin-top: 10px;
font-size: 0.9em;
word-wrap: break-word;
}

.BotWrapper > div:nth-child(even) {
background: transparent;
}

UserWrapper {
width: 100%;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 1em;
grid-auto-rows: minmax(30px, auto);
margin-right: 1em;
margin-top: 1em;
word-wrap: break-word;
background: blue;
}

.UserWrapper > div {
float: right;
margin-right: -2.2em;
background-color: #62B6CB;
padding: 1.7%;
max-width: 20em;
min-width: 6em;
min-height: 40px;
margin-top: 10px;
display: inline-block;
margin-bottom: 5px;
font-size: 0.9em;
word-wrap: break-word;
}

.UserWrapper > div:nth-child(odd) {
background: transparent;
}

.speech-bubble {
padding: 5rem;
border: 2px solid #376996;
border-top-right-radius: .6em;
border-bottom-right-radius: .6em;
border-bottom-left-radius: .6em;
}

.speech-bubble1 {
position: relative;
background: #62B6CB;
border: 2px solid #376996;
border-top-left-radius: .6em;
border-bottom-left-radius: .6em;
border-bottom-right-radius: .6em
}

.timeRight {
font-size: 0.52em;
float: right;
margin-top: -1em;
margin-bottom: 0;
font-family: 'Mina', sans-serif;
}

.timeLeft {
font-size: 0.52em;
float: right;
margin-top: -1em;
margin-bottom: 0;
font-family: 'Mina', sans-serif;
}
<div class="msgContainer" id="chat">
<div class="BotWrapper">
<div class="speech-bubble z-depth-5">
<p>Hi</p>
<p class="timeLeft">02:12:02</p>
</div>
<div class=""></div>
</div>

<div class="UserWrapper">
<div class=""></div>
<div class="speech-bubble1 z-depth-5">
<p>Bye</p>
<p class="timeRight">02:12:03</p>
</div>
</div>
</div>

第一个 div 是我使用网格的时候,第二个是我从 UserWrapper 中删除了 . 所以它不是作为 grid 执行。为什么会这样?

最佳答案

我认为您正在寻找的是容器上的justify-items: start。这会将元素对齐到与其单元格的起始边缘齐平,而默认设置是 stretch 这会强制子项填充单元格的整个宽度

CSS-Tricks Complete Guide

.BotWrapper {
position: relative;
width: 100%;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 1em;
grid-auto-rows: minmax(20px, auto);
margin-top: 0.8em;
word-wrap: break-word;
justify-items: start; /* this */
}

.BotWrapper > div {
margin-left: 2%;
background: #BEE9E8;
padding: 1.7%;
min-height: 40px;
min-width: 6em;
margin-bottom: 5px;
margin-top: 10px;
font-size: 0.9em;
word-wrap: break-word;
}

.BotWrapper > div:nth-child(even) {
background: transparent;
}

UserWrapper {
width: 100%;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 1em;
grid-auto-rows: minmax(30px, auto);
margin-right: 1em;
margin-top: 1em;
word-wrap: break-word;
background: blue;
}

.UserWrapper > div {
float: right;
margin-right: -2.2em;
background-color: #62B6CB;
padding: 1.7%;
max-width: 20em;
min-width: 6em;
min-height: 40px;
margin-top: 10px;
display: inline-block;
margin-bottom: 5px;
font-size: 0.9em;
word-wrap: break-word;
}

.UserWrapper > div:nth-child(odd) {
background: transparent;
}

.speech-bubble {
padding: 5rem;
border: 2px solid #376996;
border-top-right-radius: .6em;
border-bottom-right-radius: .6em;
border-bottom-left-radius: .6em;
}

.speech-bubble1 {
position: relative;
background: #62B6CB;
border: 2px solid #376996;
border-top-left-radius: .6em;
border-bottom-left-radius: .6em;
border-bottom-right-radius: .6em
}

.timeRight {
font-size: 0.52em;
float: right;
margin-top: -1em;
margin-bottom: 0;
font-family: 'Mina', sans-serif;
}

.timeLeft {
font-size: 0.52em;
float: right;
margin-top: -1em;
margin-bottom: 0;
font-family: 'Mina', sans-serif;
}
<div class="msgContainer" id="chat">
<div class="BotWrapper">
<div class="speech-bubble z-depth-5">
<p>Hi</p>
<p class="timeLeft">02:12:02</p>
</div>
<div class=""></div>
</div>

<div class="UserWrapper">
<div class=""></div>
<div class="speech-bubble1 z-depth-5">
<p>Bye</p>
<p class="timeRight">02:12:03</p>
</div>
</div>
</div>

关于html - 当用于根据内容改变宽度时,内联 block 不适用于网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50171750/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com