当用于根据内容改变宽度时,内联块不适用于网格
inline-block not working with grid when used for varying width according to content
我正在使用网格,但我面临的问题是行内块无法使用它。即我希望 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>
第一个 div
是我使用网格的时候,第二个我从 UserWrapper
中删除了 .
所以它不会作为 grid
.
为什么会这样?
我想你要找的是容器上的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>
我正在使用网格,但我面临的问题是行内块无法使用它。即我希望 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>
第一个 div
是我使用网格的时候,第二个我从 UserWrapper
中删除了 .
所以它不会作为 grid
.
为什么会这样?
我想你要找的是容器上的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>