Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML/CSS: total page width is wider than content

Tags:

html

css

I have a strange problem, and I can't find the cause! I have the following webpage: http://uk.translation-vocabulary.com/de-german and the perceived width of the page is perhaps 300px greater than the width of the content. So a horizontal scrollbar is present even when the viewport is horizontally stretched to match the visible content.

I have been inspecting elements with Firebug, trying to find the culprit. No success so far.

This effect observed in Firefox, Safari, Chrome. Untested: IE.

Any help greatly appreciated!

Benjamin.

like image 568
benjaminwilson Avatar asked Sep 14 '11 10:09

benjaminwilson


People also ask

How do I fix page width in HTML?

To convert it to a fixed-width layout, simply add a fixed with to the #wrapper and set the margins to auto. Setting the margins to auto will cause the left and right margins to be equal no matter how wide the browser window is, which will cause your fixed-width layout to be positioned in the center of the browser.

How do I change the width of a div according to content?

Using inline-block property: Use display: inline-block property to set a div size according to its content.


1 Answers

Two ways to diagnose the culprit.

Method A

  1. Open the Chrome dev tools in the Elements tab.
  2. Expand all elements under HTML by hovering over the triangle on the left, then Ctrl+Alt click (Windows) or Option click (Mac).
  3. Hover your mouse over each element and see if it highlights inside the area on the webpage making it too wide.

Method B

  1. Open the Chrome dev tools then hit Esc to open the console.
  2. Paste in this code and hit Enter.

    var docWidth = document.documentElement.offsetWidth;
    [].forEach.call(
      document.querySelectorAll('*'),
      function(el) {
        if (el.offsetWidth > docWidth) {
          console.log(el);
        }
      }
    );
    

This will find all the elements in the page overhanging. Thanks to Chris Coyier https://css-tricks.com/findingfixing-unintended-body-overflow/

like image 120
Mark Hewitt Avatar answered Nov 15 '22 23:11

Mark Hewitt