英文:
Create grid so that when hovers, it shows expanded text
问题
You can achieve the desired effect by making a few adjustments to your CSS. Here's the modified CSS code:
.landingcard {
position: relative;
}
.landingcard img {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 25px;
}
.landingcard .color-fans,
.landingcard .color-venue,
.landingcard .color-artists {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
pointer-events: none;
border-radius: 25px;
transition: opacity 0.3s;
}
.landingcard:hover .color-fans,
.landingcard:hover .color-venue,
.landingcard:hover .color-artists {
opacity: 0.8;
}
.landingcard .buttons-container {
position: absolute;
bottom: 0;
width: 100%;
height: 15%;
display: flex;
border-radius: 25px;
align-items: center;
justify-content: center;
transition: height 0.3s, opacity 0.3s;
}
.landingcard:hover .buttons-container {
height: 70%;
opacity: 1;
}
These changes ensure that the colored sections (.color-fans
, .color-venue
, .color-artists
) cover the entire card but remain hidden until hover, and the pointer-events: none;
style allows content beneath them to be clickable or selectable. This way, you can write new content within the expanded colored section without it affecting the hover effect.
英文:
So, I am trying to achieve this effect in a grid:
And here's the code I have:
HTML:
<div className='container'>
<div className='options-containers'>
<div className='landingcard'>
<div className='buttons-container color-venue'> </div>
<img className='background-image' src={VenueBackground} alt="" />
</div>
<div className='landingcard'>
<div className='buttons-container color-artists'></div>
<img src={BackgroundImage1} alt="Artists Picture" />
</div>
<div className='landingcard'>
<div className='buttons-container color-fans'></div>
<img src={FansBackground} alt="" />
</div>
</div>
</div>
CSS:
.landingcard{
position: relative;
}
.landingcard img {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 25px;
}
.landingcard .color-fans{
background-color: #549FE5F2;
opacity: 0.8;
}
.landingcard .color-venue{
background-color: #FABF4D;
opacity: 0.8;
}
.landingcard .color-artists{
background-color: #FFB08B;
opacity: 0.8;
}
.landingcard .buttons-container {
position: absolute;
bottom: 0;
width: 100%;
height: 15%;
display: flex;
border-radius: 25px;
align-items: center;
justify-content: center;
transition: height 0.3s, opacity 0.3s;
}
.landingcard:hover .buttons-container {
height: 70%;
opacity: 1;
}
And when compiles, each grid it turns into (I put some text in div to make it clearer):
and when hovers,
Currently, when I hover on it, the entire colored section grows, so when I write text on the div, the text also transforms up as I hovered on it. How to make it fixed so I could write new contents on the expanded yellow section? (the effect as shown in the picture above?)
答案1
得分: 0
尝试这个:
我在一个展开容器上面叠加了一个单独的按钮容器
<div className='container'>
<div className='options-containers'>
<div className='landingcard'>
<div className='expand-container color-venue'>内容已展开</div>
<div className='buttons-container color-venue'>按钮...</div>
<img className='background-image' src={VenueBackground} alt="" />
</div>
<div className='landingcard'>
<div className='expand-container color-venue'>内容已展开</div>
<div className='buttons-container color-venue'>按钮...</div>
<img src={BackgroundImage1} alt="艺术家图片" />
</div>
<div className='landingcard'>
<div className='expand-container color-venue'>内容已展开</div>
<div className='buttons-container color-venue'>按钮...</div>
<img src={FansBackground} alt="" />
</div>
</div>
</div>
.landingcard{
position: relative;
}
.landingcard img {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 25px;
}
.landingcard .color-fans{
background-color: #549FE5F2;
opacity: 0.8;
}
.landingcard .color-venue{
background-color: #FABF4D;
opacity: 0.8;
}
.landingcard .color-artists{
background-color: #FFB08B;
opacity: 0.8;
}
.landingcard .expand-container {
z-index:0;
position: absolute;
bottom: 0;
width: 100%;
height: 15%;
display: flex;
border-radius: 25px;
align-items: center;
justify-content: center;
transition: height 0.3s, opacity 0.3s;
}
.landingcard:hover .expand-container {
height: 70%;
opacity: 1;
}
.landingcard .button-container {
z-index:1;
position:absolute;
bottom:0;
left:0;
right:0;
height:30px;
}
英文:
Try this:
I layered a separate button-container above an expand-container
<div className='container'>
<div className='options-containers'>
<div className='landingcard'>
<div className='expand-container color-venue'>Content Expanded</div>
<div className='buttons-container color-venue'>Buttons...</div>
<img className='background-image' src={VenueBackground} alt="" />
</div>
<div className='landingcard'>
<div className='expand-container color-venue'>Content Expanded</div>
<div className='buttons-container color-venue'>Buttons...</div>
<img src={BackgroundImage1} alt="Artists Picture" />
</div>
<div className='landingcard'>
<div className='expand-container color-venue'>Content Expanded</div>
<div className='buttons-container color-venue'>Buttons...</div>
<img src={FansBackground} alt="" />
</div>
</div>
</div>
.landingcard{
position: relative;
}
.landingcard img {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 25px;
}
.landingcard .color-fans{
background-color: #549FE5F2;
opacity: 0.8;
}
.landingcard .color-venue{
background-color: #FABF4D;
opacity: 0.8;
}
.landingcard .color-artists{
background-color: #FFB08B;
opacity: 0.8;
}
.landingcard .expand-container {
z-index:0;
position: absolute;
bottom: 0;
width: 100%;
height: 15%;
display: flex;
border-radius: 25px;
align-items: center;
justify-content: center;
transition: height 0.3s, opacity 0.3s;
}
.landingcard:hover .expand-container {
height: 70%;
opacity: 1;
}
.landingcard .button-container {
z-index:1;
position:absolute;
bottom:0;
left:0;
right:0;
height:30px;
}
答案2
得分: 0
对于这种元素,我使用了带有 grid-template-rows
的网格,使用固定值来设置按钮高度。然后我使用了 overflow: hidden
属性,以便当面板缩小尺寸时,文本被隐藏在其后面。不幸的是,填充似乎会影响它,所以我也不得不对填充进行过渡。无论如何,请看看,看看你的想法。我使用了自定义属性,所以你可以希望看到它是如何工作的。以下是带注释的代码:
.container {
--border-rad: 1rem;
--custom-colour: #61A1D6;
--button-height: 2rem;
--transition-time: 500ms;
position: relative;
display: inline-block; /* 让容器适应图像 */
border: 1px solid var(--custom-colour);
border-radius: var(--border-rad);
overflow: hidden; /* 剪切图像和边界半径内的任何子 div */
}
.container > img {
display: block; /* 消除下沉空间 */
object-fit: cover;
}
.panel {
position: absolute;
bottom: 0;
width: 100%;
height: var(--button-height);
display: grid; /* 我们制作了 2 行,顶部适合容器,底部是按钮高度 */
grid-template-rows: 1fr var(--button-height);
align-items: stretch;
border-radius: var(--border-rad);
background-color: var(--custom-colour);
color: white;
font-weight: bold;
opacity: 0.8;
transition-property: height, opacity;
transition-duration: var(--transition-time);
}
.text {
overflow: hidden; /* 当我们缩小高度时,使内容隐藏 */
padding: 0 1rem; /* 当它被隐藏时,将文本 div 的顶部和底部填充设置为 0,以防止它试图向下移动按钮 */
transition: padding var(--transition-time);
}
.buttons {
/* 使用简单的 2 列网格布局制作按钮 */
display: grid;
grid-template-columns: 1fr 1fr;
align-items: center;
text-align: center;
}
.buttons :first-child {
border-right: 1px solid white; /* 加入分隔线 */
}
.container:hover .panel {
height: 70%;
opacity: 1;
}
.container:hover .text {
padding: 1rem;
}
<div class="container">
<img src='https://picsum.photos/id/28/300/300'>
<div class='panel'>
<div class='text'>谈谈TuneHatch对于想要新体验的粉丝有多棒。</div>
<div class='buttons'>
<div>节目</div>
<div>注册</div>
</div>
</div>
</div>
希望这有所帮助。
英文:
For this sort of element, I've used grid with grid-template-rows
using a fixed value for the button height. I've then used the overflow: hidden
property so that when the panel shrinks in size, the text is hidden behind it. Unfortunately padding seems to screw it up so I've also had to transition that too. Anyway have a look and see what you think. I've used custom properties so you can hopefully see what it does. Annotated code below:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.container {
--border-rad: 1rem;
--custom-colour: #61A1D6;
--button-height: 2rem;
--transition-time: 500ms;
position: relative;
display: inline-block; /* make the container fit the image */
border: 1px solid var(--custom-colour);
border-radius: var(--border-rad);
overflow: hidden; /* clip the image and any child divs inside the border radius */
}
.container > img {
display: block; /* get rid of the space for descenders */
object-fit: cover;
}
.panel {
position: absolute;
bottom: 0;
width: 100%;
height: var(--button-height);
display: grid; /* we're making 2 rows, the top one fits the container, the bottom is the button height */
grid-template-rows: 1fr var(--button-height);
align-items: stretch;
border-radius: var(--border-rad);
background-color: var(--custom-colour);
color: white;
font-weight: bold;
opacity: 0.8;
transition-property: height, opacity;
transition-duration: var(--transition-time);
}
.text {
overflow: hidden; /* make this hide the contents when we shrink the height */
padding: 0 1rem; /*set the top and bottom padding of the text div to 0 when it's hidden so it doesn't try to move the buttons down */
transition: padding var(--transition-time);
}
.buttons {
/* Use a simple 2 column grid layout to make the buttons */
display: grid;
grid-template-columns: 1fr 1fr;
align-items: center;
text-align: center;
}
.buttons :first-child {
border-right: 1px solid white; /*put the dividing line in */
}
.container:hover .panel {
height: 70%;
opacity: 1;
}
.container:hover .text {
padding: 1rem;
}
<!-- language: lang-html -->
<div class="container">
<img src='https://picsum.photos/id/28/300/300'>
<div class='panel'>
<div class='text'>Talk about how great TuneHatch is for fans who want new experiences here.</div>
<div class='buttons'>
<div>Shows</div>
<div>Sign Up</div>
</div>
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论