The wordSpacing
property sets or gets the distance between words.
wordSpacing |
Yes | Yes | Yes | Yes | Yes |
Return the wordSpacing property:
object.style.wordSpacing
Set the wordSpacing property:
object.style.wordSpacing='normal|length|initial|inherit'
Value | Description |
---|---|
normal | Default value. Use normal distance between words. |
length | Use length units. Negative values are allowed |
initial | Set to default value |
inherit | Inherit from parent element. |
Default Value: | normal |
---|---|
Return Value: | A string representing the word-spacing property. |
CSS Version | CSS1 |
The following code shows how to set the space between words in a <p> element to 50 pixels:
<!DOCTYPE html>
<html>
<body>
<p id="myP">This is an example paragraph.</p>
<button type="button" onclick="myFunction()">test</button>
<script>
function myFunction() {<!-- w w w . ja va2 s .com-->
document.getElementById("myP").style.wordSpacing = "50px";
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to use negative values in word spacing.
<!DOCTYPE html>
<html>
<body>
<p id="myP">This is an example paragraph.</p>
<button type="button" onclick="myFunction()">test</button>
<script>
function myFunction() {<!-- w w w.j a va 2 s. c o m-->
document.getElementById("myP").style.wordSpacing = "-3px";
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to get the word spacing.
<!DOCTYPE html>
<html>
<body>
<p id="myP" style="word-spacing:10px;">This is an example paragraph.</p>
<button type="button" onclick="myFunction()">test</button>
<script>
function myFunction() {<!-- ww w. j ava 2 s. co m-->
console.log(document.getElementById("myP").style.wordSpacing);
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to set the letterSpacing and the wordSpacing.
<!DOCTYPE html>
<html>
<body>
<!-- w w w .j a v a 2s. c o m-->
<p id="myP1">This is an example paragraph.</p>
<p id="myP2">This is an example paragraph.</p>
<button type="button" onclick="changeLetters()">letter spacing</button>
<button type="button" onclick="changeWords()">word spacing</button>
<script>
function changeLetters() {
document.getElementById("myP1").style.letterSpacing = "15px";
}
function changeWords() {
document.getElementById("myP2").style.wordSpacing = "15px";
}
</script>
</body>
</html>
The code above is rendered as follows: