Calculating width from percent to pixel then minus by pixel in LESS CSS
Asked Answered
S

4

105

I will calculate width in some element from percent to pixel so I will minus -10px via using LESS and calc(). It´s possible?

div {
    span {
        width:calc(100% - 10px);
    }
}

I using CSS3 calc() so it doesn't work: calc(100% - 10px)

Example: if 100% = 500px so width = 490px (500-10);

I made a demo for testing : http://jsfiddle.net/4DujZ/55/

so padding will say: 5 (10px / 2) all the time when I resizing.

Can I do it in LESS? I know how to do in jQuery and simple CSS like margin padding or else... but i will try to do functional in LESS with calc()

Spooner answered 11/2, 2013 at 14:55 Comment(2)
As a general rule, don't style selectors that have no semantics, like div or span.Vinegarish
Here's a cross browser mixin I wrote for using calc on any css property: https://mcmap.net/q/76252/-disable-less-css-overwriting-calc-duplicateIchthyo
P
260

You can escape the calc arguments in order to prevent them from being evaluated on compilation.

Using your example, you would simply surround the arguments, like this:

calc(~'100% - 10px')

Demo : http://jsfiddle.net/c5aq20b6/


I find that I use this in one of the following three ways:

Basic Escaping

Everything inside the calc arguments is defined as a string, and is totally static until it's evaluated by the client:

LESS Input

div {
    > span {
        width: calc(~'100% - 10px');
    }
}

CSS Output

div > span {
  width: calc(100% - 10px);
}

Interpolation of Variables

You can insert a LESS variable into the string:

LESS Input

div {
    > span {
        @pad: 10px;
        width: calc(~'100% - @{pad}');
    }
}

CSS Output

div > span {
  width: calc(100% - 10px);
}

Mixing Escaped and Compiled Values

You may want to escape a percentage value, but go ahead and evaluate something on compilation:

LESS Input

@btnWidth: 40px;
div {
    > span {
        @pad: 10px;
        width: calc(~'(100% - @{pad})' - (@btnWidth * 2));
    }
}

CSS Output

div > span {
  width: calc((100% - 10px) - 80px);
}

Source: http://lesscss.org/functions/#string-functions-escape.

Philia answered 1/6, 2013 at 5:54 Comment(3)
You rock, dude! Thank you for those examples and explanations. Do you happen to know how I could divide an unkown width of a container (let's call it div.categories) into 4 equal parts, using LESS?Depository
@ShawnSpencer: In between the lines, there's a lot of obscurity. Can you create a JSFiddle illustrating what you're trying to solve? I can try to guess the use case, but I'm inhibited by not knowing the full details, and I'm sure you know how that goes—your worst inhibitions tend to psych you out in the end.Philia
I'm not inclined to resign to maturity. Since I'm going for a straightforward CSS solution, and since I was able to make things work, using one of the answers, I'm good for now. Thank you for offering to look at a JSFiddle example though. Much appreciated.Depository
D
0

I think width: -moz-calc(25% - 1em); is what you are looking for. And you may want to give this Link a look for any further assistance

Deathful answered 11/2, 2013 at 17:57 Comment(0)
G
-3

Or, you could use the margin attribute like this:

    {
    background:#222;
    width:100%;
    height:100px;
    margin-left: 10px;
    margin-right: 10px;
    display:block;
    }
Grim answered 11/2, 2013 at 14:59 Comment(1)
OP wants the width 10px less than 100% width. Your CSS does not do that. It will make it wider than 100%.Lillia
M
-3

Try this :

width:auto;
margin-right:50px;
Merciful answered 22/1, 2016 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.