Flexible Text Block with CSS and JavaScript

Flexible Text Block with CSS and JavaScript

How we can create a flexible full-width text using HTML, CSS, & JavaSript? Solution: Responsive Full-Width Text With CSS and JavaScript

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.

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.

<!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.

/** 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.

/** 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">&nbsp;</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 liked this post, share it with all of your programming buddies! Thank you!

Further reading


Originally published on https://webdevtrick.com

javascript css html web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire CSS Developer

Want to develop a website or re-design using CSS Development? We build a website and we implemented CSS successfully if you are planning to **[Hire CSS Developer](https://hourlydeveloper.io/hire-dedicated-css-developer/ "Hire CSS Developer")**...

Web Animations with HTML, CSS, and JavaScript

Learn about some basic animations you can do with CSS, HTML and JavaScript and shows you how to add input animations to your site. Web animation is necessary for creating eye-catching websites that enable better conversions and attract users to click, view, and buy things on your website.

Top Neumorphism UI Design Using HTML, CSS & JavaScript

Top Neumorphism UI Design Using Html CSS & Javascript - Neumorphism Effect. Neumorphism Element Using HTML, CSS & JavaScript

The Ultimate Guide to Web Development with HTML and CSS

The Ultimate Guide to Web Development with HTML and CSS. Learn Web Development by creating a Blog with HTML and CSS. Make a web applications using HTML5 and CSS3. Create a blog from Scratch.

How to Create JavaScript carousel with HTML, CSS and JavaScript

Learn how to create JavaScript carousel with HTML, CSS and JavaScript. Learn how to create a JavaScript slider step by step. Now many people will just download a library to create a JavaScript carousel or JavaScript slider.