doumeitang572461 2013-12-27 22:04
浏览 55
已采纳

改变css:在使用Javascript或PHP之前和之后

#speech
{
    font-family: customfont;
    font-size: 30px;
    font-weight: bold;
    color: #0B03FE;
    position: relative;
    width: 45%;
    height: auto;
    text-align: center;
    background-color: #fff;
    border: 8px solid #F50032;
    -webkit-border-radius: 30px;
    -moz-border-radius: 30px;
    border-radius: 30px;
    -webkit-box-shadow: 2px 2px 4px #888;
    -moz-box-shadow: 2px 2px 4px #888;
    box-shadow: 2px 2px 4px #888;
}

#speech:before
{
    content: ' ';
    position: absolute;
    width: 0;
    height: 0;
    left: 30px;
    top: 150px;
    border: 25px solid;
    border-color: #F50032 transparent transparent #F50032;
}

#speech:after
{
    content: ' ';
    position: absolute;
    width: 0;
    height: 0;
    left: 38px;
    top: 150px;
    border: 15px solid;
    border-color: #fff transparent transparent #fff;
}

What I have here is a speechbubble, made in CSS. This works perfectly when I have my speech div set with a fixed height, such as height: 150px; , however I need the height to adjust itself depending on how much text the div contains. That's why I've got it like height: auto;.

Now if you take a look at #speech:before and #speech:after , you can see that both have top set to top: 150px;. This is the problem I'm having, as I need to alter both of these to whatever the height of #speech is. I already have a way to get the dynamic height by the following Javascript:

window.onload = function(){
    var element = document.getElementById('speech');
    var speechheight = element.offsetHeight;
};

so now I have the height I need for both "tops" - I just can't seem to figure out a way to actually apply it. Could anyone help me?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dsa1234569 2013-12-27 22:19
    关注

    Change the top value to 100%:

    #speech:before,
    #speech:after {
        top: 100%;
    }
    

    I don't think you can select :before and :after elements with JavaScript.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档