Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS/JS: split words with horizontal line in responsive design

enter image description here

What would be the best way to split a word in the middle (or after a specific amount of characters or syllables) and join both "word-parts" with a line. Basically imagine a very long flexible underscore.

The goal is to have "word___part" always 100% of the parent container. Meaning it should work fully responsive when scaling down or up the browser-window.

    span:first-child {
    	float:left;
    	display:inline-block;
    }
    
    span.underscore {
    
    }
    
    span:last-child {
    	float:right;
    	display:inline-block;
    }
    <span>Auto</span><span class="underscore"></span><span>mation</span>

How would you approach that? Flexbox?

Additionally the meta-goal would even be to set the word that is split apart with a dynamic-cms. Meaning the word "Automation" comes from a backend.

like image 289
matt Avatar asked Jul 31 '17 13:07

matt


People also ask

How do I put a horizontal line after text in CSS?

Adding the Horizontal line using CSS Properties: In this case, we will be using the border-style Property to create the horizontal line. We can either use the border-top property that specifies the style of the top border or the border-bottom property, which can set the style of the bottom border of an element.

How do I break text into multiple lines in CSS?

We use the word–break property in CSS that is used to specify how a word should be broken or split when reaching the end of a line. The word–wrap property is used to split/break long words and wrap them into the next line.

How do I stop words breaking in HTML?

How to Prevent Word Wrap on a Web Page: HTML Method. If you only have the one-off instance of two or more words that you want to force the browser to keep on a single line, the easiest way is to use the non-breaking space character, " &nbsp; ", to separate those words instead of a normal space.

How do you break a single line paragraph into two lines in HTML?

To do a line break in HTML, use the <br> tag. Simply place the tag wherever you want to force a line break. Since an HTML line break is an empty element, there's no closing tag. Below is an HTML file with a <p> and <br> element.


1 Answers

You can use :after pseudo-element on first span element and set align-items: flex-end; to align line at bottom of spans.

div {
  width: 70%;
  display: flex;
}
span:first-child {
  display: flex;
  align-items: flex-end;
  flex: 1;
}
span:first-child:after {
  content: '';
  height: 1px;
  background: black;
  flex: 1;
}
<div>
  <span>Auto</span><span>mation</span>
</div>

<div>
  <span>Lorem ipsum dolor </span><span>sit.</span>
</div>

You can also use js to split string at specific word and wrap each part in span elements.

function modify(selector, word) {
  var el = document.querySelector(selector);
  var text = el.textContent;
  var i = text.indexOf(word)

  if (i != -1) {
    var arr = [text.substring(0, i), text.substring(i)]
    el.innerHTML = arr.map(e => '<span>' + e + '</span>').join('');
  }
}

modify('.e1', 'mation')
modify('.e2', 'sit')
div {
  width: 70%;
  display: flex;
}
span:first-child {
  display: flex;
  align-items: flex-end;
  flex: 1;
}
span:first-child:after {
  content: '';
  height: 1px;
  background: black;
  flex: 1;
}
<div class="e1">Automation</div>
<div class="e2">Lorem ipsum dolor sit.</div>
like image 79
Nenad Vracar Avatar answered Sep 30 '22 13:09

Nenad Vracar