英文:
Having a text with line breaks, make it's underline width the same as parent's div width
问题
以下是翻译好的部分:
我正在尝试创建一个类似于填写输入的纸质文档的布局。用户输入的所有数据行都应具有相同宽度的虚线下划线。
以下是使用 text-decoration
的解决方案几乎可以,但是虚线在字母结束时结束。
这是我需要它看起来像的样子:
.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:
<!-- 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: '-';
}
<!-- language: lang-html -->
<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>
<!-- 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(".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());
}
}
<!-- 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 -->
<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>
<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>
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论