[英]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.