Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

child div expanding out of parent div issue

Tags:

html

css

layout

I want something like this, click here.

This is a simple code in my HTML:

  #mainContent {
        margin:0;
        width:100%;
        height:600px;
        padding: 0 0 30px 0;
    }
    #mainContent #sidebar { /* sidebar inside #mainContent */
        float:left;
        width:220px;
        height:560px;
        margin:10px 10px 40px 10px;
        padding:10px 5px 10px 5px;
        background-color:#CCCCCC;
    }
    #mainContent #featContent {
        height:560px;
        margin:10px 10px 40px 260px;
        padding:10px 5px 10px 5px;
        background-color:#CCCCCC;
        overflow:auto;
    }
 <div id="mainContent">
    <div id="sidebar"></div>
    <div id="featContent"></div>
</div>



  

The problem is one of the divs are improperly placed .The #featContent div is going out of its parent #mainContent div for reason I don't understand. Check out this in jsFiddle here. The margin of #featContent goes out of the #mainContent bounds.

like image 767
Ajinx999 Avatar asked Oct 04 '12 06:10

Ajinx999


People also ask

How do I make div not go out of parent div?

We can achieve this by setting the overflow property of the full-page-width container div (the full width of the page) to hidden. This will hide the content that overflows to the left and right beyond the full page width and remove our unwanted horizontal scroll.

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 stop a div from overflowing the container?

You could simply set the container to flexbox. Another option would be using CSS table, some extra HTML code is needed. And max-height doesn't work with table layout, so use height instead. Save this answer.


1 Answers

Demo Add the following style to the #mainContent

#mainContent {
  overflow:hidden;
}

Live demo

like image 163
Rohit Azad Malik Avatar answered Sep 30 '22 06:09

Rohit Azad Malik