我的div无论如何都不会居中,因为它们彼此冲突,我知道可以使它们以top,transform: translate()居中,但当我调整大小时,它看起来很糟糕,以下是代码:

.question-text {
  position: relative;
  font-size: 3rem;
  width: 50vw;
  height: 57vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.question-text2 {
  position: absolute;
  font-size: 3rem;
  width: 50vw;
  height: 0vh;
  display: flex;
  text-align: center;
  justify-content: center;
  padding: 0;
  text-shadow: 1px 1px 2px;
  left: calc(50vw + 1rem); 
  top: 26%; 
}
#valueIndex2{
  position: relative;
  color: salmon;
  z-index: 104;
  font-size: 3rem; /* Adjust the value as needed */
  width: 50vw;
  height: 50vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  margin: 0;
padding: 0;
text-align: center;
}



#valueIndex{ 
   color: salmon; 
   position: absolute;
   font-size: 3rem;
   width: 49vw;
   height: 0vh;
   display: flex;
   text-align: center;
   justify-content: center;
   padding: 0;
   left: calc(50vw + 1rem); /* Adjust the value as needed */
     top: 35%; /* Adjust the value as needed */
margin: 0;
padding: 0;
  overflow: hidden;
}
<div class="pack-term">
        <h2 class="question-text"> 
            <span>${item2.name}</span>
          </h2> 
              <h3 id="valueIndex2"> 
                <span>${item2.value}</span>  
            </h3> 
        </div>

        <div class="pack-term">
            <h3 id="valueIndex"> 
                <span>${item1.value}</span>  
            </h3> 
          <h2 class= "question-text2"> 
        <span>${item1.name}</span> 
     </h2>

推荐答案

我稍微编辑了一下您的代码:

Css:我删除了text-alignjustify-content,编辑了left

.question-text2 {
 position: absolute;
 font-size: 3rem;
 width: 50vw;
 height: 0vh;
 display: flex;

 padding: 0;
 text-shadow: 1px 1px 2px;
 left: calc(13vw + 6rem); 
 top: 9%; 
 }

Css:如果你想向上或向下移动question-text,你只需要使用top就可以了.您可以使用此代码顶部向下或向上移动所有内容.

.question-text{
  top: -13px;
}

超文本标记语言:你必须在你的代码中加<center>

<center>
 <div class="pack-term">
    <h2 class="question-text"> 
        <span>${item2.name}</span>
      </h2> 
          <h3 id="valueIndex2"> 
            <span>${item2.value}</span>  
        </h3> 
    </div>
  
    <div class="pack-term">
        <h3 id="valueIndex"> 
            <span>${item1.value}</span>  
        </h3> 
      <h2 class= "question-text2"> 
    <span>${item1.name}</span> 
 </h2>
 </div>
 </center>

Html相关问答推荐

应用于文本而不是弹性容器的Flexbox属性

CSS自定义特性中的URL是否可以相对于定义的CSS文件?

需要帮助实现悬停动画效果

容器内的SVG图像没有响应

在悬停时应用文本装饰

如何将内容元素放在侧边栏旁边?

如何使用*ngFor将模板从父级传递到子级

如何在css中在span中绘制圆弧?

如何使 CSS 网格项目像 Flexbox 项目一样匹配父级的高度

如何在 Bootstrap 5 中将两个导航栏元素放在末尾?

为什么 Flexbox 中相邻元素上的 flex:1 会影响另一个元素的固定宽度?

对齐页面左侧的单选按钮

不完整的悬停

SVG 调整大小而不是溢出

增加第一个字母的大小不再正确居中文本

如何通过可见文本使用 Selenium 定位元素

发光效果html动画

如何使用 :before 在 CSS 中为列表增加计数器

无法使用 HTML 中的 Bootstrap 自上而下居中

AWS SES:如何正确使用 CSS