简体   繁体   English

如果父级高度短,则在父级内创建一个相对子元素

[英]Make a relative child element inside parent if parent height is short

Recently I had a similar SO question with a brilliant answer.最近我有一个类似的SO 问题,答案很好。 However, that answer and that question are not applicable to today's situation.但是,那个答案和那个问题不适用于今天的情况。 Now the button has text under it.现在按钮下面有文本。 Here is the code: https://jsfiddle.net/Lo4362rk/这是代码: https://jsfiddle.net/Lo4362rk/

button {
  float: right;
  position: relative;
  right: 10%;
  top: 100px;
  max-top: 10px;
  background-color: green;
}
.third-party-block {
  //display: none;
}

and

<div style="border:1px solid red;">
  <button>
   The main thing in the text is here =>
  </button>
  <p class="third-party-block">
     some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here 
  </p>
</div>

The.third-party-block is dynamic and sometimes it is not displayed. .third-party-block 是动态的,有时不显示。 As result, the parent becomes shorter and the button becomes outside of the parent because of top: 100px;结果,由于 top: 100px; ,父级变得更短,并且按钮变得超出了父级。 The question is how to push the button inside of the parent let's say to the 10px from the top (not from 100px).问题是如何按下父级内部的按钮让我们说从顶部到 10px(而不是从 100px)。

Pure CSS solution is needed.需要纯 CSS 解决方案。

在此处输入图像描述

use position:absolute where you can set max-height / min-height and you consider the height with percentage value:使用 position:absolute 可以设置max-height / min-height并考虑百分比值的height

 .parent { position:relative; }.button { position:absolute; right:10%; top:0; height:40%; min-height:30px; max-height:120px; display:flex; flex-direction:column; } button { margin-top:auto; background-color: green; }
 <div style="border:1px solid red;" class="parent"> <div class="button"><button> The main thing in the text is here => </button></div> <p class="third-party-block"> some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here somesome text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some </p> </div> <div style="border:1px solid red;" class="parent"> <div class="button"><button> The main thing in the text is here => </button></div> <p class="third-party-block"> some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here </p> </div> <div style="border:1px solid red;" class="parent"> <div class="button"><button> The main thing in the text is here => </button></div> <p class="third-party-block"> some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes here some text comes </p> </div> <div style="border:1px solid red;" class="parent"> <div class="button"><button> The main thing in the text is here => </button></div> <p class="third-party-block"> some text comes here some text comes here some text comes here some text comes eme text comes here some text comes here some text comes here some text comes </p> </div> <div style="border:1px solid red;" class="parent"> <div class="button"><button> The main thing in the text is here => </button></div> <p class="third-party-block"> some text </p> </div>

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

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