【问题标题】:Prevent items from moving around in the DOM with CSS when other elements get displayed当显示其他元素时,使用 CSS 防止项目在 DOM 中移动
【发布时间】:2013-06-19 18:31:18
【问题描述】:

在下面的小提琴中:

http://jsfiddle.net/LkqTU/10002/

我试图将2A 保持在原位,而在引入1A1B 时不动。我尝试过使用位置和显示属性,但不知道如何将2A 保持在原位而不移动。

CSS:

.first {
    float: right;
    position: relative;
    bottom: 30px;

}
.second {
    float: right;
    position: relative;
    left: 24px;
}
.container {
    width: 170px;
}

HTML:

<div class="container">
    <p>title:</p>
    <input> 
        <!-- ko if: showTop -->
    <span data-bind="if: showFirst">
          <i class="first">1A</i>
    </span>
    <span data-bind="if: !showFirst()">
           <i class="first">1B</i>
    </span>
        <!-- /ko -->
     <span data-bind="if: showSecond">
           <i class="second">2A</i>
    </span>

    <button data-bind="click: toggleFirst">toggle 1 value</button>
    <button data-bind="click: toggleFirstVisibility">toggle 1 visibility</button>
    <p data-bind="text: showFirst"></p>
    <p data-bind="text: showSecond"></p>
</div>

我怎样才能让2A 留在原地?

【问题讨论】:

    标签: css knockout.js knockout-2.0


    【解决方案1】:

    你可以使用绝对位置:

    .first {      
        position: absolute;
        top: 15px;
        left: 160px;
    
    }
    .second {
        position: absolute;
        left: 160px;
    }
    .container {
        width: 170px;
    }
    

    see fiddle

    希望对你有帮助。

    【讨论】:

    • 在没有绝对定位的情况下有什么办法可以做到这一点?容器中的其他元素可能会改变大小,所以我想以某种方式相对于父元素定位:/
    • 从头开始 - 我通过将父元素设置为 position: relative 来使用您的代码。谢谢:)
    • 在一个元素上同时应用floatposition: absolute(或fixed)是没有意义的。 position 覆盖任何 float 行为。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 2013-05-14
    • 1970-01-01
    相关资源
    最近更新 更多