CSS 宽度减法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3819908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 22:51:36  来源:igfitidea点击:

CSS width subtraction

css

提问by Jeaf Gilbert

How to subtract width in CSS?

如何在CSS中减去宽度?

For example:

例如:

width = 100% - 10px

I'm not talking about padding or margin.

我不是在谈论填充或边距。

回答by mpgn

Now with calcthe solution will be :

现在使用calc解决方案将是:

width: calc(100% - 10px);



Calccan be use with Addition, Subtraction, Multiplication, Division.

Calc可以与加法、减法、乘法、除法一起使用。

Additional note :

附加说明:

Note: The + and - operators must always be surrounded by whitespace. The operand of calc(50% -8px) for instance will be parsed as a percentage followed by a negative length, an invalid expression, while the operand of calc(50% - 8px) is a percentage followed by a minus sign and a length. Even further, calc(8px + -50%) is treated as a length followed by a plus sign and a negative percentage. The * and / operators do not require whitespace, but adding it for consistency is allowed, and recommended.

注意: + 和 - 运算符必须始终用空格包围。例如,calc(50% -8px) 的操作数将被解析为一个百分比,后跟一个负长度,一个无效的表达式,而 calc(50% - 8px) 的操作数是一个百分比,后跟一个减号和一个长度. 更进一步, calc(8px + -50%) 被视为长度后跟加号和负百分比。* 和 / 运算符不需要空格,但为了一致性而添加它是允许的,建议使用。

回答by Peter

Simple: you can't do this. You'll have to use some workaround.

很简单:你不能这样做。您将不得不使用一些解决方法。

回答by n1313

Well, until CSS3 calc()is released in all major browsers, all you have to do is wrap one div with another and use some paddings-margins. OR, you can use some javascript, like counting the width of the screen and setting the width of a div accordingly.

好吧,在所有主要浏览器都发布CSS3 calc()之前,您所要做的就是将一个 div 与另一个 div 包装起来并使用一些 paddings-margins。或者,您可以使用一些 javascript,例如计算屏幕的宽度并相应地设置 div 的宽度。

回答by Grant Crofton

You might be able to do this with SASS, if you're using a stack which supports it. I'm only aware of Ruby, but there might well be others.

如果您使用支持它的堆栈,您也许可以使用SASS来做到这一点。我只知道 Ruby,但很可能还有其他的。

SASS is CSS-style code which generates traditional CSS, you can use variables and so on.

SASS 是生成传统 CSS 的 CSS 样式代码,您可以使用变量等。

回答by Thariama

Actually there is no such functionality. All you can do is:

实际上没有这样的功能。你所能做的就是:

width:auto;
margin-right: 10px;

which is not what you want.

这不是你想要的。

回答by Nev Stokes

The only way to achieve this is to construct your CSS using LessCSSor a similar tool and then process these files into generated CSS - you can't do it on the fly

实现这一目标的唯一方法是使用LessCSS或类似工具构建您的 CSS ,然后将这些文件处理成生成的 CSS - 您不能即时完成

回答by e2-e4

Usually, one uses a dynamic approach, that generates code on the fly. For instance, in PHP, while writing the CSS part,

通常,人们使用动态方法,即动态生成代码。例如,在 PHP 中,在编写 CSS 部分时,

 $width = $all - 10;
 echo 'width:' . $width . 'px;';