text alignment with "=" sign - html

I want to align the text with "=" sign, as of the picture below:
I can do it using mathjax but I want to make it using css if possible. But the way I did it is not making the equals to sign aligned one to another. I want your help.
This is what I tried,
Solution, <br>
Given, f(x) = 3x <br>
g(x) = x + 2 <br>
fog(x) = 18 <br>
To find: x = ?, <br>
Now, <br>
fog(x) = 18 <br>
or, f(x + 2) = 18 <br>
or, 3(x + 2) = 18 <br>
or, x + 2 = 6 <br>
&therefore; x = 4

You can use display, text-align and width in your classes:
.c-left{
text-align: right;
width: 50px;
display: inline-block;
}
.c-mid{
text-align: center;
width: 10px;
display: inline-block;
}
.c-right{
text-align: left;
width: 50px;
display: inline-block;
}
<div>
<div class="c-left">a + b</div>
<div class="c-mid">=</div>
<div class="c-right">c</div>
</div>
<div>
<div class="c-left">2a + 2b</div>
<div class="c-mid">=</div>
<div class="c-right">3c</div>
</div>
Not sure if this is what you need...

Your problem is arranging the text into a form that's easily style-able using CSS.
Assuming your text comes in the form of a JSON array of strings, i.e:
[
"Solution,",
"Given, f(x) = 3x",
"g(x) = x + 2",
"fog(x) = 18",
"To find: x = ?,",
"Now,",
"fog(x) = 18",
"or, f(x + 2) = 18",
"or, 3(x + 2) = 18",
"or, x + 2 = 6",
"&therefore; x = 4"
]
...you want a function which turns it into style-able markup.
Here's an example using vanilla Javascript and CSS grid:
[...document.querySelectorAll('math-element')].forEach(el => {
// reset, in case you run this more than once...
el.innerHTML = '';
// we need JSON.parse as dataset.text is a string
JSON.parse(el.dataset.text).forEach(text => {
const div = document.createElement('div');
// split each row by `=` sign, if it has any
text.split('=').forEach(t => {
const span = document.createElement('span');
if (text.split('=').length < 2) {
// adds `.single` to items which are single on their row
span.classList.add('single');
}
span.innerHTML = t;
el.appendChild(span);
});
// add a `<hr>` after each element
const separator = document.createElement('hr')
el.appendChild(separator);
})
})
math-element {
display: grid;
grid-template-columns: auto 1fr;
}
math-element hr {
display: none;
}
math-element span {
grid-column-start: 1;
text-align: right;
padding: 0 2px 0 1rem;
}
math-element span:not(.single) {
font-style: italic;
}
math-element span.single {
text-align: left;
padding-top: .5rem;
font-style: normal;
}
math-element span + span {
grid-column-start: 2;
text-align: left;
padding: 0 2px;
}
math-element span + span:before {
content: '=';
}
<math-element data-text='["Solution,","Given, f(x) = 3x","g(x) = x + 2","fog(x) = 18","To find: x = ?,","Now,","fog(x) = 18","or, f(x + 2) = 18","or, 3(x + 2) = 18","or, x + 2 = 6","&therefore; x = 4"]'></math-element>
You don't have to inline the value as a string, as I did, you can simply create the element(s) on the fly and run the forEach directly on the data instead.
If you find CSS grid syntax and logic confusing, you can always create a <table> with <tr> and <td>s, which will give you simpler selectors. Out of principle, I advise against it.
I used <hr>s to mark the end of each "row" (CSS grid requires all cells to be siblings). Instead, you could just nest the row contents into a single element (<div> ?) and hard-code the column widths.
Obviously, the CSS is yours to modify (i.e: remove font-style's, adjust the padding values, etc...).
A final note: if one "row" contains more than one = sign, because of this rule:
math-element span + span {
grid-column-start: 2;
}
spans 2 and 3 in a row (and subsequent, until end of row) will be displayed one below each other, each prefixed with a = sign and aligned with the rest (which is not that bad, IMHO). If you want to change this behavior, you probably want to provide a
math-element span + span + span {
grid-column-start: 3;
}
... rule, and so on. Also you'll need to change grid-template-columns to match:
math-element {
grid-template-columns: auto auto 1fr;
}
...matching the number of "columns".

Related

How to fit text in line?

I want to bind text font-size with text length (in <p> with fixed width for example). Expected result is text fit in one line if it is only one word. If there are few words, it can be few lines.
I want to reduce font-size if word is too long for fixed-width line. For example, if "abc" fit in line I want to do nothing, if "abcdefg" doesn't fit in line I want to reduce text font-size
You can use a simply div setting your personal width, inside set your text, in css use
display: flex;
flex-wrap: wrap;
That way the text will respect your div width and brake line in your text when necessary
flex-wrap
Hope this answer will satisfy your question.
The resizing part, which is the most important, is creditted to Jan Küster, with "Make text fit its parent size using JavaScript" article that you can find online.
document.getElementsByTagName("input")[0].onkeyup = execute;
function execute() {
let value = document.getElementsByTagName("input")[0].value;
let words = value.split(" ");
var html = "";
for (var i = 0; i < words.length; i++) {
html += ' <div class="text-container"><span class="text">' + words[i] + '</span></div>';
document.getElementsByClassName("parent")[0].innerHTML = html;
}
resizeText({
elements: document.querySelectorAll('.text'),
step: 0.25
})
}
//
const isOverflown = ({
clientWidth,
clientHeight,
scrollWidth,
scrollHeight
}) => (scrollWidth > clientWidth) || (scrollHeight > clientHeight)
const resizeText = ({
element,
elements,
minSize = 10,
maxSize = 512,
step = 1,
unit = 'px'
}) => {
(elements || [element]).forEach(el => {
let i = minSize
let overflow = false
const parent = el.parentNode
while (!overflow && i < maxSize) {
el.style.fontSize = `${i}${unit}`
overflow = isOverflown(parent)
if (!overflow) i += step
}
// revert to last state where no overflow happened
el.style.fontSize = `${i - step}${unit}`
})
}
body {
background: #A33;
}
.parent {
margin: 2%;
width: 150px;
height: auto;
min-height: 50px;
padding: 15px;
color: white;
display: block;
}
.text-container {
width: 100%;
height: 100%;
}
.text {
font-size: 12px;
display: block;
}
<input type="text">
<div class="parent">
</div>
You could check out Bootstrap 5, they now have responsive text incorporated.
You could also use media queries that change the text size when the screen size is smaller or larger:
/* If the screen size is 601px wide or more, set the font-size of div to 80px */
#media screen and (min-width: 601px) {
div.example {
font-size: 80px;
}
}
/* If the screen size is 600px wide or less, set the font-size of <div> to 30px */
#media screen and (max-width: 600px) {
div.example {
font-size: 30px;
}
}
A final option would be to use the viewport width as the font size. Viewport is the browser window size. 1vw = 1% of viewport width. If the viewport is 50cm wide, 1vw is 0.5cm. Here's an example:
<h1 style="font-size:8vw;">Hello World</h1>
<p style="font-size:2vw;">Resize the browser window to see how the font size scales.</p>

