What CSS will center the entire relatively-positioned body of ANY webpage, with no scrollbars, and equal clipping of overflow content on left and right of the viewport?
When the page renders, if the widest element is wider than the viewport, then i want the left and right sides of the content to "fall off" the sides of the screen. Overflow on both left and right should be clipped/hidden.
The width of the content is unpredictable, and may often be wider than the viewport. We should always see the middle slice, no scrollbars.
Not duplicate question
Please don't mark "duplicate". No answer in these questions achieves my desired aim. I've tested every one of them.
How to align entire html body to the center?
align body to center - centering body
Unlike common answers in the above questions, I do not have the option to wrap the content in a div. I can only work with the html and body tags.
Required HTML:
This question doesn't allow to alter this html. Your CSS answer should work with any webpage. These element are just examples-- there would be many more, unpredictable elements in the html. You cannot wrap the content in a container div. Your CSS answer should show the center slice of the page, clipping off to the left and right whatever content doesn't fit on the screen.
<html>
<body>
Overflow to left
<div id="wider">Center-screen</div>
</body>
</html>
Required CSS
#wider {
width: 10000px;
border: 1px solid black;
text-align: center;
}
body {
position: relative;
}
You can't change this CSS, only add new CSS. Need position:relative
on body.
The #wider element is just an example of wide content. The real-world usage would include many more elements of unpredictable width.
Output should render something like the image below, with no scrollbars. The text "Overflow to left" should be clipped off the screen to the left. The words "Center-screen" should be dead-center, horizontally.
Non-working solution 1:
The answer below, from a similar question, doesn't appear achieve my goal:
html,
body {
height: 100%;
}
html {
display: table;
width: 100%;
}
body {
display: table-cell;
text-align: center;
vertical-align: middle;
}
Non-working solution 2:
Another answer from a similar question doesn't seem to work for my scenario:
body {
width: 80%;
margin-left: auto;
margin-right: auto;
}
Fiddle:
This fiddle contains my HTML, along with the above failed answer. Feel free to test your answer in this fiddle.
Desired output from HTML above: