html - 在div中垂直居中文本

标签 html css

我有this div其中包含一个正方形内的一些文本。我希望里面的文字显示在它的中间 - 似乎我的文字的 底部 在中间,而我希望我的文字的中间在中间。

我的CSS是:

.touch {
border-radius: 3px;
background: #eee4da;
text-align: center;
font-weight: bold;
/*z-index: 10;*/
font-size: 30px; 
width: 60px;
height: 60px;
display: inline-block;
cursor: pointer;
justify-content: center;
align-items: center;
/*vertical-align: middle;*/
}

最佳答案

您应该删除 height 并向元素添加 padding。示例 here .

CSS:

.touch {
    border-radius: 3px;
    background: #eee4da;
    text-align: center;
    font-weight: bold;
    font-size: 30px; 
    width: 50px;
    cursor: pointer;
    align-items: center;
    padding: 10px;
}


.grid-row {
  margin-bottom: 10px; 
}

如您所见here 居中作用于多个元素.

关于html - 在div中垂直居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26724020/

相关文章:

html - CSS - 添加的缩进不适用于嵌套的 ul li

javascript - 在切换按钮单击时更改变量的值

javascript - JQuery循环根据它的类和php数组改变div背景

javascript - 使用 Javascript 和 asp.net 的表可见性

javascript - 在 Jquery 中应用 CSS 属性

html - 打印时为每页添加边框,没有任何边框中断

javascript - 如何使用 jQuery 附加两个创建的 html 元素

html - 子 div 不会扩展到父 div 的全高

jquery - 网站上的汉堡菜单不起作用

css - 用 css 封面覆盖页面时停止背景滚动?