简体   繁体   English

在行高容器内将文本垂直居中

[英]Vertically centring text inside line height container

在此处输入图片说明

I'm hoping someone can help because it's causing problems over the whole layout of my HTML page. 我希望有人可以提供帮助,因为这会导致我的HTML页面的整个布局出现问题。

Simply put for all my text elements on 1 website, they are all slightly off vertical centre ie there's a bit more padding underneath than above. 只需将我所有的文本元素放在一个网站上,它们都稍微偏离垂直中心,即,下面的填充比上面的填充多。 When I looked more closely at the problem with Google Chrome, I noticed that whilst the padding (green) and line height (darker blue) are equal top and bottom, the lighter blue box has the text sitting at the top rather than vertically centred, which is where the problem is coming from. 当我更仔细地研究Google Chrome的问题时,我注意到虽然填充(绿色)和行高(深蓝色)的顶部和底部相等,但较浅的蓝色框的文本位于顶部,而不是垂直居中,问题出在哪里。

I hope I don't seem pedantic but it is visually noticeable, especially on the larger elements. 我希望我似乎没有脚步,但在视觉上可以注意到,特别是在较大的元素上。

So my question is - does anyone know what this setting is and how can I change it. 所以我的问题是-有人知道此设置是什么以及如何更改它。 I've looked on other websites and the text is centred on other sites, so I've no idea what's going on here. 我看过其他网站,并且文字以其他网站为中心,所以我不知道这里发生了什么。

If it helps, it is on a Shopify website and the following styles are present site wide - it is occurring on all text elements: 如果有帮助,它可以在Shopify网站上找到,并且在整个网站上都可以使用以下样式-它出现在所有文本元素上:

h1, .h1, h2, .h2, h3, .h3, h4, .h4, h5, .h5, h6, .h6 {
    display: block;
    font-family: "Josefin Sans",sans-serif;
    font-weight: 700;
    margin: 0 0 0.5em;
    line-height: 1.4;
}

*, input, :before, :after {
    box-sizing: border-box;
}

h2 {
    display: block;
    font-size: 1.5em;
    -webkit-margin-before: 0.83em;
    -webkit-margin-after: 0.83em;
    -webkit-margin-start: 0px;
    -webkit-margin-end: 0px;
    font-weight: bold;
}

At first look margin: 0 0 0.5em; margin: 0 0 0.5em; adding 0.5em margin to the bottom, so text goes up. 在底部增加0.5em边距,因此文本会增加。

 function fix() { $('h1').css({marginBottom:0}); } 
 body { font-size: 25px; } div { display: inline-block; background: #f00; padding: 20px; } h1 { display: block; font-family: "Josefin Sans",sans-serif; font-weight: 700; margin: 0 0 0.5em; line-height: 1.4; background: #fbb; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <link href="https://fonts.googleapis.com/css?family=Josefin Sans" rel="stylesheet" type="text/css" > <div> <h1>WORKSHOPS</h1> </div> <button onclick="fix()">FIX</button> 

see my answer 看我的答案

https://jsfiddle.net/qq0t46pt/2/ https://jsfiddle.net/qq0t46pt/2/

using flexbox: 使用flexbox:

HTML: HTML:

<ul>
<li style="float:left"><a class="active"> <a onclick="window.open(this.href, this.target);return false;" href="http://link/" target="link"> <img src="Logo.png" alt="Logo"    style="width:125px;height:75px;"></a></a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>

CSS: CSS:

ul {
    list-style-type: none;
    margin: 0;
    padding: 10px;
    overflow: hidden;
    background-color: black;

    display: -webkit-flexbox;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-align: center;
    -ms-flex-align: center;
    -webkit-align-items: center;
    align-items: center;
    justify-content: center;
    height: 370px;
}

li {
    float: left;
}

li a {
    display: inline-block;
    color: white;
    text-align: center;
    padding: 14px 20px;
    text-decoration: none;
}

li a:hover:not(.active) {
    background-color: red;
}

.active {
    background-color: black;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM