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

Introduction To HTML & CSS: Web Development For Beginners

Introduction To HTML & CSS: Web Development For Beginners

Introduction To HTML & CSS: Web Development For Beginners - Complete HTML & CSS web developer bootcamp. Learn & practice website design using HTML5 & CSS3 web development resources

Welcome to Introduction To HTML & CSS: Web Development For Beginners: This is a comprehensive and Practical guide that is designed to pick up every interested student from the state of "zero-knowledge" to a state of "Hero-knowledge" in HTML and CSS. As a senior software developer and an instructor with many years of experience in the industry, I have mastered the best way to give my students packages that will help them to learn and understand HTML and CSS in one course.

What you'll learn

  • You will learn the basics of web development
  • You will be able to use the HTML tags professionally
  • You will learn how to work with images in HTML and CSS
  • You will learn how to create awesome backgrounds
  • You will how to use CSS colors
  • You will master most web development documentations used by professionals
  • You will learn how to embed multimedia in web pages
  • You will learn how to make comments in both HTML and CSS
  • You will learn the Box Model

Build a Quiz App with HTML, CSS, and JavaScript

<strong>Build a Quiz App with HTML, CSS, and JavaScript to improve your Core Web Development skills!</strong>

Build a Quiz App with HTML, CSS, and JavaScript to improve your Core Web Development skills!

In this video, we will create a simple spinning loader in CSS that will be displayed until we are finished requesting/loading questions from the API.

Course Content: https://github.com/jamesqquick/Build-A-Quiz-App-With-HTML-CSS-and-JavaScript


Learn More

The Web Developer Bootcamp

The Complete 2019 Web Development Bootcamp

The Complete Web Developer Course 2.0

Building Web APIs with ASP.NET Core

Build Progressive Web Apps with React

Build a web scraper with Node

Build a Basic Website with ASP.NET MVC and Angular

Build Your First PWA with Angular

Top 10 Podcasts for Web Developers

How to create a dual sliding panel using HTML, CSS and JavaScript

How to create a dual sliding panel using HTML, CSS and JavaScript

How to create a dual sliding panel using HTML, CSS and JavaScript? Solution: Check out this JavaScript Dual Sliding Panel With CSS, Responsive Panel Slide.

Today, you will learn to create Responsive Panel Slide with dual panel. Basically, there are two panels with 50% of the screen width and you can see only a title in each panel, but when you will click on the panel it will expand by 100% and you will see a quote and a background image. There are also two arrow buttons you can see, one is left arrow and other is right.

So, Today I am sharing JavaScript Dual Sliding Panel With CSS. There is a toggle feature you can open the panel on click and also can close the panel on another click. Also, this is a responsive design, the panel becomes top to bottom aligns on small screen size devices. This program will be very informative and useful for you.

If you are thinking now how the panels actually are, then see the preview given below.

Preview Of Responsive Panel Slide

See this video preview to getting an idea of how the dual panel looks like.


Now you can see this visually, you also can see it live by pressing the button above. If you like this, then get the source code of its.

JavaScript Dual Sliding Panel With CSS Source Code

Before sharing source code, let’s talk about it. First I have created all the divs and elements using HTML, on each panel, there has a heading, arrow button, quote, and an image. I have used SVG for creating the two arrow buttons, there I have not used any icon library. Also, I have used some tags between the divs like article tag, and section tag.

Now using CSS I have placed all the elements on the right place. Basically, there are two panels with 50% width first you can see only the heading and arrow. Using CSS I have hides the quote and image section firstly, you can see it after clicking then it will expand to single on 100% width. There is also a hover effect, when you will hover on any panel then its arrow icon starts moving.

For the responsive design, I have changed the position of the panels. On a desktop, the panel is placed as one in left and one in right but on a small screen, its becomes one in the top and another in the bottom. And when you will click on the panel to expand in small screen it will cover 100% of the height of the screen.

In there JavaScript handling the toggle feature. First JS fetches the panels using document.querySelector (info) and stored in variables. Now its read the click action and if the left panel has been clicked then its hide the right and expand the left panel. Left all other things you will understand after getting the codes, I can’t explain all in writing.

For Creating this program, you have to create 3 files. First for HTML, second for CSS, and 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 below.

