At the moment, I canβt figure out how to do this, only with CSS gradients and one element.
Given your example and assuming that an additional div is fine, then an alternative approach without gradients ( http://jsfiddle.net/jpftqc26/2/ ):
HTML
<body class="background"> <div class="foreground"/> </body>
CSS
html, body { width: 100%; height: 100%; } .background { background-color: #000000; } .foreground { background-color: #ff0000; width: 100%; max-width: 500px; height: 100%; margin-left: auto; margin-right: auto; }
This gives the same effect, uses one additional element and provides a red foreground that will increase to a maximum width of 500 pixels - except that it is black on both sides. If you want red always to be 500 pixels wide, just remove the max-width rule and change the width to 500px.
source share