html - 相对位置和绝对位置不能正常工作

标签 html css positioning

根据官方文档,

position absolute:

position an element to his first anchestor relatively positioned. So, i want to realize five stars with a number inside them (at his centre). Every star and number is placed into a container positioned relatively, but i noted that the number are always positioned absolutly respect to the FIRST container instead of his nearest parent. In fact numbers overlpas each other.

http://jsfiddle.net/FMKMr/

HTML

<div id="vota">
      <div class="contStella" id="stellauno"><img class="stellaVota" src="http://cdn5.iconfinder.com/data/icons/super-mono-reflection/yellow/star_yellow.png"/> <div class="numeroVota">1</div> </div>
      <div class="contStella" id="stelladue"><img class="stellaVota" src="http://cdn5.iconfinder.com/data/icons/super-mono-reflection/yellow/star_yellow.png"/> <div class="numeroVota">2</div> </div>
      <div class="contStella" id="stellatre"><img class="stellaVota" src="http://cdn5.iconfinder.com/data/icons/super-mono-reflection/yellow/star_yellow.png"/> <div class="numeroVota">3</div> </div>
</div>  

CSS

 .contStella
    {
        position:relative;

    }

    .numeroVota
    {
       position:absolute;
        font-size:40px;
        margin-left:15px;
        color:blue;
    }

    .stellaVota
    {
        float:left;
        height:50px;
    }

最佳答案

只需将 display: inline-block; 添加到 contStella 类中,如下所示:

.contStella
{
    position:relative;
    display: inline-block;
}

.numeroVota
{
   position:absolute;
    font-size:40px;
    margin-left:15px;
    color:blue;
}

.stellaVota
{
    float:left;
    height:50px;
}

http://jsfiddle.net/FMKMr/3/

关于html - 相对位置和绝对位置不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17472147/

相关文章:

HTML5 Canvas 图层难题(z-index)

javascript - 动态添加 Remove font awesome in to span?

html - 在整个菜单栏中显示背景

html - 轮播仅在小屏幕上显示全宽屏幕

css - 我该怎么做文本溢出 : ellipsis on two lines?

css - 双列主题,帮助,使两列分别可滚动

html - checkbox状态改变时如何防止页面跳转?

CSS偏移属性和静态位置

html - CSS定位溢出

html - 我的 div 重叠。我希望他们肩并肩,但也有反应