Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HR width 100% issue

Could you tell me how to limit content width to the screen boundary? For the following script i always get 2px width wider than screen (allowed space) width.

document.body.scrollWidth is always 2px wider than screen

<html>
<head>
    <style>
        * {margin: 0; padding: 0}
    </style>
    <script type="text/javascript">
        function test(){
            alert(document.body.scrollWidth);
            alert(document.getElementById("hrtest").scrollWidth);
            alert(document.getElementById("divtest").scrollWidth);
            alert(screen.width);
        }
    </script>
</head>
<body onLoad="test()">
    <div id="divtest">
        <hr size="2" width="100%" id="hrtest" />
    </div>
</body>
</html>
like image 602
awattar Avatar asked Sep 10 '25 10:09

awattar


2 Answers

hr {
    border-right : 0;
    border-left: 0;
}

Should do it. You may wish to add important to the style, or iterate through all hr in document and set in-line style.

like image 102
Furmek Avatar answered Sep 13 '25 01:09

Furmek


I believe that the problem is the browser is adding a 1 px border to build the hr. Inspecting the hr with chrome shows us the following styles applied by default to all hr's:

display: block;
-webkit-margin-before: 0.5em;
-webkit-margin-after: 0.5em;
-webkit-margin-start: auto;
-webkit-margin-end: auto;
border-style: inset;
border-width: 1px;

Disabling the border-width: 1px; will give the expected results, but hides the hr. Anyway, removing the hr's width works :P

like image 30
scumah Avatar answered Sep 12 '25 23:09

scumah