2

I want to have a content-editable <span> whose width grow as we type and whose text is centered. I would like a solution that does not imply Javascript if possible.

As you can see, the text-align property doesn't do anything:

#item {
  border: solid;
  text-align: center;
}
<span id="item" contenteditable="true">Hello</span>

How should I do?

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
JacopoStanchi
  • 1,962
  • 5
  • 33
  • 61

4 Answers4

1

You need to make the span inline-block:

#item {
  border: solid;
  text-align: center;
  display:inline-block;
}
<span id="item" contenteditable="true">Hello some text and more teeeexxt some text and more teeeexxt some text and more teeeexxt some text and more teeeexxt some text</span>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
0

This is a possible solution:

#container {
margin-left: auto;
margin-right: auto;
height: 100px;
width: 100px;
position: relative;
}

#item {
  border: solid;
  text-align: center;
  
}
<div id="container"><span id="item" contenteditable="true">Hello</span></div>

But of course, I wrapped it up in a div to tidy it up a bit

0

try to add "display: block;" in css.

#item {
  display: block;
  border: solid;
  text-align: center;
}
xin chung
  • 23
  • 2
  • 9
0

You just need wrap your span with a div and give it text-align: center style to achieve what you wanted.

#item {
  border: solid;
}

.text-center {
  text-align: center;
}
<div class="text-center">
  <span id="item" contenteditable="true">Hello</span>
</div>
Salvatore
  • 1,435
  • 1
  • 10
  • 21