简体   繁体   English

具有纵横比的中心 Div

[英]Center Div with aspect Ratio

I need to Center a Div in the html viewport.我需要在 html 视口中居中一个 Div。 It should be centered both, vertically and horizontally.它应该在垂直和水平方向都居中。 But the Div should keep its aspect ratio (4/3) and have a minimum margin of 10px.但是 Div 应该保持其纵横比 (4/3) 并且最小边距为 10px。

I made a Javascript:我做了一个Javascript:

function resizeWindow() {
 var wHeight = $(document).height() - 20;
 var wWidth = $(document).width() - 20;
 var gameStage = $("#gameStage");
 if ((wWidth / 4) * 3 <= wHeight) {
  gameStage.css("width", wWidth + "px");
  gameStage.css("height", ((wWidth / 4) * 3) + "px");
  gameStage.css("top", (wHeight - ((wWidth / 4) * 3)) / 2 + 9 + "px");
  gameStage.css("left", "10px");
 } else {
  gameStage.css("height", wHeight + "px");
  gameStage.css("width", ((wHeight / 3) * 4) + "px");
  gameStage.css("left", (wWidth - ((wHeight / 3) * 4)) / 2 + 9 + "px");
  gameStage.css("top", "10px");
 }
}

https://jsfiddle.net/3sw06kvb/ https://jsfiddle.net/3sw06kvb/

But User, who disabled Javascript will not be able to use my website.但是禁用 Javascript 的用户将无法使用我的网站。 And a solution with HTML/CSS should be faster(?).使用 HTML/CSS 的解决方案应该更快(?)。

My first Idea is to make a wrapper with我的第一个想法是做一个包装

position: fixed 
top, left, bottom, right = 20px;.

But my problem is making a div centering vertically and horizontally while keeping its aspect ratio.但我的问题是在保持纵横比的同时使 div 垂直和水平居中。

https://jsfiddle.net/xep2mf62/ https://jsfiddle.net/xep2mf62/

You can try the following in the CSS.您可以在 CSS 中尝试以下操作。 The new units in CSS3 vh and vw allows you to set the height depending on the size of the viewport. CSS3 vhvw的新单位允许您根据视口的大小设置高度。 height:100vh will give the element a height that is equal to the height of the browser window. height:100vh会给元素一个等于浏览器窗口高度的高度。

***1vw = 1% of viewport width ***1vw = 视口宽度的 1%

1vh = 1% of viewport height*** 1vh = 1% 视口高度***

 .wrapper { position: relative; width:100%; height:100vh; } .childdivision { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); height:50vw; width:90vw; border:2px solid #444; }
 <div class="wrapper"> <div class="childdivision"> </div> </div>

Still JS, but simpler仍然是 JS,但更简单

 window.onresize = function() { resize(); }; function resize() { var wrapper = document.querySelector(".wrapper"); var wrapperwidth = 1920; var wrapperheight = 1080; var vw = Math.max(document.documentElement.clientWidth, window.innerWidth || 0); var vh = Math.max(document.documentElement.clientHeight, window.innerHeight || 0); var ratio = Math.min(vw / wrapperwidth, vh / wrapperheight); wrapper.style.transform = `translate(-50%, -50%) scale(${ratio})`; } resize();
 .wrapper { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 1919px; height: 1079px; overflow: hidden; background-color: red; } /* demo text (smiley face) */ .wrapper::after { content: ":)"; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 200px; font-family: "Roboto", 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
 <div class="wrapper"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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