
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to set the width of the left border with JavaScript?
In this tutorial, we will learn how to set the width of the left border with JavaScript.
The border property specifies the boundary of an element. It specifies the border style, border-color values, and also border-width. One, two, or three of the values can be used to specify the border attribute. The sequence of the values is irrelevant. The border shorthand comes in handy when you want all four borders to be the same.
To differentiate them, employ the longhand border-color, and other attributes like border style and width, which take distinct values for each side. Alternatively, you may use logical attributes like border-block-start and physical attributes like border-top to target just one border at a time.
The DOM Style borderLeftWidth property is used to set or return the width of an element's left border. It returns a string indicating the width of an element's left border. The border-style property is always declared before the border-left-width property. Before you can adjust the width of an element, it must have boundaries.
Following are the ways to set the width of the left border with JavaScript.
Using the borderLeftWidth Property
The DOM Style borderLeftWidth property is used to set or return the width of an element's left border. It returns a string indicating the width of an element's left border. A thin left border is specified using the thin border attribute.
Syntax
document.getElementById("box").style.borderLeftWidth = "thin"; document.getElementById("box").style.borderLeftWidth = "20px"; document.getElementById("box").style.borderLeftWidth = "thick";
The box element is fetched using the getElementById() method, and the width of the left border is changed to thin property.
Example
In this example, we have created a box width with a thick solid black border. The width and height of the border are specified at 200px each. Using the borderLeftWidth property, the border property is changed. The user decides which button to click. When the "Thin border" button clicks, the border changes to thin. On clicking the "Length border" button is clicked, the border changes to 20px in length. The border changes to a thick value when clicking the "Thick border" button.
<html> <head> <style> #box { border: thin solid black; background-color: skyblue; width: 500px; height: 200px; } </style> </head> <body> <h2> Set the width of the left border using <i> borderLeftWidth </i> property </h2> <h4> Choose a button to select the property value: </h4> <button type="button" onclick="myFunction()"> Thin border</button> <br> <button type="button" onclick="myFunction1()"> Length border </button> <br> <button type="button" onclick="myFunction2()"> Thick border </button> <br> <div id="box"> The left border width of this box is changed. </div> <br> <br> <script> function myFunction() { document.getElementById("box").style.borderLeftWidth = "thin"; } function myFunction1() { document.getElementById("box").style.borderLeftWidth = "20px"; } function myFunction2() { document.getElementById("box").style.borderLeftWidth = "thick"; } </script> </body> </html>
Using the setProperty attribute
A property on a style declaration object is given a new value through the setProperty() function interface. The declaration block's new CSS may be set, or the old CSS can be modified using the setProperty() function. It accepts the property name, value, and priority as three inputs.
A string representing the property's name to set may be a necessary input in the property name argument. The value is a parameter that can be optionally specified and comprises a string that denotes the updated value. A string that indicates whether or whether the priority of the property should be set to important is contained in the priority argument, which is an optional input.
Syntax
function myFunction() { var x = document.getElementById("box").style; x.setProperty("border-left-width", "30px"); }
The box element is fetched through the getElementById() method. The setProperty() attribute is used to set the width of the left border to 30 px.
Example
In this example, we have created a box with some mandatory parameters. The buttons are created to change the property value of the border. The setProperty() attribute is used to set the width of the left border to different values. Clicking the "Length border" button changes the border values to 30px. Similarly, when the "Thick border" and "Medium Border" buttons are clicked, the border value changes to thick or medium.
<html> <head> <style> #box { border: thin solid grey; background-color: lightpink; width: 550px; height: 200px; } </style> </head> <body> <h3> Set the width of the left border using <i> setProperty </i> attribute </h3> <p> Choose a button to select the property value: </p> <button type="button" onclick="myFunction()">Length border </button> <br> <button type="button" onclick="myFunction1()"> Thick border </button> <br> <button type="button" onclick="myFunction2()"> Medium border </button> <br> <div id="box"> The left border width of this box is changed. </div> <script> function myFunction() { var x = document.getElementById("box").style; x.setProperty("border-left-width", "30px"); } function myFunction1() { var x = document.getElementById("box").style; x.setProperty("border-left-width", "thick"); } function myFunction2() { var x = document.getElementById("box").style; x.setProperty("border-left-width", "medium"); } </script> </body> </html>
In this tutorial, we have seen two ways to set the width of the left border with JavaScript. The first method is to use the border left-width property. The second way is to use the setProperty method for the width of the left border.
- Related Articles
- How to set the width of the right border with JavaScript?
- How to set the width of the bottom border with JavaScript?
- How to set the width of the top border with JavaScript?
- How to set the color of the left border with JavaScript?
- How to set the style of the left border with JavaScript?
- Set the width of the left border using CSS
- How to set the width of an element's border with JavaScript?
- How to set the shape of the border of the top-left corner with JavaScript?
- How to set border width, border style, and border color in one declaration with JavaScript?
- Set the width of image border with CSS
- Set the border image width with CSS
- How to set all the border left properties in one declaration with JavaScript?
- With JavaScript how to change the width of a table border?
- Set border width with CSS
- How to set the style of the top border with JavaScript?
