Can we define min-margin and max-margin, max-padding and min-padding in css?

后端 未结 19 1491
情话喂你
情话喂你 2021-01-31 01:01

Can we define min-margin and max-margin, max-padding and min-padding in CSS ?

19条回答
  •  孤城傲影
    2021-01-31 01:46

    UPDATE 2020

    With the new (yet in Editor's draft) CSS 4 properties you can achieve this by using min() and max() (also you can use clamp() as a - kind of - shorthand for both min() and max()

    clamp(MIN, VAL, MAX) is resolved as max(MIN, min(VAL, MAX))

    min() syntax:

    min( # )
    
    where 
     =  [ [ '+' | '-' ]  ]*
    
    where 
     =  [ '*'  | '/'  ]*
    
    where 
     =  |  |  | (  )
    

    max() syntax:

    max( # )
        
    where
     =  [ [ '+' | '-' ]  ]*
        
    where  
     =  [ '*'  | '/'  ]*
    
    where 
     =  |  |  | (  )
    

    clamp() syntax:

    clamp( #{3} )
    
    where 
     =  [ [ '+' | '-' ]  ]*
    
    where 
     =  [ '*'  | '/'  ]*
    
    where 
     =  |  |  | (  )
    

    Snippet

    .min {
      /* demo */
      border: green dashed 5px;
      /*this your min padding-left*/
      padding-left: min(50vw, 50px);
    }
    
    .max {
      /* demo */
      border: blue solid 5px;
      /*this your max padding-left*/
      padding-left: max(50vw, 500px);
    }
    
    .clamp {
      /* demo */
      border: red dotted 5px;
      /*this your clamp padding-left*/
      padding-left: clamp(50vw, 70vw, 1000px);
    }
    
    
    /* demo */
    
    * {
      box-sizing: border-box
    }
    
    section {
      width: 50vw;
    }
    
    div {
      height: 100px
    }
    
    
    /* end of demo */


    Old Answer

    No you can't.

    margin and padding properties don't have the min/max prefixes

    An approximately way would be using relative units (vh/vw), but still not min/max

    And as @vigilante_stark pointed out in the answer, the CSS calc() function could be another workaround, something like these:

    /* demo */
    
    * {
      box-sizing: border-box
    }
    
    section {
      background-color: red;
      width: 50vw;
      height: 50px;
      position: relative;
    }
    
    div {
      width: inherit;
      height: inherit;
      position: absolute;
      top: 0;
      left: 0
    }
    
    
    /* end of demo */
    
    .min {
      /* demo */
      border: green dashed 4px;
      /*this your min padding-left*/
      padding-left: calc(50vw + 50px);
    }
    
    .max {
      /* demo */
      border: blue solid 3px;
      /*this your max padding-left*/
      padding-left: calc(50vw + 200px);
    }

提交回复
热议问题