Copy Text To Clipboard in HTML CSS and JavaScript

Hello friends, today we will be going to how do we copy text to a clipboard using JavaScript. I have posted many videos and articles before related to the JavaScript project, now this the something new that we are going to build.

Video Tutorial of How to Copy Text To Clipboard in HTML CSS and JavaScript


As you have seen on the given video tutorial of  Copy Text To Clipboard in HTML CSS and JavaScript, 

If you are feeling bored watching the given video tutorial of Copy Text To Clipboard in HTML CSS and JavaScript then you can copy or download the given codes below:


Copy Text To Clipboard in HTML CSS and JavaScript | Free Source Code

To create this program (Copy Text To Clipboard). First, you need to create two Files one HTML File and another one is CSS File. After creating these files just paste the following codes in your file.

In the first place, make a HTML document with the name of index.html and glue the given codes in your HTML record. Keep in mind, you've to make a document with .html extension.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Copy to Clipboard</title>

    <!-- Created by S-Tech04 -->

</head>
<body>
    <div class="text-boxes">
        <div class="text-box htmlBox">
            <div class="topic">HTML Code</div>
            <textarea readonly id="HtmlCode"><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://kit.fontawesome.com/6f17055eb8.js" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="style.css">
    <title>Button</title>
</head>
<body>
    <button  class="btn">
        <span class="txt">Botton</span>
        <span class="round"><i class="fa fa-chevron-right"></i></span>
    </button>
</body>
</html>
            </textarea>
            <button id="HtmlBtn">Copy Code</button>
        </div>
        <div class="text-box htmlBox">
            <div class="topic">CSS Code</div>
            <textarea readonly id="CSSCode">*
{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: sans-serif;
}
button
{
    position: relative;
    display: inline-block;
    text-decoration: none;
    padding: 12px 53px 12px 23px;
    color: #fff;
    background-color: #f3c40f;
    border: none;
    outline: none;
    border-radius: 30px;
    text-transform: uppercase;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.3s;
}
button span
{
    position: relative;
    z-index: 3;
}
button .round
{
    border-radius: 50%;
    width: 38px;
    height: 38px;
    position: absolute;
    right: 3px;
    top: 3px;
    background-color: #f8d23b;
    transition: all 0.3s ease-out;
    z-index: 2;
}
button .round i
{
    position: absolute;
    top: 50%;
    margin-top: -6px;
    left: 50%;
    margin-left: -4px;
    transition: all 0.3s;
}
.txt
{
    font-size: 14px;
    line-height: 1.45;
}
button:hover
{
    padding-left: 48px;
    padding-right: 28px;
}
button:hover .round
{
    width: calc(100% - 6px);
    border-radius: 30px;
}
button:hover .round i
{
    left: 12%;
}
            </textarea>
            <button id="CSSBtn">Copy Code</button>
        </div>
    </div>
    <script>
        const HtmlBtn = document.getElementById('HtmlBtn');
        const HtmlCode = document.getElementById('HtmlCode');
        HtmlBtn.addEventListener('click',()=>{
            HtmlCode.select();
            document.execCommand('copy');
            HtmlBtn.innerHTML = "Code Copied"
        });
        const CSSBtn = document.getElementById('CSSBtn');
        const CSSCode = document.getElementById('CSSCode');
        CSSBtn.addEventListener('click',()=>{
            CSSCode.select();
            document.execCommand('copy');
            CSSBtn.innerHTML = "Code Copied"
        })
    </script>
</body>
</html>
  
Second, make a CSS record with the name of style.css and glue the given codes in your CSS document. Keep in mind, you've to make a record with .css extension.
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500;700;900&display=swap');
*
{
   margin: 0;
   padding: 0;
   box-sizing: border-box;
   font-family: 'Poppins', sans-serif;
}
body
{
   min-height: 100vh;
   width: 100%;
   background: #fdfcfc;
}
::-webkit-scrollbar
{
   width: 10px;
   background: #d3defa;
}
::-webkit-scrollbar-thumb
{
   height: 80px;
   background: #265df2;
   background-clip: content-box;
   border-radius: 12px;
   transition: background 0.4s;
}
::-webkit-scrollbar-thumb:hover
{
   background: #0e4bf1;
}
.text-boxes
{
   height: 100%;
   width: 100%;
   display: flex;
   align-items: center;
   flex-direction: column;
   padding: 0 30px;
   margin-bottom: 50px;
}
.text-boxes .text-box
{
   height: 380px;
   max-width: 660px;
   width: 100%;
   margin: 55px 0;
}
.text-boxes .text-box .topic
{
   font-size: 18px;
   font-weight: 600;
   color: #265df2;
   margin: 4px;
   width: 100%;
}
.text-boxes .text-box textarea
{
   display: block;
   height: 100%;
   width: 100%;
   padding: 30px;
   font-size: 15px;
   font-weight: 400;
   outline: none;
   border: 1px solid #265df2;
   border-radius: 8px;
   background: #e7edfe;
   overflow-x: scroll;
   overflow-wrap: inherit;
}
.text-boxes .text-box textarea::-webkit-scrollbar
{
   display: none;
}
.text-boxes .text-box button
{
   display: block;
   height: 45px;
   width: 155px;
   background: #265df2;
   margin: 8px 0px;
   color: #fff;
   font-size: 17px;
   font-weight: 400;
   outline: none;
   border: none;
   border-radius: 3px;
   cursor: pointer;
   transition: all 0.5s;
}
.text-boxes .text-box button:hover
{
   background: #0e4bf1;
}
@media(max-width: 400px)
{
   .text-boxes .text-box button
   {
      width: 100%;
   }
}
  
That’s all, now you’ve successfully created a program to Copy text to clipboard using Html, CSS & JavaScript. If your code doesn’t work or you’ve faced any error/problem, please download the source code files from the given download button. It’s free and a .zip file will be downloaded then you’ve to extract it.
Click on the following download button to download all source code files.



Download Now
Please Wait...
Direct Link click here.

How can I help you

Post a Comment

How can I help you

Post a Comment (0)

Previous Post Next Post