In Word-break: Break-word, I Want To Capitalize The First Word In The Next Line Either Using CSS / JS, Is This Possible?
I have a requirement to capitalize the first word of the wrapped sentence (wrapped to the next line due to word break). I know about the first-letter, first-line selectors but none would be suitable for this case. I am stuck, help me!!!
Further Explaination:
e.g. in one view it would be
Lorem ipsum and
Prometheus
further scaling down, it should be like this:
Lorem
Ipsum
And
Prometheus
Answer
You can convert the text so each word is in its own span
, then check the position
of each span
every time the page resizes.
This may not perform well, so you would likely want to debounce
the resize.
To make use of ::first-letter
the element must be a block-level element, which span
, by default, is not. So it must be display:inline-block
.
This doesn't handle 'break-word' as OPs samples do not include that as an example and break-word
is deprecated.
// wrap each word in a span
$("p").each(function() {
$(this).html($(this).text().split(" ").map(t => "<span>" + t + "</span>").join(" "));
});
// determine what is "left" (it's not 0)
var farLeft = $("p").position().left;
// add a class to each "left" element and let css handle the rest
function firstLetter() {
$(".farleft").removeClass("farleft");
$("p>span").each(function() {
if ($(this).position().left == farLeft) {
$(this).addClass("farleft");
}
})
}
$(window).on("resize", firstLetter);
firstLetter();
p>span { display:inline-block; }
.farleft::first-letter {
text-transform: uppercase;
font-weight:bold;
}
/* these are just for the demo so it's easier to see what's going on */
p { width: 50% }
.farleft { background-color: pink; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
Related Questions
- → October CMS create a multi select Form field
- → How to update data attribute on Ajax complete
- → laravel blade templating error
- → should I choose reactjs+f7 or f7+vue.js?
- → How to dynamically add class to parent div of focused input field?
- → Setting the maxlength of text in an element that is displayed
- → Undefined Result for Variable with Javascript innerHTML function
- → Expanding search bar not expanding before search
- → Get the calling element with vue.js
- → Blade: how to include a section in one page but not in another
- → How to print/log reactjs rendered dom?
- → how to write react component to construct HTML DOM
- → How to add a timer in HTML5 and Javascript?