Why does inline-block remove spaces between words?

Result has no space.
Code has a space between words.
How do I fix this? I need to use inline-block (or any inline display) because my animation won't work without it.
CSS
This is the javascript for the animation.
const text = document.querySelector("h1");
const strText = text.textContent;
const splitTxt = strText.split("");
text.textContent = "";
for(let i=0; i < splitTxt.length; i++){
text.innerHTML += "<span>"+ splitTxt[i] + "</span>";
}
let char = 0;
let timer = setInterval(onTick, 50);
function onTick() {
const span = text.querySelectorAll('span')[char];
span.classList.add('fade');
char++;
if(char === splitTxt.length) {
complete();
return;
}
}
function complete() {
clearInterval(timer);
timer = null;
}
Edit: I fixed this by putting a in between the letters.
If you want every word of the h1 to be inside of a span tag, you should do the split with a whitespace, like this:
const splitTxt = strText.split(" ");
In your code you have the quotes inside the split function without the space between, and that will cause that every character to be in a span.
You mean this?
header {
position:relative;
height: 200px;
width: 200px;
border: 1px solid #333;
display: flex;
align-items: center;
justify-content: center;
}
<header>
<h1>
myText
</h1>
</header>

Building adjustable css grid

I am trying to build "adjustable" css grid. I am making my blog and i want my news to be displayed in "blocks" and that they me placed like this :
http://pokit.org/get/img/1dfa7b74c6be5bee6c92b886e0b8270b.jpg
And not like this what i did made here
Here is my code.
HTML
<div id="wrapper">
<div class="d_1">1</div>
<div class="d_2">2</div>
<div class="d_3">3</div>
<div class="d_4">4</div>
<div class="d_5">5</div>
<div class="d_6">6</div>
</div>
CSS
#wrapper{
width:200px;
}
#wrapper div{
background-color:lightgray;
width:50px;
float:left;
margin:0px 5px 5px 0px;
}
.d_1{
height:60px;
}
.d_2{
height:30px;
}
.d_3{
height:33px;
}
.d_4{
height:70px;
}
.d_5{
height:60px;
}
.d_6{
height:40px;
}
I suppose that is not possible to obtain the desired result simply using one of the known layout modes (flexbox, grid-layout, inline, ...) nor using CSS columns. Every solution will lead to an unwanted result.
But you can obtain the result using a combination of CSS grid-layout and Javascipt code.
This is the wrapper CSS style block:
#wrapper{
width: 200px; /* CSS grid-layout will expand contained divs to cover this size */
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* "1fr" for each column */
grid-column-gap: 10px;
grid-row-gap: 5px;
}
And this is the Javascript code (add it after #wrapper is closed):
"strict mode";
Array.prototype.max = function() {
return Math.max.apply(null, this);
};
function compute_gaps(a) {
let max = a.max();
return a.map(function(el) {
return el - max;
});
}
function compose_gaps(a, b) {
return b.map(function(el, i) {
return a[i] + el;
});
}
var counter = 0;
var columns = 3; // number of columns
var gaps = [];
var heights = [];
for (let el of document.querySelectorAll("#wrapper > div")) {
let colIdx = counter % columns;
if (counter % columns === 0) {
//compute gaps
if (counter) gaps.push(compute_gaps(heights));
if (gaps.length > 1) {
gaps[gaps.length - 1] = compose_gaps(
gaps[gaps.length - 1],
gaps[gaps.length - 2]
);
}
heights = [];
}
if (gaps.length) {
el.style.marginTop = gaps[Math.floor(counter / columns - 1)][colIdx];
}
heights.push(el.offsetHeight); // apply gap as margin
counter++;
}
Tested the code in a little more complex situation and worked in this way.
The code computes, in each row, gaps between the highest block and the others in the row (compute_gaps); after that, applied the gap as a CSS margin-top. Gaps are summed with the previous ones (compose_gaps).
I hope this answers your question.
Warning:
I am not the best at CSS and JS right now. I tend to brute force things until they work. I am pretty sure this is not the best solution, however, I want to post it so that maybe others can improve upon it. This may not be functional once all content is put in or may not by responsive or may not be dynamic enough to solve the issue, I don't know. I do know that the desired look from the question is achieved through this method, right now, without content etc.
I do welcome any and all feedback regarding why this isn't the best and/or what is wrong with it so I can learn.
With that being said, here is the fiddle:
http://jsfiddle.net/jz4p4Lzk/13/
HTML
<div id="wrapper">
<div class="d_1" id="d1">1</div>
<div class="d_2" id="d2">2</div>
<div class="d_3" id="d3">3</div>
<div class="d_4" id="d4">4</div>
<div class="d_5" id="d5">5</div>
<div class="d_6" id="d6">6</div>
CSS
#wrapper{
width:200px;
}
#wrapper div{
background-color:lightgray;
width:50px;
position:relative;
margin:0px 5px 5px 0px;
}
.d_1{
height:60px;
}
.d_2{
height:30px;
}
.d_3{
height:33px;
}
.d_4{
height:70px;
}
.d_5{
height:60px;
}
.d_6{
height:40px;
}
JS
var d1 = document.getElementById('d1');
var d1Loc = d1.getBoundingClientRect();
var d2 = document.getElementById('d2');
var d2Loc = d2.getBoundingClientRect();
var d3 = document.getElementById('d3');
var d3Loc = d3.getBoundingClientRect();
var d4 = document.getElementById('d4');
var d4Loc = d4.getBoundingClientRect();
var d5 = document.getElementById('d5');
var d5Loc = d5.getBoundingClientRect();
var d6 = document.getElementById('d6');
d2.style.left = d1Loc.right -5+ "px";
d2.style.top = - d1.offsetHeight - 5 + "px";
d3.style.left = d2Loc.right + d1Loc.right -10 +"px";
d3.style.top = - d1.offsetHeight - d2.offsetHeight - 10 + "px";
d4.style.top = - d1.offsetHeight - d2.offsetHeight - d3.offsetHeight + 50 + "px";
d5.style.top = - d1.offsetHeight - d2.offsetHeight - d3.offsetHeight - d4.offsetHeight + 15 + "px";
d6.style.top = - d1.offsetHeight - d2.offsetHeight - d3.offsetHeight - d4.offsetHeight - d5.offsetHeight +12.5 + "px";
d5.style.left = d4Loc.right -5+ "px";
d6.style.left = d5Loc.right + d4Loc.right -10 + "px";
If you don't care about old browsers support (doesn't work with IE9 or above) then you can reorder your divs vertically by using the CSS3 column-count Property and setting it to 3 columns :
Add this to #wrapper :
-webkit-column-count: 3;
-webkit-column-fill: auto;
-moz-column-count: 3;
-moz-column-fill: auto;
column-count: 3;
column-fill: auto;
Then replace float:left; in your #wrapper div by display: inline-block;
Here is a CODEPEN DEMO.
NOTE: If browser support and div order are important then an elegant solution may be found in this StackOverFlow post : how to replicate pinterest.com's absolute div stacking layout
you know that there is an actual "css grid" that you can use? It doesn't quite work yet in IE (it does, but only somewhat), but in all other relevant browsers it works well. Basically, you specify gridlines and then place the boxes that you want to within them.
I made a codepen so you can see it in action. (oh, and no javascript needed)
https://codepen.io/quibble/pen/NaKdMo
#wrapper{
display:grid;
grid-gap:10px;
grid-template-columns:50px 50px 50px;
grid-template-rows: 30px 3px 27px 13px 17px 40px; /*the pixels add up to the corresponding bottoms of each grid container. There are a few ways to do this but I like this one.*/
grid-template-areas:
/*This allows you to specify which grid blocks go where. Notice that some are repeated, this just means they span two or more grid areas. For example, box 3 is 33 px so must span one column and two rows (the 30 and 3px one)*/
"one two three"
"one five three"
"one five six"
"four five six"
"four five ."
"four . .";/* the . is for a blank gridspace */
}
#wrapper>div{
background-color:gray;
}
.d_1{
grid-area:one;
}
.d_2{
grid-area:two;
}
.d_3{
grid-area:three;
}
.d_4{
grid-area:four;
}
.d_5{
grid-area:five;
}
.d_6{
grid-area:six;
}
I'm pretty sure this is exactly what you want. You can even mess around with the order of the numbers (in case you want to rearrange your blog posts or pictures) and you can add more pretty easily. You even have "grid-template-areas:" which allows you to specify EXACTLY where each item will go. NO MORE HACKING FOR POSITIONSSS
Good luck out there! Please mark right if this helped.
(P.S., if you need more information on grid, one of the people that pushed for it very heavily (Rachel Andrew) made a tutorial: https://gridbyexample.com/)
This seems similar to another topic: how-create-grid-out-of-images-of-different-sizes
I fully agree with #Quibble on this.
He basically used the layout you wanted. I made a different one, it just has a different approach, though areas are the more elegant way. Just something to bear in mind, you can do it in several ways, none of which involve JS-based coding. My JSfiddle example.
.container {
display: grid;
padding: 60pt;
grid-template-columns: 2fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 2fr;
/* there are five values for five columns, each column gets a relative
width in relation to the grid*/
grid-template-rows: 10% 45% 35% 10%;
grid-column-gap: 10px;
grid-row-gap: 5px;
/*this means there are three rows*/
}
.container div img {
width: 100%;
height: 100%;
}
.main_1 {
grid-column: 2/5;
grid-row: 2/3;
}
.main_2 {
grid-column: 5/8;
grid-row: 2/3;
}
.main_3 {
grid-column: 8/11;
grid-row: 2/3;
}
.main_4 {
grid-column: 2/4;
grid-row: 3/4;
}
.main_5 {
grid-column: 4/7;
grid-row: 3/4;
}
.main_6 {
grid-column: 7/11;
grid-row: 3/4;
}
.footer {
grid-row: 4/5;
grid-column: 1/6;
}

Ellipsis on span inside table cell for max width cell

Small update
Note: Target is mainly Chrome (perhaps Firefox) – as it is for an extension. But, of course, if there is an all-round solution that would be nice to know as well.
Another update
Note: Noticed that if there are no spaces in the text for A and it is too big, it pushes out B. Problem became worse and looks like I'm further from a solution then first thought. Updated sample code to allow input from user. Try sample without spaces.
I am having a fixed positioned box of full width and fixed height. It is placed inside an environment of variable / dynamic width.
+-----------------------------+
| |
| |
[=============================] << The box.
| |
| |
...
<- dynamic width ->
The box has two main children A and B. B takes precedence on width and are to hold all of it contents on given height, but not in a greedy way. A is eats remainder of space in a greedy fashion. On text-overflow in A, ellipsis should be added – and that is where I'm stuck.
Some examples.
1. B fills it's space, A expand to fill width.
+-------------------+--------+
| A________ | __B___ |
+-------------------+--------+
2. A has overflow, ellipsis added.
+-------------------+--------+
| A_____________... | __B___ |
+-------------------+--------+
3. B, has grown, A shrinks.
+----------------+-----------+
| A__________... | __B______ |
+----------------+-----------+
After fiddling around with various approaches from floats to absolute, relative and other types I finally landed on a table layout (as in CSS-table, not HTML). Can't get desired effect with other approach. If anyone know how it is OK to tell :)
Problem is the ellipsis part on overflow in A.
Can I somehow tweak e.g. how A is set up to get this?
Sample code:
Ignore the JavaScript, it is only a convenience routine to display the over and under-flow. (Found it nice to have whilst editing in Firefox's Style Editor)
/* Yes. It is all a mess and weird usage of variables vs this etc. */
function Hack() {
var x = 0,
n = 150,
wrap = document.querySelector('#wrap'),
left = document.querySelector('#left'),
right = document.querySelector('#right'),
txt = document.querySelector('#txt'),
ent = document.querySelectorAll('.entry'),
log = document.querySelector('#log'),
run = document.querySelector('#run'),
rt = document.querySelector('#rt'),
samp = document.querySelector('#sample'),
t = samp.value
;
this.rt = parseInt(rt.value) || 1000;
function getComp(e) {
var x = window.getComputedStyle(e, null);
return ''+
~~(parseInt(x.getPropertyValue('height'))) + 'x' +
~~(parseInt(x.getPropertyValue('width')))
;
}
this.status = function () {
log.textContent = 'Height / Width for:\n' +
' wrap : ' + getComp(wrap) + '\n' +
' left : ' + getComp(left) + '\n' +
' right : ' + getComp(right) + '\n' +
' sample: ' + getComp(txt) + '\n'
;
}
/* Change between long and short text in sample cells. */
this.flip = function () {
txt.textContent = x ? t : (new Array(n)).join(t);
Array.prototype.forEach.call(ent, function (e) {
e.textContent = x ? 'abc' : 'abcabc';
});
x ^= 1;
this.status();
}
/* Toggle auto run. */
this.update = function () {
t = samp.value;
this.rt = parseInt(rt.value);
if (!this.rt || this.rt < 10)
rt.value = this.rt = 100;
clearInterval(this.ttt);
if (run.checked)
this.ttt = setInterval(this.flip.bind(this), this.rt);
}
document.addEventListener('click', this.flip.bind(this));
run.addEventListener('click', this.update.bind(this));
rt.addEventListener('change', this.update.bind(this));
samp.addEventListener('keyup', this.update.bind(this));
this.update();
}
window.addEventListener('load', function () {
var hack = new Hack();
hack.flip();
});
* { margin : 0; padding : 0; }
#log { margin : 5pt; border : 1px solid #ccc; }
#filler { margin-top : 90px; height : 2000px; background : #efefef; }
label,
input { cursor : pointer; }
/* inner elements of cells in right -- (B) */
.hdr,
.entry { padding : 2px 5px; }
.hdr { font-weight: bold; }
#wrap { /* the red thing -- aka (the box) */
position : fixed;
top : 135px;
height : 23px;
background : #600;
color : #999;
height : 20px;
width : 100%;
display : table-row;
}
#left { /* the green thing -- aka (A) */
background : #044;
display : table-cell;
width : 100%;
}
#txt { /* sample text in left */ /* Where I want ellipsis */
display : block;
height : 20px;
width : 100%;
overflow : hidden;
text-overflow: ellipsis;
}
#right { /* the purple / blue thing -- aka (B) */
background : rgba(0,0,200,.5);
height : 20px;
display : table-cell;
width : 100%;
white-space: nowrap;
}
<p>Click document to expand text, or auto-run:</p>
<div>
<input type="checkbox" id="run" checked /><label for="run">Change every </label>
<input type="number" id="rt" value="1000" step="100" /> millisecond.
Sample text: <input type="text" value=" sample" id="sample" />
</div>
<pre id="log"></pre>
<!-- The box -->
<div id="wrap">
<div id="left">
<span id="txt">sample <!-- ellipsis here --> </span>
</div>
<div id="right">
<span class="hdr">Foo</span><span class="entry">abcd</span>
<span class="hdr">Bar</span><span class="entry">abcd</span>
</div>
</div>
<!-- EOF: The box -->
<div id="filler">dummy filler page height</div>
Is using Javascript ok?
text-overflow only works in table cells if there is a fixed width. The following snippet sets the max-width of the span to the correct width.
Place this in your flip function and in a resize event listener (if the containing div does not have a fixed width).
txt.style["max-width"] = "calc(" + wrap.clientWidth + "px - " + right.clientWidth +"px)";
Youu will also have to add white-space: nowrap to your #txt style.
function Hack() {
var x = 0,
n = 150,
t = 'sample ',
wrap = document.querySelector('#wrap'),
left = document.querySelector('#left'),
right = document.querySelector('#right'),
txt = document.querySelector('#txt'),
ent = document.querySelectorAll('.entry'),
log = document.querySelector('#log'),
run = document.querySelector('#run'),
rt = document.querySelector('#rt')
;
this.rt = parseInt(rt.value) || 1000;
function getComp(e) {
var x = window.getComputedStyle(e, null);
return ''+
~~(parseInt(x.getPropertyValue('height'))) + 'x' +
~~(parseInt(x.getPropertyValue('width')))
;
}
this.status = function () {
log.textContent = 'Height / Width for:\n' +
' wrap : ' + getComp(wrap) + '\n' +
' left : ' + getComp(left) + '\n' +
' right : ' + getComp(right) + '\n' +
' sample: ' + getComp(txt) + '\n'
;
}
/* Change between long and short text in sample cells. */
this.flip = function () {
txt.textContent = x ? t : (new Array(n)).join(t);
Array.prototype.forEach.call(ent, function (e) {
e.textContent = x ? 'abc' : 'abcabc';
});
txt.style["max-width"] = "calc(" + wrap.clientWidth + "px - " + right.clientWidth +"px)";
x ^= 1;
this.status();
}
/* Toggle auto run. */
this.toggle = function (r) {
this.rt = parseInt(rt.value);
if (!this.rt || this.rt < 10)
rt.value = this.rt = 100;
clearInterval(this.ttt);
if (run.checked)
this.ttt = setInterval(this.flip.bind(this), this.rt);
}
document.addEventListener('click', this.flip.bind(this));
run.addEventListener('click', this.toggle.bind(this));
rt.addEventListener('change', this.toggle.bind(this, 1));
this.toggle();
}
window.addEventListener('load', function () {
var hack = new Hack();
hack.flip();
});
* { margin : 0; padding : 0; }
#log { margin : 5pt; border : 1px solid #ccc; }
#filler { margin-top : 90px; height : 2000px; background : #efefef; }
label,
input { cursor : pointer; }
/* inner elements of cells in right -- (B) */
.hdr,
.entry { padding : 2px 5px; }
.hdr { font-weight: bold; }
#wrap { /* the red thing -- aka (the box) */
position : fixed;
top : 135px;
height : 23px;
background : #600;
color : #999;
height : 20px;
width : 100%;
display : table-row;
}
#left { /* the green thing -- aka (A) */
background : #044;
display : table-cell;
width : 100%;
}
#txt { /* sample text in left */ /* Where I want ellipsis */
display : block;
height : 20px;
width : 100%;
overflow : hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
#right { /* the purple / blue thing -- aka (B) */
background : rgba(0,0,200,.5);
height : 20px;
display : table-cell;
width : 100%;
white-space: nowrap;
}
<p>Click document to expand text, or auto-run:</p>
<div>
<input type="checkbox" id="run" checked /><label for="run">Change every </label>
<input type="number" id="rt" value="1000" step="100" /> millisecond.
</div>
<pre id="log"></pre>
<!-- The box -->
<div id="wrap">
<div id="left">
<span id="txt">sample <!-- ellipsis here --> </span>
</div>
<div id="right">
<span class="hdr">Foo</span><span class="entry">abcd</span>
<span class="hdr">Bar</span><span class="entry">abcd</span>
</div>
</div>
<!-- EOF: The box -->
<div id="filler">dummy filler page height</div>

How do I make the text in the next line to be centered and not aligned to the left?

I have a div that has the following CSS:
.div_stuff {
width: 830px;
margin: 20px auto;
font-size:22px;
}
How do I prevent the text that goes to the next line to not align left right under the text above but simply become centered on the next line?
Although I'm not sure if I understood what you want to achieve and I can't find a use case for this... Changing the alignment from the second line on would be possible by adding some span tags with javascript/jQuery (DEMO):
$(function() {
var box = $('.div_stuff');
var text = box.text();
var words = text.split(' ');
box.text(words[0]);
var height = box.height();
var chars = 0;
for(var i = 1; i < words.length; i++){
box.text(box.text() + ' ' + words[i]);
chars += words[i-1].length + 1;
if(box.height() > height){
height = box.height();
box.html('<span class="first-line">' + text.substring(0,chars) + '</span><span class="following-lines">' + text.substring(chars+1, text.length)+'</span>');
break;
}
}
});
And set a different alignment to them:
span {
display: block;
}
.first-line {
text-align: left;
}
.following-lines {
text-align: center;
}
I've used that answer for determining auto line breaks.
Add text-align: center to center the text inside. You are centering the div position only.
you can try css3 text justified
http://www.w3schools.com/cssref/css3_pr_text-justify.asp