0
#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.

Tienus McVinger
  • 467
  • 9
  • 24

1 Answers1

2

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.

Popnoodles
  • 28,090
  • 2
  • 45
  • 53
pstenstrm
  • 6,339
  • 5
  • 41
  • 62
  • ...Wow. This actually solved it. That's much easier than what I had in mind... I feel so stupid now. :/ Thanks for this! – Tienus McVinger Dec 27 '13 at 22:24
  • This makes sense. Also, if you look at other questions similar to this, they mention that pseudo-elements are _not_ DOM elements – Josh Beam Dec 27 '13 at 22:26
  • I'm horrible with names. Never know what people mean with pseudo-elements and DOM elements. I have looked up such things many times, but I always forget. – Tienus McVinger Dec 27 '13 at 22:29
  • `bottom:-px` would have worked also. – Christoph Dec 27 '13 at 22:30
  • @Tienus McVinger: The DOM (Document Object Model) is a mystical fairy object that exists behind the scenes in your browser (it's a bunch of code based on your HTML document, I think C++ for Firefox, for example). Not sure how it works, but I guess :before and :after don't exist in there. – Josh Beam Dec 27 '13 at 22:36
  • 1
    [There are workaround available for manipulating `:before` and `:after` with JavaScript](http://stackoverflow.com/questions/5041494/manipulating-css-pseudo-elements-using-jquery-e-g-before-and-after). – Blazemonger Mar 26 '14 at 12:55