我正在构建一个小项目,面临着严重的CSS挑战:
我有一个<main>
元素,它被设置为行的flexbox.里面有3个子元素:
:root {
--clr-light-text: #f2f2f2;
--clr-dark-text: #48484a;
--shadow-default: 0px 2px 8px rgba(0, 0, 0, 0.2);
--vid-container-h: 0px;
color: var(--clr-light-text);
font-family: 'Poppins', sans-serif;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
list-style: none;
text-decoration: none;
}
h1,
h2,
h3,
h4 {
font-weight: 400;
}
body {
background-color: var(--clr-dark-text);
text-align: center;
overflow-x: hidden;
scroll-behavior: smooth;
}
.glass {
background-image: linear-gradient( 135deg, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0));
backdrop-filter: blur(5px);
border: 1px solid rgba(255, 255, 255, 0.15);
box-shadow: var(--shadow-default);
border-radius: 0.5rem;
}
main {
display: flex;
justify-content: center;
padding: 0 1rem;
gap: 2rem;
}
.main-container {
background-color: hsl(0, 0%, 75%);
padding-bottom: 10rem;
position: relative;
}
.weather-box {
width: clamp(13.5rem, 18%, 20rem);
display: flex;
flex-direction: column;
justify-content: space-evenly;
padding: 0 1em 1em 1em;
}
#weather-icon {
margin: -15% -10%;
}
.weather-details {
display: flex;
flex-direction: column;
justify-content: space-between;
}
#forecast,
#temp,
#humidity {
display: flex;
flex-direction: column;
align-items: center;
}
#temp {
margin-bottom: 1em;
font-weight: 200;
}
#temp-num {
font-size: 3em;
}
#humid {
font-size: 1.5em;
}
#humid-value {
font-size: 1.5em;
font-weight: 200;
}
#humid-value img {
max-width: 1em;
}
#forecast-desc {
font-size: 2.5em;
font-weight: 400;
}
.playlist-box {
width: clamp(13.5rem, 18%, 20rem);
display: flex;
align-items: center;
flex-direction: column;
padding: 1rem 0.5em;
flex-shrink: 0;
}
.playlist-box h3::after {
content: '';
width: 70%;
height: 1px;
background-color: rgba(255, 255, 255, 0.3);
border-radius: 100vmax;
display: block;
margin: 1em auto;
}
#playlist {
display: grid;
width: 100%;
padding: 1em;
overflow-y: scroll;
scroll-behavior: smooth;
}
#playlist li {
margin-bottom: 1rem;
width: 100%;
padding: 1rem;
border: 1px solid rgba(255, 255, 255, 0);
}
#playlist li:hover,
#playlist li:focus {
background-image: linear-gradient( 135deg, rgba(255, 255, 255, 0.4), rgba(255, 255, 255, 0));
border: 1px solid rgba(255, 255, 255, 0.15);
box-shadow: var(--shadow-default);
border-radius: 0.5rem;
}
.song-title {
margin-bottom: 0.5em;
}
.vid-thumb {
width: 100%;
/* object-position: center;
object-fit: cover; */
}
.video-container {
align-self: flex-start;
padding: 1em;
}
#song-frame {
position: relative;
overflow: hidden;
width: 100%;
padding-top: 56.25%;
}
#song-frame iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
<div class="main-container">
<main>
<aside class="weather-box glass">
<div id="forecast">
<img id="weather-icon" src="http://openweathermap.org/img/wn/02d@4x.png" alt="Weather Icon" />
<span id="forecast-desc">(--)</span>
</div>
<div class="weather-details">
<div id="temp">
<span id="temp-num">0°c</span>
<span id="feels-like">feels like 0°c</span>
</div>
<div id="humidity">
<span id="humid">Humidity</span>
<div id="humid-value">
<span id="humid-num">0%</span>
</div>
</div>
</div>
</aside>
<div class="video-container glass">
<div id="song-frame">
<iframe src="https://www.youtube.com/embed/oG08ukJPtR8" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" width="1024" height="576" frameborder="0"></iframe>
</div>
</div>
<aside class="playlist-box glass">
<h3>Browse Playlist:</h3>
<ul id="playlist">
<li>
<h4>Song Title</h4>
<img class="vid-thumb" src="https://i.ytimg.com/vi/ru0K8uYEZWw/hqdefault.jpg" alt="" />
</li>
<li>
<h4>Song Title</h4>
<img class="vid-thumb" src="https://i.ytimg.com/vi/pkCyfBibIbI/hqdefault.jpg" alt="" />
</li>
</ul>
</aside>
</main>
</div>
我将这<aside>
个元素应用于以下width: clamp(13.5rem, 18%, 20rem)
个元素,以获得响应宽度.中间的div使用视口的其余可用宽度.总的来说,这种布局还是可行的.
weather-container
的夹钳工作正常.playlist-container
虽然能够缩小到13.5rem(216px)以下,但在一些屏幕尺寸中,我的两侧留下了不均匀的容器.
有趣的是,这个问题不知何故与<img>
个缩略图有关.它们的设定宽度为<img>
%.如果我取消它,则容器的行为正常,但缩略图被切断.
- 设置固定的
width
值不起作用. - 将
min-width
设置为元素不起作用. - 将
flex-shrink: 0;
设置为元素不起作用. - 与
flex-basis
人打交道对我也没用. - 用
background-image
将<img>
改为<div>
会产生相同的结果.
我很想克服这个问题,谢谢你.
Edit:我制作了一个代码笔,它重新创建了这个问题:https://codepen.io/xandertem/pen/XWZLaVR
try 进入响应模式,看到右侧部分正在缩小<;560像素.