tags:

views:

113

answers:

1
  1. By default the CSS Width property is set to value “Auto”. What width value is used on an element when Width is set to “Auto”?

  2. If we nest a textbox ( <input type="text" /> ) inside div element, the width of which is smaller than that of a textbox, then part of textbox is displayed outside the div element (I think it's called overflowing, but I’m not sure)

.

<div style="width:120px; background-color:Aqua;">
    <input type="text" style="width:1000px;" />
</div>

I realize one way to handle this problem is for nested element to have its CSS width property set to some percentage value V ( 0% < V < 100% ), but is there also a more elegant way of forcing nested elements to automatically adjust their width to that of the parent element and so that they wouldn’t overflow?

+1  A: 

1) By default the CSS Width property is set to value “Auto”. What width value is used on an element when Width is set to “Auto”?

It depends on the element. Usually elements will expand to display their entire contents, unless that expansion would exceed existing constraints, in which case that constraint is its width.

is there also a more elegant way of forcing nested elements to automatically adjust their width to that of the parent element and so that they wouldn’t overflow?

You just described it. Setting width: 100% is the way to have the element fill its parent. If you want to constrain the nested element's width further, you can use the max-width and min-width properties, which allow you to enforce maximum and minimum sizes on elements that have variable widths.

Welbog
Welbog: “If you want to constrain the nested element's width further, you can use the max-width and min-width “ As far as I can tell, max-width doesn’t take into account padding and margin and thus element may still overflow parent element due to having large padding or margin. Is there also a property that sets the maximum size ( this size also includes padding and margin ) an element can have?
carewithl
Welbog: “It depends on the element. Usually elements will expand to display their entire contents, unless that expansion would exceed existing constraints, in which case that constraint is its width.” What are those constrains? For example, shouldn’t the width of a parent element be considered a constrain?
carewithl
`max-width` is such a constraint, as well as the parent's width (if the parent's width is given in an absolute unit like pixels). There are no properties that I know of that constrain the effective space (i.e. padding, border, margins, width) that an element takes up. You'll have to subtract those values from the `max-width` value yourself.
Welbog
thank you for helping me
carewithl