I'm trying to use the "VW+padding" technique described at css-tricks to get full-width bars. I have a minimal set of css styles applied to the entire html document inspired from one of those "better*$%!#*website" designs which is causing some conflict with the linked implementation.
The following works as intended until the browser is resized to less than 70ch. I thought the straightforward solution was to set a minimum of margin-left to either be 0 or the calculated negative margin. This positions the div in unexpected places during resizing though.
I see the css-tricks recommendation was last updated in 2014. Maybe there's a better approach to this now? Thanks for your advice.
* {
--width: 70ch;
--font: #101010;
--background: #f5f5f5;
}
html {
font-size: 1em;
max-width: var(--width);
padding: 0 2ch;
margin: 2ch auto;
color: var(--font);
background: var(--background);
}
body {
line-height: 1.3;
}
.full-width-bar {
max-width: none;
width: 100vw;
box-sizing: border-box;
padding: 1ch calc(50vw - (var(--width)/2));
/* top version of margin-left doesn't seem to work */
margin-left: min(0, calc(-50vw + (var(--width)/2)));
margin-left: calc(-50vw + (var(--width)/2));
background: var(--font);
color: var(--background);
}
<html>
<body>
<p>
But I must explain to you how all this mistaken idea of reprobating pleasure and extolling pain arose. To do so, I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness.
</p>
<div class="full-width-bar">
<p>No one rejects, dislikes or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but occasionally circumstances occur in which toil and pain can procure him some great pleasure.
</p></div>
<p>
To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? </p>
</body>
</html>
EDIT: Trying this technique as inspiration: https://stackoverflow.com/a/64322555/10527635 The idea being if the view window is greater than the global body width, the left margin is multiplied by 1. If the view window is resized less than the body width, the left margin will return 0. This implementation doesn't quite work though.
.full-width-bar {
--is-width-below-limit: min(1, max(calc(var(--width) - 100vw), 0)); /* 1 if <70ch 0 if >70ch*/
--is-width-above-limit: calc(1 - var(--is-width-below-limit)); /* 1 if yes 0 if no */
max-width: none;
width: 100vw;
box-sizing: border-box;
padding: 1ch calc(50vw - (var(--width)/2));
margin-left: calc(calc(-50vw + (var(--width)/2))*var(--is-width-above-limit));
background: var(--font);
color: var(--background);
}
The variable is undefined when I inspect it:
console.log(window.getComputedStyle(document.documentElement).getPropertyValue('--is-width-above-limit'));
EDIT 2: One issue with the if/else above: can't compare unitless and unit values. A different approach with the same idea (also not working as intended):
.full-width-bar {
--is-width-above-limit: 1;
max-width: none;
width: 100vw;
box-sizing: border-box;
padding: 1ch calc(50vw - var(--width)/2);
margin-left: calc(var(--is-width-above-limit) * (-50vw + var(--width)/2));
background: var(--font);
color: var(--background);
}
@media only screen and (max-width: var(--width)) {
.full-width-bar {
--is-width-above-limit: 0;
}
}