Reputation: 159
I have a block of text with unknown width and I would like to place another text right after it that would always stick to the last word. If the first block is one line then setting them both to 'inline' or 'inline-block' is enough, but if the first block is more than one line, the second block always goes to the next line.
Code:
html
<div id="text">sit amet, consectetur adipiscing elit. Donec facilisis eros arcu, sed dictum lorem consequat a. Duis sodales rhoncus felis at convallis.</div>
<div id="new">New</div>
css
div {
float: left;
display: inline-block;
}
'New' needs to always stick to the last word of the previous block. How can I do this?
EDIT: To clarify, I cannot change the markup. The content is always in two separate divs.
Upvotes: 0
Views: 8380
Reputation: 152
Your text is pushed down because if you have an 'inline-block' element, and the text is long enough to fill 100% width of a parent container the second line will also have 100% width. That's why the second div will start rendering below that first div. If you want your divs in one line you have to give them 'display: inline;' property.
If it's a static and short text, for example name of an author, you can use pseudo-element ':after', like this:
div.text:after{
content: ' put you text here'; /*remember to put whitespace on the beginning*/
background-color: red;
}
but if you want to use 'div' as inline element just use 'display: inline;' without float:
div.text{
display: inline;
}
Hope I helped.
Upvotes: 1
Reputation: 8981
Like this
css
#new {
background: red;
}
div {
display: inline-block;
}
Upvotes: 0
Reputation: 2565
you can make use of span
<span id="text"> sit amet, consectetur adipiscing elit. Donec facilisis eros arcu, sed dictum lorem consequat a. Duis sodales rhoncus felis at convallis. </span>
<span id="new">New</span>
Upvotes: 0
Reputation: 711
Like this:
html
<div id="text"> sit amet, consectetur adipiscing elit. Donec facilisis eros arcu, sed dictum lorem consequat a. Duis sodales rhoncus felis at convallis.
<div id="new">New</div></div>
css
#new {
background: red;
}
div {
display: inline;
}
Upvotes: 2
Reputation: 27600
Just remove the float
, if the two divs are display: inline
that should be enough.
Upvotes: 0
Reputation: 1451
Getting rid of the float:left;
on your fiddle seems to do what you're looking for.
Upvotes: 1