溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

css如何實(shí)現(xiàn)一個(gè)元素高度固定寬度按比例顯示效果

發(fā)布時(shí)間:2021-03-19 09:54:09 來(lái)源:億速云 閱讀:208 作者:小新 欄目:web開(kāi)發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)css如何實(shí)現(xiàn)一個(gè)元素高度固定寬度按比例顯示效果,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

解決后效果如圖:

css如何實(shí)現(xiàn)一個(gè)元素高度固定寬度按比例顯示效果

紅框標(biāo)注的即是我在上面高度比例固定的范圍內(nèi)寬度自適應(yīng)的效果;

css代碼:

.content {
      margin: 0 auto;
      height: 79vh;
      .video_box {
        position: relative;
        height: 100%;
        overflow: hidden;
        margin: 0 auto;
        width: 79vh*1.778;
        max-width: 100vw;
        .ad_pic {
          display: none;
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          height: 100%;
          background: url(6.jpg) no-repeat center;
          background-size: 100% 100%;
          img {
            width: 100%;
            height: 100%;
          }
          .btn_play {
            display: inline-block;
            .width(50);
            .height(50);
            position: absolute;;
            left: 50%;
            top: 50%;
            .margin-left(-25);
            .margin-top(-25);
            background: url(../../public/img/icon_play.png) no-repeat center;
            background-size: 100% 100%;
          }
        }
        iframe, 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;
        }
      }
    }

html:

<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
                   preload
                   controls=""
                   loop
                   poster="6.jpg"
                   webkit-playsinline="true"
                   playsinline="true"
                   x-webkit-airplay="allow"
                   x5-playsinline
                   x5-video-player-type="h6"
                   x5-video-player-fullscreen="true"
                   x5-video-orientation="portrait"
                   >
            </video>
        </div>
    </div>

由于視頻是有寬高比的,這里給視頻的高度直接是外面盒子的高度*比例,就等于視頻的寬,為了防止視頻過(guò)寬超出屏幕,這里加一個(gè)max-width:100vw;限制一下,然后通過(guò)margin:0 auto;居中顯示,成功解決!

關(guān)于“css如何實(shí)現(xiàn)一個(gè)元素高度固定寬度按比例顯示效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

css
AI