0

I have an editable span with rounded corners as shown in the picture below. As you can see, the text runs off the corner and I would love a way to keep the text inside the box. is there a good way to do this? I don't want to add padding that goes all the way down the sides or across the top, I really just want to avoid the corners with border-radii. Here is a JSFiddle demonstrating the current state.

Thanks! Text overflowing the span's border

Current CSS:

#container {
  box-sizing: border-box;
    position: absolute;
    transition: border 0.2s;
    border: 2px solid rgba(128, 223, 255, 0);
    border-radius: 7px;
    display: table-row;
  height: 200px;
  width: 300px;
}

.typeSpace {
  background-color: green;
    display: inline;
    position: absolute;
    bottom: 0;
    text-align:center;
  white-space: nowrap;
    left: 10px;
    right: 10px;
    
    overflow: auto;
    -ms-overflow-style: none;
    overflow-y: hidden;
  text-align: left;
}

#borderText {
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    font-size: 17px);
    white-space: normal;
    line-height: 15px);
    border-radius: 20px 20px 0 0;
}
4
  • Create a fiddle to get help better.
    – Abu Nooh
    Commented Jul 27, 2022 at 6:49
  • add some padding , that should help
    – G-Cyrillus
    Commented Jul 27, 2022 at 7:01
  • I've added a JSFiddle and some explanation as to why I don't want to use padding. Let me know if there is a better way to use padding that I don't know about!
    – klaytonme
    Commented Jul 27, 2022 at 7:14
  • For the padding, besides box-sizing that can modify how it is applied, it is quiet simple and here is not much tricks around it. You may also take a look at text-indent . example with your fiddle jsfiddle.net/rdsx6174 (floatting pseudo(or not) element could also do the trick jsfiddle.net/rdsx6174/1 ) shape outside can also be an hint codepen.io/gc-nomade/pen/vwqKyR
    – G-Cyrillus
    Commented Jul 27, 2022 at 9:02

1 Answer 1

0

So what you want this to look like, is something like this? _ representing automatic "spacing".

_XX XXX X_
XXX XXX XX
X XXXX XXX

This could be done by inserting two elements into the span before the text, and then floating one to the left, and one to the right.

Only, your span is editable here (was that supposed to be contenteditable, or are you using some other mechanism? Either way, shouldn't matter) - so those would be likely to be removed by the user, when they modify the text content.

Okay, pseudo elements to the rescue! But wait, only ::before would actually come before the text, ::after would come behind it - and behind doesn't do us any good, when it comes to floating.

But we can use the ::before pseudo element to generate the space on the right - and use text-indent for that on the left.

#container {
  box-sizing: border-box;
  position: absolute;
  transition: border 0.2s;
  border: 2px solid rgba(128, 223, 255, 0);
  border-radius: calc(var(--scale)*7px);
  display: table-row;
  height: 200px;
  width: 300px;
}

.typeSpace {
  background-color: green;
  display: inline;
  position: absolute;
  bottom: 0;
  text-align: center;
  white-space: nowrap;
  left: calc(var(--scale) * 10px);
  right: calc(var(--scale) * 10px);

  overflow: auto;
  -ms-overflow-style: none;
  overflow-y: hidden;
  text-align: left;
  text-indent: 1em; /* provides the spacing on the left */
}

#borderText {
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  font-size: calc(var(--scale) * 17px);
  white-space: normal;
  line-height: calc(var(--scale) * 15px);
  border-radius: 20px 20px 0 0;
}

/* provides the spacing on the right */
.typeSpace::before {
  content: "";
  float: right;
  width: 1em;
}
<div id="container">
  <span class="typeSpace" id="borderText" editable>I wish I could see the corners of this text right here</span>
</div>

Of course this will only work for the top corners - if you needed the same thing at the bottom, this method won't do.

Using CSS Shapes would probably be another way to do it - but this one here is easier, I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.