我正在使用网格,我遇到的问题是内联阻止无法使用它。我希望我的div的高度和宽度随着里面的内容而变化。如果我没有使用网格,那么它工作正常但是网格延伸到列的宽度
.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>
&#13;
第一个div
是我使用网格的时候,而第二个我已经从.
删除UserWrapper
所以它没有被执行为{{1} }}。
为什么会这样?
答案 0 :(得分:3)
我认为你要找的是容器上的justify-items: start
。这会使项目与其单元格的起始边缘齐平,而默认值为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;
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>