两列,整个宽度,左对齐内容,第二列对齐右页边。

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

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:

&lt;p&gt;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.&lt;/p&gt;

&lt;table&gt;
  &lt;tr&gt;
    &lt;td&gt;Great-grandson of Alexios III&lt;/td&gt;
    &lt;td&gt;Son of Michael VIII&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Son and co-ruler of Andronikos II&lt;/td&gt;
    &lt;td&gt;Son of Michael IX&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Son of Andronikos III&lt;/td&gt;
    &lt;td&gt;Son of John V&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;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.&lt;/p&gt;
p {
  text-align: justify;
}

table {
  width: 100%;
}

table td {
  white-space: nowrap;
}

table tr &gt; 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&gt;td:first-child {
  width: 100%;
}

@media only screen and (max-width: 400px) {
  tr, td {
    display: block;
  }
  
  tr {
    margin-bottom: 10px;
  }
  
  .value::before {
    content: &quot;- &quot;;
    padding-left: 10px;
  }
}

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

&lt;p&gt;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.&lt;/p&gt;

&lt;table&gt;
  &lt;tr&gt;
    &lt;td&gt;Great-grandson of Alexios III&lt;/td&gt;
    &lt;td class=&quot;value&quot;&gt;Son of Michael VIII&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Son and co-ruler of Andronikos II&lt;/td&gt;
    &lt;td class=&quot;value&quot;&gt;Son of Michael IX&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Son of Andronikos III&lt;/td&gt;
    &lt;td class=&quot;value&quot;&gt;Son of John V&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;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.&lt;/p&gt;

<!-- 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:

&lt;div class=&quot;container&quot;&gt;
        &lt;div class=&quot;top-section&quot;&gt;
            &lt;p&gt;
                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.
            &lt;/p&gt;
        &lt;/div&gt;

        &lt;div class=&quot;middle-section&quot;&gt;
            &lt;div class=&quot;left&quot;&gt;
                &lt;ul&gt;
                    &lt;li&gt;Great-grandson of Alexios III&lt;/li&gt;
                    &lt;li&gt;Son and co-ruler of Andronikos II&lt;/li&gt;
                    &lt;li&gt;Son of Andronikos III&lt;/li&gt;
                &lt;/ul&gt;
            &lt;/div&gt;
            &lt;div class=&quot;right&quot;&gt;
                &lt;ul&gt;
                    &lt;li&gt;Son of Michael VIII&lt;/li&gt;
                    &lt;li&gt;Son of Michael IX&lt;/li&gt;
                    &lt;li&gt;Son of John V&lt;/li&gt;
                &lt;/ul&gt;
            &lt;/div&gt;
        &lt;/div&gt;

        &lt;div class=&quot;bottom-section&quot;&gt;
            &lt;p&gt;
                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.
            &lt;/p&gt;
        &lt;/div&gt;
    &lt;/div&gt;
.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:

两列,整个宽度,左对齐内容,第二列对齐右页边。

huangapple
  • 本文由 发表于 2023年7月3日 18:45:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604000.html
匿名

发表评论

匿名网友

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

确定