/

HTML / CSS / JavaScript Tutorial

CSS Property: justify-content

[this page | pdf | back links]

The CSS (CSS3) justify-content property indicates how to align a flexible container’s items when the items do not use all available space along the horizontal axis.

 

Valid property values (other than inherit and initial) are:

 

Value

Description

center

Items are centred

flex-start

(default value). Items positioned at start of container

flex-end

Items positioned at end of container

space-around

Items positioned with space before, between and after lines

space-between

Items positioned with space between lines

 

Default Value:

flex-start

JavaScript syntax:

e.g. object.style.justifyContent="space-around"

Inherited:

No

Animatable:

Yes

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
.x1 {display: flex; width: 100px; border: 2px solid red; justify-content: center;}
</style>
</head>
<body>
1. Element with default property<br>
<div style="display: flex; width: 100px; border: 2px solid red;" >
<div>A</div><div>B</div><div>C</div>
</div>
<br>
2. Element set using in-file HTML style<br>
<div class="x1">
<div>A</div><div>B</div><div>C</div>
</div>
<br>
3. Element set using JavaScript<br>
<div id="x2">
<div>A</div><div>B</div><div>C</div>
</div>
<br>

<script>
document.getElementById("x2").style.display = "flex";
document.getElementById("x2").style.width = "100px";
document.getElementById("x2").style.border = "2px solid red";
document.getElementById("x2").style.justifyContent = "center";
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedCSSPropertyJustifyContent() {
  var x = document.createElement("DIV"); x.style.justifyContent = "center"; return (window.getComputedStyle(x, null).justifyContent == "center");
}


NAVIGATION LINKS
Contents | Prev | Next | CSS Properties


Desktop view | Switch to Mobile