# TextWidth is not the correct width!

**URL:** https://discourse.processing.org/t/textwidth-is-not-the-correct-width/39158
**Category:** Beginners
**Created:** [October 8, 2022, 11:23am UTC](https://discourse.processing.org/t/textwidth-is-not-the-correct-width/39158 "2022-10-08T11:23:04Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Lissie](https://avatars.discourse-cdn.com/v4/letter/l/f19dbf/32.png) [@Lissie](https://discourse.processing.org/u/Lissie)
#### Post date: [October 8, 2022, 11:23am UTC](https://discourse.processing.org/t/textwidth-is-not-the-correct-width/39158/1 "2022-10-08T11:23:04Z")

</div>

If i create a normal line, with “line(startX, startY, startX+textWidth(STRING), startY)” the line is not the correct width. I fixed it by making a “rect()” with the y size on “0.5”!

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [October 8, 2022, 3:44pm UTC](https://discourse.processing.org/t/textwidth-is-not-the-correct-width/39158/2 "2022-10-08T15:44:26Z")

</div>

Hello @Lissie  
Welcome the forum. 😁

Please correct the spelling error in title of your post so that it will be searchable for others with the same question.

**I believe “with” should be “width”.**

Also, it is not necessary to add clickbait verbiage. The extra wordage takes extra effort to scan the topics.  
**Kindly remove “if you have the same prob. read this!”**  
This forum is not a YouTube channel… 🙃 🙂

🤓

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [October 8, 2022, 4:28pm UTC](https://discourse.processing.org/t/textwidth-is-not-the-correct-width/39158/3 "2022-10-08T16:28:54Z")

</div>

Something to do with having to set textSize beforehand…

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [October 8, 2022, 5:41pm UTC](https://discourse.processing.org/t/textwidth-is-not-the-correct-width/39158/4 "2022-10-08T17:41:45Z")

</div>

I have edited the title for this discussion.

---

<div class="post-metadata">

### Author: ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)
#### Post date: [October 8, 2022, 7:03pm UTC](https://discourse.processing.org/t/textwidth-is-not-the-correct-width/39158/5 "2022-10-08T19:03:59Z")

</div>

`textWidth()` includes glyph whitespace (aka padding) for every character. Ideally you’d want to ignore this for the first and last characters.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [October 8, 2022, 9:56pm UTC](https://discourse.processing.org/t/textwidth-is-not-the-correct-width/39158/6 "2022-10-08T21:56:06Z")

</div>

In my experience, use createFont and textFont and textSize before textWidth

---

<div class="post-metadata">

### Author: ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)
#### Post date: [October 9, 2022, 1:37am UTC](https://discourse.processing.org/t/textwidth-is-not-the-correct-width/39158/7 "2022-10-09T01:37:41Z")

</div>

Yes, at least three criteria need to be established to create a context in order for [`textWidth()`](https://p5js.org/reference/#/p5/textWidth) to operate properly. We need a font, a text size, and of course a string. If you don’t set the first two of those criteria explicitly, some default settings will prevail. See the following p5.js code and output:

```auto
function setup() {
  createCanvas(400, 400);
  noLoop();
}

function draw() {
  background(220);
  textSize(24);
  textFont('Helvetica');
  print(textWidth('Jabberwocky'));
  textSize(48);
  print(textWidth('Jabberwocky'));
  textFont('Verdana');
  print(textWidth('Jabberwocky'));
  print(textWidth('Fiddlesticks'));
}

```

Output:

```
140.0625
280.125
309.7734375
277.8515625

```
