Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make CSS width to fill parent?

Tags:

css

I am sure this problem has been asked before but I cannot seem to find the answer.

I have the following markup:

<div id="foo">     <div id="bar">         here be dragons     </div> </div> 

My desire is to make foo to have width of 600px (width: 600px;) and to make bar have the following behaviors:

padding-left: 2px; padding-right: 2px; margin-left: 2px; margin-right: 2px; outerWidth: 100%; 

In other words instead of setting width of bar to 592px I would like to set the outer width of bar to 100% so that it is computed to 592px. The importance here is that I can change foo's width to 800px and bar will calculate when rendered instead of me having to do the math for all these instances manually.

Is this possible in pure CSS?

Some more fun with it:

  • What if #bar is a table?
  • What if #bar is a textarea?
  • What if #bar is an input?

  • What if #foo is a table cell (td)? (Does this change the problem or is the problem identical?)


So far the table#bar, input#bar has been discussed. I have not seen a good solution for textarea#bar. I Think a textarea with no border/margin/padding with a div wrap might work with the div styled to work as borders for the textarea.

like image 449
Dmitriy Likhten Avatar asked Feb 09 '10 20:02

Dmitriy Likhten


People also ask

How do you make a div fill its parent container?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.

How do you make a full width in CSS?

Using width, max-width and margin: auto; As mentioned in the previous chapter; a block-level element always takes up the full width available (stretches out to the left and right as far as it can). Setting the width of a block-level element will prevent it from stretching out to the edges of its container.

How do you make an element occupy full width?

The . header-square is missing width attribute. Therefore you should expand to 100% by doing width: calc(100% / 7); to achieve your desired result.


1 Answers

EDIT:

Those three different elements all have different rendering rules.

So for:

table#bar you need to set the width to 100% otherwise it will be only be as wide as it determines it needs to be. However, if the table rows total width is greater than the width of bar it will expand to its needed width. IF i recall you can counteract this by setting display: block !important; though its been awhile since ive had to fix that. (im sure someone will correct me if im wrong).

textarea#bar i beleive is a block level element so it will follow the rules the same as the div. The only caveat here is that textarea take an attributes of cols and rows which are measured in character columns. If this is specified on the element it will override the width specified by the css.

input#bar is an inline element, so by default you cant assign it width. However the similar to textarea's cols attribute, it has a size attribute on the element that can determine width. That said, you can always specifiy a width by using display: block; in your css for it. Then it will follow the same rendering rules as the div.

td#foo will be rendered as a table-cell which has some craziness to it. Bottom line here is that for your purposes its going to act just like div#foo as far as restricting the width of its contents. The only issue here is going to be potential unwrappable text in the column somewhere which would make it ignore your width setting. Also all cells in the column are going to get the width of the widest cell.


Thats the default behavior of block level element - ie. if width is auto (the default) then it will be 100% of the inner width of the containing element. so in essence:

#foo {width: 800px;} #bar {padding-left: 2px; padding-right: 2px; margin-left: 2px; margin-right: 2px;} 

will give you exactly what you want.

like image 94
prodigitalson Avatar answered Oct 14 '22 10:10

prodigitalson