Skip to content
Related Articles

Related Articles

HTML | DOM Style justifyContent Property

View Discussion
Improve Article
Save Article
  • Last Updated : 08 Aug, 2022
View Discussion
Improve Article
Save Article

The style justifyContent property in HTML DOM is used to align the items horizontally when they are not able to use all the available space. It is used to set the position of the element. By default, the items are positioned at the beginning of the container. 

Syntax:

  • It returns the justifyContent property.
object.style.justifyContent 
  • It used to set the justifyContent property.
object.style.justifyContent = "flex-start|flex-end|center|
space-between|space-around|initial|inherit"

Property Values:

  • flex-start: It is used to align flex items from the start of the container.
  • flex-end: It is used to align flex items at the end of the container.
  • center: It align flex items at the center of container.
  • space-between: The flex items are placed with even spacing where the item is pushed to start and last item is pushed to end.
  • space-around: The flex items are placed with equal spacing from each others, the corners.
  • space-evenly: The items are positioned with equal spacing between them but the spacing from corners differ.
  • initial: The items are placed according to the default value.
  • inherit: The items are placed according to its inherited parent element value.

Return Value: It returns a string that represents the justifyContent property of an element. 

Example 1: This example describes space-between property value. 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        DOM Style justifyContent Property
    </title>
     
    <style>
        #GFG {
            width:500px;
            display:flex;
            height: 80px;
            border:2px solid black;
            background-color:white;
            justify-content: space-around;
        }
         
        #GFG div {
            width: 60px;
            height: 80px;
        }
    </style>
</head>
     
<body>
    <center>
        <h1 style = "color:green;width:40%;">
            GeeksForGeeks
        </h1>
         
        <h2>DOM Style justifyContent Property </h2>        
         
        <div id = "GFG">
            <div>
                <img src =
                width = "60px" height = "80px">
            </div>
             
            <div>
                <img src =
                width = "60px" height = "80px">
            </div>
             
            <div>
                <img src =
                width = "60px" height = "80px">
            </div>
             
            <div>
                <img src =
                width = "60px" height = "80px">
            </div>
             
        </div>
         
        <button type = "button" onclick = "geeks()">
            Submit
        </button>
         
        <!-- Style script used here -->
        <script>
            function geeks() {
            document.getElementById("GFG").style.justifyContent= "space-between";
            }
        </script>
    </center>
</body>
 
</html>                   


Output: 

Before Click on the button: 

 

After Click on the button:

  

Example 2: This example describes flex-start property value. 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        DOM Style justifyContent Property
    </title>
     
    <style>
        #GFG {
            width:500px;
            display:flex;
            height: 80px;
            border:2px solid black;
            background-color:white;
            justify-content: space-around;
        }
         
        #GFG div {
            width: 60px;
            height: 80px;
        }
    </style>
</head>
     
<body>
    <center>
        <h1 style = "color:green;width:40%;">
            GeeksForGeeks
        </h1>
         
        <h2>DOM Style justifyContent Property </h2>        
         
        <div id = "GFG">
            <div>
                <img src =
                width = "60px" height = "80px">
            </div>
             
            <div>
                <img src =
                width = "60px" height = "80px">
            </div>
             
            <div>
                <img src =
                width = "60px" height = "80px">
            </div>
             
            <div>
                <img src =
                width = "60px" height = "80px">
            </div>
             
        </div>
         
        <button type = "button" onclick = "geeks()">
            Submit
        </button>
         
        <!-- Style script used here -->
        <script>
        function geeks() {
            document.getElementById("GFG").style.justifyContent= "flex-start";
        }
        </script>
    </center>
</body>
 
</html>                   


Output: 

Before Click on the button: 

 

After Click on the button: 

 

Supported Browsers: The browser supported by DOM Style justifyContent property are listed below:

  • Google Chrome 29
  • Edge 12
  • Internet Explorer 11
  • Firefox 20
  • Opera 12.1
  • Safari 9

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!