Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 在改变高度和宽度的元素中居中垂直文本_Html_Css - Fatal编程技术网

Html 在改变高度和宽度的元素中居中垂直文本

Html 在改变高度和宽度的元素中居中垂直文本,html,css,Html,Css,我有以下代码 HTML 我已经尝试了一系列关于堆栈溢出的技术,但是只有知道宽度和高度时,一切似乎都是有效的。当您调整浏览器时,这两个属性都将更改。有没有办法使文本始终垂直居中?我在stackoverflow上也发现了这个技巧,我会在再次找到它时添加一个链接: height: auto; // (this is the default) position: absolute; top: 50%; transform: translateY(-50%); 这是因为translateY获取元素的高

我有以下代码

HTML


我已经尝试了一系列关于堆栈溢出的技术,但是只有知道宽度和高度时,一切似乎都是有效的。当您调整浏览器时,这两个属性都将更改。有没有办法使文本始终垂直居中?

我在stackoverflow上也发现了这个技巧,我会在再次找到它时添加一个链接:

height: auto; // (this is the default)
position: absolute;
top: 50%;
transform: translateY(-50%);
这是因为
translateY
获取元素的高度,而
top
使用周围元素的高度


我在stackoverflow上也发现了这个技巧,我将在再次找到它时添加一个链接:

height: auto; // (this is the default)
position: absolute;
top: 50%;
transform: translateY(-50%);
这是因为
translateY
获取元素的高度,而
top
使用周围元素的高度


您应该尝试使用
flexbox
,如果可能的话,对html进行一些更改

在我看来,为了实现这一目标,我将使用:

HTML

我刚刚为充当容器的标签添加了图像作为
背景图像
,并使用了
display:flex;证明内容:中心;对齐项目:居中
以水平和垂直居中文本和容器内的所有元素

您可以调整
.col inner
高度
,并看到无论类有多高,它都将始终居中

我已经设置了一个代码笔来使用它:


请记住,如果要使用
flexbox
,应在代码前面加前缀。使用此网站查看您应该添加到
flexbox
的前缀和相关属性。

您应该尝试使用
flexbox
,如果可能,在html中进行一些更改

在我看来,为了实现这一目标,我将使用:

HTML

我刚刚为充当容器的标签添加了图像作为
背景图像
,并使用了
display:flex;证明内容:中心;对齐项目:居中
以水平和垂直居中文本和容器内的所有元素

您可以调整
.col inner
高度
,并看到无论类有多高,它都将始终居中

我已经设置了一个代码笔来使用它:

请记住,如果要使用
flexbox
,应在代码前面加前缀。使用此网站查看您应该向flexbox添加哪些前缀以及相关属性。

可能重复的
height: auto; // (this is the default)
position: absolute;
top: 50%;
transform: translateY(-50%);
<a href="#" class="col-inner">
  <div class="content">
    <h3>Hello World</h3>
    <p>Lorem Ipsum Dolor Sit Amet</p>
  </div>
</a>
    .col-inner{
  max-width: 100%;
  min-height: 150px;
  position: relative;
  background-image: url('http://placehold.it/450x450');
  background-repeat: no-repeat;
  background-size: cover;
  background-position: 50% 50%;
  display: flex;
  justify-content:center;
  align-items:center;
}
.content{
  color: #ff0000;
}