如何在我的按钮元素内垂直居中文本?

huangapple go评论68阅读模式
英文:

How can I vertically center text within my button element?

问题

I've created a button element, and am having a difficult time getting the text vertically centered. The margin and padding appear to not be an issue. I feel like I'm missing something straightforward, or not fully understanding how the element is being rendered.

button with vertically uncentered text

close-up screenshot

https://jsfiddle.net/morcant/tovp1s6c/1/

I've tried centering the element with display: inline-flex; align-items: center;, and adjusting the line-height value. Changing the line height appears to increase the height of the line, but doesn't seem to help with vertically centering the text.

I'm using Firefox and Windows 10, with the Simple CSS framework.

英文:

I've created a button element, and am having a difficult time getting the text vertically centered. The margin and padding appear to not be an issue. I feel like I'm missing something straightforward, or not fully understanding how the element is being rendered.

button with vertically uncentered text

close-up screenshot

https://jsfiddle.net/morcant/tovp1s6c/1/

I've tried centering the element with display: inline-flex; align-items: center;, and adjusting the line-height value. Changing the line height appears to increase the height of the line, but doesn't seem to help with vertically centering the text.

I'm using Firefox and Windows 10, with the Simple CSS framework.

答案1

得分: 1

您可以在联系按钮之前/之后添加一些HTML内容,并指定它们的高度百分比:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

    html > body > header {
        padding: 0 2rem;
    }

    .contact > div {
      height: 35%;
    }

    header > ul.nav {
        list-style: none;
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        padding-top: 1rem;
        padding-left: 0;
        margin-top: 0;
        margin-bottom: 0;
    }

    header > ul.nav li {
        display: flex;
        flex-direction: column;
    }

    header > ul.nav .nav-title-container {
        text-align: left;
        flex-grow: 1;
        padding-right: 1rem;
        padding-bottom: 1rem;
    }

    header > ul.nav .nav-title {
        margin-top: 0;
        margin-left: 0;
    }

    header > ul.nav .nav-title a {
        color: var(--code);
    }

    header > ul.nav .nav-subtitle {
        margin-bottom: .25rem;
        margin-top: .25rem;
    }

    header > ul.nav .contact {
        display: flex;
        flex-direction: column;
        padding-bottom: 1rem;
    }

    header > ul.nav .contact button {
        margin-top: 0;
        margin-bottom: 0;
        background-color: transparent;
        color: var(--code);
        border: 1px solid var(--code);
    }

<!-- language: lang-html -->

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>First Last</title>
        <link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
      </head>
      <body>
        <header>
            <ul class="nav">
                <li class="nav-title-container">
                    <h1 class="nav-title"><a href="/index.html">First Last</a></h1>
                    <h4 class="nav-subtitle">Title</h4>
                    <h4 class="nav-subtitle">Subtitle</h4>
                </li>
                <li class="contact">
                    <div>
                    </div>
                    <a href="/contact.html"><button>联系</button></a>
                    <div>
                    </div>
                </li>
            </ul>
        </header>
        <main>

        </main>
        <footer></footer>
      </body>
    </html>

<!-- end snippet -->
英文:

You can prepend and append some HTML content before/after the contact button and specify their height in percentage:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

html &gt; body &gt; header {
padding: 0 2rem;
}
.contact &gt; div {
height: 35%;
}
header &gt; ul.nav {
list-style: none;
display: flex;
flex-direction: row;
flex-wrap: wrap;
padding-top: 1rem;
padding-left: 0;
margin-top: 0;
margin-bottom: 0;
}
header &gt; ul.nav li {
display: flex;
flex-direction: column;
}
header &gt; ul.nav .nav-title-container {
text-align: left;
flex-grow: 1;
padding-right: 1rem;
padding-bottom: 1rem;
}
header &gt; ul.nav .nav-title {
margin-top: 0;
margin-left: 0;
}
header &gt; ul.nav .nav-title a {
color: var(--code);
}
header &gt; ul.nav .nav-subtitle {
margin-bottom: .25rem;
margin-top: .25rem;
}
header &gt; ul.nav .contact {
display: flex;
flex-direction: column;
padding-bottom: 1rem;
}
header &gt; ul.nav .contact button {
margin-top: 0;
margin-bottom: 0;
background-color: transparent;
color: var(--code);
border: 1px solid var(--code);
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
&lt;title&gt;First Last&lt;/title&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.simplecss.org/simple.min.css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;header&gt;
&lt;ul class=&quot;nav&quot;&gt;
&lt;li class=&quot;nav-title-container&quot;&gt;
&lt;h1 class=&quot;nav-title&quot;&gt;&lt;a href=&quot;/index.html&quot;&gt;First Last&lt;/a&gt;&lt;/h1&gt;
&lt;h4 class=&quot;nav-subtitle&quot;&gt;Title&lt;/h4&gt;
&lt;h4 class=&quot;nav-subtitle&quot;&gt;Subtitle&lt;/h4&gt;
&lt;/li&gt;
&lt;li class=&quot;contact&quot;&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;a href=&quot;/contact.html&quot;&gt;&lt;button&gt;contact&lt;/button&gt;&lt;/a&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/header&gt;
&lt;main&gt;
&lt;/main&gt;
&lt;footer&gt;&lt;/footer&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月31日 23:10:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76374969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定