Word Wrap detection in JavaScript










2















I am trying to work out a way to detect wordwrap in a specific span tag inside a banner. If it wraps to 2 lines then increase the overall height of the container by 56px. There is also a sub headline (headline2) which also needs to increase (or decrease) the height by 40px.



I have written some basic JS code here which checks the div height of the span but its not great & also will only work for 3 lines.



 // Variable banner heights
var hl11sub = 368;
var hl21sub = 448;
var hl31sub = 548;

var hl12sub = 416;
var hl22sub = 496;
var hl32sub = 576;

var hLFontSizeCSS = window.getComputedStyle(headlineText, null).getPropertyValue("font-size");
var hL2FontSizeCSS = window.getComputedStyle(headline2text, null).getPropertyValue("font-size");
var bannerHeightCSS = window.getComputedStyle(banner, null).getPropertyValue("height");
var headlineHeight = headlineText.offsetHeight;
var hL2HeadHeight = headline2text.offsetHeight;

var headHeight = headlineText.style.lineHeight = parseInt(hLFontSizeCSS) + 10 + "px";
var hL2Height = headline2text.style.lineHeight = parseInt(hL2FontSizeCSS) + 10 + "px";

// Text Height values
var hL1LineHeight = parseInt(headHeight); // 8 is line height & padding
var hL2LinesHeight = 140;
var hL3LinesHeight = 195;

// HL2 height values
var hL2TextOver1LineHeight = parseInt(hL2Height); // 8 is line height & padding
var hL2TextOver2LineHeight = 84;

if(hL2HeadHeight == hL2TextOver1LineHeight && headlineHeight == hL1LineHeight)
banner.style.height = hl11sub + "px";

else if(hL2HeadHeight == hL2TextOver1LineHeight && headlineHeight == hL2LinesHeight)
banner.style.height = hl21sub + "px";

else if(hL2HeadHeight == hL2TextOver1LineHeight && headlineHeight >= hL3LinesHeight)
banner.style.height = hl31sub + "px";

else if(hL2HeadHeight == hL2TextOver2LineHeight && headlineHeight == hL1LineHeight)
// Single headline with 2 lines sub
banner.style.height = hl12sub + "px";

else if(hL2HeadHeight == hL2TextOver2LineHeight && headlineHeight == hL2LinesHeight)
// 2 headlines with 2 lines sub
banner.style.height = hl22sub + "px";

