How we can create a flexible full-width text using HTML, CSS, & JavaSript? Solution: Responsive Full-Width Text With CSS and JavaScript, Flexible Text Block.
I am sure that you get what I am talking about. Basically, This is an experiment to create flexible or responsive text with 100% width of screen size. Previously I have shared a CSS blockquote design, this is also a blockquote but this time it’s about full width.
Today you will learn to create a flexible paragraph with the screen’s full width. This is responsive design these texts will fit on any screen size, you can see also live transformation if you resize the window slowly. This font resize function is created using JavaScript. You can use this program on your review section, quote section, etc.
So, Today I am sharing Responsive Full Width Text With CSS and JavaScript. In other words, Flexible Text Block or Paragraph quote. I have used normal HTML CSS & JavaScript, I did not use any library for creating that. I think you should know this technique to create flexible texts. This program is very easy to understand, nothing is difficult here. If you have basic knowledge of JS then you can get it easily.
If you are thinking now how this Responsive Text Block actually is, Then see the preview given below.
Preview Of Flexible Text Block With 100% Width
See this video preview to getting an idea of how this program looks like.
Now you can see this visually. If you like this, Then get the source code of its.
You May Also Like:
- Responsive Testimonial Slider
- CSS Breadcrumb UI Design
- Vertical Image Column
- Detect Browser Using JavaScript
Responsive Full Width Text With CSS and JavaScript Source Code
Before sharing source code, Let’s talk about it. As you know I have used JavaScript mostly to create this. Basically, I have created an HTML Blockquote and put every line in a separate span. Also with this, I have created another span with a different class for command & full stop.
In the CSS section, First I have put 100vh of height. After that, I have put other properties for the font family, grid display, etc. With JavaScript, I have created 2 variables for text and special characters like comma, full stop. The main thing is in the javascript files is put the size of the font. For this, I have divided 120 by text length (info), The code looks like this: elem.style.fontSize = 120 / elem.innerHTML.length + "vw"
You will understand fully after getting the codes because codes are easy to understand. For creating this full width text program you have to create 3 files. First for HTML, second for CSS, & third for JavaScript. Follow the steps to creating this without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given here below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<!DOCTYPE html> <html lang="en" > <!--Code By Webdevtrick ( https://webdevtrick.com )--> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>CSS Full-Width Text | Webdevtrick.com</title> <link href="https://fonts.googleapis.com/css?family=Lato&display=swap" rel="stylesheet"> <link rel="stylesheet" href="style.css"> </head> <body> <blockquote class="block"> <span class="texts">Success is no accident</span><span class="append">.</span> <span class="texts">It is hard work</span><span class="append">,</span> <span class="texts">learning, studying, sacrifice</span> <span class="texts">and most of all</span><span class="append">,</span> <span class="texts">love of what</span> <span class="texts"> you are doing</span> <span class="texts">or learning to do</span><span class="append">.</span> <cite> <span class="texts"> —Pele</span> </cite> </blockquote> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
/** Code By Webdevtrick ( https://webdevtrick.com ) **/ body { display: flex; font-family: 'Lato', sans-serif; margin: 0; min-height: 100vh; } cite { font-stretch: condensed; font-style: normal; font-weight: bold; } .block { display: grid; grid-template-columns: max-content 1fr; margin: auto; text-transform: uppercase; width: max-content; } .texts, .block cite { display: flex; grid-column: 1; justify-content: space-between; } |
function.js
The final step, Create a JavaScript file named ‘function.js‘ and put the codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/** Code By Webdevtrick ( https://webdevtrick.com ) **/ var lines = document.querySelectorAll(".texts"); var appendages = document.querySelectorAll(".append"); var wrapCharacters = function(lines) { return lines.forEach(function(line) { var characters = line.innerHTML.split(""); var wrappedCharacters = characters .map(function(character) { if (character === " ") { return '<span class="texts"> </span>'; } return '<span class="texts">' + character + "</span>"; }) .join(""); return (line.innerHTML = wrappedCharacters); }); }; var useSiblingFontSize = function(elem) { elem.style.fontSize = elem.previousSibling.style.fontSize; }; var setFontSize = function(elems) { return elems.forEach(function(elem) { if (!elem.classList.contains("append")) { return (elem.style.fontSize = 120 / elem.innerHTML.length + "vw"); } return useSiblingFontSize(elem); }); }; var formatTextBlocks = function() { setFontSize(lines); setFontSize(appendages); wrapCharacters(lines); }; formatTextBlocks(); |
That’s It. Now you have successfully created Responsive Full Width Text With CSS and JavaScript, Flexible Text Block with 100% screen width. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visting.
thanks, this is a good and nice trick.
Can this be done by CSS?