html - How can I use the exact width of wrapped text rather than that of the full container? -


i have text constrained particular width, wraps. want display border around wrapped text. when add border, wraps text plus unused space around it. there way apply border just text? here's illustration of mean: have drawn red line want right-hand edge of element be:

enter image description here

html:

  <div>     supercalifragilistic expialidocious   </div> 

css:

div {   max-width: 200px;   display: inline-block;   border: 1px solid black; } 

jsfiddle

could work short text. combining attr() , pseudo.

.outer {    width: 200px;    background-color: #ccc;      border: 1px solid black;    display: inline-block;  }  .inner {          display: inline;    position:relative;  }  .inner:before {    content: attr(data-text) " ";    color: transparent;    position: absolute;    left:0;top:0;    border: 1px solid red;    pointer-events: none;  }
<div class="outer">    <div class="inner" data-text="supercalifragilistic expialidocious">supercalifragilistic expialidocious</div>  </div>

edit: absolute positioning inside inline elements & limitations of above example.

from css2.1 specs 10.1.4: if element absolute positioned inside inline element spanning multiple lines, containing block undefined.

by defining top , left positions,we browsers align pseudo corresponding top , left edges of first box created 'ancestor'. definition, pseudo virtual child of selected element , absolute looks nearest positioned ancestor. though inline rule points no containing block, position rule works in favour top/left sides.

we allow pseudo take height computed content/intrinsic dimension. since attr() pulls same content, more or less overlaps on text in ancestor. (when without font inconsistencies).

but note, while may work single line text dependent on white-space/wrap/font , not consistent across browsers when dealing multi-line inline elements.

here mean.

.outer {    width: 200px;    background-color: #ccc;    border: 1px solid black;    margin-bottom: 30px;    font-family: monospace;  }  .inner {    color: black;    position: relative;  }  .inner:after {    content: attr(data-text) " ";      position: absolute;    left:-1px;top:-1px;right:-1px; /* compensating border */    pointer-events: none;    color: transparent;    border: 1px solid red;  }
<div class="outer"><span class="inner" data-text="single line inline">single line inline</span></div><div class="outer"><span class="inner" data-text="this multi-line inline element may not hold absolute positioned pseudo element. fails in chrome.">this multi-line inline element may not hold absolute positioned pseudo element. fails in chrome.</span></div>


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -