CSS shorthand for positioning
Asked Answered
A

7

70

There are any shorthand for top right bottom left or for width and height ?

I have a lot of css like this

#topDiv {
    position:absolute;
    top:0px;
    left:0px;
    right:0px;
    height:100px;
}
#centerDiv {
    position:absolute;
    top:100px;
    bottom:120px;
    left:0px;
    right:0px;
}
#consoleDiv {
    position:absolute;
    left:0px;
    right:0px;
    bottom:0px;
    height:120px;
}

I would like to do anything like this

position: absolute 10px 50px 50px 100px;

or

size: 400px 200px; 
Ahmed answered 1/6, 2012 at 18:9 Comment(3)
CSS doesn't have that. However, you can use a CSS framework (such as LESS) to create such behavior (using Parametric Mixins).Busey
Make a .class with { position:absolute; left:0; right:0; }Aboveboard
@Aboveboard the example values was arbitrary, thanks anyway.Ahmed
S
63

2021 Update: The inset property is currently gaining adoption. This property uses the same multi-value syntax as the shorthand margin property. For browser compatibility, please see MDN.


No short-hand exists to combine all of these values. These are all different properties, unlike, for instance background, which has colors, images, positions and repeat instructions and as such can be coalesced into a short-hand form.

If you really wanted this type of control, you could use something like SASS and create a mixin.

Seigneur answered 1/6, 2012 at 18:11 Comment(4)
Would SASS be an overkill for this ?Nutrition
@Nutrition You could definitely use that; or roll your own solution using PostCSS.Seigneur
Not true anymore, checkout inset.Battle
@YihaoGao Inset is currently an Editor's Draft, with no support in Safari. Neat feature, but definitely should be used sparingly at this time.Seigneur
H
19

I just found this, was looking for the same, I ended up using sass for top, bottom, left, right

here is my solution

@mixin position($top, $right: $top, $bottom: $top, $left: $right) {
    top: $top;
    right: $right;
    bottom: $bottom;
    left: $left;
 }

works like most css shorthands

@include position(5) // all 4

@include position(5,4) // vertical, horizontal

@include position(5,4,3) // top, horizontal, bottom

@include position(5,4,3,2) // top, right, bottom, left

Hydrocellulose answered 14/9, 2017 at 15:43 Comment(1)
This doesn't answer his question as he explicitly asked for css.Aer
V
8

The answer is no as they are different properties so can not be combined. You can however consolidate your css a little bit rather than repeating certain properties, e.g:

#topDiv,
#centerDiv,
#consoleDiv {
    position:absolute;
    left:0;
    right:0;
}
#topDiv {
    top:0;
    height:100px;
}
#centerDiv {
    top:100px;
    bottom:120px;
}
#consoleDiv {
    bottom:0;
    height:120px;
}
Vitiated answered 1/6, 2012 at 18:23 Comment(1)
doesn't directly answer the question of combining directional properties with a position property, which is something that will probably never happen in CSSCommissioner
C
2

inset is what you can use in 2020 as positioning shorthand, but you need to use PostCSS and this plugin https://github.com/jonathantneal/postcss-inset

Example:

.example {
  inset: 10px 20px 80px;
}

/* becomes */

.example {
  top: 10px;
  right: 20px;
  bottom: 80px;
  left: 20px;
}

More info and spec here: https://www.w3.org/TR/css-logical-1/#propdef-inset

Commissioner answered 15/4, 2020 at 14:21 Comment(3)
Actually you can't use inset anywhere outside of Firefox right now, and that spec is only a working draft, so you shouldn't be using this property even for production Firefox sites yet. This also doesn't directly answer the question of combining directional properties with a position property, which is something that will probably never happen in CSS.Coveney
A lot of things implemented by browsers are working draft and editors draft. Anyway, like people use SASS for everything, you can use PostCSS with a bunch of js lines and start using this with a plugin github.com/jonathantneal/postcss-insetCommissioner
Keep checking browser compatibility. It's documented at developer.mozilla.org/en-US/docs/Web/CSS/insetUbiquitarian
T
1

you can use inset: top right bottom left;.

#topDiv {
    position: absolute;
    inset: 0;
    min-height: 100px;
}

#centerDiv {
    position: absolute;
    inset: 100px 0 120px;
}

#consoleDiv {
    position: absolute;
    inset: auto 0 0 0;
}
Thyroiditis answered 15/1, 2022 at 8:10 Comment(0)
U
0

If you want shorthand for this, you're gonna need to make what's called a mixin with Sass. Don't know what it is? Look it up!

@mixin position($position, $args) {
  @each $o in top right bottom left {
        $i: index($args, $o);

    @if $i and $i + 1< = length($args) and type-of(nth($args, $i + 1)) == number  {
          #{$o}: nth($args, $i + 1);
    }
  }

  position: $position;
}

@mixin absolute($args) {
        @include position("absolute", $args);
}

@mixin fixed($args) {
        @include position("fixed", $args);
}

@mixin relative($args) {
        @include position("relative", $args);
}

Here's a link that explains it:

http://hugogiraudel.com/2013/08/05/offsets-sass-mixin/

Umbel answered 10/6, 2015 at 19:22 Comment(1)
doesn't directly answer the question of combining directional properties with a position property, which is something that will probably never happen in CSSCommissioner
D
0

It is better to use Inset link to inset MDN page here

Drone answered 16/3, 2022 at 22:59 Comment(2)
Welcome to the comunity. Please notice somebody else already posted inset as a solution. Also, when answering a question, please avoid "Link answers" like yours. An answer should be elaborated and explanatory. For example, in this case, an example of use would have improve your answer a lot.Vicegerent
oh, I am sorry about that, will take of this from now onwards @ÒscarRayaDrone

© 2022 - 2024 — McMap. All rights reserved.