如何在CSS中获取内部文本的精确宽度?

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

How do I get the exact width of the inner text in CSS?

问题

以下是翻译好的内容:

"Hi, I need to know the exact width of the inner text in CSS. As you can see in the image, I want those menu items to have a proper width respectively, not including paddings or margins, so that I can give them a hover effect like "Posts" item. Is there any method I can use to get the width of the inner text?"

"嗨,我需要知道CSS中内部文本的确切宽度。正如您在图像中所看到的,我希望这些菜单项分别具有适当的宽度,不包括填充或边距,以便我可以为它们提供类似“Posts”项目的悬停效果。是否有任何方法可以用来获取内部文本的宽度?"

如果您需要更多帮助,请随时提问。

英文:

如何在CSS中获取内部文本的精确宽度?

Hi, I need to know the exact width of the inner text in CSS. As you can see in the image, I want those menu items to have a proper width respectively, not including paddings or margins, so that I can give them a hover effect like "Posts" item. Is there any method I can use to get the width of the inner text?

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

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

/* Basic settings */
@import url(&#39;https://fonts.googleapis.com/css?family=Roboto:100,400,700&amp;display=swap&#39;);
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    transition: all .3s;
    color: #333;
}
a {
    text-decoration: none;
    color: inherit;
}
li {
    list-style-type: none;
}
img {
    border: 0;
}
html, body {
    height: 100%;
}
body {
    font-family: &quot;Roboto&quot;, Helvetica, sans-serif;
}

/* Main */
body {
    background-color: whitesmoke;
}
header {
    border-bottom: 1px solid #EEE;
    height: 70px;
    overflow: hidden;
    background-color: white;
}
header &gt; div {
    vertical-align: middle;
}
header &gt; #logo {
    width: 15%;
    padding: 20px;
    border-right: 1px solid #EEE;
    text-align: right;
    display: inline-block;
}
header &gt; #logo img {
    width: 30px;
    height: auto;
}
header &gt; #search {
    border-right: 1px solid #EEE;
    width: 200px;
    display: inline-block;
    padding: 20px;
}
header &gt; #search input {
    border: 0;
    outline: 0;
    display: inline-block;
    width: 80%;
    vertical-align: middle;
    padding-left: 10px;
}
header &gt; #search a {
    font-size: 1.2rem;
    display: inline-block;
    vertical-align: middle;
    width: 20%;
}
header &gt; #search &gt; #search-box {
    border: 1px solid #CCC;
    border-radius: 100px;
    overflow: hidden;
}
header &gt; #gnb {
    height: 70px;
    display: inline-block;
    vertical-align: middle;
    border-right: 1px solid #EEE;
}
header &gt; #gnb ul {
    margin: 0 30px;
    line-height: 70px;
}
header &gt; #gnb li {
    display: inline-block;
}
header &gt; #gnb li:not(:last-child) {
    margin-right: 20px;
}
header &gt; #gnb a {
    display: inline-block;
    font-weight: lighter;
}
header &gt; #gnb a:hover {
    transform: translateY(-5px);
}
.premium {
    color: orangered;
}
#social-media-icons {
    display: inline-block;
    vertical-align: middle;
    border-right: 1px solid #EEE;
    height: 70px;
}
#social-media-icons ul {
    padding: 0 20px;
    line-height: 70px;
}
#social-media-icons li {
    display: inline-block;
}
#social-media-icons li:not(:last-child) {
    margin-right: 15px;
}
#social-media-icons li a {
    font-size: 2rem;
    display: inline-block;
}
#social-media-icons li a:hover {
    transform: translateY(-5px);
}
#profile {
    display: inline-block;
    vertical-align: middle;
}
#profile a {
    margin-left: 20px;
}
#profile a img {
    width: 50px;
    height: 50px;
    border-radius: 50%;
}
#sub-header {
    border-bottom: 1px solid #EEE;
    height: 50px;
    background-color: white;
}
#sub-header ul {
    width: 800px;
    margin: 0 auto;
}
#sub-header li {
    line-height: 47px;
    display: inline-block;
    border-bottom: 3px solid transparent;
    padding: 0 20px;
}
#sub-header li:hover {
    border-bottom: 3px solid cornflowerblue !important;
}
#sub-header a {
    text-align: center;
    display: inline-block;
    width: 100%;
}
.selected {
    border-bottom: 3px solid cornflowerblue !important;
}
main {
    width: 1000px;
    margin: 30px auto;
}
aside {
    width: 200px;
    background-color: white;
}
#bill-gates {
    padding: 20px;
    border-bottom: 1px solid #EEE;
}
#bill-gates-image {
    margin-bottom: 20px;
}
#bill-gates-image &gt; img {
    margin: 0 auto;
    display: block;
    width: 100px;
    height: 100px;
    border-radius: 50%;
}
#bill-gates-bio {
    text-align: center;
    margin-bottom: 30px;
}
#bill-gates-bio &gt; h2 {
    margin-bottom: 5px;
}
#bill-gates-bio &gt; p {
    font-weight: lighter;
    margin-bottom: 3px;
}
#bill-gates-bio &gt; p:last-child {
    color: #CCC;
}
#bill-gates-follow-button {
    text-align: center;
    margin-bottom: 10px;
}
#bill-gates-follow-button &gt; a {
    display: inline-block;
    line-height: 20px;
    padding: 10px 50px;
    background-color: cornflowerblue;
    color: white;
    border-radius: 30px;
    border: 1px solid transparent;
}
#bill-gates-follow-button &gt; a:hover {
    background-color: rgb(70, 134, 253);
}
#bill-gates-connect-button {
    text-align: center;
}
#bill-gates-connect-button &gt; a {
    line-height: 20px;
    display: inline-block;
    padding: 10px 40px;
    background-color: white;
    color: cornflowerblue;
    border: 1px solid cornflowerblue;
    border-radius: 30px;
}
#bill-gates-connect-button &gt; a:hover {
    background-color: #EEE;
}
#aside-middle {
    overflow: hidden;
    border-bottom: 1px solid #EEE;
    height: 80px;
}
#connections {
    text-align: center;
    float: left;
    width: 50%;
    height: 100%;
    border-right: 1px solid #EEE;
}
#followers {
    text-align: center;
    float: right;
    width: 50%;
    height: 100%;
}

