Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • CSS Tutorial
  • CSS Exercises
  • CSS Interview Questions
  • CSS Selectors
  • CSS Properties
  • CSS Functions
  • CSS Examples
  • CSS Cheat Sheet
  • CSS Templates
  • CSS Frameworks
  • Bootstrap
  • Tailwind
  • CSS Formatter
Open In App
Next Article:
Change the Border of HTML Element in JavaScript
Next article icon

How to set the color of an element border with JavaScript ?

Last Updated : 12 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will see how to set the color of an element's border with JavaScript. To set the color of an element's border with JavaScript, we can use the element's style property.

Method 1: First, we will create a text element and then apply some CSS styles including the border color of the element. After that, we use JavaScript borderColor property to set the color of the border element.

Syntax:

document.querySelector(".box-element").style.borderColor = "green";
 

Example: In this example, we will set the border color from black to green.

HTML
<!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">     <title>         How to set the color of an element          border with JavaScript?     </title>      <style>         body {             text-align: center;         }                  h1 {             color: green;         }                .box-element {             width: 350px;             border: 5px solid;             font-size: 18px;             padding: 20px;             margin: auto;         }          input {             margin-top: 20px;             font-size: 18px;             font-weight: bold;             padding: 5px 15px;         }     </style> </head>  <body>     <h1>GeeksforGeeks</h1>      <h3>         How to set the color of an elements          border with JavaScript?     </h3>      <div class="box-element">         Welcome to GeeksforGeeks     </div>      <input type="button"          value="Change Border Color"          onclick="myFun()">      <script>         function myFun() {             document.querySelector(".box-element")                 .style.borderColor = "green";         }     </script> </body>  </html> 

Output:

 

Method 2: First, we will create a text element and then apply some CSS styles to the element. After that, we use JavaScript style property to add the new border with some color of the border element.

Syntax:

let styles = `border: 5px solid green;`;  document.querySelector(".box-element").style = styles;

Example: In this example, we will add the border to the element.

HTML
<!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">     <title>         How to set the color of an elements          border with JavaScript?     </title>      <style>         body {             text-align: center;         }          h1 {             color: green;         }          .box-element {             width: 350px;             font-size: 18px;             padding: 20px;             margin: auto;         }          input {             margin-top: 20px;             font-size: 18px;             font-weight: bold;             padding: 5px 15px;         }     </style> </head>  <body>     <h1>GeeksforGeeks</h1>      <h3>         How to set the color of an elements          border with JavaScript?     </h3>      <div class="box-element">         Welcome to GeeksforGeeks     </div>      <input type="button"          value="Change Border Color"          onclick="myFun()">      <script>         let styles = `border: 5px solid green;`;          function myFun() {             document.querySelector(".box-element")                 .style = styles;         }     </script> </body>  </html> 

Output:

 

Next Article
Change the Border of HTML Element in JavaScript

V

vkash8574
Improve
Article Tags :
  • Web Technologies
  • CSS
  • CSS-Questions

Similar Reads

  • How to set the style of the bottom border with JavaScript ?
    In this article, we will see how to set the style of the bottom border using JavaScript. To set the border-bottom of an element using JavaScript, we will use the borderBottomStyle property. The style borderBottomStyle property is used to set or return the style of the bottom border of an element. Ap
    2 min read
  • How to Change the Color of HTML Element in JavaScript?
    Changing the text color of an HTML element in JavaScript improves the user experience. To change the font color of an HTML element using JavaScript, we use the DOM to access and modify its style properties, allowing for dynamic color changes. Syntax:element.style.color = "green";ApproachSelect the T
    2 min read
  • How to change style/class of an element using JavaScript ?
    In this article, we will learn how we can change the style/class of an element. If you want to build a cool website or app then UI plays an important role. We can change, add or remove any CSS property from an HTML element on the occurrence of any event with the help of JavaScript. There are two com
    4 min read
  • How to get/change the HTML with DOM element in JavaScript ?
    In order to get/access the HTML for a DOM element in JavaScript, the first step is to identify the element base on its id, name, or its tag name. Then, we can use inner.HTML or outer.HTML to get the HTML. Using the getElementById() method: This method gets/identifies the DOM elements using its ID an
    3 min read
  • Change the Border of HTML Element in JavaScript
    In this article, we will see how to change the Border of HTML element using JavaSCript. To change the border of an element, first, we select the element, and then use HTML DOM border style property to change the border. The CSS border properties are given below: border-style: The border-style In Jav
    3 min read
  • How to Change the Color of the Alert Box in JavaScript ?
    An alert box is a dialogue box that pops up on the screen to provide information to the user. It contains a message and an OK button, which allow users to acknowledge the message and close the box. Alert boxes are commonly used in web development to display important messages, warnings, or notificat
    5 min read
  • How to Change the Color of the Alert Box in JavaScript ?
    Alert boxes, often utilized in JavaScript applications, provide users with important notifications or messages. However, their default appearance may not always blend seamlessly with the overall design of your website or application. JavaScript offers several methods to customize alert boxes as list
    3 min read
  • How to Change Border Width of Div in JavaScript ?
    Div elements are essential in creating modern web applications, and manipulating their properties dynamically can enhance user experience. In this article, we'll see how to change the border width of a div element using JavaScript. The border-width property in CSS is used to specify the width of the
    2 min read
  • How to change style attribute of an element dynamically using JavaScript ?
    Given an HTML document and the task is to change the style properties (CSS Properties) of an element dynamically with the help of JavaScript. Approach: Using element.style PropertyThe element.style property is represented by the CSSStyleDeclaration object, which is returned via the style property. S
    2 min read
  • How to Select an Element by ID in JavaScript ?
    In JavaScript, we can use the "id" attribute to interact with HTML elements. The "id" attribute in HTML assigns a unique identifier to an element. This uniqueness makes it easy for JavaScript to precisely target and manipulate specific elements on a webpage. Selecting elements by ID helps in dynamic
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences