css – div占据的奇怪位置

前端之家收集整理的这篇文章主要介绍了css – div占据的奇怪位置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个奇怪的情况,我的中间div略微向下.
这是一个截图:

HTML

  1. <div id="footer">
  2. <div class="content">
  3. <div id="info1">
  4. <img src="img/temp.png" alt="About me" />
  5. <h4>About Me</h4>
  6. <p>this is about me section</br>and this is the other line</br>and this is a third line</p>
  7. </div>
  8. <div id="info2">
  9. <h4>Random Photos</h4>
  10. <div id="randomPhotos"></div>
  11. </div>
  12. <div id="info3">
  13. <h3>Follow Me</h3>
  14. <div>
  15. <img src="img/temp.png" alt="facebook" /><a href="#">Facebook</a>
  16. </div>
  17. <div>
  18. <img src="img/temp.png" alt="twitter" /><a href="#">Twitter</a>
  19. </div>
  20. <div>
  21. <img src="img/temp.png" alt="email" /><a href="#">E-mail</a>
  22. </div>
  23. </div>
  24. </div>
  25. </div>

CSS:

  1. #info1,#info2,#info3
  2. {
  3. padding: 10px;
  4. display:inline-block;
  5. }
  6.  
  7. #info1
  8. {
  9. width:20%;
  10. }
  11.  
  12. #info1 img
  13. {
  14. margin-right:3px;
  15. width:20px;
  16. height:20px;
  17. background-image:url('../img/glyphicons-halflings.png');
  18. background-repeat:no-repeat;
  19. background-position:-162px 1px;
  20. }
  21.  
  22. #info1 img,#info1 h4
  23. {
  24. display:inline-block;
  25. }
  26.  
  27. #info2
  28. {
  29. width:55%;
  30. border-left:1px solid gray;
  31. border-right : 1px solid gray;
  32. }
  33. #info3
  34. {
  35. width:15%;
  36. }
  37.  
  38. #info3 img
  39. {
  40. width:20px;
  41. height:20px;
  42. margin-right:5px;
  43. }
  44.  
  45. #info3 img[alt="facebook"]
  46. {
  47. background : url('../img/result.png') no-repeat 0px -30px;
  48. }
  49.  
  50. #info3 img[alt="twitter"]
  51. {
  52. background : url('../img/result.png') no-repeat 0px -60px;
  53. }
  54.  
  55. #info3 img[alt="email"]
  56. {
  57. background : url('../img/result.png') no-repeat 0px 0px;
  58. }
  59.  
  60. #info2 div
  61. {
  62. padding:3px;
  63.  
  64. }
  65.  
  66. #randomPhotos
  67. {
  68. height : 100px;
  69. }

我真的不擅长CSS,所以它可能是一个小问题.我只是找不到它.

解决方法

大多数浏览器,对于使用display:inline-block的元素,将自动在该元素上使用vertical-align:baseline,除非你使用CSS重置(这可能也会将defacto标准定义为vertical-align:baseline.)

这就是你所看到的原因,你的每个信息div都与基线对齐.由于中央div的高度较小,因此您可以在顶部看到间隙.

解决这个问题:

  1. #info1,#info3
  2. {
  3. padding: 10px;
  4. display:inline-block;
  5. vertical-align:top;
  6. }

您可能遇到的第二个问题是,现在它对齐顶部,底部有一个“间隙”,没有左边框或右边框.要么由全高度divs管理边框,要么使所有div具有相同的高度.

猜你在找的CSS相关文章