Eliminate span line break without moving text to new line - css

I have this right now:
#outer-div {
border: 1px dashed red;
line-height: 50px;
width: 350px;
}
.inner-div {
border: 1px dashed red;
float:left;
height: 50px;
width:80px;
}
#title {
/*white-space: nowrap;*/
}
<div id="outer-div">
<div class="inner-div"></div>
<div class="inner-div"></div>
<span id="title">Random long text not to break<span>
</div>
and it looks like:
and I'd need this:
Tried solutions from other threads but they not work as this. white-space: nowrap moves the entire span to the next line.
Here is a working code: https://codepen.io/neptune01/pen/ppMyEQ
Can this be done using only css?

It's a bit tricky, but doable using Flexbox (as is almost anything), text-overflow: ellipsis, and, yes, white-space: nowrap :
Bonus: you don't need line-height or float.
#outer-div {
border: 1px dashed red;
width: 300px;
display: flex;
align-items: center;
}
.inner-div {
border: 1px dashed red;
height: 50px;
width: 80px;
flex-shrink: 0;
}
#title {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
flex-shrink: 1;
border: blue dashed 1px;
}
<div id="outer-div">
<div class="inner-div"></div>
<div class="inner-div"></div>
<span id="title">Random long text not to break</span>
</div>

You can do it with the white-space: nowrap together with the text-overflow: ellipsis and overflow: hidden, also the #title span needs to be displayed as block or you can just use another div instead:
#outer-div {
border: 1px dashed red;
line-height: 50px;
width: 350px;
}
.inner-div {
border: 1px dashed red;
float: left;
height: 50px;
width: 80px;
}
#title {
display: block;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
<div id="outer-div">
<div class="inner-div"></div>
<div class="inner-div"></div>
<span id="title">Random long text not to break</span>
</div>

Assign id just to your parent div and not to other elements present inside.
white-space:nowrap makes contents present inside applied div to not wrap them next line, so you need to add overflow:hidden too, which hides content from overflowing outside .title or targeted div, thus this hides content and then apply text-overflow:ellipsis to get (...) in end of overflowed content.
#outer-div {
border: 1px dashed red;
line-height: 50px;
width: 350px;
}
.inner-div {
border: 1px dashed red;
float: left;
height: 50px;
width: 80px;
}
.title {
display: block; /*Changed display to block*/
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
<div id="outer-div">
<div class="inner-div"></div>
<div class="inner-div"></div>
<span class="title">Random long text overflows content out of box.</span>
</div>

Related

Prevent text overflowing DIV when parent DIV width is set by percentage

I know there are a number of similar posts but I have read over 20 and the solutions I've found are not helping me.
Some of my content has a layout similar to the one I've detailed here:
Fiddle Example
There is a container div "Strip" holding 3 child divs; img,title,description. The contain div has a fixed width of 944px.
The first div img is a fixed size. This works fine.
The second div title should auto size to accommodate the length of the title. This works fine.
The third div description should fill the remainder of the unused Strip Div without overflowing. This is not working.
I can get it to work if I set the description div to a fixed width size but that doesn't help me as the size will change depending on how long the title is.
I'm sure this is very simple but I've gone through so many iterations with no luck.
CSS:
font-weight: bold;
}
.strip {
width: 944px;
max-width: 944px;
display: inline-block;
border: Solid;
border-width: 2px;
max-height: 28px;
white-space: nowrap;
}
.img {
display: inline-block;
border: solid;
border-width: 1px;
border-color: red;
max-width: 50px;
max-height: 28px;
height: 28px;
white-space: nowrap;
text-wrap: hidden;
}
.img p {
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
.title {
display: inline-block;
border: solid;
border-width: 1px;
border-color: green;
max-height: 28px;
}
.title p {
white-space: nowrap;
overflow: hidden;
}
.description {
display: inline-block;
border: solid;
border-width: 1px;
border-color: blue;
max-height: 28px;
}
.description p {
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}```
**HTML:**
```<html>
<div class="strip">
<div class="img">
<p>
Test
</p>
</div>
<div class="title">
<p>
Rotten Tomatoes
</p>
</div>
<div class="description">
<p>
Rotten Tomatoes, home of the Tomatometer, is the most trusted measurement of quality for Movies & TV. Rotten Tomatoes, home of the Tomatometer, is the most trusted measurement of quality for Movies & TV.
</p>
</div>
</div>
</html>```
Flexbox can do that:
.strip {
width: 90vw;
display: flex;
margin:auto;
}
.img {
border: 1PX solid red;
width: 50px;
}
.title {
border: 1px solid green;
white-space: nowrap;
}
.description {
flex:1;
border: 1px solid blue;
min-width: 0;
}
.description p {
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
<div class="strip">
<div class="img">
IMG
</div>
<div class="title">
<p>
Rotten Tomatoes
</p>
</div>
<div class="description">
<p>
Rotten Tomatoes, home of the Tomatometer, is the most trusted measurement of quality for Movies & TV. Rotten Tomatoes, home of the Tomatometer, is the most trusted measurement of quality for Movies & TV.
</p>
</div>
</div>

how to vertical align bottom inline block elements in container [duplicate]

This question already has answers here:
vertical align bottom + inline block
(2 answers)
Closed 6 years ago.
I have the following html structure
<div class="examples">
<div class="option"></div>
<div class="option"></div>
<div class="option"></div>
</div>
CSS:
.examples {
whitespace: nowrap;
min-height: 400px;
}
.option {
width: 18%;
max-width: 18%;
margin-top: 10px;
margin-bottom: 10px;
margin-right: 1%;
vertical-align: bottom;
cursor: pointer;
border: 4px solid red;
display: inline-block;
}
How can I vertically align inline blocks (option) at the bottom of the container (examples) using CSS? These are a row of images inside the examples container. I tried vertical align: bottom, but that doesn't work and I want to stay away from flex because of lack of cross browser support. I also want to stay away from absolute position because the elements (option) are a row of images.
You'll probably want to use flexbox and use display: flex; justify-content: flex-end; on the container div. That should put them at the bottom.
.examples {
whitespace: nowrap;
min-height: 400px;
position: relative;
background: blue;
}
.aligner {
position: absolute;
bottom: 0;
width: 100%;
}
.option {
width: 18%;
max-width: 18%;
margin-top: 10px;
margin-bottom: 10px;
margin-right: 1%;
cursor: pointer;
border: 4px solid red;
display: inline-block;
}
<div class="examples">
<div class="aligner">
<div class="option"></div>
<div class="option"></div>
<div class="option"></div>
</div>
</div>

Wrap container around inline divs

Here is my problem. I have inline-block divs inside another div.
.timeEvents {
width: 100%;
overflow: hidden;
text-align: center;
}
.timeline {
border: 1px solid;
}
.events1, .events2 {
border: 1px solid;
}
.ev1, .ev3 {
border: 1px solid red;
}
.ev2 {
margin: 0 auto;
border: 1px solid red;
display: inline-block;
}
.mDiv {
display: inline-block;
padding-left: 12px;
padding-right: 12px;
border: 1px solid blue;
}
<div class="timeEvents">
<div class="events1">
<div class="ev1">Data Field 1</div>
</div>
<div class="timeline">
<div class="ev2">
<div class="mDiv">5</div>
<div class="mDiv">10</div>
<div class="mDiv">15</div>
<div class="mDiv">20</div>
<div class="mDiv">25</div>
</div>
</div>
<div class="events2">
<div class="ev3">Data Field 2</div>
</div>
</div>
I want the .ev2 to be wrapped around its children which are inline. Then, the two data fields, respectively .ev1 and .ev3 placed above and below, should have the same width as .ev2. Which means that all divs with a red border (in my JSFiddle) should have the same width (dynamic, I don't know it) and that width should not be 100% as it's in the jsFiddle example: https://jsfiddle.net/mzjqw2wx/17/.
EDIT - I updated my fiddle. I don't want to lose the outside 100% divs, I want to align the three red sections to have the same width, the page and the outside divs all remain 100%. The tip to use inline-block on the wrapper was great, it did what I wanted with the middle one. I wanted to align all red containers and I did it with jQuery.
You need to also set display: inline-block; for the common wrapper (and give text-align: center to its parent)
body { text-align: center; }
.timeEvents {
display: inline-block;
margin: 0 auto;
}
JSFiddle
Result:
That's pretty easy to implement using Flexbox.
Just assign display: flex; to .ev2 and flex-grow: 1; to the the .myDiv class.
You can see it in the following code:
.timeEvents {
width: 100%;
overflow: hidden;
text-align: center;
}
.timeline {
border: 1px solid;
}
.events1, .events2 {
border: 1px solid;
}
.ev1, .ev3 {
border: 1px solid red;
}
.ev2 {
margin: 0 auto;
border: 1px solid red;
display: flex;
}
.mDiv {
display: inline-block;
padding-left: 12px;
padding-right: 12px;
border: 1px solid blue;
flex-grow: 1;
}
<div class="timeEvents">
<div class="events1">
<div class="ev1">Data Field 1</div>
</div>
<div class="timeline">
<div class="ev2">
<div class="mDiv">5</div>
<div class="mDiv">10</div>
<div class="mDiv">15</div>
<div class="mDiv">20</div>
<div class="mDiv">25</div>
</div>
</div>
<div class="events2">
<div class="ev3">Data Field 2</div>
</div>
</div>
Check out CSS-Trick's Complete guide to Flexbox for more information.
Use display:table to timeEvents and remove width:100% will make as per your expected.
.timeEvents {
display: table;
overflow: hidden;
text-align: center;
}
Fiddle

CSS - Dynamic padding to expand divs to fill a dynamic width

So I have a variable number of elements in a div that has a variable width. The elements inside have a fixed space between them, 5px, but each one needs to expand to fill the full width of the space of the outer div with padding, so the text can be centerized.
Example:
.button-container{
width: 100%;
height: 50px;
}
.button-container .button{
min-width: 75px;
display: inline-block;
text-align: center;
border: 1px solid #FF0000;
}
.button-container .button + .button-container .button{
margin-left: 5px;
}
<div class='button-container'>
<div class='button'>B1</div>
<div class='button'>B2</div>
<div class='button'>B3</div>
<div class='button'>B4</div>
</div>
So how can I make the padding inside of the button class elements have a dynamic left and right padding to fill the space of the button-container class div?
Ideally, the solution will be a CSS only solution, as I don't want to have jQuery to do the spacing.
CSS tables would work here.
.button-container {
width: 100%;
height: 50px;
display: table;
border-collapse: separate;
border-spacing: 5px;
}
.button-container .button {
display: table-cell;
text-align: center;
border: 1px solid #FF0000;
vertical-align: middle;
}
<div class='button-container'>
<div class='button'>B1</div>
<div class='button'>B2</div>
<div class='button'>B3</div>
<div class='button'>B4</div>
</div>
You can use flexbox:
.button-container {
display: flex; /* Magic begins */
}
.button-container > .button {
flex: 1; /* Distribute the width equally */
text-align: center;
margin-left: 5px;
border: 1px solid #FF0000;
}
.button-container > .button:first-child {
margin-left: 0;
}
<div class='button-container'>
<div class='button'>B1</div>
<div class='button'>B2</div>
<div class='button'>B3</div>
<div class='button'>B4</div>
</div>

Keeping content centered vertically with only CSS?

I have a content area that should behave in the following way:
Content is centered vertically if there's no vertical overflow (currently achieved via display:table/-cell)
No scrollbar is displayed unless there is vertical overflow
the height of the containing div never changes
I've only been able to satisfy the first point - fiddle:
http://jsfiddle.net/PTSkR/125/
Here's my html:
<div class="row-fluid card-box">
<div class="span4 side-study-box">
<div class="side-box-content">
<pre class="text-content-saved">TEST
TEST</pre>
</div>
</div>
</div>
Css:
.side-study-box {
background-color: white;
color: black;
border: 1px solid #3D6AA2;
text-align: center;
height: 160px;
max-height: 160px;
display: table ;
margin: 0px ;
margin-left: -1px;
position: relative;
overflow-y: scroll ;
}
.side-study-box .side-box-content {
width: calc(100%);
height: 160px;
float: right;
display: table;
overflow-y: scroll ;
background-color: white;
}
/*#region CONTENT AREAS */
/*#region TEXT CONTENT */
.side-study-box .text-content-saved {
width: calc(100%+29px);
font-size: 24px;
display: table-cell;
vertical-align: middle;
text-align: center;
height: 160px !important;
max-height: 160px ;
background-color: white;
padding: 0px ;
margin: 0px ;
border: 0px ;
overflow-y: scroll;
}
Unfortunately I can't use js as part of the solution... is this possible with only css?
You can use overflow:auto; to achieve the second point and set the following:
word-break: normal !important;
word-wrap: normal !important;
white-space: pre !important;
fiddle: http://jsfiddle.net/PTSkR/134/
Keep in mind that since you are using a pre tag, it means that all the white spaces and line breaks formatting counts so any white space or extra line could cause the overflow and you might miss that. See here

Resources