英文:
Two columns over full width with left-aligned content, second column aligned at right page border
问题
我目前正在格式化一本电子书,并遇到了一个小问题。假设要实现以下所需的布局:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur vehicula,
ipsum accumsan sollicitudin mollis, leo est porttitor lacus, id facilisis
tellus lacus blandit metus. Aenean imperdiet in eros facilisis mollis.
Great-grandson of Alexios III Son of Michael VIII
Son and co-ruler of Andronikos II Son of Michael IX
Son of Andronikos III Son of John V
Aliquam quam eros, eleifend vel erat ut, eleifend laoreet quam. Phasellus
euismod ex pellentesque lacus auctor, malesuada efficitur nunc pharetra.
Aliquam ut condimentum neque. Suspendisse ornare nibh non nisi pharetra,
ac porttitor sapien venenatis. Suspendisse potenti.
文本两端对齐,两列,都包含左对齐的文本,但第二列对齐到右页边。
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur vehicula,
ipsum accumsan sollicitudin mollis, leo est porttitor lacus, id facilisis
tellus lacus blandit metus. Aenean imperdiet in eros facilisis mollis.</p>
<table>
<tr>
<td>Great-grandson of Alexios III</td>
<td>Son of Michael VIII</td>
</tr>
<tr>
<td>Son and co-ruler of Andronikos II</td>
<td>Son of Michael IX</td>
</tr>
<tr>
<td>Son of Andronikos III</td>
<td>Son of John V</td>
</tr>
</table>
<p>Aliquam quam eros, eleifend vel erat ut, eleifend laoreet quam. Phasellus
euismod ex pellentesque lacus auctor, malesuada efficitur nunc pharetra.
Aliquam ut condimentum neque. Suspendisse ornare nibh non nisi pharetra,
ac porttitor sapien venenatis. Suspendisse potenti.</p>
p {
text-align: justify;
}
table {
width: 100%;
}
table td {
white-space: nowrap;
}
table tr > td:first-child {
width: 100%;
}
然而,当字体大小增加和/或显示宽度减小时,这种方法存在一个严重的问题。内容的nowrap
属性会导致在这些情况下无法阅读,因为行永远不会换行,而是开始重叠在一起,或者被推出边界。
是否有一种解决方案,可以实现这种布局,同时可以包含文本的换行,以适应更大的字体和较小的显示器,在电子书阅读器上工作?理想情况下,它还应该保持项目的顺序(从左到右,从上到下),以便文本转语音软件可以正确处理,而不仅仅是视觉上的呈现。
英文:
I'm currently formatting an ebook and hit a slight problem. Assume the following wanted layout:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur vehicula,
ipsum accumsan sollicitudin mollis, leo est porttitor lacus, id facilisis
tellus lacus blandit metus. Aenean imperdiet in eros facilisis mollis.
Great-grandson of Alexios III Son of Michael VIII
Son and co-ruler of Andronikos II Son of Michael IX
Son of Andronikos III Son of John V
Aliquam quam eros, eleifend vel erat ut, eleifend laoreet quam. Phasellus
euismod ex pellentesque lacus auctor, malesuada efficitur nunc pharetra.
Aliquam ut condimentum neque. Suspendisse ornare nibh non nisi pharetra,
ac porttitor sapien venenatis. Suspendisse potenti.
Text is justified, two columns, both contain left-aligned text but the second column is aligned at the right page border.
Here is a demonstration of my current solution using a table:
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur vehicula,
ipsum accumsan sollicitudin mollis, leo est porttitor lacus, id facilisis
tellus lacus blandit metus. Aenean imperdiet in eros facilisis mollis.</p>
<table>
<tr>
<td>Great-grandson of Alexios III</td>
<td>Son of Michael VIII</td>
</tr>
<tr>
<td>Son and co-ruler of Andronikos II</td>
<td>Son of Michael IX</td>
</tr>
<tr>
<td>Son of Andronikos III</td>
<td>Son of John V</td>
</tr>
</table>
<p>Aliquam quam eros, eleifend vel erat ut, eleifend laoreet quam. Phasellus
euismod ex pellentesque lacus auctor, malesuada efficitur nunc pharetra.
Aliquam ut condimentum neque. Suspendisse ornare nibh non nisi pharetra,
ac porttitor sapien venenatis. Suspendisse potenti.</p>
p {
text-align: justify;
}
table {
width: 100%;
}
table td {
white-space: nowrap;
}
table tr > td:first-child {
width: 100%;
}
However, that has the serious downside of behaving very poorly when the font-size increases and/or the display width decreases. The nowrap
of the content makes it impossible to read in these circumstances as the lines will never be wrapped and instead start to overlap each other and/or getting pushed out-of-bounds.
Is there a solution to have such a layout but with wrapping of the content to accommodate larger fonts and smaller displays which will work with ebook readers? Ideally it should also preserve the order of items (left-right, top-bottom) for text-to-speech software, not just visually.
答案1
得分: 1
想象一下这样做。这样他们将为“文本转语音”软件保留顺序,并使其可读,无需横向滚动。
p {
text-align: justify;
}
table {
width: 100%;
}
table td {
white-space: nowrap;
}
table tr>td:first-child {
width: 100%;
}
@media only screen and (max-width: 400px) {
tr, td {
display: block;
}
tr {
margin-bottom: 10px;
}
.value::before {
content: "- ";
padding-left: 10px;
}
}
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur vehicula, ipsum accumsan sollicitudin mollis, leo est porttitor lacus, id facilisis tellus lacus blandit metus. Aenean imperdiet in eros facilisis mollis.</p>
<table>
<tr>
<td>Great-grandson of Alexios III</td>
<td class="value">Son of Michael VIII</td>
</tr>
<tr>
<td>Son and co-ruler of Andronikos II</td>
<td class="value">Son of Michael IX</td>
</tr>
<tr>
<td>Son of Andronikos III</td>
<td class="value">Son of John V</td>
</tr>
</table>
<p>Aliquam quam eros, eleifend vel erat ut, eleifend laoreet quam. Phasellus euismod ex pellentesque lacus auctor, malesuada efficitur nunc pharetra. Aliquam ut condimentum neque. Suspendisse ornare nibh non nisi pharetra, ac porttitor sapien venenatis. Suspendisse potenti.</p>
英文:
Imagine something like that. That way they will preserve the order for the text-to-speech
software and will make it readable without the need for side-scrolling.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
p {
text-align: justify;
}
table {
width: 100%;
}
table td {
white-space: nowrap;
}
table tr>td:first-child {
width: 100%;
}
@media only screen and (max-width: 400px) {
tr, td {
display: block;
}
tr {
margin-bottom: 10px;
}
.value::before {
content: "- ";
padding-left: 10px;
}
}
<!-- language: lang-html -->
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur vehicula, ipsum accumsan sollicitudin mollis, leo est porttitor lacus, id facilisis tellus lacus blandit metus. Aenean imperdiet in eros facilisis mollis.</p>
<table>
<tr>
<td>Great-grandson of Alexios III</td>
<td class="value">Son of Michael VIII</td>
</tr>
<tr>
<td>Son and co-ruler of Andronikos II</td>
<td class="value">Son of Michael IX</td>
</tr>
<tr>
<td>Son of Andronikos III</td>
<td class="value">Son of John V</td>
</tr>
</table>
<p>Aliquam quam eros, eleifend vel erat ut, eleifend laoreet quam. Phasellus euismod ex pellentesque lacus auctor, malesuada efficitur nunc pharetra. Aliquam ut condimentum neque. Suspendisse ornare nibh non nisi pharetra, ac porttitor sapien venenatis. Suspendisse potenti.</p>
<!-- end snippet -->
To test it in the code snippet you can resize the window to be more narrow on desktop or open it on mobile.
答案2
得分: 0
我认为你不必使用表格来完成这个任务。你可以简单地这样做:
<div class="container">
<div class="top-section">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur vehicula,
ipsum accumsan sollicitudin mollis, leo est porttitor lacus, id facilisis
tellus lacus blandit metus. Aenean imperdiet in eros facilisis mollis.
</p>
</div>
<div class="middle-section">
<div class="left">
<ul>
<li>Great-grandson of Alexios III</li>
<li>Son and co-ruler of Andronikos II</li>
<li>Son of Andronikos III</li>
</ul>
</div>
<div class="right">
<ul>
<li>Son of Michael VIII</li>
<li>Son of Michael IX</li>
<li>Son of John V</li>
</ul>
</div>
</div>
<div class="bottom-section">
<p>
Aliquam quam eros, eleifend vel erat ut, eleifend laoreet quam. Phasellus
euismod ex pellentesque lacus auctor, malesuada efficitur nunc pharetra.
Aliquam ut condimentum neque. Suspendisse ornare nibh non nisi pharetra,
ac porttitor sapien venenatis. Suspendisse potenti.
</p>
</div>
</div>
.container {
width: 50%;
margin: auto;
}
.middle-section {
display: flex;
justify-content: space-between;
align-items: center;
}
ul {
margin: 0;
padding: 0;
}
li {
margin: 0;
padding: 0;
list-style: none;
}
然后你会得到这样的效果:
英文:
I don't think you have to use a table to do this. You can simply do this:
<div class="container">
<div class="top-section">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur vehicula,
ipsum accumsan sollicitudin mollis, leo est porttitor lacus, id facilisis
tellus lacus blandit metus. Aenean imperdiet in eros facilisis mollis.
</p>
</div>
<div class="middle-section">
<div class="left">
<ul>
<li>Great-grandson of Alexios III</li>
<li>Son and co-ruler of Andronikos II</li>
<li>Son of Andronikos III</li>
</ul>
</div>
<div class="right">
<ul>
<li>Son of Michael VIII</li>
<li>Son of Michael IX</li>
<li>Son of John V</li>
</ul>
</div>
</div>
<div class="bottom-section">
<p>
Aliquam quam eros, eleifend vel erat ut, eleifend laoreet quam. Phasellus
euismod ex pellentesque lacus auctor, malesuada efficitur nunc pharetra.
Aliquam ut condimentum neque. Suspendisse ornare nibh non nisi pharetra,
ac porttitor sapien venenatis. Suspendisse potenti.
</p>
</div>
</div>
.container {
width: 50%;
margin: auto;
}
.middle-section {
display: flex;
justify-content: space-between;
align-items: center;
}
ul {
margin: 0;
padding: 0;
}
li {
margin: 0;
padding: 0;
list-style: none;
}
And you get this:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论