Dont wrap row of divs - css

How to create set of float:right divs one next to another and make them not wrap, no matter how much of them exists or how wide is any of them. If they together are wider than viewport, then x-scroll should appear.
Content inside those divs should wrap normally.
CSS only would be good.

Style the parent element with white-space: nowrap;, though this only works with display: inline (or display: inline-block;) elements. Given the following HTML:
<div id="parent">
<div class="child"></div>
<!-- there's quite a lot of these... -->
<div class="child"></div>
</div>
And the CSS:
#parent {
white-space: nowrap;
}
#parent .child {
display: inline-block;
/* there's some other CSS for aesthetics */
}
JS Fiddle demo.
Unfortunately I don't think there is a way of forcing float-ed elements to not wrap to a new line.
To preserve or, rather, force normal line-wrapping for descendant elements you'll have to explicitly over-ride the inheritance and set white-space: normal (as well as, possibly, define a width or max-width)
/* other CSS remains intact */
#parent .child {
display: inline-block;
/* irrelevant/aesthetic CSS */
white-space: normal;
max-width: 8em;
}
JS Fiddle demo.

Few elements: http://jsfiddle.net/thirtydot/A8duy/
Many elements: http://jsfiddle.net/thirtydot/A8duy/1/
HTML:
<div class="block-container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div>​
CSS:
.block-container {
text-align: right;
white-space: nowrap;
float: left;
width: 100%;
margin-bottom: 1em;
border: 1px solid red;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.block-container > div {
width: 50px;
height: 50px;
vertical-align: top;
display: inline-block;
*display: inline;
zoom: 1;
text-align: left;
white-space: normal;
border: 1px solid blue;
}

Related

Keeping an :after element on ellipsis

I want to have an icon (checkmark) behind a line with variable width.
if the line becomes too long, i want it to be truncated with ellipsis.
But the checkmark is supposed to stay AFTER the ellipsis
https://jsfiddle.net/Lkvt39re/
.inner {
width: 80%;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
i've set the width to 80%, and want to have the :afterinserted ..well, after the ellipsis.
how can i do that?
Thanks
Try adding a ::before pseudo element instead, then style it to float right. This way, your pseudo content won't become trimmed out by the restrictions set to the element width.
CSS
.inner::before {
content: 'X';
float: right;
}
Alternatively
You can set the ::after pseudo element to the parent element .outer, then set the nested .inner element to display inline-block (allowing the pseudo element of .outer to fall after initial width of .inner) with a max-width declared; once this max-width is exceeded your overflow rule will apply, giving you the ellipsis but still keeping the pseudo element of .outer visible after the text-overflow.
The problem is trying to declare this pseudo element to an element that you've also declared width restrictions and overflow rules to. You'll need to declare the pseudo element outside of the element that will, at some point, begin trimming out content.
.inner {
width: 80%;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.inner::before {
content: 'X';
float: right;
}
.outer {
width: 200px;
}
/* Alternative */
.alternative .inner {
max-width: 80%;
width: auto;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
display: inline-block;
}
.alternative .inner.no-max-width {
max-width: none;
}
.alternative .inner::before {
display: none;
}
.alternative.outer::after {
content: 'X';
display: inline-block;
}
<div class="outer">
<div class="inner">
this is pretty longggggg
</div>
</div>
<br>
<p><strong>Alternative</strong></p>
<div class="alternative outer">
<div class="inner">
this is pretty longgggggggggggg
</div>
</div>
<div class="alternative outer">
<div class="inner no-max-width">
this is pretty long
</div>
</div>
Devman,
You need to give the pseudo element some shape and define it as either an inline-block or a block element to do so. You can then set the dimensions appropriate to your styling.
Check out this edit:
.inner {
width: 80%;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
position:relative;
/** give your container some extra space for the pseudo **/
padding-right: 25px;
}
.inner::after {
content: 'X';
color:red;
/** define it as a "block" element and add dimension **/
display: inline-block;
height: 1.0rem;
width: 1.0rem;
}
.outer {
width: 180px;
}
http://codepen.io/jonrandahl/pen/rLMKwR

Cross-browser CSS text-overflow with floating elements and fluid text width

The scenario I have here: http://jsfiddle.net/b2xLgkqu/4/ (approximate minimal reproduction of my actual usage scenario)
Basically, I have a base element with width: 100% which has three children - leftside segment, rightside segment and main text. The left and right side have a known width in rems, but it's completely fluid otherwise. The text-overflow works nicely in Chrome, IE11 and Chrome for Android, but not on the latest Firefox. I'm looking for something that would make it work there too, without breaking any of the other browsers. (I also can't use workarounds like hiding the main text with a background-color on the side elements due to the containing element having semitransparent background color, and I'd like to keep it that way.)
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
.head {
width: 100%;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
position: relative;
}
.left, .right {
width: 5rem;
padding: 0 0.1rem;
position: relative;
z-index: 1;
}
.left {
float: left;
}
.right {
float: right;
}
span {
position: relative;
z-index: 0;
}
<div class="head">
<div class="left">FOO BAR</div>
<div class="right">FOO BAR</div>
<span>
long text
<span style="color: red">that</span>
should end up
<b>wrapping with</b>
text overflow ellipsis blah blah more blogging here
</span>
</div>
You set some styles to .head but you should set them to .head > span instead:
.head > span {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.left, .right {
width: 5rem;
padding: 0 0.1rem;
}
.left {
float: left;
}
.right {
float: right;
}
.head > span {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
<div class="head">
<div class="left">FOO BAR</div>
<div class="right">FOO BAR</div>
<span>
long text
<span style="color: red">that</span>
should end up
<b>wrapping with</b>
text overflow ellipsis blah blah more blogging here
</span>
</div>
Also note it needs display: block in order to interact like you want with the floating elements.

Nested div elements wrapping with float left

I have four nested div elements with float:left and the fourth element is wrapping below the first due to the length of the container.
.container {
width: 320px;
height: 110px;
overflow-x:scroll;
border: 1px solid blue;
}
.nested {
width: 80px;
height: 80px;
background: red;
float:left;
margin:5px;
}
<div class='container'>
<div class='nested'></div>
<div class='nested'></div>
<div class='nested'></div>
<div class='nested'></div>
</div>
How do I stop the wrapping so that the viewed elements are scrollable in the x axis (or even hidden/truncated)?
http://jsfiddle.net/Tku65/
Demo
white-space: nowrap;
Add this property to your container.
Try to avoid float properties. Use display: inline-block; in this case.

Why word-break doesn't work here?

http://codepen.io/rick-li/pen/oshCb
<div class="wrapper">
<div class="left"></div>
<span>lfjaslkjasldjfaljdfaldflasjflasjdfldasfsdafafasdfsdafadsfazxcvzvzxv</span>
</div>
.wrapper{
width: 400px;
height: 300px;
border: solid 1px;
}
.wrapper .left{
float: left;
width: 200px;
height: 200px;
border: solid 1px;
background-color: #111111
}
.wrapper span{
word-wrap: break-word;
}
you will notice the text dropped to the bottom,
if there're spaces exist in the text, it will wrap correct and lies on the wright, so I wonder why the word-wrap: break-word or word-wrap: break-all doesn't work?
You could also use, word-wrap: break-word;
.wrapper .right{
display: block;
width:200px;
}
.wrapper .right{
word-wrap:break-word;
}
.right should not be inline. Also, assign some width to it.
.wrapper .right{
display: block;
width: 200px;
}
.wrapper .right{
word-break: break-all;
}
DEMO here.
NoobEditor in reference to your answer did you mean to say "word-wrap only works when there are no spaces rather than when there are spaces in the word? The reason why word-wrap is not working in the code above is due to wrong placement of closing div.
<div class="wrapper">
<div class="left"></div> <!-- here is your problem -->
<span>lfjaslkjasldjfaljdfaldflasjflasjdfldasfsdafafasdfsdafadsfazxcvzvzxv</span>
</div>
If you close div tag in the right place, word-wrap will work:
<div class="wrapper">
<div class="left">
<span>lfjaslkjasldjfaljdfaldflasjflasjdfldasfsdafafasdfsdafadsfazxcvzvzxv</span>
</div> <!-- close statement here -->
</div> <!-- close statement here -->
See it here
You can do this a couple of ways, you can change the display from inline so that you can set a max-width, allowing it to know when it should break the word:
http://codepen.io/anon/pen/ibvKq
.wrapper .right{
display: inline-block;
max-width: 290px;
word-wrap: break-word;
}
Alternatively, if you want to keep the inline, you need to couple the word-wrap: break-word; with white-space: pre; but this does mean it will preserve line-breaks and spaces. Personally, I'd use the first one over this one.
http://codepen.io/anon/pen/cfhwI
.wrapper .right{
display: inline;
white-space: pre;
word-wrap: break-word;
}

CSS: inline-block vs inline text

I want my block to be set by line-height (just like i do with text). As i know i should use display: inline-block in this case, but this doesn't work for me. Why?
HTML:
<div class="block">
<div></div>
</div>
<div class="block">
test
</div>
CSS:
.block {
line-height: 50px;
height: 50px;
width: 50px;
border: 1px solid black;
}
.block div {
height: 40px;
width: 28px;
background-color: #f0f;
display: inline-block;
}
Live demo: jsFiddle
hi now add your div aertical-align middle in your css
.block div {
vertical-align: middle;
}
Demo
--------------------------------------------
now if you want to center this box than add text-align center as like this
.block {
text-align: center;
}
Demo
i guess you are trying to center the purple block vertical?
in that case your mixing thing up:
a <div> is a block-level element, where text is not. so if you say line-height, you specify text-alignment of the content for that element, not positioning of a block element, to solve the centering of that purple block, use padding or margin:
.block div {
height: 40px;/* 50 - 40 = 10pixel/2 = 5px space */
width: 28px;
background-color: #f0f;
margin-top: 5px;
}
Demo over here jsFiddle

Resources