Calculate value with CSS3

ε祈祈猫儿з 提交于 2020-01-03 23:12:21

问题


Is there any way to achieve this in CSS3?:

height: 100% -110px;

My context:


回答1:


What you want to use is calc() that is comming to FF and propably webkit, but don't count on it being widely supported anytime soon.

As for your example, maybe sticky footer will be some inspiration for you.


Edit

Nowadays it's well supported by major browsers:

http://caniuse.com/calc




回答2:


You can't calulate it with pure CSS. (it will not work in all browsers, as mentioned by Litek ) But there is a organizational way to handle this, but you will need to wrap you element in a other one:

body {
  height; 100%;
  padding: 0 0 20px;
}

div#wrap {
  background: #fff;
  height: 100%;
  padding: 0 0 20px;
  margin: 0 0 -20px;
}

div#wrap div { //this would be your actual element
  height: 100%;
  background: pink;
}



回答3:


Directly like that i'm not aware of any feature widely adopted to do that.

But there is a easy method to achieve the effect.

Put all element inside a container <div> with 'height: 100%', this container should have position relative so you can position the other elements inside it relative to its position. place the header on top and the footer at bottom with absolute positioning and calculate with javascript the height that the content div must have.

You can also subscribe the 'window.onResize' event to recalculate when the window is resized.

I know this is not a clean and prety solution, but is the one the you can make work well in almost any browser.




回答4:


In the context it was given the 2nd div height value doesn't really matter. Actually it's only important where that div starts and where it ends.

In other words height = vertical end - vertical start:

#div2 {
    position:absolute;
    top:90px;/*20+50+20*/
    bottom:20px;
}

http://jsfiddle.net/cGwrw/3/



来源:https://stackoverflow.com/questions/7309170/calculate-value-with-css3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!