最佳答案
我试图做一个简单的网页,具有以下特点:
将这些需求转化为一个线性规划问题,我们得到:
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 */
解决这个线性规划,我们得到:
BODY = (BRWS <= 66) ? 60 : (BRWS - 6)
HSCR = (BRWS >= 60) ? 0 : (60 - BRWS)
LRMG = (BRWS + HSCR - BODY) / 2
(不好意思,这个数学题太无聊了,但我不确定原来的英文解释是否足够清楚。)
现在回到实际的页面。在用 google 搜索我能用 CSS 做些什么之后,我设法用以下代码实现了前两个需求:
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. */
}
如果 CSS 具有 max-margin
属性,满足所有要求将很容易:
body > div {
max-margin: 0 3em;
max-width: 100%;
}
但是,当然,max-margin
并不存在,我怎么能伪造它呢?