Determining which DOM element is causing the dimensions to increase for a parent element - html

I run into this problem sometimes when a site is not contained inside the mobile viewport, and I need to determine what is causing the width to exceed.
Usually I do this by trial and error of hiding different elements until the elements resets to the desired 100% width. Then I repeat for each child element until I find the one that is causing it.
Is there a way in Firefox or Chrome devtools (or using a plugin) to see which DOM child element is deciding the current elements dimensions?

Determining what is defining an element's calculated width and height can be quite tricky. And neither the Chrome nor the Firefox DevTools provide an easy way to get that information in all cases. And to my knowledge, there are also no extensions available that make this easier. The Firefox DevTools team started a discussion on this some time ago, though.
There are two reasons why an element might be wider or higher than expected: Some CSS or some text on the element itself or on one of its descendant elements.
When there is no other solution, the one with hiding or removing the elements is probably the fastest one.
Nonetheless, here are some tips how to use the DevTools to determine what's defining an element's width and height:
Select the element and check in the Computed side panel whether the computed value is defined via a CSS rule. Expand the entries for width or height to see what CSS rules applied. Also check min-width and max-width or min-height and max-height and the other layout related properties like margin, border, and padding but also `line-height, etc.!
Check the text within the element influences its width or height. Long words with no break opportunities like spaces can be the culprit but also CSS property definitions like white-space: nowrap.
When you've checked the above for the element itself and couldn't find the reason, the width or height is influenced by one or more descendant elements. So you need to repeat those two steps for them.
a) To quickly see the dimensions of the direct child elements, first press → to expand the element if it isn't already, then ↓ to toggle through them. While stepping through them, check their CSS and text as noted in steps 1 and 2.
b) When you see one that is as wide as the element you are observing, repeat the previous step to get one more level down in the DOM structure.
The steps mentioned above can also be automated by using some JavaScript to walk through the tree and check the element widths or heights. A relatively simple snippet for that (which can be executed in the DevTools console) is
rootElement = $0;
rootWidth = rootElement.getBoundingClientRect().width;
walker = document.createTreeWalker(rootElement, NodeFilter.SHOW_ELEMENT, {
acceptNode: element =>
element.getBoundingClientRect().width === rootWidth ?
NodeFilter.FILTER_ACCEPT :
NodeFilter.FILTER_SKIP
});
currentNode = walker.currentNode;
while (currentNode) {
console.log(currentNode);
currentNode = walker.nextNode();
}
Where the $0 refers to the currently selected element.

Related

Is there a way to determine the source of an "auto" css property using Browser Developer Tools

I have a complicated page where the width:auto returns a width that is smaller than I think it should be for several of the divs. If I go to the Computed section in Developer Tools, I can see where the width for a given element is obtained by css line (this usually points to a line for a given element that says width:auto. If the width is a smaller number derived elsewhere though, it is grayed out. Is there a way to see where in the cascade that that width is determined more absolutely?

How to know if a Element is associated to a CSS layout box?

Some times I need to know whether a element is visible. Looking in forums I got that a easy way to know this is getting the offsetParent (not null). But, sometimes, the parent exists but height and width is zero, resulting in a no visible element. Now I realize that there are situations in which the element is visible but client{Width,Height} properties are zero!!!
In this question (clientWidth and clientHeight report zero while getBoundingClientRect is correct), a new feature overcame to me: in order to these properties to work properly, the element need to be associated to a CSS layout box!
1.If the element has no associated CSS layout box or if the CSS layout box is inline, return zero.
How to know if a element is associated to CSS layout box?

Measure the grid width

I'd like to imitate my favorite site and measure it grid arrangement:
How to measure its width of each part?
If you are using Google Chrome, you can see the width of every element in pixels, by opening Inspect Element (F12), and hovering over the element.
There are some useful JS properties and methods you can use via the Developer Console. This is useful if you have a list of elements you want to get dimensions for and want to get them at different browser dimensions.
You could type/paste in the following in the browser console:
console.log("#content width: " + document.getElementById("content").offsetWidth)
console.log("#answers width: " + document.getElementById("answers").offsetWidth)
Which would provide the following results that you could copy and paste out.
#content width: 1600
#answers width: 728
There may be a way to do this with the Chrome debugging protocol as well.
Here are some JS properties and methods that may be of use:
Use HTMLElement.offsetWidth for width as an integer. You can also use HTMLElement.offsetWidth for height.
MDN: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetWidth
MDN: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetHeight
The HTMLElement.offsetWidth read-only property returns the layout width of an element. Typically, an element's offsetWidth is a measurement which includes the element borders, the element horizontal padding, the element vertical scrollbar (if present, if rendered) and the element CSS width. If the element is hidden (for example, by style.display on the element or one of its ancestors to "none"), then 0 is returned.
Use HTMLElement.getBoundingClientRect() for fractional values:
MDN: https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
The Element.getBoundingClientRect() method returns the size of an element and its position relative to the viewport.
I think following illustration will help you:

What factors affect the calculation of an element's width and height?

What I have is an element whose styling is broken: the width and height are quite wrong. The problem is that there is no width or height specified on this element: if you look at it in Chrome Dev Tools, you see those properties are greyed out, as seen below, because they're just being calculated by the browser based on other information.
And comparing diffs between commits, I can see no styling rules were changed on this element. Which means something around the element was changed: one of its many children, or a parent.
What I would like to know is how can you determine what elements or rules are affecting that calculation, so as to debug it?
I am hoping there is sort of a checklist, something like "Check width property on all child elements. If it is set, then ..., otherwise move on to look at possible JavaScript affecting ... "
Edit: The question can be rephrased as ""What factors do browsers check, in what order and with what priority, to determine an element's dimensions?"
See these specifications: https://www.w3.org/TR/CSS2/visudet.html#propdef-height
There are a few different types of height calculations, as outlined in section 10.6. Take a look at the subsections there to determine how the height of your element is being calculated.
(There's a warning of some of the sections on that page being updated by other specifications, but a quick check on the updates doesn't seem to include any changes for height.)

"Whatever is Left" in a CSS layout

I have 4 elements inside a container element. The container element will have its height set to 100% of the browser window. The 4 inner elements will appear vertically stacked on each other (as normal). The first two elements and the last element should have a "natural" height (ie: enough to fit their contents). The 3rd element should expand to fill the space available in the container, after the other 3 eat all they need to.
So, it would look something like this:
I cannot set explicit heights for Element-1, Element-2, or Element-4, nor do I know the height of the Container. I don't know the natural height of Element-3 either; I plan on using overflow-scroll if it gets larger then what's available. I've added spacing between the elements for illustration, but there will be spacing (margins/padding) between the real elements too.
How do you achieve this using HTML/CSS? If compromises have to be made to get a decent layout, I'll consider them. Bonus points if the technique also applies horizontally (which I've needed on occasion).
First off, great visual.
Secondly.. would a javascript solution be out of the question?
Update
This was just intended to be a sample, but I have updated the code to appease some of the more picky people out there.
http://jsfiddle.net/tsZAV/9/
There are a number of things that make this impossible in pure css.
The browser window could be shorter than the dynamic height of the first 3 elements.
There is no way to force an element to take up the rest of the container's height.
CSS is a document styling language, not a programming language. Think of writing CSS as a set of guidelines that the page should try to follow, rather than a way of explicitly setting sizes (although you can explicitly set sizes).
This is relatively simple to do with JavaScript resizing the fourth element. You'll have to listen for a resize event so that the fourth element gets sized accordingly. Also, you'll want to set a min-height value for element-4, in case there isn't enough space for the fourth element.