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

后端 未结 19 1474
情话喂你
情话喂你 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:51

    You can also use @media queries to establish your max/min padding/margin (but only according to screen size):

    Let's say you want a max padding of 8px, you can do the following

    div {
      padding: 1vh 0;
    }
    @media (max-height: 800px) {
      div {
        padding: 8px 0;
      }
    }
    
    0 讨论(0)
  • 2021-01-31 01:52

    See this CodePen https://codepen.io/ella301/pen/vYLNmVg which I created. I used 3 CSS functions min, max and calc to make sure the left and right paddings are fluid between minimum 20px and maximum 120px.

     padding: 20px max(min(120px, calc((100% - 1198px) / 2)), 20px);
    

    Hope it helps!

    0 讨论(0)
  • 2021-01-31 01:54

    I think I just ran into a similar issue where I was trying to center a login box (like the gmail login box). When resizing the window, the center box would overflow out of the browser window (top) as soon as the browser window became smaller than the box. Because of this, in a small window, even when scrolling up, the top content was lost.

    I was able to fix this by replacing the centering method I used by the "margin: auto" way of centering the box in its container. This prevents the box from overflowing in my case, keeping all content available. (minimum margin seems to be 0).

    Good Luck !

    edit: margin: auto only works to vertically center something if the parent element has its display property set to "flex".

    0 讨论(0)
  • 2021-01-31 01:56
    var w = window.innerWidth;
    var h = window.innerHeight;
    if(w < 1116)
        document.getElementById("profile").style.margin = "25px 0px 0px 975px";
    else
        document.getElementById("profile").style.margin = "25px 0px 0px 89%";
    

    Use code above as an example of how to set min-margin and padding

    0 讨论(0)
  • 2021-01-31 01:56

    This seems to work for me to make a responsive iframe embed with a max height set.

    .embed-container {
        position: relative;
        padding-bottom: min(80vh, 100%);
        width: 100%;
        height: 100%;
        max-height: 80vh;
        overflow: hidden;
        max-width: 100%;
      }
      .embed-container iframe,
      .embed-container object,
      .embed-container embed {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        max-height: 80vh;
      }
    
    <div class="embed-container">
      <iframe
        src="https://player.vimeo.com/video/405184815?title=0&byline=0&portrait=0"
        frameborder="0"
        webkitAllowFullScreen
        mozallowfullscreen
        allowfullscreen
      ></iframe>
    

    0 讨论(0)
  • 2021-01-31 01:56

    I was looking for a solution to make the contents of a row behave like in a fixed width container, but with shrinking browser width make a minimal margin from the left (in my case: so that the row contents do not hide under a big logo with position: absolute in left-top).

    This is what worked for me:

    HTML

    <div class="parent-container">
      <div class="child-container">
        <h1>Some header</h1>
      </div>
    </div>
    

    CSS

    .parent-container {
      padding-left: min(150px);
    }
    .child-container {
      padding-left: calc( 50vw - 500px );
    }
    

    When I scale the browser window left and right, my h1 doesn't go to the left more than the 150px, but goes right following the behavior of my next row's content set to display in a fixed container.

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