<!-- 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;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot; type=&quot;text/css&quot;&gt;
    &lt;title&gt;Bill Gates&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;header&gt;
        &lt;div id=&quot;logo&quot;&gt;
            &lt;a href=&quot;#&quot;&gt;
                &lt;img src=&quot;./img/logo.png&quot; alt=&quot;LinkedIn logo&quot;&gt;
            &lt;/a&gt;
        &lt;/div&gt;&lt;!--
     --&gt;&lt;div id=&quot;search&quot;&gt;
            &lt;div id=&quot;search-box&quot;&gt;
                &lt;input type=&quot;search&quot; name=&quot;search&quot; placeholder=&quot;Search&quot;&gt;&lt;!--
             --&gt;&lt;a href=&quot;#&quot;&gt;&#128269;&lt;/a&gt;
            &lt;/div&gt;
        &lt;/div&gt;&lt;!--
        --&gt;&lt;div id=&quot;gnb&quot;&gt;
            &lt;ul&gt;
                &lt;li&gt;
                    &lt;a href=&quot;#&quot;&gt;Home&lt;/a&gt;
                &lt;/li&gt;
                &lt;li&gt;
                    &lt;a href=&quot;#&quot;&gt;Profile&lt;/a&gt;
                &lt;/li&gt;
                &lt;li&gt;
                    &lt;a href=&quot;#&quot;&gt;Connections&lt;/a&gt;
                &lt;/li&gt;
                &lt;li&gt;
                    &lt;a href=&quot;#&quot;&gt;Interest&lt;/a&gt;
                &lt;/li&gt;
                &lt;li class=&quot;premium&quot;&gt;
                    &lt;a href=&quot;#&quot;&gt;Try Premium&lt;/a&gt;
                &lt;/li&gt;
            &lt;/ul&gt;
        &lt;/div&gt;&lt;!--
        --&gt;&lt;div id=&quot;social-media-icons&quot;&gt;
            &lt;ul&gt;
                &lt;li&gt;
                    &lt;a href=&quot;#&quot;&gt;&#128483;&lt;/a&gt;
                &lt;/li&gt;
                &lt;li&gt;
                    &lt;a href=&quot;#&quot;&gt;&#128276;&lt;/a&gt;
                &lt;/li&gt;
                &lt;li&gt;
                    &lt;a href=&quot;#&quot;&gt;➕&lt;/a&gt;
                &lt;/li&gt;
            &lt;/ul&gt;
        &lt;/div&gt;&lt;!--
        --&gt;&lt;div id=&quot;profile&quot;&gt;
            &lt;a href=&quot;#&quot;&gt;
                &lt;img src=&quot;./img/avatar.jpg&quot; alt=&quot;Profile image&quot;&gt;
            &lt;/a&gt;
        &lt;/div&gt;
    &lt;/header&gt;
    &lt;div id=&quot;sub-header&quot;&gt;
        &lt;ul&gt;
            &lt;li class=&quot;selected&quot;&gt;
                &lt;a href=&quot;#&quot;&gt;Posts&lt;/a&gt;
            &lt;/li&gt;&lt;li&gt;
                &lt;a href=&quot;#&quot;&gt;Background&lt;/a&gt;
            &lt;/li&gt;&lt;li&gt;
                &lt;a href=&quot;#&quot;&gt;Recommendations&lt;/a&gt;
            &lt;/li&gt;&lt;li&gt;
                &lt;a href=&quot;#&quot;&gt;Following&lt;/a&gt;
            &lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
    &lt;main&gt;
        &lt;aside&gt;
            &lt;div id=&quot;bill-gates&quot;&gt;
                &lt;div id=&quot;bill-gates-image&quot;&gt;
                    &lt;img src=&quot;./img/bill-gates-avatar.png&quot; alt=&quot;Bill Gates Profile image&quot;&gt;
                &lt;/div&gt;
                &lt;div id=&quot;bill-gates-bio&quot;&gt;
                    &lt;h2&gt;Bill Gates&lt;/h2&gt;
                    &lt;p&gt;Microsoft Founder&lt;/p&gt;
                    &lt;p&gt;Greater Seattle Area&lt;/p&gt;
                &lt;/div&gt;
                &lt;div id=&quot;bill-gates-follow-button&quot;&gt;
                    &lt;a href=&quot;#&quot;&gt;Follow&lt;/a&gt;
                &lt;/div&gt;
                &lt;div id=&quot;bill-gates-connect-button&quot;&gt;
                    &lt;a href=&quot;#&quot;&gt;Connect ↓&lt;/a&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div id=&quot;aside-middle&quot;&gt;
                &lt;div id=&quot;connections&quot;&gt;
                    &lt;h3&gt;200&lt;/h3&gt;
                    &lt;p&gt;connections&lt;/p&gt;
                &lt;/div&gt;
                &lt;div id=&quot;followers&quot;&gt;
                    &lt;h3&gt;1.9M&lt;/h3&gt;
                    &lt;p&gt;followers&lt;/p&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/aside&gt;
    &lt;/main&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 1

使用元素的width属性,它获取内容区域的宽度,不包括任何内边距、边框或外边距。

.bg-tag {
    font-family: Arial, sans-serif;
    font-size: 14px;
    text-decoration: none;
}

.bg-tag:focus {
    padding-bottom: 2px;
    border-bottom: 2px solid #000; #根据需要更改颜色
    width: 100%;
}
<a href="#" class="bg-tag">Background</a>
英文:

Use the width property of an element, which gets the width of the content area excluding any paddings, borders or margins.

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

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

.bg-tag {
    font-family: Arial, sans-serif;
    font-size: 14px;
    text-decoration: none;
}

.bg-tag:focus {
    padding-bottom: 2px;
    border-bottom: 2px solid #000; #change colour as you like
    width: 100%;
}

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

&lt;a href=&quot;#&quot; class=&quot;bg-tag&quot;&gt;Background&lt;/a&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 13:30:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573620.html
匿名

发表评论

匿名网友

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

确定