CSS shorthand for positioning

前端 未结 5 1234
小鲜肉
小鲜肉 2020-12-05 17:17

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

I have a lo

相关标签:
5条回答
  • 2020-12-05 17:24

    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;
    }
    
    0 讨论(0)
  • 2020-12-05 17:42

    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

    0 讨论(0)
  • 2020-12-05 17:42

    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/

    0 讨论(0)
  • 2020-12-05 17:43

    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.

    0 讨论(0)
  • 2020-12-05 17:47

    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

    0 讨论(0)
提交回复
热议问题