好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

css实现一个元素高度固定宽度按比例显示效果

用padding -t op百分比可以实现 宽 度固定高度按比例展示,现在的需求是对一个video视频的 盒子 div高度是固定的,宽度如何按比例展示?

解决后效果如图:

红框标注的即是我在上面高度比例固定的范围内宽度自适应的效果;

css代码:

.content {
      m arg in: 0 auto;
      h ei ght: 79vh;
      .video_box {
        pos IT ion: relative;
        height: 100%;
        overflow: hidden;
        mar gin : 0 auto;
        width: 79vh*1.778;
        max-width: 100vw;
        .ad_pic {
          dis play : none;
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          height: 100%;
          background: url(6. jpg ) no-re PE at  center ;
           background-size : 100% 100%;
           img  {
            width: 100%;
            height: 100%;
          }
          . BT n_play {
            display: inline-block;
            .width(50);
            .height(50);
            position: absolute;;
            left: 50%;
            top: 50%;
            .margin-left(-25);
            .margin-top(-25);
            background: url( .. /p ub lic/img/icon_play.png) no-repeat center;
            background- Size:  100% 100%;
          }
        }
        ifr am e, object, embed, video {
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          height: 100%;
        }
        .video_card {
          position: absolute;
          top: 0;
          left: 0;
          width: 2px;
          height: 1px;
        }
      }
    }

ht ML :

<div class="content">
        <div class="video_box">
            <div class="ad_pic">
                <span class="btn_play"></span>
            </div>
            <video id="ad_video"
                   autobuffer
                   src="a.mp4"
                   autoplay
                    PR eload
                   controls=""
                   loop
                   poster="6.jpg"
                    webkit -playsinline="true"
                   playsinline="true"
                   x-webkit- airplay ="allow"
                   x5-playsinline
                   x5-video- player -type="h5"
                   x5-video-player-fullscreen="true"
                   x5-video- orientation ="portr ai t"
                   >
            </video>
        </div>
    </div>

由于视频是有宽高比的,这里给视频的高度直接是外面盒子的高度*比例,就等于视频的宽,为了 防 止视频过宽超出屏幕,这里加一个max-width:100vw;限制一下,然后通过margin:0 auto;居中显示,成功解决!
&nbs p;

总结

以上所述是小编给大家介绍的css实现一个元素高度固定宽度按比例显示效果, 希望对大家有所帮助 ,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

总结

以上是 为你收集整理的 css实现一个元素高度固定宽度按比例显示效果 全部内容,希望文章能够帮你解决 css实现一个元素高度固定宽度按比例显示效果 所遇到的问题。

如果觉得 网站内容还不错, 推荐好友。

查看更多关于css实现一个元素高度固定宽度按比例显示效果的详细内容...

  阅读:22次