Alex
Alex

Reputation: 2206

How to make a absolute div adjust width to its content

I have a absolute div with some content, like this:

HTML:

<div class="testimonials_carousel">
    <div class="testimonials_carousel_item"> 
        <div class="item_profile"> 
        <img src="http://placehold.it/150x150"/>
        </div> 
        <div class="item_story_container">
            <div class="item_story"> 
                <p> blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah </p>
                <p class="customer_name">- John Doe</p>
             </div> 
         </div> 
     </div> 
</div>

CSS:

.testimonials_carousel {
    width: 100%;
    margin: 0 auto;
    border:1px solid black;
    height:250px;
    position:relative;
}

.testimonials_carousel_item {
    opacity: 1;
    position:absolute;
    border: 1px solid blue;
    display: flex;
    left:0;
    right:0;
    top:20px;
    margin: auto;
    max-width:90%;
}

.item_profile {
    width: 20%;
    float:left;
    border:1px solid red;
    align-self: center;
    display: inline-block;
    vertical-align: middle;
}

.item_story_container {
    max-width: 75%;
    float: right;
    border:1px solid yellow;

}

.item_story {
    float: left;
    background-color: #fff;
    border-radius: 10px;
    border: 1px solid #9c9c9c;
    padding: 20px;
}

.item_story p {
    font-size: 20px;
    font-family: 'Open Sans', sans-serif;
}

.customer_name {
    color: #de2b2b;
    font-family: 'Shadows Into Light', cursive !important;
}

.item_profile img {
    border-radius: 100%;
    width: 80%;
    border: 1px solid #9c9c9c;
    margin: 0 auto;
    display: block;
}

jsfiddle: http://jsfiddle.net/keb9qcan/

So far everything looks fine. But if I reduce the number for "blah" down to 3 so the story class gets less width. Then the absolute-parent div still remains at the width 90% making the content look not centered.

How can I make the div auto-adjust its width based on its content?

I am looking for a solution using CSS/HTML. But if that is not possible a jQuery solution would be okay.

I haven been going tru alots of SO QA. But no matter how I do I always end up with only one of the divs (alot/less text) working as I want. If I get it correct with less text, then it does'nt look correct with alot of text. And vice versa.

Any help on this will be appriciated.

EDIT: After getting a answer I feel I also need to say this. The class .testimonials_carousel_item needs to be positions:absolute. Because other things in my layout and jQuery-functions depends on that this class is position:absolute.

Upvotes: 3

Views: 385

Answers (1)

Ivin Raj
Ivin Raj

Reputation: 3429

please Try this one:

DEMO

.testimonials_carousel_item {
    opacity: 1;
    position:fixed;
    border: 1px solid blue;
    display: flex;
    left:0;
    right:0;
    top:20px;
    margin: auto;
    max-width:90%;
}

Upvotes: 1

Related Questions