如何使用CSS将图像添加到div气泡

时间:2019-02-16 09:15:52

标签: css

如何使用CSS将图像添加到div气泡中。

以下代码显示左右气泡。请如何使用CSS将左右图像分别添加到聊天气泡工具提示中。

任何帮助将不胜感激。

屏幕截图更新:

添加了屏幕截图,显示了要实施的内容。聊天气泡左侧的left.png图像,右侧则是right.png的图像。 screenshot

下面是我要添加到聊天中的左右图像气泡

  <img src="right.png" width="20px" height="20px">
    <img src="left.png" width="20px" height="20px">

以下是到目前为止的代码。

* {
  margin: 0px;
  padding: 0px;
}

.box3 {
  width: 300px;
  margin: 10px auto;
  border-radius: 15px;
  background: #00bfb6;
  color: #fff;
  padding: 20px;
  text-align: center;
  font-weight: 900;
  font-family: arial;
  position: relative;
}


/* right bubble */

.right:before {
  content: "";
  width: 0px;
  height: 0px;
  position: absolute;
  border-left: 15px solid #00bfb6;
  border-right: 15px solid transparent;
  border-top: 15px solid #00bfb6;
  border-bottom: 15px solid transparent;
  right: -16px;
  top: 0px;
}


/* left bubble */

.left:before {
  content: "";
  width: 0px;
  height: 0px;
  position: absolute;
  border-left: 15px solid transparent;
  border-right: 15px solid #00bfb6;
  border-top: 15px solid #00bfb6;
  border-bottom: 15px solid transparent;
  left: -16px;
  top: 0px;
}
<div class="box3 right">right bubble using css</div>

<div class="box3 left">left bubble using css</div>

3 个答案:

答案 0 :(得分:0)

只需将图像包含在气泡内,并使用绝对位置即可获取正确位置。

相关代码:

char a = {'j', 'o', 'h', 'n'}, 

完整代码:

.box3 > img {
  border-radius:50%;
  position:absolute;
  top:-15px;
}

.right > img {
  left:calc(100% + 20px);
}
.left > img {
  right:calc(100% + 20px);
}
* {
  margin: 0px;
  padding: 0px;
}

.box3 {
  width: 300px;
  margin: 20px auto;
  border-radius: 15px;
  background: #00bfb6;
  color: #fff;
  padding: 20px;
  text-align: center;
  font-weight: 900;
  font-family: arial;
  position: relative;
}
.box3 > img {
  border-radius:50%;
  position:absolute;
  top:-15px;
}

.right > img {
  left:calc(100% + 20px);
}
.left > img {
  right:calc(100% + 20px);
}

/* right bubble */

.right:before {
  content: "";
  width: 0px;
  height: 0px;
  position: absolute;
  border-left: 15px solid #00bfb6;
  border-right: 15px solid transparent;
  border-top: 15px solid #00bfb6;
  border-bottom: 15px solid transparent;
  right: -16px;
  top: 0px;
}


/* left bubble */

.left:before {
  content: "";
  width: 0px;
  height: 0px;
  position: absolute;
  border-left: 15px solid transparent;
  border-right: 15px solid #00bfb6;
  border-top: 15px solid #00bfb6;
  border-bottom: 15px solid transparent;
  left: -16px;
  top: 0px;
}

答案 1 :(得分:0)

将气泡+图像包装在flex容器中是将它们放在一起并具有灵活的移动元素的好方法。 display: flex;justify-content: center;基本上足以实现它。查看下面的代码段。

* {
    margin: 0px;
    padding: 0px;
}
.container {
    /*ADDED*/
    display: flex;
    justify-content: center;
    margin: 60px 0;
}
img {
    /*ADDED*/
    border-radius: 50%;
    width: 40px;
    height: 40px;
}
#img-right {
    /*ADDED*/
    margin-left: 20px;
}
#img-left {
    /*ADDED*/
    margin-right: 20px;
}
.box3 {
    width: 300px;
    border-radius: 15px;
    background: #00bfb6;
    color: #fff;
    padding: 20px;
    text-align: center;
    font-weight: 900;
    font-family: arial;
    position: relative;
}
/* right bubble */

.right:before {
    content: "";
    width: 0px;
    height: 0px;
    position: absolute;
    border-left: 15px solid #00bfb6;
    border-right: 15px solid transparent;
    border-top: 15px solid #00bfb6;
    border-bottom: 15px solid transparent;
    right: -16px;
    top: 0px;
}
/* left bubble */

.left:before {
    content: "";
    width: 0px;
    height: 0px;
    position: absolute;
    border-left: 15px solid transparent;
    border-right: 15px solid #00bfb6;
    border-top: 15px solid #00bfb6;
    border-bottom: 15px solid transparent;
    left: -16px;
    top: 0px;
}
<div class="container">
    <div class="box3 right">
        right bubble using css
    </div>
    <img id="img-right" src="https://s3.amazonaws.com/kairos-media/team/Ben_Virdee-Chapman.jpeg"/>
</div>
<div class="container">
    <img id="img-left" src="https://cdn.madaracosmetics.com/media/catalog/category/FACE_OK_3.jpg"/>
    <div class="box3 left">
        left bubble using css
    </div>
</div>

答案 2 :(得分:0)

作为难题,我将在几年内完成。.:-P

HTML

<div class="box3 right-up"><img src="https://picsum.photos/50/50?image=1069">right bubble using css</div>
<div class="box3 right-down"><img src="https://picsum.photos/50/50?image=1069">right bubble using css</div>
<div class="box3 left"><img src="https://picsum.photos/50/50?image=1069">left bubble using css</div>

CSS

    * {
  margin: 0px;
  padding: 0px;
}

.box3 {
  margin: 0 auto;
  width: 300px;
  border-radius: 15px;
  background: #00bfb6;
  color: #fff;
  padding: 20px;
  text-align: center;
  font-weight: 900;
  font-family: arial;
  position: relative;
}
.box3 > img {
  border-radius:50%;
  position:absolute;
  top:-15px;
}

.right-up > img {
display:none;
}
.right-down > img {
  left:calc(100% + 20px);
}
.left > img {
  right:calc(100% + 20px);
}

/* right-up bubble */

.right-up {
    margin-top: 30px;
}

.right-up:before {
  margin: 20px auto;
  content: "";
  width: 0px;
  height: 0px;
  position: absolute;
    border-left: 15px solid #00bfb6;
    border-right: 15px solid transparent;
    border-top: 15px solid transparent;
    border-bottom: 15px solid #00bfb6;
  right: -16px;
  top: 33px;
}

.right-down {
    margin-bottom: 30px;
}

.right-down:before {
  margin: 20px auto;
  content: "";
  width: 0px;
  height: 0px;
  position: absolute;
  border-left: 15px solid #00bfb6;
  border-right: 15px solid transparent;
  border-top: 15px solid #00bfb6;
  border-bottom: 15px solid transparent;
  right: -16px;
  top: 0px;
}


/* left bubble */

.left:before {
  margin: 20px auto;
  content: "";
  width: 0px;
  height: 0px;
  position: absolute;
  border-left: 15px solid transparent;
  border-right: 15px solid #00bfb6;
  border-top: 15px solid #00bfb6;
  border-bottom: 15px solid transparent;
  left: -16px;
  top: 0px;
}