使具有换行的文本的下划线宽度与父div的宽度相同。

huangapple go评论62阅读模式
英文:

Having a text with line breaks, make it's underline width the same as parent's div width

问题

以下是翻译好的部分:

我正在尝试创建一个类似于填写输入的纸质文档的布局。用户输入的所有数据行都应具有相同宽度的虚线下划线。

以下是使用 text-decoration 的解决方案几乎可以,但是虚线在字母结束时结束。

这是我需要它看起来像的样子:

使具有换行的文本的下划线宽度与父div的宽度相同。

.fillable {
  text-decoration: underline dotted from-font;
  text-underline-position: under;
  word-break: break-word;
  overflow-wrap: break-word;
  vertical-align: top;
  hyphens: auto;
  -webkit-hyphens: auto;
  -webkit-hyphenate-character: '-';
}
<div style="font-family: times new roman, serif; max-width: 250mm">
  <div style="border: 3px solid black;">
    <div style="margin: 1px;">

      <div class="fillable" style="min-height: 3rem; width: 50%; margin-left: 0.25rem; display: flex; display: -webkit-box; -webkit-box-align: end;">
        <div style="align-self: flex-end; font-size: 1.30rem;">B_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA_B</div>
      </div>

      <div style="width: 50%; margin-left: 0.25rem; font-size: 0.8rem;">
        (some text some text some text some text some text)
      </div>
      <div class="fillable" style="min-height: 1.5rem; width: 57%; margin-left: 0.25rem; display: flex; display: -webkit-box; -webkit-box-align: end;">
        <div style="align-self: flex-end; font-size: 1.30rem;">B_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA_B</div>
      </div>
    </div>
    <div style="width: 57%; margin-left: 0.25rem; font-size: 0.8rem;">
      (some text some text)
    </div>
  </div>
</div>
英文:

I'm trying to create layout that looks like the paper document with fillable inputs. All the rows with data entered by user should have dotted underline OF THE SAME WIDTH.

The solution below with text-decoration is almost ok, but the dotted lines end when the letters end.

Here's what I need it to look like:

使具有换行的文本的下划线宽度与父div的宽度相同。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.fillable {
  text-decoration: underline dotted from-font;
  text-underline-position: under;
  word-break: break-word;
  overflow-wrap: break-word;
  vertical-align: top;
  hyphens: auto;
  -webkit-hyphens: auto;
  -webkit-hyphenate-character: &#39;-&#39;;
}

<!-- language: lang-html -->

&lt;div style=&quot;font-family: times new roman, serif; max-width:250mm&quot;&gt;
  &lt;div style=&quot;border: 3px solid black;&quot;&gt;
    &lt;div style=&quot;margin:1px;&quot;&gt;

      &lt;div class=&quot;fillable&quot; style=&quot;min-height:3rem; width:50%; margin-left:0.25rem; display: flex; display: -webkit-box; -webkit-box-align:end;&quot;&gt;
        &lt;div style=&quot;align-self: flex-end; font-size:1.30rem;&quot;&gt;B_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA_B&lt;/div&gt;
      &lt;/div&gt;

      &lt;div style=&quot;width:50%; margin-left:0.25rem; font-size:0.8rem&quot;&gt;
        (some text some text some text some text some text)
      &lt;/div&gt;
      &lt;div class=&quot;fillable&quot; style=&quot;min-height:1.5rem; width:57%; margin-left:0.25rem; display: flex; display: -webkit-box;-webkit-box-align:end;&quot;&gt;
        &lt;div style=&quot;align-self: flex-end; font-size:1.30rem;&quot;&gt;B_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA_B&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div style=&quot;width:57%; margin-left:0.25rem; font-size:0.8rem&quot;&gt;
      (some text some text)
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

文本装饰不会按照你想要的方式起作用,你需要找到一个创造性的解决方案。

也许类似这样,它展示了所需的行为,控制文本的行高,然后为每一行应用不同的 div 元素,对于这种方法,行高是关键。

.text-container {
  border-right: 1px dotted black;
  width: 300px;
  font-size: 16px;
  line-height: 16px;
  position: relative;
  margin-bottom: 30px;
}

.text {
  position: relative;
  z-index: 2;
}

.underlines {
  position: absolute;
  z-index: 1;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.line {
  width: 100%;
  border-bottom: 1px dotted black;
}
const lineHeight = 16;
const textContainers = document.querySelectorAll(".text-container");
const textContainersLength = textContainers.length;

for (let x = 0; x < textContainersLength; x++) {
  const underlines = textContainers[x].querySelector(".underlines");
  const lines = Math.ceil(textContainers[x].clientHeight / lineHeight);

  const line = document.createElement("div");
  line.style.paddingTop = (lineHeight - 1) + "px";
  line.classList.add("line");
  underlines.innerHTML = ""; // clean
  for (let y = 0; y < lines; y++) {
    underlines.appendChild(line.cloneNode());
  }
}
<div class="text-container">
  <div class="underlines"></div>
  <div class="text">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
    dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
    ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
    nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
    anim id est laborum.
  </div>
</div>
英文:

Text decoration doesn't work in the way you are looking for, you will need to found a creative solution

Maybe something like this, it shows the desired beahviour, controlling the line height of the text, and then applying different divs for every line, for this method line-height is everything

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const lineHeight = 16;
const textContainers = document.querySelectorAll(&quot;.text-container&quot;);
const textContainersLength = textContainers.length;

for (let x = 0; x &lt; textContainersLength; x++) {
  const underlines = textContainers[x].querySelector(&quot;.underlines&quot;);
  const lines = Math.ceil(textContainers[x].clientHeight / lineHeight);

  const line = document.createElement(&quot;div&quot;);
  line.style.paddingTop = (lineHeight - 1) + &quot;px&quot;;
  line.classList.add(&quot;line&quot;);
  underlines.innerHTML = &quot;&quot;; // clean
  for (let y = 0; y &lt; lines; y++) {
    underlines.appendChild(line.cloneNode());
  }
}

<!-- language: lang-css -->

.text-container {
  border-right: 1px dotted black;
  width: 300px;
  font-size: 16px;
  line-height: 16px;
  position: relative;
  margin-bottom: 30px;
}

.text {
  position: relative;
  z-index: 2;
}

.underlines {
  position: absolute;
  z-index: 1;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.line {
  width: 100%;
  border-bottom: 1px dotted black;
}

<!-- language: lang-html -->

&lt;div class=&quot;text-container&quot;&gt;
  &lt;div class=&quot;underlines&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;text&quot;&gt;
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
    dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
    ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
    nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
    anim id est laborum.
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;text-container&quot;&gt;
  &lt;div class=&quot;underlines&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;text&quot;&gt;
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
    dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
    ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
    nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
    anim id est laborum.
  &lt;/div&gt;
&lt;/div&gt;


&lt;div class=&quot;text-container&quot;&gt;
  &lt;div class=&quot;underlines&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;text&quot;&gt;
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
    dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
    ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
    nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
    anim id est laborum.
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月15日 01:20:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定