【问题标题】:Make div height fit hidden hover content使 div 高度适合隐藏的悬停内容
【发布时间】:2018-08-16 01:24:02
【问题描述】:

我为画廊类别创建了一系列图片链接。每个“按钮”在背景中都有一个图像,并且画廊标题覆盖它。我已将其设置为更改背景,并且标题也更改为使用 css 悬停时的画廊,但我遇到的问题是高度。正在加载 div 以适应标题,但随后在鼠标悬停时跳得更大以容纳描述文本。我需要它加载足够大以使描述文本很好地适合其中。

我希望它具有响应性,因此文本需要能够换行,并且 div 需要相应地扩展(并且永远不会在鼠标悬停时跳跃高度)。所以高度不能是固定的高度,否则当 div 是全宽时,这些高度会太高。我在这里设置了一个测试

body{
    font-family: Arial, Helvetica, sans-serif;
}



.item1{
    width: auto;
     display: table-cell;
        
}
a{
    text-decoration:none;
}
.label1 {
    padding-top: 20px;
    padding-bottom: 20px;
    text-align: center;
    font-size: 24px;
    font-weight: bold;
    color: #ffffff;
    text-transform: uppercase;
    white-space: nowrap;
    text-decoration: none;
    background: url('https://premium.wpmudev.org/blog/wp-content/uploads/2015/02/fullwidth-small.png') no-repeat; 
    background-position: center top;
    background-size: 100% auto;
    transition: background 0.5s ease;

}
.label1.success {
    background-color: #FFFFFF;
}

.label1:hover {
    width: auto;
    height: auto;
    padding-left: 20px;
    padding-right: 20px;
    background: #FFFFFF ;
    background-position: center top;
    background-size: 100% auto;
     -webkit-box-shadow:inset 0px 0px 0px 4px #000000;
    -moz-box-shadow:inset 0px 0px 0px 4px #000000;
    box-shadow:inset 0px 0px 0px 4px #000000;
}

.item1 a p.new-label1 span{
  position: relative;
  content: 'NEW'
}
.item1:hover a p.new-label1 span{
  display: none;
}
.item1:hover a p.new-label1:after{
    content:attr(data-title);
    white-space: normal !important;
    overflow-wrap: normal;
    font-size: 14px;
    color: #000000;
}
<div style="width: 50%; display: table;">

<div class="item1">


    <a href="">
         <p class="label1 success new-label1" data-title="I Show up on Hover and have lots and lots of things to say and it takes up too much space" ><span class="align">New</span></p>
    </a>
</div>


</div>
<div class="cleafix">
</div>

我也遇到了过渡效果的问题,但这个问题要紧迫得多。也就是说,如果有人想给我一个解决方案,我一点也不介意;)

【问题讨论】:

    标签: css hover height mouseover


    【解决方案1】:

    我不知道这正是你想要的,但我根据自己的理解做了演示。它可能会有所帮助

    body {
    	font-family: Arial, Helvetica, sans-serif;
    }
    .item1 {
    	width: auto;
    	display: table-cell;
    }
    a {
    	text-decoration: none;
    }
    .label1 {
    	position: relative;
    	text-align: center;
    	font-size: 24px;
    	font-weight: bold;
    	color: #ffffff;
    	text-transform: uppercase;
    	white-space: nowrap;
    	text-decoration: none;
    	background: url('https://premium.wpmudev.org/blog/wp-content/uploads/2015/02/fullwidth-small.png') no-repeat;
    	background-position: center top;
    	background-size: cover;
    	transition: all .4s;
    	margin: 0;
    }
    .label1.success {
    	background-color: #fff;
    }
    .item1:hover .label1 {
    }
    .item1 a p.new-label1 span {
    	content: 'NEW';
    	position: absolute;
    	right: 0;
    	left: 0;
    	top: 50%;
    	transform: translateY(-50%);
    	opacity: 1;
    	visibility: visible;
    	transition: all .4s;
    }
    .item1:hover a p.new-label1 span {
    	opacity: 0;
    	visibility: hidden;
    }
    .item1 a p.new-label1:after {
    	content: attr(data-title);
    	background-color: transparent;
    	white-space: normal;
    	overflow-wrap: normal;
    	font-size: 14px;
    	color: #000;
    	padding: 20px 10px;
    	opacity: 0;
    	visibility: hidden;
    	display: block;
    	transition: all .4s;
    }
    .item1:hover a p.new-label1:after {
    	content: attr(data-title);
    	background-color: #fff;
    	opacity: 1;
    	visibility: visible;
    	-webkit-box-shadow: inset 0px 0px 0px 4px #000000;
    	-moz-box-shadow: inset 0px 0px 0px 4px #000000;
    	box-shadow: inset 0px 0px 0px 4px #000000;
    }
    <div style="width: 50%; display: table;">
        <div class="item1">
            <a href="">
                 <p class="label1 success new-label1" data-title="I Show up on Hover and have lots and lots of things to say and it takes up too much space" ><span class="align">New</span></p>
            </a>
        </div>
    </div>
    <div class="cleafix"></div>

    【讨论】:

    • 是的!这适用于第一个,但我只是在下一个上尝试过,它没有相同数量的文本,并且框现在具有不同的高度和不同的宽度。你让我开始了表格和表格单元路径,但我已经改变了我的覆盖方式。你能用表格和单元格看看我的小提琴,看看你是否能弄清楚为什么我不能让它们都保持相同的高度? jsfiddle.net/graphicfixations/0vhsu5ce
    • 我在一个新问题中进一步写了出来。stackoverflow.com/questions/49178088/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 2016-05-12
    • 2016-01-02
    • 1970-01-01
    相关资源
    最近更新 更多