简体   繁体   中英

how do I get width in css, without js

is there a way to retrieve the width of an element only in css, without using javascript. I know how to use document.getElementsById("iframe").offsetWidth and set that to run repeatedly.

var video = document.getElementsByClassName("video");
var ratio = 16/9;
var milliseconds = 100;

setInterval(function(){
  IframeSizeRatioFix(video, ratio);
}, milliseconds);

function IframeVideoFix(video, ratio){
  for(i = 0; i < video.length; i++){
    video[i].style.height = video[i].offsetWidth/ratio;
  }
}

but even if I set the milliseconds to 1, or 0.001, the browser can't handle this speed and skips updates, and when a user resizes the window, the iframe size change moves a bit jagged, and jumps a lot. I was wondering if there was a way to use the height: calc(100% / (16/9)); function in css, but 100% seems to show the image squished, and using 100vw just stretches it to the page, not the container(which also changes size at a different rate than the window).

is there a way to get an elements width in css? or alternatively, a way to update javascript faster, without it being unsupported speeds by the browser?

This seems to work. I just put it on a window.onresize, I'll keep this up incase someone else finds it helpful.

var video = document.getElementsByClassName("video");
var ratio = 16/9;

window.onresize = IframeVideoFix;

function IframeVideoFix(){
  for(i = 0; i < video.length; i++){
    video[i].style.height = video[i].offsetWidth/ratio;
  }
}

setTimeout(IframeVideoFix, 500);

An pure CSS example of a div that keeps a 16:9 aspect ratio based on the width.

 * { box-sizing: border-box; } .aspect-16-9 { position: relative; padding-bottom: 56.25%; overflow: hidden; } .aspect-content { position: absolute; width: 100%; height: 100%; /*and some styling*/ padding: 15px; overflow-y: auto; background: #DDD; } 
 <div style="width: 100%; max-width: 960px; padding: 20px;"> <div class="aspect-16-9"> <div class="aspect-content"> Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar. The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn't listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached the first hills of the Italic Mountains, she had a last view back on the skyline of her hometown Bookmarksgrove, the headline of Alphabet Village and the subline of her own road, the Line Lane. Pityful a rethoric question ran over her cheek, then </div> </div> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM