Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make the text of this div wrap?

Tags:

html

css

I have a simple HTML structure that looks like:

<div class="container">
    <div class="caption">
        <div class="title">This is a very, very long title!!!</div>
        <div class="details">Details</div>
    </div>
    <div class="content"></div>
</div>

I've styled this very simply:

.container {
    min-width: 200px;
}
.caption {
    overflow: hidden;
}
.title {
    float: left;
}
.details {
    float: right;
}
.content {
    height: 200px;
    background-color: #c0c0c0;
}

Which looks like:

css-example

If I shrink the window enough eventually the "details" div will wrap to the next line:

css-example2

What I would like to happen is the text inside the "title" div wrap to the next line but keep both "title" and "details" at the same line (not wrapping).

Something like:

This is a very, very         Details
long title!!!
+----------------------------------+

With the title only wrapping once there isn't enough space as the window is resized.

Can anyone point me in the right direction for achieving this?

Here is a jsFiddle of the above code if anyone is interested.

Edit: To clarify, I'd like to not specify a fixed width for .title if possible. In most cases I'd like to let this div get as wide as it needs.

like image 302
KingNestor Avatar asked Jun 12 '13 20:06

KingNestor


People also ask

How do you make text wraps?

Select the picture or object. Go to Picture Format or Shape Format and select Arrange > Wrap Text. If the window is wide enough, Word displays Wrap Text directly on the Picture Format tab. Choose the wrapping options that you want to apply.

How do you force text wrap in CSS?

You can force long (unbroken) text to wrap in a new line by specifying break-word with the word-wrap property. For example, you can use it to prevent text extending out the box and breaking the layout. This commonly happens when you have a long URL in the sidebar or comment list.


2 Answers

this should work:

http://jsfiddle.net/KV8UW/

<div class="container">
<div class="caption">
    <div class="details">Details</div>
    <div class="title">This is a very, very long title!!!</div>
</div>
<div class="content"></div>

.container {
    min-width: 200px;
}
.caption {
    overflow: hidden;
}
.details {
    float: right;
}
.content {
    height: 200px;
    background-color: #c0c0c0;
}
like image 115
bob Avatar answered Oct 10 '22 19:10

bob


Use display for your layout:http://jsfiddle.net/3nBDd/15/

.caption {
    display:table;
    width:100%;
}
.title {
    display:table-cell;
    text-align: left;
}
.details {
    display:table-cell;
    text-align right;
}
like image 1
G-Cyrillus Avatar answered Oct 10 '22 18:10

G-Cyrillus