简体   繁体   English

1 个 div 内有 2 个元素,我希望另一个元素与第一个元素一起缩放

[英]2 elements inside 1 div, i want the other element to scale with the first one

I have a button component with a little accent styling on the left of the button.我有一个按钮组件,在按钮的左侧带有一点重音样式。 I want the button to get height based on the content.我希望按钮根据内容获取高度。 The "text"-variable could be any text, so it could change any time. “text”变量可以是任何文本,因此它可以随时更改。 i want the button accent to scale with the text, but I can't use 100% or anything like that cause its parent doesnt have any sett height (because I want it to fit content).我希望按钮重音与文本一起缩放,但我不能使用 100% 或类似的东西,因为它的父级没有任何设置高度(因为我希望它适合内容)。 The button_accent do get height when i set manually, but i want it dynamicly当我手动设置时,button_accent 确实会获得高度,但我希望它是动态的

  <div className={`${buttonStyles.button}`} >
    <div className={`${buttonStyles.button_accent} h-full`}></div>
    <div className="mr-3 ml-3 justify-start w-full flex items-center">
      {text}
    </div>
  </div>

Button.module.css:按钮.module.css:

.button {
  display: flex;
  height: auto;
  min-height: 80px;
  border: 1px solid #d8fc9133;
  font-size: 24px;
  color: white;
  font-weight: medium;
  background: rgb(128, 128, 128, 0.5);
  align-items: center;
}
.button:hover {
  background: rgb(128, 128, 128, 1);
  cursor: pointer;
}
.button_accent {
  width: 12.77px;
  background-color: #d8fc91;
}

Heres the button with the accent with height set to 65px.这是带有高度设置为 65px 的重音的按钮。 (with no height set, the accent gets 0 height) (没有设置高度,重音的高度为 0)

在此处输入图片说明

You could add position: relative to the parent container .button and then absolutely position the first div .button_accent relative to the parent.您可以添加position: relative to the parent container .button然后绝对定位第一个 div .button_accent相对于父容器。 This way you don't need to set any explicit absolute length unit for the height value of .button_accent but now can use height: 100% and let the accent container grow or "scale" to match the height of its parent.这样你就不需要为.button_accentheight值设置任何明确的绝对长度单位,但现在可以使用height: 100%并让重音容器增长或“缩放”以匹配其父级的高度。

If you remove the text content you will see that the accent still displays and scales to fill its parent which is forced to be at minimum 80px in height from your min-height: 80px declaration.如果您删除文本内容,您将看到重音仍然显示并缩放以填充其父级,该父级的高度必须至少为min-height: 80px声明的min-height: 80px When you get rid of this explicit min-height and there isn't text content in the <td> , the accent mark wont display as the parents content box is empty with zero height.当您摆脱此显式min-height并且<td>没有文本内容时,重音标记不会显示,因为父内容框为空且高度为零。

After positioning the elements, we can use height: 100% or another relative length unit for the accent to scale with the text because its absolutely positioned relative to the parent container which is a flexbox and has a auto height and min-height of 80px.定位元素后,我们可以使用height: 100%或另一个相对长度单位让重音与文本一起缩放,因为它相对于父容器是绝对定位的,父容器是一个 flexbox 并且具有 80px 的auto高度和min-height If you don't want the accent to occupy 100% of the parent containers height, but to have some top/bottom space like shown in your included image, just use height: 80% or some relative length unit to size the accent to your liking.如果您不希望重音占据父容器高度的 100%,但要像包含的图像中显示的那样有一些顶部/底部空间,只需使用height: 80%或一些相对长度单位来调整重音的大小喜欢。

 .button { position: relative; display: flex; height: auto; min-height: 80px; border: 1px solid #d8fc9133; font-size: 24px; color: white; font-weight: medium; background: rgb(128, 128, 128, 0.5); align-items: center; } .button:hover { background: rgb(128, 128, 128, 1); cursor: pointer; } .button div:last-child { margin: 0 1.5rem; } .button_accent { position: absolute; left: 0; width: 12.77px; height: 80%; /* 100% fills entire height of parent */ background-color: #d8fc91; }
 <div class="button"> <div class="button_accent"></div> <div class="mr-3 ml-3 justify-start w-full flex items-center"> Some text </div> </div>

I recommend, you need to set the line-height value to implement your goal.我建议,您需要设置line-height值来实现您的目标。 Check out the following article .查看以下文章

There may also be a way to calculate the accent height by counting the number of button text strings in a responsive manner.还可以通过以响应方式计算按钮文本字符串的数量来计算accent height

However, it can be implemented simply by putting an accent in the string as in the following code.但是,它可以简单地通过在字符串中添加一个accent来实现,如下面的代码所示。

  <div id="content" className={`flex m-0 p-0 leading-none`}>
    <div className={`flex h-auto w-4 mr-3 bg-green-400`} />
    Søk i kart
    <br />
    asdfasdf
  </div>

try it, happy coding :)试试吧,快乐编码:)

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

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