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.

3
  • Add a css class with JS. And do the height changes in the css file. Commented Dec 27, 2013 at 22:09
  • That's kind of what I've been trying and failing miserably at. Commented Dec 27, 2013 at 22:12
  • element.style.top = speechHeight + 'px'; Maybe I'm missing what you are trying to accomplish. Commented Dec 27, 2013 at 22:17

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

...Wow. This actually solved it. That's much easier than what I had in mind... I feel so stupid now. :/ Thanks for this!
This makes sense. Also, if you look at other questions similar to this, they mention that pseudo-elements are not DOM elements
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.
bottom:-<height of the pseudo-element>px would have worked also.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.