【发布时间】:2020-02-19 16:18:27
【问题描述】:
我正在创建一个网页来为我的学生展示一些问答。我在网上搜索,发现下面的 CSS 可以用来创建一个左侧或右侧带有箭头的对话气泡。
.speech {
background: #efefef;
-webkit-border-radius: 4px;
border-radius: 4px;
font-size: 1.2rem;
line-height: 1.3;
margin: 0px 30px 40px 160px;
max-width: 88%;
padding: 15px;
position: relative;
filter: drop-shadow(6px 4px 0px rgba(0, 0, 0, 0.2));
border: 1px solid black;
}
.onLeft::after {
border-left: 11px solid transparent;
border-right: 11px solid #efefef;
border-top: 11px solid #efefef;
border-bottom: 11px solid transparent;
content: "";
position: absolute;
left: -20px;
top: 8px;
filter: drop-shadow(-2px -1px 0px black);
}
.onRight:after {
content: "";
position: absolute;
border-left: 11px solid #efefef;
border-right: 11px solid transparent;
border-top: 11px solid #efefef;
border-bottom: 11px solid transparent;
right: -20px;
top: 8px;
filter: drop-shadow(2px -1px 0px black);
}
我只能在 Intranet 上托管我的网站,以下 HTML 将显示捕获中所见的内容
<div style="font-size: 12pt; margin-bottom: 20px; clear: both;">
<img style="float: left; margin: 15px; src="logopic.png" width="120" />
<div class="speech onLeft">
Here is my example of a speech bubble created in CSS and HTML.
</div>
</div>
logopic.png 的宽度约为 120 像素,因此我的语音气泡向右移动了约 160 像素以留出一些间隙。
看起来不错。但是,当我尝试使用右侧的箭头和右侧的 logopic 浮动应用语音气泡时,位置就搞砸了
<div style="font-size: 12pt; margin-bottom: 20px; clear: both;">
<img style="float: left; margin: 15px; src="logopic.png" width="120" />
<div class="speech onLeft">
Here is my example of a speech bubble created in CSS and HTML. </div>
</div>
</div>
<div style="font-size: 12pt; margin-bottom: 20px; clear: both;">
<img style="float: right; margin: 15px; src="logopic.png" width="120" />
<div class="speech onRight">
Here is my example of a speech bubble created in CSS and HTML. The arrow is on the right and the image is floating to the right.
</div>
</div>
对话气泡一直向右移动。我试图为气泡的 div 上的“左”属性(如左:-20px)添加移位,但没有帮助。有解决此问题的想法吗?
附加问题:我正在尝试使用 flexbox 修改 CSS
.dia {
display: flex;
align-items: flex-start;
flex-direction: row;
padding: 1em;
border-radius: 3px;
max-width: 88%;
}
.speecher {
margin-right: 30px;
order: 0;
}
<div class="dia">
<img class="speecher" src="logopic.jpg"/>
<div class="speech onLeft">
testing ... testing ... testing ... testing ... testing ... testing ... testing ... testing ... testing ... testing ... testing ...
</div>
</div>
我发现当设备屏幕很大时,CSS 效果很好,图像在左侧对齐,对话气泡在右侧对齐。但是当我使用移动设备或缩小浏览器屏幕时,它仍然排成一排,并且对话气泡变得如此纤细。如何根据设备/浏览器大小更改布局。
【问题讨论】: