css – 保持div高度与宽高比相关

前端之家收集整理的这篇文章主要介绍了css – 保持div高度与宽高比相关前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在为我们公司的主要网站建立一个移动友好的网站.视网膜的设计方式大约是2x.我打算做的是将主要内容设置为最大宽度640px,宽度设置为100%.我有一定的背景图像很适合做到这一点.但是随着div的宽度变小,我需要调整高度.有任何想法吗?

这是css:

  1. *{margin:0;padding:0}h1,h2,h3,h4,h5,p,li,a,cite{font-size:14px;font-weight:normal}button,img{border:0}body{font-family:Arial,sans-serif;}
  2.  
  3. body {
  4. margin:0;
  5. background-color:#fff;
  6. }
  7.  
  8. .top,.body {
  9. max-width:640px;
  10. width:100%;
  11. margin:0 auto;
  12. }
  13.  
  14. .top {
  15. background: white url(images/top.jpg) no-repeat;
  16. background-size:auto;
  17. overflow:hidden;
  18. height:124px;
  19. max-height:124px;
  20. }
  21.  
  22. .top ul {
  23. list-style:none;
  24. height:100%;
  25. }
  26.  
  27. .top ul li {
  28. height:100%;
  29. float:left;
  30. display:block;
  31. }

解决方法

我确实找到了一个答案.它增加了一点不客气的标记,但效果很好.
可以在这里找到: http://jsfiddle.net/AdQ3P/

逻辑是在底部底部.基本上这需要(img_height / img_width)* 100.

编辑这里的代码,所以不依赖于jsfiddle.

  1. <div class="container">
  2. <div class="hero"></div>
  3. </div>
  4.  
  5. .container {
  6. width:100%;
  7. max-width:500px;
  8. }
  9.  
  10. .hero {
  11. width:100%;
  12. height:0;
  13. background-size:100%;
  14. background:url(http://img97.imageshack.us/img97/3410/photo2ue.jpg) no-repeat;
  15. padding-bottom:75%;
  16. }

也是一个凌乱的桌子我有哈哈.

猜你在找的CSS相关文章