Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stop div resizing parent div using css

Tags:

css

resize

http://jsfiddle.net/ETkkR/

<div id="Blog1">
    <div class="post">
    <img src="http://dummyimage.com/180x120/000/fff" alt="Image 1" title="This is my first image"/>
        <div class="post-info">
            <span>post title post title post title</span>
        </div>
    </div>
    <div class="post">
    <img src="http://dummyimage.com/175x104/f0f/fff" alt="Image 2" title="The second one is pretty"/>
                <div class="post-info">
            <span>post title post title post title</span>
        </div>
    </div>        
</div>​

The div.post-info in some cases(images of width greater than the div.post-info content) fits the div.post parent however sometimes the width of the div.post-info is greater having an affect on the parent div.post by resizing it. how can i make the div.post-info fit the width of the div.parent and not resizing it if it is greater.

my css

#Blog1{
    padding:10px;
}
#Blog1 .post{
    border:1px solid #000000;
    margin:3px;
    text-align:center;
    position: relative;
    display: inline-block;
    *display: inline;
    zoom: 1
}
.post img{
    height:100px;
}
.post .post-info{
    text-align:left;
}
.post .post-info span{
    word-wrap:break-word;
}​

Edit

YOU CAN CHANGE THE ELEMENTS AS LONG AS THE CONTENT REMAINS SAME TO CREATE A SOLUTION people keep giving solutions which are not suitable for what i'm asking...what i need is for the child div .post-info to not be a greater width than that of the .post parent div

like image 261
Yusaf Khaliq Avatar asked May 21 '12 20:05

Yusaf Khaliq


2 Answers

It seems to work with the following change to your CSS:

.post .post-info span{
    display: block;
    max-width: 90%;
    margin: 0 auto;
}​

JS Fiddle demo.

Reasoning:

  • display: block; allows a width (and therefore max-width) to be assigned to the element.
  • max-width: 90%; to ensure that the element's maximum width is less than the width of the parent, allowing some space between the content of the element and the borders of the parent.
  • margin: 0 auto; horizontally centres the element within its parent.
like image 31
David Thomas Avatar answered Nov 13 '22 10:11

David Thomas


Here is a demo, but you should really explain: do you want the height to be variable ?

jsFiddle demo

edited CSS (only changed elements):

#Blog1 .post{
    border:1px solid #000000;
    margin:3px;
    text-align:center;
    position: relative;
    display:block;         /**/
    float:left;            /**/
    overflow:hidden;       /**/
    padding-bottom:24px;   /**/
}
.post .post-info span{
    position:absolute;     /**/
    word-wrap:break-word;
} 

P.S: this question associates me to an old answer of mine :)
Pop Images like Google Images
If you need help ... I'm here

like image 64
Roko C. Buljan Avatar answered Nov 13 '22 09:11

Roko C. Buljan