如何将元素直接放在居中元素的右侧?

时间:2020-07-16 17:20:36

标签: html css flexbox

所以我有一个看起来像这样的元素:screenshot

根据设计师的计划,我需要加一点“?”就在文字右侧(8.00 $)。 我使用flexbox将元素居中。基本上,我需要?直接在数字的右边我不希望此元素将数字向左推。因此,这8个居中居中?直接向右移动而不更改8的位置。

我做了自己的研究,给出的所有解决方案都使用绝对定位。我的意思是,它可以工作,但是如果我的人数更多,该?可能会超过文本。

您有什么建议吗?

代码,我不知道它是否可以工作:

/* weirdly, display:flex and width:100% have a stroke over them in the console */

.compare-row-small-box {
  display: flex;
  text-align: center;
  width: 100%;
  line-height: 1;
  padding-top: 1rem;
  padding-bottom: 1rem;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.compare-row-box-title {
  --text-opacity: 1;
  color: #4a5568;
  color: rgba(74, 85, 104, var(--text-opacity));
  font-size: 12px;
}

.compare-row-box-content {
  font-size: 16px;
  display: flex;
  flex: 1 1;
  flex-direction: column;
  justify-content: flex-start;
}
<div class="space-y-1 compare-row-small-box">
  <p class="compare-row-box-title">Frais mensuels estimés</p>
  <div class="compare-row-box-content"><span>8.00 $</span></div>
</div>

3 个答案:

答案 0 :(得分:1)

在内容后使用CSS可以添加?在$

之后

.compare-row-small-box {
  display: flex;
  text-align: center;
  width: 100%;
  line-height: 1;
  padding-top: 1rem;
  padding-bottom: 1rem;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.compare-row-box-title {
  --text-opacity: 1;
  color: #4a5568;
  color: rgba(74, 85, 104, var(--text-opacity));
  font-size: 12px;
}

.compare-row-box-content {
  font-size: 16px;
  display: flex;
  flex: 1 1;
  flex-direction: column;
  justify-content: flex-start;
}

.compare-row-box-content span {
  position: relative; 
}
.compare-row-box-content span::after{
  content: '?';
  position: absolute;
}
<div class="space-y-1 compare-row-small-box">
  <p class="compare-row-box-title">Frais mensuels estimés</p>
  <div class="compare-row-box-content"><span>8.00 $</span></div>
</div>


<div class="space-y-1 compare-row-small-box">
  <p class="compare-row-box-title">Frais mensuels estimés</p>
  <div class="compare-row-box-content x"><span>1238.00 $</span></div>
</div>

答案 1 :(得分:0)

放入“?”在这样的:after伪元素中。

/* weirdly, display:flex and width:100% have a stroke over them in the console */

.compare-row-small-box {
  display: flex;
  text-align: center;
  width: 100%;
  line-height: 1;
  padding-top: 1rem;
  padding-bottom: 1rem;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.compare-row-box-title {
  --text-opacity: 1;
  color: #4a5568;
  color: rgba(74, 85, 104, var(--text-opacity));
  font-size: 12px;
}

.compare-row-box-content {
  font-size: 16px;
  display: flex;
  flex: 1 1;
  flex-direction: column;
  justify-content: flex-start;
}

.compare-row-box-content span::after {
  content: '?';
  position: absolute;
  margin-left: 5px;
}
<div class="space-y-1 compare-row-small-box">
  <p class="compare-row-box-title">Frais mensuels estimés</p>
  <div class="compare-row-box-content"><span>8.00 $</span></div>
</div>

答案 2 :(得分:0)

使用pseudo-element添加您的角色,并使用相对位置为其留出一定的空间。

/* weirdly, display:flex and width:100% have a stroke over them in the console */

.compare-row-small-box {
  display: flex;
  text-align: center;
  width: 100%;
  line-height: 1;
  padding-top: 1rem;
  padding-bottom: 1rem;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.compare-row-box-title {
  --text-opacity: 1;
  color: #4a5568;
  color: rgba(74, 85, 104, var(--text-opacity));
  font-size: 12px;
}

.compare-row-box-content {
  font-size: 16px;
  display: flex;
  flex: 1 1;
  flex-direction: column;
  justify-content: flex-start;
}

.compare-row-box-content span::after {
  content: "?"; 
  color: darkorange;
  font-weight: 700;
  position: relative;
  left: 0.5em;
  opacity: 0.5;
  transition: opacity 300ms linear;
}

.compare-row-box-content:hover span::after {
  opacity: 1;
}
<div class="space-y-1 compare-row-small-box">
  <p class="compare-row-box-title">Frais mensuels estimés</p>
  <div class="compare-row-box-content"><span>8.00 $</span></div>
</div>

<div class="space-y-1 compare-row-small-box">
  <p class="compare-row-box-title">Frais mensuels estimés</p>
  <div class="compare-row-box-content"><span>800000.00 $</span></div>
</div>