如何才能使链接的宽度仅为文本宽度的宽度?

时间:2014-12-23 12:56:21

标签: html css hover

我设法编写了一个功能,在链接的标题上悬停链接的信息将会淡入,但每当我将鼠标悬停在标题文本的剩余空间上时,它会触发链接的信息淡入淡出-在。有没有办法我只能获得链接标题的激活宽度?我也在寻找一个只有CSS的解决方案。如果您能想出一种更有效的方法来实现这一目标,请告诉我:)

Fiddle here

这是问题的可视化,因为我不太熟悉代码/开发术语: enter image description here

非常感谢提前!

HTML:

<div class="page">
    <div class="text">
        <span class="title">Title &#40;More&#41;</span>
        <span class="info">
            Title Description over here
        </span>
    </div>
    <div class="image">
        <img src="..." alt="" />
    </div>
</div>

CSS:

html, body {
    margin: 0;
    padding: 0;
    width:100%;
    height:100%;
}
body {
    font-family: helvetica;
    font-size: 18px;
    line-height: 26px;
}
div.page {
    width:80%;
    max-width:960px;
    margin:0 auto;
    height:100%;
}
div.text {
    width:80%;
    max-width:960px;
    margin:0 auto;
}
div.text span.title {
    -webkit-transition: all .5s; 
    transition: all .5s;
}
div.text:hover span.title {
    opacity: 0;
    visibility: hidden;
}
div.text span.info {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;  
    opacity: 0;
    visibility: hidden;
    -webkit-transition: all .5s; 
    transition: all .5s;
}
div.text:hover span.info {
    opacity: 1;
    visibility: visible;
}
.image img {
    width: 100%;
    height: auto;
    padding-top: 10px;
}
.text{
    position: absolute;
    z-index: 9999;
}
.image{
    position:relative;
    top:20px;
}

2 个答案:

答案 0 :(得分:0)

因为您的悬停事件位于div,这是一个块级别(宽度:100%)元素。

您需要将其设置为显示内联或宽度自动,以便它不占用整个宽度,然后在悬停时恢复:

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}
body {
  font-family: helvetica;
  font-size: 18px;
  line-height: 26px;
}
div.page {
  width: 80%;
  max-width: 960px;
  margin: 0 auto;
  height: 100%;
}
div.text {
  display: inline-block;
  max-width: 960px;
  margin: 0 auto;
}
div.text:hover {
  display: block;
  width: 80%;
}
div.text span.title {
  -webkit-transition: all .5s;
  transition: all .5s;
}
div.text:hover span.title {
  opacity: 0;
  visibility: hidden;
}
div.text span.info {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  opacity: 0;
  visibility: hidden;
  -webkit-transition: all .5s;
  transition: all .5s;
}
div.text:hover span.info {
  opacity: 1;
  visibility: visible;
}
.image img {
  width: 100%;
  height: auto;
  padding-top: 10px;
}
.text {
  position: absolute;
  z-index: 9999;
}
.image {
  position: relative;
  top: 20px;
}
<div class="page">
  <div class="text">
    <span class="title">Title &#40;More&#41;</span>
    <span class="info">
            Title Description over here<br/>
            More text<br/>
            More text<br/>
            And you know, text.<br/>
        </span>
  </div>
  <div class="image">
    <img src="http://www.graphicthoughtfacility.com/media/uploads/images/FW-SmallTray-WEB.jpg" alt="" />
    <img src="http://www.graphicthoughtfacility.com/media/uploads/images/FW-LargeBox-WEB.jpg" alt="" />
    <img src="http://www.graphicthoughtfacility.com/media/uploads/images/Formwork1_RGB-WEB.jpg" alt="" />
  </div>
</div>

答案 1 :(得分:0)

div.text宽度为80%,因此您可以将鼠标悬停在右侧。为div.text增加一个较小的宽度,并使span.info的宽度更大。

div.text {
    width:15%;
    max-width:960px;
    margin:0 auto;
}

div.text span.info {
    width: 250px;       
    position: absolute;
    top: 0;
    right: 0;
    left: 0;  
    opacity: 0;
    visibility: hidden;
    -webkit-transition: all .5s; 
    transition: all .5s;
}

See Fiddle