else {
banner.style.height = hl32sub + "px";
// 3 headlines with 2 lines sub


It needs to only change the height of the banner depending on if the span words wrap once, twice, three times etc.



Any suggestions or help with this would be greatly appreciated.










share|improve this question



















  • 2





    use a window resize event and check when the height of the text element is the line-height * (the amount of lines you checking for) and when true reduce the font size

    – Joe Warner
    Nov 13 '18 at 12:10











  • Thanks Joe, will this work if the window is never resized but just the height of the div? Its not the entire page which needs to be changed btw. Cheers

    – ste
    Nov 13 '18 at 12:36











  • what do you mean just the height how does it reduce in size?

    – Joe Warner
    Nov 13 '18 at 12:37















2















I am trying to work out a way to detect wordwrap in a specific span tag inside a banner. If it wraps to 2 lines then increase the overall height of the container by 56px. There is also a sub headline (headline2) which also needs to increase (or decrease) the height by 40px.



I have written some basic JS code here which checks the div height of the span but its not great & also will only work for 3 lines.



 // Variable banner heights
var hl11sub = 368;
var hl21sub = 448;
var hl31sub = 548;

var hl12sub = 416;
var hl22sub = 496;
var hl32sub = 576;

var hLFontSizeCSS = window.getComputedStyle(headlineText, null).getPropertyValue("font-size");
var hL2FontSizeCSS = window.getComputedStyle(headline2text, null).getPropertyValue("font-size");
var bannerHeightCSS = window.getComputedStyle(banner, null).getPropertyValue("height");
var headlineHeight = headlineText.offsetHeight;
var hL2HeadHeight = headline2text.offsetHeight;

var headHeight = headlineText.style.lineHeight = parseInt(hLFontSizeCSS) + 10 + "px";
var hL2Height = headline2text.style.lineHeight = parseInt(hL2FontSizeCSS) + 10 + "px";

// Text Height values
var hL1LineHeight = parseInt(headHeight); // 8 is line height & padding
var hL2LinesHeight = 140;
var hL3LinesHeight = 195;

// HL2 height values
var hL2TextOver1LineHeight = parseInt(hL2Height); // 8 is line height & padding
var hL2TextOver2LineHeight = 84;

if(hL2HeadHeight == hL2TextOver1LineHeight && headlineHeight == hL1LineHeight)
banner.style.height = hl11sub + "px";

else if(hL2HeadHeight == hL2TextOver1LineHeight && headlineHeight == hL2LinesHeight)
banner.style.height = hl21sub + "px";

else if(hL2HeadHeight == hL2TextOver1LineHeight && headlineHeight >= hL3LinesHeight)
banner.style.height = hl31sub + "px";

else if(hL2HeadHeight == hL2TextOver2LineHeight && headlineHeight == hL1LineHeight)
// Single headline with 2 lines sub
banner.style.height = hl12sub + "px";

else if(hL2HeadHeight == hL2TextOver2LineHeight && headlineHeight == hL2LinesHeight)
// 2 headlines with 2 lines sub
banner.style.height = hl22sub + "px";

else {
banner.style.height = hl32sub + "px";
// 3 headlines with 2 lines sub


It needs to only change the height of the banner depending on if the span words wrap once, twice, three times etc.



Any suggestions or help with this would be greatly appreciated.










share|improve this question



















  • 2





    use a window resize event and check when the height of the text element is the line-height * (the amount of lines you checking for) and when true reduce the font size

    – Joe Warner
    Nov 13 '18 at 12:10











  • Thanks Joe, will this work if the window is never resized but just the height of the div? Its not the entire page which needs to be changed btw. Cheers

    – ste
    Nov 13 '18 at 12:36











  • what do you mean just the height how does it reduce in size?

    – Joe Warner
    Nov 13 '18 at 12:37













2












2








2








I am trying to work out a way to detect wordwrap in a specific span tag inside a banner. If it wraps to 2 lines then increase the overall height of the container by 56px. There is also a sub headline (headline2) which also needs to increase (or decrease) the height by 40px.



I have written some basic JS code here which checks the div height of the span but its not great & also will only work for 3 lines.



 // Variable banner heights
var hl11sub = 368;
var hl21sub = 448;
var hl31sub = 548;

var hl12sub = 416;
var hl22sub = 496;
var hl32sub = 576;

var hLFontSizeCSS = window.getComputedStyle(headlineText, null).getPropertyValue("font-size");
var hL2FontSizeCSS = window.getComputedStyle(headline2text, null).getPropertyValue("font-size");
var bannerHeightCSS = window.getComputedStyle(banner, null).getPropertyValue("height");
var headlineHeight = headlineText.offsetHeight;
var hL2HeadHeight = headline2text.offsetHeight;

var headHeight = headlineText.style.lineHeight = parseInt(hLFontSizeCSS) + 10 + "px";
var hL2Height = headline2text.style.lineHeight = parseInt(hL2FontSizeCSS) + 10 + "px";

// Text Height values
var hL1LineHeight = parseInt(headHeight); // 8 is line height & padding
var hL2LinesHeight = 140;
var hL3LinesHeight = 195;

// HL2 height values
var hL2TextOver1LineHeight = parseInt(hL2Height); // 8 is line height & padding
var hL2TextOver2LineHeight = 84;

if(hL2HeadHeight == hL2TextOver1LineHeight && headlineHeight == hL1LineHeight)
banner.style.height = hl11sub + "px";

else if(hL2HeadHeight == hL2TextOver1LineHeight && headlineHeight == hL2LinesHeight)
banner.style.height = hl21sub + "px";

else if(hL2HeadHeight == hL2TextOver1LineHeight && headlineHeight >= hL3LinesHeight)
banner.style.height = hl31sub + "px";

else if(hL2HeadHeight == hL2TextOver2LineHeight && headlineHeight == hL1LineHeight)
// Single headline with 2 lines sub
banner.style.height = hl12sub + "px";

else if(hL2HeadHeight == hL2TextOver2LineHeight && headlineHeight == hL2LinesHeight)
// 2 headlines with 2 lines sub
banner.style.height = hl22sub + "px";

else {
banner.style.height = hl32sub + "px";
// 3 headlines with 2 lines sub


It needs to only change the height of the banner depending on if the span words wrap once, twice, three times etc.



Any suggestions or help with this would be greatly appreciated.










share|improve this question
















I am trying to work out a way to detect wordwrap in a specific span tag inside a banner. If it wraps to 2 lines then increase the overall height of the container by 56px. There is also a sub headline (headline2) which also needs to increase (or decrease) the height by 40px.



I have written some basic JS code here which checks the div height of the span but its not great & also will only work for 3 lines.



 // Variable banner heights
var hl11sub = 368;
var hl21sub = 448;
var hl31sub = 548;

var hl12sub = 416;
var hl22sub = 496;
var hl32sub = 576;

var hLFontSizeCSS = window.getComputedStyle(headlineText, null).getPropertyValue("font-size");
var hL2FontSizeCSS = window.getComputedStyle(headline2text, null).getPropertyValue("font-size");
var bannerHeightCSS = window.getComputedStyle(banner, null).getPropertyValue("height");
var headlineHeight = headlineText.offsetHeight;
var hL2HeadHeight = headline2text.offsetHeight;

var headHeight = headlineText.style.lineHeight = parseInt(hLFontSizeCSS) + 10 + "px";
var hL2Height = headline2text.style.lineHeight = parseInt(hL2FontSizeCSS) + 10 + "px";

// Text Height values
var hL1LineHeight = parseInt(headHeight); // 8 is line height & padding
var hL2LinesHeight = 140;
var hL3LinesHeight = 195;

// HL2 height values
var hL2TextOver1LineHeight = parseInt(hL2Height); // 8 is line height & padding
var hL2TextOver2LineHeight = 84;

if(hL2HeadHeight == hL2TextOver1LineHeight && headlineHeight == hL1LineHeight)
banner.style.height = hl11sub + "px";

else if(hL2HeadHeight == hL2TextOver1LineHeight && headlineHeight == hL2LinesHeight)
banner.style.height = hl21sub + "px";

else if(hL2HeadHeight == hL2TextOver1LineHeight && headlineHeight >= hL3LinesHeight)
banner.style.height = hl31sub + "px";

else if(hL2HeadHeight == hL2TextOver2LineHeight && headlineHeight == hL1LineHeight)
// Single headline with 2 lines sub
banner.style.height = hl12sub + "px";

else if(hL2HeadHeight == hL2TextOver2LineHeight && headlineHeight == hL2LinesHeight)
// 2 headlines with 2 lines sub
banner.style.height = hl22sub + "px";

else {
banner.style.height = hl32sub + "px";
// 3 headlines with 2 lines sub


It needs to only change the height of the banner depending on if the span words wrap once, twice, three times etc.



Any suggestions or help with this would be greatly appreciated.







javascript dynamic height






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 12:31







ste

















asked Nov 13 '18 at 12:08









steste

3217




3217







  • 2





    use a window resize event and check when the height of the text element is the line-height * (the amount of lines you checking for) and when true reduce the font size

    – Joe Warner
    Nov 13 '18 at 12:10











  • Thanks Joe, will this work if the window is never resized but just the height of the div? Its not the entire page which needs to be changed btw. Cheers

    – ste
    Nov 13 '18 at 12:36











  • what do you mean just the height how does it reduce in size?

    – Joe Warner
    Nov 13 '18 at 12:37












  • 2





    use a window resize event and check when the height of the text element is the line-height * (the amount of lines you checking for) and when true reduce the font size

    – Joe Warner
    Nov 13 '18 at 12:10











  • Thanks Joe, will this work if the window is never resized but just the height of the div? Its not the entire page which needs to be changed btw. Cheers

    – ste
    Nov 13 '18 at 12:36











  • what do you mean just the height how does it reduce in size?

    – Joe Warner
    Nov 13 '18 at 12:37







2




2





use a window resize event and check when the height of the text element is the line-height * (the amount of lines you checking for) and when true reduce the font size

– Joe Warner
Nov 13 '18 at 12:10





use a window resize event and check when the height of the text element is the line-height * (the amount of lines you checking for) and when true reduce the font size

– Joe Warner
Nov 13 '18 at 12:10













Thanks Joe, will this work if the window is never resized but just the height of the div? Its not the entire page which needs to be changed btw. Cheers

– ste
Nov 13 '18 at 12:36





Thanks Joe, will this work if the window is never resized but just the height of the div? Its not the entire page which needs to be changed btw. Cheers

– ste
Nov 13 '18 at 12:36













what do you mean just the height how does it reduce in size?

– Joe Warner
Nov 13 '18 at 12:37





what do you mean just the height how does it reduce in size?

– Joe Warner
Nov 13 '18 at 12:37












1 Answer
1






active

oldest

votes


















1














Here is a very basic implementation on how to detect when a line is wrapped hopefully this gives you a good idea where to start and integrate it into your app.



Heres the docs for stuff used




https://developer.mozilla.org/en/docs/Web/API/EventTarget/addEventListener
https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle
https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver




You mentioned the height changing and you needing to know when its wrapped you can use a mutation observer to check when the style has changed then check if its wrapped.



Resize the demo window to see results



any questions i'll try get to them asap if i've misunderstood i'll happily change :)






const h1 = document.querySelector('h1');
const banner = document.querySelector('.banner');


//handles style changes on banner to check wrapping
const observer = new MutationObserver(mutations =>
mutations.forEach(mutationRecord => onLineWrapDoSomething())
);
observer.observe(banner, attributes : true, attributeFilter : ['style'] );


// handles window resize events
window.addEventListener('resize', onLineWrapDoSomething)

function onLineWrapDoSomething()
const lineHeight = getComputedStyle(h1);
const lineHeightParsed = parseInt(lineHeight.split('px')[0]);
const amountOfLinesTilAdjust = 2;

if (h1.offsetHeight >= (lineHeightParsed * amountOfLinesTilAdjust))
console.log('your h1 now wrapped')
else
console.log('your h1 on one line')



// shows it logs when style changes and it wraps, ignore the disgusting code below
setTimeout(() =>
banner.style.width = '50%'
setTimeout(() =>
banner.style.width = '100%'
, 1500)
, 1500)

.banner 
width: 100%;

h1
line-height: 1.5

<div class="banner">
<h1>This is some text that will eventually wrap</h1>
</div>








share|improve this answer




















  • 1





    thats extremely useful Joe. I've never come across MutationObserver before. Defo beats using setTimeOut as a hacky way to keep checking. I am going to take this and attempt to manipulate so it can detect, 1, 2, 3, 4 lines etc. Thank you very much. Learnt something new & really useful here.

    – ste
    Nov 13 '18 at 15:21











  • yeah I only really saw the native MutationObserver the other day, very cool we use it at work for scrollbar detection! :) glad to help any questions let me know

    – Joe Warner
    Nov 13 '18 at 15:47










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280724%2fword-wrap-detection-in-javascript%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Here is a very basic implementation on how to detect when a line is wrapped hopefully this gives you a good idea where to start and integrate it into your app.



Heres the docs for stuff used




https://developer.mozilla.org/en/docs/Web/API/EventTarget/addEventListener
https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle
https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver




You mentioned the height changing and you needing to know when its wrapped you can use a mutation observer to check when the style has changed then check if its wrapped.



Resize the demo window to see results



any questions i'll try get to them asap if i've misunderstood i'll happily change :)






const h1 = document.querySelector('h1');
const banner = document.querySelector('.banner');


//handles style changes on banner to check wrapping
const observer = new MutationObserver(mutations =>
mutations.forEach(mutationRecord => onLineWrapDoSomething())
);
observer.observe(banner, attributes : true, attributeFilter : ['style'] );


// handles window resize events
window.addEventListener('resize', onLineWrapDoSomething)

function onLineWrapDoSomething()
const lineHeight = getComputedStyle(h1);
const lineHeightParsed = parseInt(lineHeight.split('px')[0]);
const amountOfLinesTilAdjust = 2;

if (h1.offsetHeight >= (lineHeightParsed * amountOfLinesTilAdjust))
console.log('your h1 now wrapped')
else
console.log('your h1 on one line')



// shows it logs when style changes and it wraps, ignore the disgusting code below
setTimeout(() =>
banner.style.width = '50%'
setTimeout(() =>
banner.style.width = '100%'
, 1500)
, 1500)

.banner 
width: 100%;

h1
line-height: 1.5

<div class="banner">
<h1>This is some text that will eventually wrap</h1>
</div>








share|improve this answer




















  • 1





    thats extremely useful Joe. I've never come across MutationObserver before. Defo beats using setTimeOut as a hacky way to keep checking. I am going to take this and attempt to manipulate so it can detect, 1, 2, 3, 4 lines etc. Thank you very much. Learnt something new & really useful here.

    – ste
    Nov 13 '18 at 15:21











  • yeah I only really saw the native MutationObserver the other day, very cool we use it at work for scrollbar detection! :) glad to help any questions let me know

    – Joe Warner
    Nov 13 '18 at 15:47















1














Here is a very basic implementation on how to detect when a line is wrapped hopefully this gives you a good idea where to start and integrate it into your app.



Heres the docs for stuff used




https://developer.mozilla.org/en/docs/Web/API/EventTarget/addEventListener
https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle
https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver




You mentioned the height changing and you needing to know when its wrapped you can use a mutation observer to check when the style has changed then check if its wrapped.



Resize the demo window to see results



any questions i'll try get to them asap if i've misunderstood i'll happily change :)






const h1 = document.querySelector('h1');
const banner = document.querySelector('.banner');


//handles style changes on banner to check wrapping
const observer = new MutationObserver(mutations =>
mutations.forEach(mutationRecord => onLineWrapDoSomething())
);
observer.observe(banner, attributes : true, attributeFilter : ['style'] );


// handles window resize events
window.addEventListener('resize', onLineWrapDoSomething)

function onLineWrapDoSomething()
const lineHeight = getComputedStyle(h1);
const lineHeightParsed = parseInt(lineHeight.split('px')[0]);
const amountOfLinesTilAdjust = 2;

if (h1.offsetHeight >= (lineHeightParsed * amountOfLinesTilAdjust))
console.log('your h1 now wrapped')
else
console.log('your h1 on one line')



// shows it logs when style changes and it wraps, ignore the disgusting code below
setTimeout(() =>
banner.style.width = '50%'
setTimeout(() =>
banner.style.width = '100%'
, 1500)
, 1500)

.banner 
width: 100%;

h1
line-height: 1.5

<div class="banner">
<h1>This is some text that will eventually wrap</h1>
</div>








share|improve this answer




















  • 1





    thats extremely useful Joe. I've never come across MutationObserver before. Defo beats using setTimeOut as a hacky way to keep checking. I am going to take this and attempt to manipulate so it can detect, 1, 2, 3, 4 lines etc. Thank you very much. Learnt something new & really useful here.

    – ste
    Nov 13 '18 at 15:21











  • yeah I only really saw the native MutationObserver the other day, very cool we use it at work for scrollbar detection! :) glad to help any questions let me know

    – Joe Warner
    Nov 13 '18 at 15:47













1












1








1







Here is a very basic implementation on how to detect when a line is wrapped hopefully this gives you a good idea where to start and integrate it into your app.



Heres the docs for stuff used




https://developer.mozilla.org/en/docs/Web/API/EventTarget/addEventListener
https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle
https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver




You mentioned the height changing and you needing to know when its wrapped you can use a mutation observer to check when the style has changed then check if its wrapped.



Resize the demo window to see results



any questions i'll try get to them asap if i've misunderstood i'll happily change :)






const h1 = document.querySelector('h1');
const banner = document.querySelector('.banner');


//handles style changes on banner to check wrapping
const observer = new MutationObserver(mutations =>
mutations.forEach(mutationRecord => onLineWrapDoSomething())
);
observer.observe(banner, attributes : true, attributeFilter : ['style'] );


// handles window resize events
window.addEventListener('resize', onLineWrapDoSomething)

function onLineWrapDoSomething()
const lineHeight = getComputedStyle(h1);
const lineHeightParsed = parseInt(lineHeight.split('px')[0]);
const amountOfLinesTilAdjust = 2;

if (h1.offsetHeight >= (lineHeightParsed * amountOfLinesTilAdjust))
console.log('your h1 now wrapped')
else
console.log('your h1 on one line')



// shows it logs when style changes and it wraps, ignore the disgusting code below
setTimeout(() =>
banner.style.width = '50%'
setTimeout(() =>
banner.style.width = '100%'
, 1500)
, 1500)

.banner 
width: 100%;

h1
line-height: 1.5

<div class="banner">
<h1>This is some text that will eventually wrap</h1>
</div>








share|improve this answer















Here is a very basic implementation on how to detect when a line is wrapped hopefully this gives you a good idea where to start and integrate it into your app.



Heres the docs for stuff used




https://developer.mozilla.org/en/docs/Web/API/EventTarget/addEventListener
https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle
https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver




You mentioned the height changing and you needing to know when its wrapped you can use a mutation observer to check when the style has changed then check if its wrapped.



Resize the demo window to see results



any questions i'll try get to them asap if i've misunderstood i'll happily change :)






const h1 = document.querySelector('h1');
const banner = document.querySelector('.banner');


//handles style changes on banner to check wrapping
const observer = new MutationObserver(mutations =>
mutations.forEach(mutationRecord => onLineWrapDoSomething())
);
observer.observe(banner, attributes : true, attributeFilter : ['style'] );


// handles window resize events
window.addEventListener('resize', onLineWrapDoSomething)

function onLineWrapDoSomething()
const lineHeight = getComputedStyle(h1);
const lineHeightParsed = parseInt(lineHeight.split('px')[0]);
const amountOfLinesTilAdjust = 2;

if (h1.offsetHeight >= (lineHeightParsed * amountOfLinesTilAdjust))
console.log('your h1 now wrapped')
else
console.log('your h1 on one line')



// shows it logs when style changes and it wraps, ignore the disgusting code below
setTimeout(() =>
banner.style.width = '50%'
setTimeout(() =>
banner.style.width = '100%'
, 1500)
, 1500)

.banner 
width: 100%;

h1
line-height: 1.5

<div class="banner">
<h1>This is some text that will eventually wrap</h1>
</div>








const h1 = document.querySelector('h1');
const banner = document.querySelector('.banner');


//handles style changes on banner to check wrapping
const observer = new MutationObserver(mutations =>
mutations.forEach(mutationRecord => onLineWrapDoSomething())
);
observer.observe(banner, attributes : true, attributeFilter : ['style'] );


// handles window resize events
window.addEventListener('resize', onLineWrapDoSomething)

function onLineWrapDoSomething()
const lineHeight = getComputedStyle(h1);
const lineHeightParsed = parseInt(lineHeight.split('px')[0]);
const amountOfLinesTilAdjust = 2;

if (h1.offsetHeight >= (lineHeightParsed * amountOfLinesTilAdjust))
console.log('your h1 now wrapped')
else
console.log('your h1 on one line')



// shows it logs when style changes and it wraps, ignore the disgusting code below
setTimeout(() =>
banner.style.width = '50%'
setTimeout(() =>
banner.style.width = '100%'
, 1500)
, 1500)

.banner 
width: 100%;

h1
line-height: 1.5

<div class="banner">
<h1>This is some text that will eventually wrap</h1>
</div>





const h1 = document.querySelector('h1');
const banner = document.querySelector('.banner');


//handles style changes on banner to check wrapping
const observer = new MutationObserver(mutations =>
mutations.forEach(mutationRecord => onLineWrapDoSomething())
);
observer.observe(banner, attributes : true, attributeFilter : ['style'] );


// handles window resize events
window.addEventListener('resize', onLineWrapDoSomething)

function onLineWrapDoSomething()
const lineHeight = getComputedStyle(h1);
const lineHeightParsed = parseInt(lineHeight.split('px')[0]);
const amountOfLinesTilAdjust = 2;

if (h1.offsetHeight >= (lineHeightParsed * amountOfLinesTilAdjust))
console.log('your h1 now wrapped')
else
console.log('your h1 on one line')



// shows it logs when style changes and it wraps, ignore the disgusting code below
setTimeout(() =>
banner.style.width = '50%'
setTimeout(() =>
banner.style.width = '100%'
, 1500)
, 1500)

.banner 
width: 100%;

h1
line-height: 1.5

<div class="banner">
<h1>This is some text that will eventually wrap</h1>
</div>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 13:49

























answered Nov 13 '18 at 12:59









Joe WarnerJoe Warner

1,9521522




1,9521522







  • 1





    thats extremely useful Joe. I've never come across MutationObserver before. Defo beats using setTimeOut as a hacky way to keep checking. I am going to take this and attempt to manipulate so it can detect, 1, 2, 3, 4 lines etc. Thank you very much. Learnt something new & really useful here.

    – ste
    Nov 13 '18 at 15:21











  • yeah I only really saw the native MutationObserver the other day, very cool we use it at work for scrollbar detection! :) glad to help any questions let me know

    – Joe Warner
    Nov 13 '18 at 15:47












  • 1





    thats extremely useful Joe. I've never come across MutationObserver before. Defo beats using setTimeOut as a hacky way to keep checking. I am going to take this and attempt to manipulate so it can detect, 1, 2, 3, 4 lines etc. Thank you very much. Learnt something new & really useful here.

    – ste
    Nov 13 '18 at 15:21











  • yeah I only really saw the native MutationObserver the other day, very cool we use it at work for scrollbar detection! :) glad to help any questions let me know

    – Joe Warner
    Nov 13 '18 at 15:47







1




1





thats extremely useful Joe. I've never come across MutationObserver before. Defo beats using setTimeOut as a hacky way to keep checking. I am going to take this and attempt to manipulate so it can detect, 1, 2, 3, 4 lines etc. Thank you very much. Learnt something new & really useful here.

– ste
Nov 13 '18 at 15:21





thats extremely useful Joe. I've never come across MutationObserver before. Defo beats using setTimeOut as a hacky way to keep checking. I am going to take this and attempt to manipulate so it can detect, 1, 2, 3, 4 lines etc. Thank you very much. Learnt something new & really useful here.

– ste
Nov 13 '18 at 15:21













yeah I only really saw the native MutationObserver the other day, very cool we use it at work for scrollbar detection! :) glad to help any questions let me know

– Joe Warner
Nov 13 '18 at 15:47





yeah I only really saw the native MutationObserver the other day, very cool we use it at work for scrollbar detection! :) glad to help any questions let me know

– Joe Warner
Nov 13 '18 at 15:47



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280724%2fword-wrap-detection-in-javascript%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Darth Vader #20

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Ondo