我正在寻找填充屏幕区域并保持内容的宽高比的方法。
容器应缩放以覆盖整个屏幕,并保持其纵横比。图像的部分内容可能会被裁剪以确保完全适合。
问题是: - 在垂直屏幕中,会出现一个滚动条,显示应裁剪的正确内容。 -“容器”的内容应该缩放他的内容
我可以尝试什么? 只用css可行吗?
/* Stili di base */
body,
html {
height: 100%;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
background-color: #f0f0f0;
}
/* Contenitore esterno per gestire l'overflow */
.outer-container {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
/* Nasconde l'overflow */
display: flex;
justify-content: center;
align-items: center;
}
/* Container principale */
.container {
position: relative;
background-color: lightblue;
border: 1px solid black;
border-radius: 65px;
box-sizing: border-box;
overflow: hidden;
/* Nasconde l'eventuale overflow interno */
/* Adattamento come object-fit: cover */
width: 100vmax;
height: 100vmax;
}
/* Parola posizionata rispetto al centro */
.word {
position: absolute;
transform: translate(-50%, -50%);
font-size: 2rem;
/* Aggiusta la dimensione per visibilità */
}
/* Posizioniamo le parole in base al centro */
.word1 {
top: 10%;
left: 50%;
}
.word2 {
top: 50%;
left: 10%;
}
.word3 {
top: 50%;
left: 90%;
}
.word4 {
top: 90%;
left: 50%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Parole Posizionate</title>
</head>
<body>
<div class="outer-container">
<div class="container">
<div class="word word1">Parola 1</div>
<div class="word word2">Parola 2</div>
<div class="word word3">Parola 3</div>
<div class="word word4">Parola 4</div>
</div>
</div>
</body>
</html>
transform : scale()
来管理您的容器,然后overflow
将通过简单地裁剪任何多余的内容/数据来确保不会出现滚动条。
object-fit
<style>
/* Basic styles */
body, html {
height: 100%;
margin: 0;
overflow: hidden; /* Prevent scrollbars */
display: flex;
justify-content: center;
align-items: center;
background-color: #f0f0f0;
}
/* container to handle overflow */
.outer-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
display: flex;
justify-content: center;
align-items: center;
}
/* Main container that scales */
.container {
position: absolute;
top: 50%;
left: 50%;
width: 100%;
height: 100%;
max-width: 100vmax; /* keeps aspect ratio to max size */
max-height: 100vmax;
background-color: lightblue;
border: 1px solid black;
border-radius: 65px;
overflow: hidden; /* hides internal overflow */
transform: translate(-50%, -50%); /* centers the container */
object-fit: cover; /* ensures content fits container */
aspect-ratio: 1 / 1;
}
.word {
position: absolute;
transform: translate(-50%, -50%);
font-size: 2rem; /* adjust the size for better viewing */
}
.word1 {
top: 10%;
left: 50%;
}
.word2 {
top: 50%;
left: 10%;
}
.word3 {
top: 50%;
left: 90%;
}
.word4 {
top: 90%;
left: 50%;
}
</style>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Aspect Ratio Scaling</title>
</head>
<body>
<div class="outer-container">
<div class="container">
<div class="word word1">Parola 1</div>
<div class="word word2">Parola 2</div>
<div class="word word3">Parola 3</div>
<div class="word word4">Parola 4</div>
</div>
</div>
</body>
</html>