<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title>JS Dual Sliding Panels | Webdevtrick.com</title>
  <meta name="viewport" content="width=device-width">
  <link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
 
  <div class="container">
  <section class="panels">
    <article class="panel left-panel">
      <div class="panel inner-left-panel">
        <p>"Success is a lousy teacher. It seduces smart people into thinking they can't lose."</p>
      </div>
      <div class="panel name">
        <h1 class="title">Bill Gates</h1>      
        <svg class="arrow arrow-left" width="40" height="40" viewBox="0 0 24 24"><path d="M0 0h24v24h-24z" fill="none"/><path d="M20 11h-12.17l5.59-5.59-1.42-1.41-8 8 8 8 1.41-1.41-5.58-5.59h12.17v-2z"/></svg>
      </div>
    </article>
    <article class="panel right-panel">
      <div class="panel name">
        <h1 class="title">Steve Jobs</h1>
        <svg class="arrow arrow-right" width="40" height="40" viewBox="0 0 24 24"><path d="M0 0h24v24h-24z" fill="none"/><path d="M12 4l-1.41 1.41 5.58 5.59h-12.17v2h12.17l-5.58 5.59 1.41 1.41 8-8z"/></svg>
      </div>
      <div class="panel inner-right-panel">
        <p>"Design is not just what it looks like and feels like. Design is how it works."</p>
      </div>
    </article>
  </section>
</div>
 
  <script  src="function.js"></script>
 
</body>
</html>

style.css

Now create a CSS file named ‘style.css‘ and put these codes given here.

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: rgba(255,255,255,0);
  overflow-x: hidden;
}
body {
  font-family: 'Montserrat';
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
.container {
  height: 100vh;
  container: hidden;
}
.panels {
  width: 200%;
}
.panel {
  float: left;
  width: 50%;
  -webkit-perspective: 400px;
          perspective: 400px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.5s cubic-bezier(0.23, 1, 0.32, 1);
}
.left-panel,
.right-panel {
  will-change: transform;
  position: relative;
  left: -25%;
  -webkit-transform: translate(0, 0);
          transform: translate(0, 0);
}
.left-panel {
  background: #fce77d;
}
.left-panel:hover .arrow {
  -webkit-transform: translate(-100%, -50%);
          transform: translate(-100%, -50%);
}
.right-panel {
  background: #f96167;
}
.right-panel:hover .arrow {
  -webkit-transform: translate(0, -50%);
          transform: translate(0, -50%);
}
.left-panel-active {
  -webkit-transform: translate(50%, 0);
          transform: translate(50%, 0);
}
.left-panel-active .inner-left-panel {
  -webkit-transform: rotateY(0);
          transform: rotateY(0);
}
.left-panel-active .arrow {
  -webkit-transform: translate(-50%, -50%) rotate(180deg) !important;
          transform: translate(-50%, -50%) rotate(180deg) !important;
}
.right-panel-active {
  -webkit-transform: translate(-50%, 0);
          transform: translate(-50%, 0);
}
.right-panel-active .inner-right-panel {
  -webkit-transform: rotateY(0);
          transform: rotateY(0);
}
.right-panel-active .arrow {
  -webkit-transform: translate(-50%, -50%) rotate(180deg) !important;
          transform: translate(-50%, -50%) rotate(180deg) !important;
}
.left-panel-hidden {
  -webkit-transform: translate(-50%, 0);
          transform: translate(-50%, 0);
}
.right-panel-hidden {
  -webkit-transform: translate(50%, 0);
          transform: translate(50%, 0);
}
.name {
  cursor: pointer;
}
.inner-left-panel,
.inner-right-panel,
.name {
  will-change: transform;
  padding: 0 5vw;
  height: 100vh;
}
.inner-left-panel {
  -webkit-transform-origin: right center;
          transform-origin: right center;
  -webkit-transform: rotateY(-90deg);
          transform: rotateY(-90deg);
  transition-delay: 0.1s;
  background: url("https://webdevtrick.com/wp-content/uploads/bill.jpg") center/cover;
}
.inner-left-panel:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: #fce77d;
  z-index: -1;
  opacity: 0.85;
}
.inner-right-panel {
  -webkit-transform-origin: left center;
          transform-origin: left center;
  -webkit-transform: rotateY(90deg);
          transform: rotateY(90deg);
  transition-delay: 0.1s;
  background: url("https://webdevtrick.com/wp-content/uploads/steve.jpg") center/cover;
}
.inner-right-panel:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: #f96167;
  z-index: -1;
  opacity: 0.85;
}
.title {
  margin: 0;
  font-size: 40px;
  text-align: center;
  color: #212121;
}
.panels p {
  font-size: 30px;
  margin: 0;
}
@media (max-width: 640px) {
  .panels {
    width: 100%;
    height: 200vh;
  }
  .panel {
    float: none;
    width: 100%;
    height: 100vh;
    display: block;
    text-align: center;
  }
  .left-panel,
  .right-panel {
    top: -25%;
    left: 0;
  }
  .left-panel:hover .arrow {
    -webkit-transform: translate(-50%, -80%) rotate(90deg);
            transform: translate(-50%, -80%) rotate(90deg);
  }
  .right-panel:hover .arrow {
    -webkit-transform: translate(-50%, -20%) rotate(90deg);
            transform: translate(-50%, -20%) rotate(90deg);
  }
  .left-panel-active {
    -webkit-transform: translate(0, 50%);
            transform: translate(0, 50%);
  }
  .left-panel-active .inner-left-panel {
    -webkit-transform: rotateX(0);
            transform: rotateX(0);
  }
  .left-panel-active .arrow {
    -webkit-transform: translate(-50%, -50%) rotate(-90deg) !important;
            transform: translate(-50%, -50%) rotate(-90deg) !important;
  }
  .right-panel-active {
    -webkit-transform: translate(0, -50%);
            transform: translate(0, -50%);
  }
  .right-panel-active .inner-right-panel {
    -webkit-transform: rotateX(0);
            transform: rotateX(0);
  }
  .right-panel-active .arrow {
    -webkit-transform: translate(-50%, -50%) rotate(-90deg) !important;
            transform: translate(-50%, -50%) rotate(-90deg) !important;
  }
  .left-panel-hidden {
    -webkit-transform: translate(0, -50%);
            transform: translate(0, -50%);
  }
  .right-panel-hidden {
    -webkit-transform: translate(0, 50%);
            transform: translate(0, 50%);
  }
  .inner-left-panel,
  .inner-right-panel,
  .name {
    height: 50vh;
    padding: 2vh 8vw;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .inner-left-panel {
    -webkit-transform-origin: center bottom;
            transform-origin: center bottom;
    -webkit-transform: rotateX(90deg);
            transform: rotateX(90deg);
  }
  .inner-right-panel {
    -webkit-transform-origin: center top;
            transform-origin: center top;
    -webkit-transform: rotateX(-90deg);
            transform: rotateX(-90deg);
  }
  .title {
    font-size: 32px;
  }
  .panels p {
    font-size: 20px;
  }
}
.arrow {
  position: absolute;
  top: 75%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
          transform: translate(-50%, -50%);
  fill: #212121;
  border: 3px solid #212121;
  border-radius: 50%;
  transition: all 0.5s cubic-bezier(0.23, 1, 0.32, 1);
}
@media (max-width: 640px) {
  .arrow {
    -webkit-transform: translate(-50%, -50%) rotate(90deg);
            transform: translate(-50%, -50%) rotate(90deg);
  }
  .arrow-left {
    top: 25%;
  }
}

function.js

The final step, Create a JavaScript file named ‘function.js‘ and put the codes.

var Panels = (function() {
  
  var panelLeft = document.querySelector('.left-panel');
  var panelRight = document.querySelector('.right-panel');
 
  var openLeft = function() {
		panelLeft.classList.toggle('left-panel-active');
    panelRight.classList.toggle('right-panel-hidden');
  };
 
  var openRight = function() {
    panelRight.classList.toggle('right-panel-active');
    panelLeft.classList.toggle('left-panel-hidden');
  };
  
  var bindActions = function() {
    panelLeft.addEventListener('click', openLeft, false);
    panelRight.addEventListener('click', openRight, false);
  };
  
  var init = function() {
    bindActions();
  };
  
  return {
    init: init
  };

}());
 
Panels.init();

That’s It. Now you have successfully created JavaScript Dual Sliding Panel With CSSResponsive Panel Slide. If you have any doubt or question comment down below.

I hope this tutorial will surely help and you if you liked this tutorial, please consider sharing it with others.

Thank you !