像素不完美布局

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

Pixel Imperfect Layout

问题

My layout is not obeying my commands.

我的布局没有遵循我的命令。

I had trouble with a layout so I created a new basic box with sub-boxes to see what's going on, and no matter what I do the two 300px boxes won't fit side-by-side into the 600px box.

我在布局方面遇到了问题,所以我创建了一个新的基本框,带有子框,以查看发生了什么,但无论我做什么,两个300px的框都无法并排放入600px的框中。

Also, when the 2nd 300px box is forced to a 2nd line there's an unexplained margin between it and the box above.

而且,当第二个300px的框被迫移到第二行时,它和上面的框之间有一个无法解释的边距。

I checked CSS & HTML validators and they presented no errors.

我检查了CSS和HTML验证器,它们没有显示任何错误。

Any insight to the cause of the annoyance would be greatly appreciated.

对于这个问题的原因的任何洞察都将不胜感激。

System:
Chrome: Version 114.0.5735.106 (Official Build) (64-bit)
Linux Mint 19.3 Cinnamon v:4.4.8, Kernel: 5.4.0-150-generic
(Potential) pebkac model: 1969
HTML代码已提供,无需翻译。

英文:

My layout is not obeying my commands.

I had trouble with a layout so I created a new basic box with sub-boxes to see what's going on, and no matter what I do the two 300px boxes won't fit side-by-side into the 600px box.

Also, when the 2nd 300px box is forced to a 2nd line there's an unexplained margin between it and the box above.

I checked CSS & HTML validators and they presented no errors.

Any insight to the cause of the annoyance would be greatly appreciated.

System:
Chrome: Version 114.0.5735.106 (Official Build) (64-bit)
Linux Mint 19.3 Cinnamon v:4.4.8, Kernel: 5.4.0-150-generic
(Potential) pebkac model: 1969

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>widthtest</title>
  5. <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0">
  6. <style>
  7. html, body {
  8. border: 0;
  9. height: 100%;
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. font: normal 16px/16px Arial;}
  14. main {
  15. background: #000;
  16. height: 600px;
  17. margin: 25px;
  18. width: 600px;
  19. }
  20. #left, #right {
  21. display: inline-block;
  22. height: 300px;
  23. width: 300px;
  24. }
  25. #left {
  26. background: #cfc;
  27. }
  28. #right {
  29. background: #ccf;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <main>
  35. <div id="left">
  36. </div>
  37. <div id="right">
  38. </div>
  39. </main>
  40. </body>
  41. </html>

答案1

得分: 2

这是由于空白间隙引起的。如果删除所有div之间的空白,您将获得所需的像素完美结果。

  1. <!-- begin snippet: js hide: false console: true babel: false -->
  2. <!-- language: lang-css -->
  3. html, body {
  4. border: 0;
  5. height: 100%;
  6. margin: 0;
  7. padding: 0;
  8. box-sizing: border-box;
  9. font: normal 16px/16px Arial;}
  10. main {
  11. background: #000;
  12. height: 600px;
  13. margin: 25px;
  14. width: 600px;
  15. }
  16. #left, #right {
  17. display: inline-block;
  18. height: 300px;
  19. width: 300px;
  20. }
  21. #left {
  22. background: #cfc;
  23. }
  24. #right {
  25. background: #ccf;
  26. }
  27. <!-- language: lang-html -->
  28. <main>
  29. <div id="left">
  30. </div><div id="right">
  31. </div>
  32. </main>
  33. <!-- end snippet -->

然而,我认为更好的方法是使用Flexbox。

  1. <!-- begin snippet: js hide: false console: true babel: false -->
  2. <!-- language: lang-css -->
  3. html, body {
  4. border: 0;
  5. height: 100%;
  6. margin: 0;
  7. padding: 0;
  8. box-sizing: border-box;
  9. font: normal 16px/16px Arial;}
  10. main {
  11. display: flex; /* <- */
  12. background: #000;
  13. height: 600px;
  14. margin: 25px;
  15. width: 600px;
  16. }
  17. #left, #right {
  18. /* display: inline-block; */ /* <- */
  19. height: 300px;
  20. width: 300px;
  21. }
  22. #left {
  23. background: #cfc;
  24. }
  25. #right {
  26. background: #ccf;
  27. }
  28. <!-- language: lang-html -->
  29. <main>
  30. <div id="left">
  31. </div>
  32. <div id="right">
  33. </div>
  34. </main>
  35. <!-- end snippet -->
英文:

This is due to the white space. If you remove all white space between the divs, you'll get your intended pixel-perfect result.

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

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

  1. html, body {
  2. border: 0;
  3. height: 100%;
  4. margin: 0;
  5. padding: 0;
  6. box-sizing: border-box;
  7. font: normal 16px/16px Arial;}
  8. main {
  9. background: #000;
  10. height: 600px;
  11. margin: 25px;
  12. width: 600px;
  13. }
  14. #left, #right {
  15. display: inline-block;
  16. height: 300px;
  17. width: 300px;
  18. }
  19. #left {
  20. background: #cfc;
  21. }
  22. #right {
  23. background: #ccf;
  24. }

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

  1. &lt;main&gt;
  2. &lt;div id=&quot;left&quot;&gt;
  3. &lt;/div&gt;&lt;div id=&quot;right&quot;&gt;
  4. &lt;/div&gt;
  5. &lt;/main&gt;

<!-- end snippet -->

<br>However, I think a better way to go about this is to use Flexbox.

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

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

  1. html, body {
  2. border: 0;
  3. height: 100%;
  4. margin: 0;
  5. padding: 0;
  6. box-sizing: border-box;
  7. font: normal 16px/16px Arial;}
  8. main {
  9. display: flex; /* &lt;- */
  10. background: #000;
  11. height: 600px;
  12. margin: 25px;
  13. width: 600px;
  14. }
  15. #left, #right {
  16. /* display: inline-block; */ /* &lt;- */
  17. height: 300px;
  18. width: 300px;
  19. }
  20. #left {
  21. background: #cfc;
  22. }
  23. #right {
  24. background: #ccf;
  25. }

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

  1. &lt;main&gt;
  2. &lt;div id=&quot;left&quot;&gt;
  3. &lt;/div&gt;
  4. &lt;div id=&quot;right&quot;&gt;
  5. &lt;/div&gt;
  6. &lt;/main&gt;

<!-- end snippet -->

答案2

得分: 1

2个宽度为300像素的元素无法容纳在一个宽度为600像素的元素中。尝试将它们各自调整为295像素,然后您应该能够看到效果。然后您可以进行实验。

英文:

2 x 300px elements won't fit into a 600px element. Try making them 295px each and you should see it work. Then you can experiment.

像素不完美布局

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

发表评论

匿名网友

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

确定