html – Inline-Block div元素不按预期排列

前端之家收集整理的这篇文章主要介绍了html – Inline-Block div元素不按预期排列前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Why is this inline-block element pushed downward?5个答案我有一些HTML和CSS创建一个可以在着陆页上找到的内嵌块元素(div)。但是,当它们在div中包含一些内容(无序列表)时,它们只能正确对齐。如果div中没有​​内容,则该元素被按下。这是一个 jsfiddle.这是代码。任何人都可以解释为什么第三个div块不是垂直对齐的?

编辑:虽然我很满意这个问题的“修复”是为了确保每个div在样式中使用“vertical-align:top”,但我仍然对我为什么要使用这个造型在第一位。我会认为,div元素将始终排列均匀,不管div内的内容如何。

  1. <html>
  2. <head>
  3. <style type="text/css">
  4. body {
  5. font-family: Helvetica;
  6. }
  7.  
  8. h1 {
  9. margin: 0px;
  10. padding: 10px;
  11. font-weight: bold;
  12. border-bottom: 1px solid #aaaaaa;
  13. font-size: 12px;
  14. }
  15.  
  16. a {
  17. text-decoration: none;
  18. }
  19.  
  20. ul {
  21. padding-left: 20px;
  22. }
  23.  
  24. li {
  25. list-style-type: none;
  26. font-size: 12px;
  27. }
  28.  
  29. .landing-block {
  30. display: inline-block;
  31. background-color: #eeeeee;
  32. margin-right: 30px;
  33. width: 192px;
  34. height: 140px;
  35. border: 1px solid #aaaaaa;
  36. -moz-Box-shadow: 3px 3px 5px #535353;
  37. -webkit-Box-shadow: 3px 3px 5px #535353;
  38. Box-shadow: 3px 3px 5px #535353;
  39. }
  40.  
  41. .header {
  42. padding: 10px;
  43. background-color: red;
  44. border-bottom: 1px solid #aaaaaa;
  45. color: #ffffff;
  46. }
  47.  
  48. a:hover {
  49. text-decoration:underline;
  50. }
  51.  
  52. h1 > a {
  53. color: #ffffff;
  54. }
  55.  
  56. h1 > a:hover {
  57. color:#ffffff;
  58. }
  59.  
  60. li > a {
  61. color: #000000;
  62. }
  63.  
  64. li > a:hover {
  65. color: #000000;
  66. }
  67. </style>
  68. </head>
  69. <body>
  70. <div>
  71. <div class='landing-block'>
  72. <h1 style='background-color: #3991db;'>
  73. <a href='#'>COMPANIES</a>
  74. </h1>
  75. <ul>
  76. <li><a href='#'>Search Companies</a></li>
  77. <li><a href='#'>New Company</a></li>
  78. <ul>
  79. </div>
  80. <div class='landing-block'>
  81. <h1 style='background-color: #9139db;'>
  82. <a href='#'>PEOPLE</a>
  83. </h1>
  84. <ul>
  85. <li><a href='#'>Search People</a></li>
  86. <li><a href='#'>New Person</a></li>
  87. <ul>
  88. </div>
  89. <div class='landing-block'>
  90. <h1 style='background-color: #c2db39;'>
  91. <a href='#'>Products</a>
  92. </h1>
  93. </div>
  94. <div>
  95. </body>
  96. </html>

解决方法

@H_301_9@ 内嵌块元素是vertical-align:baseline;默认。将其更改为vertical-align:top;
  1. .landing-block {
  2. display: inline-block;
  3. background-color: #eeeeee;
  4. margin-right: 30px;
  5. width: 192px;
  6. height: 140px;
  7. border: 1px solid #aaaaaa;
  8. -moz-Box-shadow: 3px 3px 5px #535353;
  9. -webkit-Box-shadow: 3px 3px 5px #535353;
  10. Box-shadow: 3px 3px 5px #535353;
  11. vertical-align:top; /* add this rule */
  12.  
  13. }

猜你在找的HTML相关文章