Div centerer

Force center the main content of all websites to remove horizontal scrollbars

Versione datata 19/06/2025. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Div centerer
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Force center the main content of all websites to remove horizontal scrollbars
// @author       Saumil
// @match        *://*/*
// @grant        GM_addStyle
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    GM_addStyle(`
        body {
            margin-left: auto !important;
            margin-right: auto !important;
            max-width: 100% !important;
            overflow-x: hidden !important;
        }
        html {
            overflow-x: hidden !important;
        }
    `);

    const fixWideElements = () => {
        document.querySelectorAll('body > *').forEach(el => {
            if (el.offsetWidth > window.innerWidth) {
                el.style.maxWidth = '100%';
                el.style.marginLeft = 'auto';
                el.style.marginRight = 'auto';
                el.style.boxSizing = 'border-box';
            }
        });
    };

    fixWideElements();
    window.addEventListener('resize', fixWideElements);
    setTimeout(fixWideElements, 1000);
})();