简体   繁体   English

文档流程和CSS样式:正确隐藏元素

[英]Document flow & CSS styling: Hiding elements properly

I'm running into an issue hiding graphs in a modal. 我遇到了在模式中隐藏图形的问题。 The user can click through the modal and the click events are hiding the other elements. 用户可以单击模式,而单击事件将隐藏其他元素。 However, some of the charts are out of the flow of the document by the hidden charts with position:absolute. 但是,某些图表由于位置为“绝对”的隐藏图表而超出了文档流程。 I read up on position: absolute on MDN and the elements should be placed to their closest parent and they're not. 我仔细阅读以下内容:在MDN上是绝对的,元素应放置在最接近的父元素上,而不是在最接近的父元素上。 Am I missing something? 我想念什么吗?

absolute The element is removed from the normal document flow; absolute从常规文档流中删除该元素; no space is created for the element in the page layout. 在页面布局中没有为元素创建空间。 Instead, it is positioned relative to its closest positioned ancestor if any; 相反,它相对于其最接近的祖先(如果有)定位。 otherwise, it is placed relative to the initial containing block. 否则,它相对于初始包含块放置。 Its final position is determined by the values of top, right, bottom, and left. 它的最终位置由top,right,bottom和left的值确定。 This value creates a new stacking context when the value of z-index is not auto. 当z-index的值不是auto时,此值将创建新的堆叠上下文。 Absolutely positioned boxes can have margins, and they do not collapse with any other margins. 绝对定位的框可以有边距,并且不会与其他任何边距一起折叠。

Screenshots 屏幕截图

Graph in intended flow 预期流程中的图形

Other Charts out of flow 其他图表流出

Code

$(".table-bordered").css({'visibility': 'hidden'});
$(".kendoOptionsLinear").css({'visibility': 'hidden'});
$(".kendoOptionsChart").css({'visibility': 'hidden'});
$(".kendoOptionsRadial").css({'visibility': 'visible'});  






<div  style="position:relative">
 <div kendo-chart  class="kendoOptionsChart" 
   k-options="options1" style="position:absolute;"></div>
 <div kendo-radialGauge class="kendoOptionsRadial" 
   k-options="options2" style="position:absolute;"></div>
 <div kendo-linearGauge class="kendoOptionsLinear" 
   k-options="options3" style="position:absolute;"></div>
 <div kendo-radialGauge class="kendoOptionsRadial" 
   k-options="options4" style="position:absolute;"></div> 
</div

When you apply the CSS style visibility: hidden , the element you have hidden is still part of the document flow. 当您应用CSS样式visibility: hidden ,您隐藏的元素仍然是文档流的一部分。 Though you cannot see the element, it takes up space in the layout and affects where sibling elements appear in the layout. 尽管您看不到元素,但它会占用布局中的空间并影响同级元素在布局中的显示位置。

If you want to hide an element and not have that element continue to take up space and affect where other elements appear, use display: none 如果要隐藏元素而不让该元素继续占用空间并影响其他元素的显示位置,请使用display: none

Elements with absolute position are taken out of the document flow. 具有绝对位置的元素将从文档流中取出。 Their position is relative to the viewport. 它们的位置相对于视口。 For example, an absolutely positioned element with top: 10px; left: 10px; 例如,绝对定位的元素的top: 10px; left: 10px; top: 10px; left: 10px; would appear in the upper left hand corner of the viewport, 10 pixels from the top and 10 pixels from the left. 将出现在视口的左上角,从顶部开始10像素,从左侧开始10像素。 If however, the parent container has position: relative , the absolute position of the child will be relative to its parent. 但是,如果父容器的position: relative ,则子容器的绝对位置将相对于其父容器。 Thus, the child would be 10 pixels from the top and 10 pixels from the left of its parent. 因此,子级将比其父级的顶部高10个像素,而左侧则距左侧10个像素。 not the viewport. 不是视口。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM