4

I've got some elements on the page that have a before pseudo element whose height is styled with the CSS calc() function; something like this:

.el:before: {
    content: "";
    height: calc(50% + 10px);
}

I want to use this method to get the height of the :before element - and in Webkit-based browsers it works and returns a pixel value.

var height = window.getComputedStyle(
    document.querySelector('.el'), ':before'
).getPropertyValue('height');

In Firefox, however, it returns the actual string of the CSS rule (exactly 'calc(50% + 10px)').

(function() {
  var height = window.getComputedStyle(
    document.querySelector('.myelem'), ':before'
  ).getPropertyValue('height');
  
	document.getElementById('result').innerHTML = 'Calculated height is: ' + height;
})();
.myelem {
  position: relative;
  padding-left: 20px;
}
.myelem:before {
  content: "";
  position: absolute;
  top: 0px;
  left: 0px;
  width: 1px;
  height: calc(50% + 2px);
  background-color: red;
}
<div>
  <div class="myelem">
    <span>Some stuff here</span>
  </div>
</div>

<div id="result">

</div>

Is there any work-around for this?

1 Answer 1

3

That's indeed a bug,

For a fix, you can vote for this bug-report from 2013 and hope that it will finally get fixed, or even propose a patch if you've got some time and C++ knowledge ;-)

For a workaround, you'll have to compute it yourself...

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

2 Comments

PS : for the workaround, e.generalov does provide something in the comments of the bug report that seems to work : jsfiddle.net/3FecG/3

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.