css - 将文本放置在图库中间,使其具有响应性
问题描述
如何将标题放置在图库顶部,如下图所示。
我正在使用我的主题附带的画廊简码。
我可以使用 position:absolute; 以及顶部和底部值,然后将其与 display:block 居中;和边距:0自动;。然而,当我改变屏幕的大小时,它就变得不正常了。
html是
<div class="gallery-column">
[shortcode goes here]
<h2>Title goes here</h2>
</div>
解决方案
保持元素水平和垂直居中的最简单方法是使用flexbox。
您需要的所有父元素是:
display: flex
将元素视为 flexboxalign-items: center
用于垂直对齐justify-content: center
用于水平对齐- 一个固定的
height
(创建填充) -100vh
全屏
这可以在下面看到:
body {
margin: 0;
}
.gallery-column {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
<div class="gallery-column">
<div class="centered">
<h2>Centered Element</h2>
</div>
</div>
无论屏幕大小如何,相关元素都将保留在页面的确切中心。
在您的情况下,您还需要 az-index
以确保元素保持在顶部。z-index: 1
应该足够了。
推荐阅读
- laravel - Laravel - 使用 Rule::unique 验证输入
- javascript - jQuery-modal ajax child 中的 Javascript 失败
- html - 具有相同宽度的两个元素显示的宽度不同
- matlab - 是否可以抑制“输入”跳转到下一行?
- javascript - 将 R Shiny Code 发送到 Javascript 时会多次执行
- ruby - 有什么方法可以在 Ruby 中将多个属性/方法与 & 和冒号运算符一起使用?
- javascript - 操作视口宽度单位 vw
- java - 如何仅使用“switch and if”这样的输入“January”或“janUARY”在Java解决方案中创建
- c++ - 在封闭范围内重新声明变量是未定义的行为吗?
- python - 将 csv 读取到 pandas 保留原样的值