简体   繁体   English

使用 CSS,即使不存在 DIV 元素,我如何才能防止一块空白空间折叠?

[英]Using CSS, how I can keep a block of white space from collapsing , even if there is no DIV element present?

I have the following elements: Title, Description, and Image.我有以下元素:标题、描述和图像。 There should be 20px space above the Title, like this:标题上方应该有 20px 的空间,如下所示:

像这样

As you can see from the picture, there is a block of space above the title.从图片中可以看出,标题上方有一块空格。 I want the space to remain there, even if the Title and Description are not present.我希望空间保留在那里,即使标题和描述不存在。 For example like this:例如像这样:

像这样

Right now my problem is that, if there is no title and description, then the space is collapsing and the image is moving up and basically occupying that space.现在我的问题是,如果没有标题和描述,那么空间正在崩溃,图像正在向上移动,基本上占据了那个空间。

How can I keep the space, even if there is no Title and Description present or not?即使没有标题和描述,我如何保留空间? I thought of applying min-height to the "titleName", but that has not worked.我想将min-height应用于“titleName”,但这没有奏效。

<div class="myComponent">
 <div class="myContainer">
   <div id="head_Title" class="mainTitle">
    <h2 class="titleName"> This is a Title</h2>
   </div>
   <div id="description" class="mainDescription"></div>
 </div>
</div>

If I am able to understand the question correctly, your problem would easily be solved by adding如果我能够正确理解这个问题,您的问题很容易通过添加来解决

margin-top: 20px;

to myComponent class我的组件myComponent

As I understand from your question, when we remove the div, the entire div is removed from DOM element and also the space gets removed.正如我从您的问题中了解到的那样,当我们删除 div 时,整个 div 都会从 DOM 元素中删除,并且空间也会被删除。

We can use the Visibility CSS property.我们可以使用可见性 CSS 属性。 When we use visibility:hidden property, the text content from div is hidden from the HTML DOM but not removed from the div tag.当我们使用visibility:hidden属性时,来自 div 的文本内容对 HTML DOM 隐藏,但不会从 div 标签中删除。

You can also dynamically add a class that contains hidden property.您还可以动态添加包含隐藏属性的 class。 The total space will be equivalent to the combined space of title and description.总空间将相当于标题和描述的组合空间。

I created a demo for reference with & without title and description, have a look here https://jsfiddle.net/alepsh/todenjsr我创建了一个没有标题和描述的演示供参考,看看这里https://jsfiddle.net/alepsh/todenjsr

暂无
暂无

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

相关问题 即使 header 元素不存在,如何保持 Header 元素上方的空间? - How to keep the space above an Header element, even if the header element is not present? 添加元素时如何通过展开/折叠 div 来制作动画 - How can I animate by expanding/collapsing a div when adding an element 即使使用react和css更改div中的内容,如何始终使div中的分隔符与div的顶部箭头保持同步? - How to keep the divider in a div in sync with the top arrow of the div always even if the content changes in the div using react and css? 使用CSS display时如何保持DOM中分配的空间:无 - how can I keep the space allocated in DOM when using CSS display:none 如何使用 CSS 标签作为选择器 select 某个元素/元素块? - How can I select a certain element/element block using their CSS tag as selector? 如何从引导容器中删除空白? - How can I remove white space from a bootstrap container? 如何让 div 的第一个图像元素默认被点击? - How can i keep the first Image Element of the div to be clicked by default? 如何避免过滤器列表崩溃? - How do I keep my filter list from collapsing? 我如何保持弹出 div 始终打开,即使在页面刷新/重新加载时,直到用户使用 HTML5 本地存储关闭它 - How can i keep a popup div always open, even on page refresh/reload until a user close it using HTML5 local storage 使用Javascript或CSS,如何在文本块的末尾添加额外的空间,但前提是它不能以新行开头? - Using Javascript or CSS, how can I add additional space at the end of a block of text, but only if it does not start on a new line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM