使用CSS防止在两栏HTML报告中过早换行。

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

Prevent premature wrapping in 2-column HTML report using CSS

问题

我正在使用HTML和CSS构建一个两列的“报告”,然后通过Python中的Weasyprint将其打印成PDF。我的问题是第一列中的内容过早地换行到第二列,最终导致表格损坏,应该保持在一列中。

HTML文件调用了CSS文件:

  1. <html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <link href="report.css" rel="stylesheet">
  5. <title>Report</title>
  6. <meta name="description" content="Report example">
  7. </head>
  8. ...

在某个地方,我在CSS中创建了一个名为“satgeom”的页面样式:

  1. @page {
  2. @top-left {
  3. background: #FF874A;
  4. content: counter(page);
  5. height: 1cm;
  6. text-align: center;
  7. width: 1cm;
  8. }
  9. ...
  10. }
  11. html {
  12. color: #393939;
  13. font-family: Montserrat;
  14. font-size: 11pt;
  15. font-weight: 300;
  16. line-height: 1.5;
  17. }
  18. .column {
  19. display: flex;
  20. flex-direction: column;
  21. flex-basis: 100%;
  22. flex: 1;
  23. }
  24. #satgeom section {
  25. columns: 2;
  26. column-gap: 1cm;
  27. }

并在HTML中调用这个样式。这个页面的内容包括一个很长的表格和文本。我的问题是表格过早地换行,我无法找出原因。理想情况下,我希望在第一列填满后将文本换行到第二列。以下是“satgeom”页面的HTML片段:

  1. <article id="satgeom">
  2. <h2 id="satgeom-title">Satellite geometry</h2>
  3. <h3>Satellite geometry, depiction, and description</h3>
  4. <section>
  5. <img src="./satellite.png" alt="">
  6. <p>
  7. <table class="tg" style="table-layout: fixed; width: 300px">
  8. <colgroup>
  9. <col style="width: 150px">
  10. <col style="width: 150px">
  11. </colgroup>
  12. <tr>
  13. <td class="tg-zv4m">Name</th>
  14. <td class="tg-ofj5">Uydu</th>
  15. </tr>
  16. <!-- 更多表格行 -->
  17. </table>
  18. </p>
  19. <p>
  20. Launched in 2024, the Uydu satellite was manufactured by the Turkish
  21. Aerospace Industries for roughly $600,000,000. The satellite's mission
  22. is
  23. </p>
  24. <p>
  25. Construction-wise, the Uydu satellite comprises a main body and two
  26. solar panel arrays extending laterally to its side. For power consumption,
  27. the solar panels can be rotated to face the sun.
  28. </p>
  29. </section>
  30. </article>

我已经尝试过在我的CSS文件中添加div{}并调整了nowrap属性、修改了CSS文件,还进行了多次Google和Stack Overflow搜索,但没有找到解决方法。坦白说,我不确定自己是否在寻找正确的短语。

编辑:Stefan的答案在下面提供了我寻找的“棒球卡”解决方案。

英文:

I'm building a two-column "report" in HTML and CSS (I'm new to both) and printing it to a PDF via Weasyprint in Python. My problem is that content in the first column is wrapping into the second column prematurely, ultimately resulting in a broken table that should remain in one column:

使用CSS防止在两栏HTML报告中过早换行。

The HTML file calls the CSS file:

  1. &lt;html&gt;
  2. &lt;head&gt;
  3. &lt;meta charset=&quot;UTF-8&quot;&gt;
  4. &lt;link href=&quot;report.css&quot; rel=&quot;stylesheet&quot;&gt;
  5. &lt;title&gt;Report&lt;/title&gt;
  6. &lt;meta name=&quot;description&quot; content=&quot;Report example&quot;&gt;
  7. &lt;/head&gt;
  8. ...

at some point, I create a page style in CSS called "satgeom":

  1. @page {
  2. @top-left {
  3. background: #FF874A;
  4. content: counter(page);
  5. height: 1cm;
  6. text-align: center;
  7. width: 1cm;
  8. }
  9. @top-center {
  10. background: #FF874A;
  11. content: &#39;&#39;;
  12. display: block;
  13. height: .05cm;
  14. opacity: .5;
  15. width: 100%;
  16. }
  17. @top-right {
  18. content: string(heading);
  19. font-size: 9pt;
  20. height: 1cm;
  21. vertical-align: middle;
  22. width: 100%;
  23. }
  24. }
  25. @page :blank {
  26. @top-left { background: none; content: &#39;&#39; }
  27. @top-center { content: none }
  28. @top-right { content: none }
  29. }
  30. @page no-chapter {
  31. @top-left { background: none; content: none }
  32. @top-center { content: none }
  33. @top-right { content: none }
  34. }
  35. @page :first {
  36. background: url(report_cover.png) no-repeat center;
  37. background-size: cover;
  38. margin: 0;
  39. }
  40. @page chapter {
  41. background: #FF874A;
  42. margin: 0;
  43. @top-left { content: none }
  44. @top-center { content: none }
  45. @top-right { content: none }
  46. }
  47. html {
  48. color: #393939;
  49. font-family: Montserrat;
  50. font-size: 11pt;
  51. font-weight: 300;
  52. line-height: 1.5;
  53. }
  54. h1 {
  55. color: #FF874A;
  56. font-size: 38pt;
  57. margin: 5cm 2cm 0 2cm;
  58. page: no-chapter;
  59. width: 100%;
  60. }
  61. h2, h3, h4 {
  62. color: black;
  63. font-weight: 400;
  64. }
  65. h2 {
  66. break-before: always;
  67. font-size: 28pt;
  68. string-set: heading content();
  69. }
  70. h3 {
  71. font-weight: 300;
  72. font-size: 15pt;
  73. }
  74. h4 {
  75. font-size: 13pt;
  76. }
  77. .column {
  78. display: flex;
  79. flex-direction: column;
  80. flex-basis: 100%;
  81. flex: 1;
  82. }
  83. #satgeom section {
  84. columns: 2;
  85. column-gap: 1cm;
  86. }
  87. #satgeom section p {
  88. text-align: justify;
  89. }
  90. /* Table */
  91. .tg {border-collapse:collapse;border-spacing:0;}
  92. .tg td{border-color:black;border-style:solid;border-width:1px;word-break:normal;}
  93. .tg th{border-color:black;border-style:solid;border-width:1px;word-break:normal;}
  94. .tg .tg-zv4m{border-color:#fcbb9a;text-align:left;vertical-align:top}
  95. .tg .tg-ofj5{border-color:#fcbb9a;text-align:right;vertical-align:top}

and call this style in the HTML. The contents of this page contain a lengthy table and text. My problem is that the table is wrapping prematurely, and I cannot figure out why. Ideally, I would like to wrap the text into the second column after the first column fills up. A snippet of my HTML for the "satgeom" page is as follows:

  1. &lt;article id=&quot;satgeom&quot;&gt;
  2. &lt;h2 id=&quot;satgeom-title&quot;&gt;Satellite geometry&lt;/h2&gt;
  3. &lt;h3&gt;Satellite geometry, depiction, and description&lt;/h3&gt;
  4. &lt;section&gt;
  5. &lt;img src=&quot;./satellite.png&quot; alt=&quot;&quot;&gt;
  6. &lt;p&gt;
  7. &lt;table class=&quot;tg&quot; style=&quot;table-layout: fixed; width: 300px&quot;&gt;
  8. &lt;colgroup&gt;
  9. &lt;col style=&quot;width: 150px&quot;&gt;
  10. &lt;col style=&quot;width: 150px&quot;&gt;
  11. &lt;/colgroup&gt;
  12. &lt;tr&gt;
  13. &lt;td class=&quot;tg-zv4m&quot;&gt;Name&lt;/th&gt;
  14. &lt;td class=&quot;tg-ofj5&quot;&gt;Uydu&lt;/th&gt;
  15. &lt;/tr&gt;
  16. &lt;tr&gt;
  17. &lt;td class=&quot;tg-zv4m&quot;&gt;Cost [$]&lt;/th&gt;
  18. &lt;td class=&quot;tg-ofj5&quot;&gt;600,000,000&lt;/th&gt;
  19. &lt;/tr&gt;
  20. &lt;tr&gt;
  21. &lt;td class=&quot;tg-zv4m&quot;&gt;Manufacturer&lt;/td&gt;
  22. &lt;td class=&quot;tg-ofj5&quot;&gt;TAI&lt;/td&gt;
  23. &lt;/tr&gt;
  24. &lt;tr&gt;
  25. &lt;td class=&quot;tg-zv4m&quot;&gt;Duration [years]&lt;/td&gt;
  26. &lt;td class=&quot;tg-ofj5&quot;&gt;15&lt;/td&gt;
  27. &lt;/tr&gt;
  28. &lt;tr&gt;
  29. &lt;td class=&quot;tg-zv4m&quot;&gt;Orbit altitude [km]&lt;/td&gt;
  30. &lt;td class=&quot;tg-ofj5&quot;&gt;35,785&lt;/td&gt;
  31. &lt;/tr&gt;
  32. &lt;tr&gt;
  33. &lt;td class=&quot;tg-zv4m&quot;&gt;Max. velocity [km/s]&lt;/td&gt;
  34. &lt;td class=&quot;tg-ofj5&quot;&gt;11,051&lt;/td&gt;
  35. &lt;/tr&gt;
  36. &lt;tr&gt;
  37. &lt;td class=&quot;tg-zv4m&quot;&gt;Dy mass [kg]&lt;/td&gt;
  38. &lt;td class=&quot;tg-ofj5&quot;&gt;1,577&lt;/td&gt;
  39. &lt;/tr&gt;
  40. &lt;tr&gt;
  41. &lt;td class=&quot;tg-zv4m&quot;&gt;NORAD ID&lt;/td&gt;
  42. &lt;td class=&quot;tg-ofj5&quot;&gt; - &lt;/td&gt;
  43. &lt;/tr&gt;
  44. &lt;tr&gt;
  45. &lt;td class=&quot;tg-zv4m&quot;&gt;Uplink [GHz]&lt;/td&gt;
  46. &lt;td class=&quot;tg-ofj5&quot;&gt;7.3 - 18.10&lt;/td&gt;
  47. &lt;/tr&gt;
  48. &lt;tr&gt;
  49. &lt;td class=&quot;tg-zv4m&quot;&gt;Downlink [GHz]&lt;/td&gt;
  50. &lt;td class=&quot;tg-ofj5&quot;&gt;11.70 - 12.75&lt;/td&gt;
  51. &lt;/tr&gt;
  52. &lt;tr&gt;
  53. &lt;td class=&quot;tg-zv4m&quot;&gt;Reference frame&lt;/td&gt;
  54. &lt;td class=&quot;tg-ofj5&quot;&gt;Geocentric&lt;/td&gt;
  55. &lt;/tr&gt;
  56. &lt;tr&gt;
  57. &lt;td class=&quot;tg-zv4m&quot;&gt;Regime&lt;/td&gt;
  58. &lt;td class=&quot;tg-ofj5&quot;&gt;Geostationary&lt;/td&gt;
  59. &lt;/tr&gt;
  60. &lt;/table&gt;
  61. &lt;/p&gt;
  62. &lt;p&gt;
  63. Launched in 2024, the Uydu satellite was manufactured by the Turkish
  64. Aerospace Industries for roughly $600,000,000. The satellite&#39;s mission
  65. is
  66. &lt;/p&gt;
  67. &lt;p&gt;
  68. Construction-wise, the Uydu satellite comprises a main body and two
  69. solar panel arrays extending laterally to its side. For power consumption,
  70. the solar panels can be rotated to face the sun.
  71. &lt;/p&gt;
  72. &lt;/section&gt;
  73. &lt;/article&gt;

I've tried adding a div{} to my CSS file and messed with the nowrap property, modifying the CSS file, and have also done a number of Google / SO searches, but haven't found a solution. Honestly, I'm not sure I'm looking for the right phrases.


Edit:
Stefan's answer below resulted in the "baseball card" solution I was looking for:
使用CSS防止在两栏HTML报告中过早换行。

答案1

得分: 1

为什么不使用一个包含两个内部div的div来表示每一列。

类似于这样的结构:

  1. <article id="satgeom">
  2. <h2 id="satgeom-title">卫星几何</h2>
  3. <h3>卫星几何、描绘和描述</h3>
  4. <div style="display: flex;">
  5. <div style="width: 50%; padding-right: 2em;">
  6. <img style="width: 100%;" src="./satellite.png" alt="">
  7. <table class="tg" style="table-layout: fixed; width: 100%;">
  8. <colgroup>
  9. <col style="width: 150px">
  10. <col style="width: 150px">
  11. </colgroup>
  12. <tr>
  13. <td class="tg-zv4m">名称</td>
  14. <td class="tg-ofj5">Uydu</td>
  15. </tr>
  16. <tr>
  17. <td class="tg-zv4m">成本 [$]</td>
  18. <td class="tg-ofj5">600,000,000</td>
  19. </tr>
  20. <tr>
  21. <td class="tg-zv4m">制造商</td>
  22. <td class="tg-ofj5">TAI</td>
  23. </tr>
  24. <tr>
  25. <td class="tg-zv4m">运行时间 [年]</td>
  26. <td class="tg-ofj5">15</td>
  27. </tr>
  28. <tr>
  29. <td class="tg-zv4m">轨道高度 [km]</td>
  30. <td class="tg-ofj5">35,785</td>
  31. </tr>
  32. <tr>
  33. <td class="tg-zv4m">最大速度 [km/s]</td>
  34. <td class="tg-ofj5">11,051</td>
  35. </tr>
  36. <tr>
  37. <td class="tg-zv4m">动力质量 [kg]</td>
  38. <td class="tg-ofj5">1,577</td>
  39. </tr>
  40. <tr>
  41. <td class="tg-zv4m">NORAD ID</td>
  42. <td class="tg-ofj5"> - </td>
  43. </tr>
  44. <tr>
  45. <td class="tg-zv4m">上行链路 [GHz]</td>
  46. <td class="tg-ofj5">7.3 - 18.10</td>
  47. </tr>
  48. <tr>
  49. <td class="tg-zv4m">下行链路 [GHz]</td>
  50. <td class="tg-ofj5">11.70 - 12.75</td>
  51. </tr>
  52. <tr>
  53. <td class="tg-zv4m">参考框架</td>
  54. <td class="tg-ofj5">地心</td>
  55. </tr>
  56. <tr>
  57. <td class="tg-zv4m">制度</td>
  58. <td class="tg-ofj5">静止</td>
  59. </tr>
  60. </table>
  61. </div>
  62. <div style="width: 50%;">
  63. <p>
  64. Uydu卫星于2024年发射,由土耳其航空航天工业制造,成本约为$600,000,000。该卫星的任务是
  65. </p>
  66. <p>
  67. 在构造上,Uydu卫星由一个主体和两个太阳能电池板数组组成,横向延伸到其侧面。为了供电消耗,太阳能电池板可以旋转以面向太阳。
  68. </p>
  69. </div>
  70. </div>
  71. </article>
英文:

Why not use a div with two divs inside it for each column.

Something like this:

  1. &lt;article id=&quot;satgeom&quot;&gt;
  2. &lt;h2 id=&quot;satgeom-title&quot;&gt;Satellite geometry&lt;/h2&gt;
  3. &lt;h3&gt;Satellite geometry, depiction, and description&lt;/h3&gt;
  4. &lt;div style=&quot;display: flex;&quot;&gt;
  5. &lt;div style=&quot;width: 50%; padding-right: 2em;&quot;&gt;
  6. &lt;img style=&quot;width: 100%;&quot; src=&quot;./satellite.png&quot; alt=&quot;&quot;&gt;
  7. &lt;table class=&quot;tg&quot; style=&quot;table-layout: fixed; width: 100%&quot;&gt;
  8. &lt;colgroup&gt;
  9. &lt;col style=&quot;width: 150px&quot;&gt;
  10. &lt;col style=&quot;width: 150px&quot;&gt;
  11. &lt;/colgroup&gt;
  12. &lt;tr&gt;
  13. &lt;td class=&quot;tg-zv4m&quot;&gt;Name&lt;/td&gt;
  14. &lt;td class=&quot;tg-ofj5&quot;&gt;Uydu&lt;/td&gt;
  15. &lt;/tr&gt;
  16. &lt;tr&gt;
  17. &lt;td class=&quot;tg-zv4m&quot;&gt;Cost [$]&lt;/td&gt;
  18. &lt;td class=&quot;tg-ofj5&quot;&gt;600,000,000&lt;/td&gt;
  19. &lt;/tr&gt;
  20. &lt;tr&gt;
  21. &lt;td class=&quot;tg-zv4m&quot;&gt;Manufacturer&lt;/td&gt;
  22. &lt;td class=&quot;tg-ofj5&quot;&gt;TAI&lt;/td&gt;
  23. &lt;/tr&gt;
  24. &lt;tr&gt;
  25. &lt;td class=&quot;tg-zv4m&quot;&gt;Duration [years]&lt;/td&gt;
  26. &lt;td class=&quot;tg-ofj5&quot;&gt;15&lt;/td&gt;
  27. &lt;/tr&gt;
  28. &lt;tr&gt;
  29. &lt;td class=&quot;tg-zv4m&quot;&gt;Orbit altitude [km]&lt;/td&gt;
  30. &lt;td class=&quot;tg-ofj5&quot;&gt;35,785&lt;/td&gt;
  31. &lt;/tr&gt;
  32. &lt;tr&gt;
  33. &lt;td class=&quot;tg-zv4m&quot;&gt;Max. velocity [km/s]&lt;/td&gt;
  34. &lt;td class=&quot;tg-ofj5&quot;&gt;11,051&lt;/td&gt;
  35. &lt;/tr&gt;
  36. &lt;tr&gt;
  37. &lt;td class=&quot;tg-zv4m&quot;&gt;Dy mass [kg]&lt;/td&gt;
  38. &lt;td class=&quot;tg-ofj5&quot;&gt;1,577&lt;/td&gt;
  39. &lt;/tr&gt;
  40. &lt;tr&gt;
  41. &lt;td class=&quot;tg-zv4m&quot;&gt;NORAD ID&lt;/td&gt;
  42. &lt;td class=&quot;tg-ofj5&quot;&gt; - &lt;/td&gt;
  43. &lt;/tr&gt;
  44. &lt;tr&gt;
  45. &lt;td class=&quot;tg-zv4m&quot;&gt;Uplink [GHz]&lt;/td&gt;
  46. &lt;td class=&quot;tg-ofj5&quot;&gt;7.3 - 18.10&lt;/td&gt;
  47. &lt;/tr&gt;
  48. &lt;tr&gt;
  49. &lt;td class=&quot;tg-zv4m&quot;&gt;Downlink [GHz]&lt;/td&gt;
  50. &lt;td class=&quot;tg-ofj5&quot;&gt;11.70 - 12.75&lt;/td&gt;
  51. &lt;/tr&gt;
  52. &lt;tr&gt;
  53. &lt;td class=&quot;tg-zv4m&quot;&gt;Reference frame&lt;/td&gt;
  54. &lt;td class=&quot;tg-ofj5&quot;&gt;Geocentric&lt;/td&gt;
  55. &lt;/tr&gt;
  56. &lt;tr&gt;
  57. &lt;td class=&quot;tg-zv4m&quot;&gt;Regime&lt;/td&gt;
  58. &lt;td class=&quot;tg-ofj5&quot;&gt;Geostationary&lt;/td&gt;
  59. &lt;/tr&gt;
  60. &lt;/table&gt;
  61. &lt;/div&gt;
  62. &lt;div style=&quot;width: 50%;&quot;&gt;
  63. &lt;p&gt;
  64. Launched in 2024, the Uydu satellite was manufactured by the Turkish
  65. Aerospace Industries for roughly $600,000,000. The satellite&#39;s mission
  66. is
  67. &lt;/p&gt;
  68. &lt;p&gt;
  69. Construction-wise, the Uydu satellite comprises a main body and two
  70. solar panel arrays extending laterally to its side. For power consumption,
  71. the solar panels can be rotated to face the sun.
  72. &lt;/p&gt;
  73. &lt;/div&gt;
  74. &lt;/div&gt;
  75. &lt;/article&gt;

答案2

得分: 0

Here is the translated code:

  1. 在你的代码中有两个问题首先你已经在`#satgeom section`选择器内声明了部分的列这将使`<section>`元素跨越两列包括表格让我们将表格保留在一列中
  2. #satgeom section table {
  3. column-span: all;
  4. }
  5. 另外表格被包裹在`<p>`标签中让我们替换它
  6. <table class="tg" style="table-layout: fixed; width: 300px">
  7. ...
  8. </table>
英文:

There is 2 problem in your code, first you have declared your columns for the section within the #satgeom section selector, that will make the &lt;section&gt; element distributed across two columns, including the table, lets make the table stays in one column.

  1. #satgeom section table {
  2. column-span: all;
  3. }

Also the table is wrapped table with a &lt;p&gt; tag, lets replace this:

  1. &lt;p&gt;
  2. &lt;table class=&quot;tg&quot; style=&quot;table-layout: fixed; width: 300px&quot;&gt;
  3. ...
  4. &lt;/table&gt;
  5. &lt;/p&gt;

with

  1. &lt;table class=&quot;tg&quot; style=&quot;table-layout: fixed; width: 300px&quot;&gt;
  2. ...
  3. &lt;/table&gt;

huangapple
  • 本文由 发表于 2023年6月6日 03:13:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409390.html
匿名

发表评论

匿名网友

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

确定