I need a max-margin CSS property, but it doesn't exist. How could I fake it?

Css

Css Problem Overview


I am trying to make a simple page with the following characteristics:

  1. Its body must be at least 60em wide.
  2. Its left and right margins must be equally wide and at most 3em wide.
  3. When the browser window is resized, the document's margins should be resized so that the browser window's horizontal scrollbar covers the least wide possible range.

Transforming these requirements into a linear programming problem, we get:

DEFINITIONS:
BRWS = (width of the browser window, not a variable)
BODY = (width of the document's body)
LRMG = (width of the document's left and right margins)
HSCR = (range of the browser window's horizontal scroll bar)

OBJECTIVE:
MIN HSCR   /* Third requirement */

CONSTRAINTS:
HSCR = BODY + 2*LRMG - BRWS  /* From the definition of how a browser's
                              * horizontal scrollbar works. */
BODY >= 60  /* First requirement */
LRMG <= 3   /* Second requirement */
LRMG >= 0   /* Physical constraint, margins cannot be negative */
HSCR >= 0   /* Physical constraint, scroll bars cannot have negative ranges */

Solving this linear program, we get:

BODY = (BRWS <= 66) ? 60 : (BRWS - 6)
HSCR = (BRWS >= 60) ?  0 : (60 - BRWS)
LRMG = (BRWS + HSCR - BODY) / 2

(Sorry for the boring math, but I am not confident that the original explanation in English was clear enough.)


Now back to the actual page. After googling to find what I could do with CSS, I managed to implement the first two requirements using the following code:

body {
  min-width: 60em; /* First requirement */
}

/* The document's body has only two children, both of which are divs. */
body > div {
  margin: 0 3em;    /* Second requirement, but in a way that makes */
  max-width: 100%;  /* it impossible to satisfy the third one. */
}

If CSS had a max-margin property, satisfying all requirements would be easy:

body > div {
  max-margin: 0 3em;
  max-width: 100%;
}

But, of course, max-margin does not exist. How could I fake it?

Css Solutions


Solution 1 - Css

Spacer divs on either side of the content divs. Those are your margins. Set their max width using the max-width property.

Solution 2 - Css

To mimic a variable-width left margin:

.div-class {
    display: inline-block;
}
.div-class:before {
	content: '';
	width: 10%;
	min-width: 100px;
	max-width: 200px;
	display: inline-block;
}

Solution 3 - Css

For pure CSS that works in any scenario:

  1. use @media to create a break point.
  2. Set a max-width rule below a certain size using responsive width measurements such as an em or % and over that size use static widths such as px.

You'll just need to do the math to figure out the static sizes at the break point to make sure it scales fluidly.

Sample Code:

.my_class {
   margin: 0px 10%;
} 
@media screen and (min-width: 480px) {
   .my_class { 
      margin: 0px 10px;
   } 
}

This will make .my_class follow both:

  • The margin: 0px 10%; at over a screen width of 480px
  • The margin: 0px 10px; at under 480px.

Solution 4 - Css

Matthew's answer is the correct one here, but to expand on what he's saying:

<div id="left"></div>
<div id="content">Content goes here</div>
<div id="right"></div>
<!-- probably need a cleanup div to fix floats here -->

CSS:

#left, #right {
    float: left;
    max-width: 3em;
}

#content {
    min-width: 60em;
}

Solution 5 - Css

For anyone coming to this post in 2021. I would like to point you to the answer given in this post: https://stackoverflow.com/questions/38078957/can-we-define-min-margin-and-max-margin-max-padding-and-min-padding-in-css.

TLDR: Use css math function min

Solution 6 - Css

body {
    margin-left: auto;
    margin-right: auto;
    width: 60em;
}

Solution 7 - Css

None of the answers worked for me 100%.

Best way is to use CSS table and table-cell. Supported by all browsers (even IE 8). This handles wrapping way better than float or inline-block solutions, when the page is too narrow.

CSS

.wrapper {
    display: table;
    position: relative;
    width: 100%;
}

.wrapper:before {
    content: '';
    display: table-cell;
    min-width: 100px;
    width: 25%;
    max-width: 300px;
}

.wrapper > .content {
    display: table-cell;
    width: 100%;
}

HTML

<div class="wrapper>
    <div class="content>
        <!-- content here -->
    </div>
</div>

Solution 8 - Css

Spacer divs are bad practice. Setup another DIV on top of your DIV on your template with text-align:right (if you want to maximize the margin-left) or text-align:left if you want to maximize the margin-right, this will do the job.

Solution 9 - Css

I've seen some very appealing solutions alreaady, but here's a solution that require's relatively little code:

Place the contents of the body in a div inside it, and use display: flex; justify-content: space-around;

body {
    display: flex;
    justify-content: space-around;
}

div {
    margin: 3em;
    width: 100%;
    min-width: 60em;
    background-color: gray;
}

<body>
<div>
... contents of the page ...
</div>
</body>

The gray background is only to visualize the resulting body.

Solution 10 - Css

See http://www.labite.com/

Re-size the window to observe the changes to the ui.

The website uses min-width max-width with width:100%

The black frame has max-width which is little wider then the container.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpyonView Question on Stackoverflow
Solution 1 - CssMatthewView Answer on Stackoverflow
Solution 2 - CssJonathan LinView Answer on Stackoverflow
Solution 3 - CssnosajimikiView Answer on Stackoverflow
Solution 4 - CssJordanView Answer on Stackoverflow
Solution 5 - CssRogier de RuijterView Answer on Stackoverflow
Solution 6 - CssErikaiView Answer on Stackoverflow
Solution 7 - CssOnur YıldırımView Answer on Stackoverflow
Solution 8 - CssPalDevView Answer on Stackoverflow
Solution 9 - CssYoranView Answer on Stackoverflow
Solution 10 - CssEmilView Answer on Stackoverflow