Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Parent-Div does not grow with it's children (horizontal)

Tags:

css

width

parent

I would like the parent-div (red) to grow with the green child-div. Now it just stops at the viewport.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="de" xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">
<head>
<title>simple document</title>
<style type="text/css">
 * {
  font-family: verdana;
  margin: 0px;
  padding: 0px;
 }
</style>
</head>
<body>
<div style="margin: 30px; background: red; padding: 10px;">
 <div style="background: green; width: 2000px;">dxyf</div>
</div>
</body>
</html>

I don't want to use display:table; since it does not work well in IE.

Any ideas?

like image 710
vyden_st Avatar asked Mar 30 '10 13:03

vyden_st


People also ask

How do you make a div expand to fit a parent?

There are two methods to stretch the div to fit the container using CSS tat are discussed below: Method 1: First method is to simply assign 100% width and 100% height to the child div so that it will take all available space of the parent div.

How do I stop DIVS from expanding?

Answer: Use the CSS display Property You can simply use the CSS display property with the value inline-block to make a <div> not larger than its contents (i.e. only expand to as wide as its contents).

Why child div is bigger than parent?

A child div can also be wider than its parent by utilizing different positioning such as absolute or fixed positioning. Different results can occur depending on the specified position of the parent div but as long as the element is either absolute/fixed or contains a specified width, it will grow outside the parent.

How do I make my div take 100% height of parent div?

Answer: Set the 100% height for parents too And we all know that the default value of the height property is auto , so if we also set the height of <body> and <html> elements to 100%, the resulting height of the container div becomes equal the 100% height of the browser window.


2 Answers

Use display: inline-block; on the parent <div> and it will work as expected

like image 115
Josh Stodola Avatar answered Oct 14 '22 17:10

Josh Stodola


Make the parent div float:left; and it will be expanded as desired.

like image 38
Paul Avatar answered Oct 14 '22 17:10

Paul