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

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

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 -->

  1. /* Basic settings */
  2. @import url(&#39;https://fonts.googleapis.com/css?family=Roboto:100,400,700&amp;display=swap&#39;);
  3. * {
  4. margin: 0;
  5. padding: 0;
  6. box-sizing: border-box;
  7. transition: all .3s;
  8. color: #333;
  9. }
  10. a {
  11. text-decoration: none;
  12. color: inherit;
  13. }
  14. li {
  15. list-style-type: none;
  16. }
  17. img {
  18. border: 0;
  19. }
  20. html, body {
  21. height: 100%;
  22. }
  23. body {
  24. font-family: &quot;Roboto&quot;, Helvetica, sans-serif;
  25. }
  26. /* Main */
  27. body {
  28. background-color: whitesmoke;
  29. }
  30. header {
  31. border-bottom: 1px solid #EEE;
  32. height: 70px;
  33. overflow: hidden;
  34. background-color: white;
  35. }
  36. header &gt; div {
  37. vertical-align: middle;
  38. }
  39. header &gt; #logo {
  40. width: 15%;
  41. padding: 20px;
  42. border-right: 1px solid #EEE;
  43. text-align: right;
  44. display: inline-block;
  45. }
  46. header &gt; #logo img {
  47. width: 30px;
  48. height: auto;
  49. }
  50. header &gt; #search {
  51. border-right: 1px solid #EEE;
  52. width: 200px;
  53. display: inline-block;
  54. padding: 20px;
  55. }
  56. header &gt; #search input {
  57. border: 0;
  58. outline: 0;
  59. display: inline-block;
  60. width: 80%;
  61. vertical-align: middle;
  62. padding-left: 10px;
  63. }
  64. header &gt; #search a {
  65. font-size: 1.2rem;
  66. display: inline-block;
  67. vertical-align: middle;
  68. width: 20%;
  69. }
  70. header &gt; #search &gt; #search-box {
  71. border: 1px solid #CCC;
  72. border-radius: 100px;
  73. overflow: hidden;
  74. }
  75. header &gt; #gnb {
  76. height: 70px;
  77. display: inline-block;
  78. vertical-align: middle;
  79. border-right: 1px solid #EEE;
  80. }
  81. header &gt; #gnb ul {
  82. margin: 0 30px;
  83. line-height: 70px;
  84. }
  85. header &gt; #gnb li {
  86. display: inline-block;
  87. }
  88. header &gt; #gnb li:not(:last-child) {
  89. margin-right: 20px;
  90. }
  91. header &gt; #gnb a {
  92. display: inline-block;
  93. font-weight: lighter;
  94. }
  95. header &gt; #gnb a:hover {
  96. transform: translateY(-5px);
  97. }
  98. .premium {
  99. color: orangered;
  100. }
  101. #social-media-icons {
  102. display: inline-block;
  103. vertical-align: middle;
  104. border-right: 1px solid #EEE;
  105. height: 70px;
  106. }
  107. #social-media-icons ul {
  108. padding: 0 20px;
  109. line-height: 70px;
  110. }
  111. #social-media-icons li {
  112. display: inline-block;
  113. }
  114. #social-media-icons li:not(:last-child) {
  115. margin-right: 15px;
  116. }
  117. #social-media-icons li a {
  118. font-size: 2rem;
  119. display: inline-block;
  120. }
  121. #social-media-icons li a:hover {
  122. transform: translateY(-5px);
  123. }
  124. #profile {
  125. display: inline-block;
  126. vertical-align: middle;
  127. }
  128. #profile a {
  129. margin-left: 20px;
  130. }
  131. #profile a img {
  132. width: 50px;
  133. height: 50px;
  134. border-radius: 50%;
  135. }
  136. #sub-header {
  137. border-bottom: 1px solid #EEE;
  138. height: 50px;
  139. background-color: white;
  140. }
  141. #sub-header ul {
  142. width: 800px;
  143. margin: 0 auto;
  144. }
  145. #sub-header li {
  146. line-height: 47px;
  147. display: inline-block;
  148. border-bottom: 3px solid transparent;
  149. padding: 0 20px;
  150. }
  151. #sub-header li:hover {
  152. border-bottom: 3px solid cornflowerblue !important;
  153. }
  154. #sub-header a {
  155. text-align: center;
  156. display: inline-block;
  157. width: 100%;
  158. }
  159. .selected {
  160. border-bottom: 3px solid cornflowerblue !important;
  161. }
  162. main {
  163. width: 1000px;
  164. margin: 30px auto;
  165. }
  166. aside {
  167. width: 200px;
  168. background-color: white;
  169. }
  170. #bill-gates {
  171. padding: 20px;
  172. border-bottom: 1px solid #EEE;
  173. }
  174. #bill-gates-image {
  175. margin-bottom: 20px;
  176. }
  177. #bill-gates-image &gt; img {
  178. margin: 0 auto;
  179. display: block;
  180. width: 100px;
  181. height: 100px;
  182. border-radius: 50%;
  183. }
  184. #bill-gates-bio {
  185. text-align: center;
  186. margin-bottom: 30px;
  187. }
  188. #bill-gates-bio &gt; h2 {
  189. margin-bottom: 5px;
  190. }
  191. #bill-gates-bio &gt; p {
  192. font-weight: lighter;
  193. margin-bottom: 3px;
  194. }
  195. #bill-gates-bio &gt; p:last-child {
  196. color: #CCC;
  197. }
  198. #bill-gates-follow-button {
  199. text-align: center;
  200. margin-bottom: 10px;
  201. }
  202. #bill-gates-follow-button &gt; a {
  203. display: inline-block;
  204. line-height: 20px;
  205. padding: 10px 50px;
  206. background-color: cornflowerblue;
  207. color: white;
  208. border-radius: 30px;
  209. border: 1px solid transparent;
  210. }
  211. #bill-gates-follow-button &gt; a:hover {
  212. background-color: rgb(70, 134, 253);
  213. }
  214. #bill-gates-connect-button {
  215. text-align: center;
  216. }
  217. #bill-gates-connect-button &gt; a {
  218. line-height: 20px;
  219. display: inline-block;
  220. padding: 10px 40px;
  221. background-color: white;
  222. color: cornflowerblue;
  223. border: 1px solid cornflowerblue;
  224. border-radius: 30px;
  225. }
  226. #bill-gates-connect-button &gt; a:hover {
  227. background-color: #EEE;
  228. }
  229. #aside-middle {
  230. overflow: hidden;
  231. border-bottom: 1px solid #EEE;
  232. height: 80px;
  233. }
  234. #connections {
  235. text-align: center;
  236. float: left;
  237. width: 50%;
  238. height: 100%;
  239. border-right: 1px solid #EEE;
  240. }
  241. #followers {
  242. text-align: center;
  243. float: right;
  244. width: 50%;
  245. height: 100%;
  246. }

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

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  6. &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
  7. &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot; type=&quot;text/css&quot;&gt;
  8. &lt;title&gt;Bill Gates&lt;/title&gt;
  9. &lt;/head&gt;
  10. &lt;body&gt;
  11. &lt;header&gt;
  12. &lt;div id=&quot;logo&quot;&gt;
  13. &lt;a href=&quot;#&quot;&gt;
  14. &lt;img src=&quot;./img/logo.png&quot; alt=&quot;LinkedIn logo&quot;&gt;
  15. &lt;/a&gt;
  16. &lt;/div&gt;&lt;!--
  17. --&gt;&lt;div id=&quot;search&quot;&gt;
  18. &lt;div id=&quot;search-box&quot;&gt;
  19. &lt;input type=&quot;search&quot; name=&quot;search&quot; placeholder=&quot;Search&quot;&gt;&lt;!--
  20. --&gt;&lt;a href=&quot;#&quot;&gt;&#128269;&lt;/a&gt;
  21. &lt;/div&gt;
  22. &lt;/div&gt;&lt;!--
  23. --&gt;&lt;div id=&quot;gnb&quot;&gt;
  24. &lt;ul&gt;
  25. &lt;li&gt;
  26. &lt;a href=&quot;#&quot;&gt;Home&lt;/a&gt;
  27. &lt;/li&gt;
  28. &lt;li&gt;
  29. &lt;a href=&quot;#&quot;&gt;Profile&lt;/a&gt;
  30. &lt;/li&gt;
  31. &lt;li&gt;
  32. &lt;a href=&quot;#&quot;&gt;Connections&lt;/a&gt;
  33. &lt;/li&gt;
  34. &lt;li&gt;
  35. &lt;a href=&quot;#&quot;&gt;Interest&lt;/a&gt;
  36. &lt;/li&gt;
  37. &lt;li class=&quot;premium&quot;&gt;
  38. &lt;a href=&quot;#&quot;&gt;Try Premium&lt;/a&gt;
  39. &lt;/li&gt;
  40. &lt;/ul&gt;
  41. &lt;/div&gt;&lt;!--
  42. --&gt;&lt;div id=&quot;social-media-icons&quot;&gt;
  43. &lt;ul&gt;
  44. &lt;li&gt;
  45. &lt;a href=&quot;#&quot;&gt;&#128483;&lt;/a&gt;
  46. &lt;/li&gt;
  47. &lt;li&gt;
  48. &lt;a href=&quot;#&quot;&gt;&#128276;&lt;/a&gt;
  49. &lt;/li&gt;
  50. &lt;li&gt;
  51. &lt;a href=&quot;#&quot;&gt;➕&lt;/a&gt;
  52. &lt;/li&gt;
  53. &lt;/ul&gt;
  54. &lt;/div&gt;&lt;!--
  55. --&gt;&lt;div id=&quot;profile&quot;&gt;
  56. &lt;a href=&quot;#&quot;&gt;
  57. &lt;img src=&quot;./img/avatar.jpg&quot; alt=&quot;Profile image&quot;&gt;
  58. &lt;/a&gt;
  59. &lt;/div&gt;
  60. &lt;/header&gt;
  61. &lt;div id=&quot;sub-header&quot;&gt;
  62. &lt;ul&gt;
  63. &lt;li class=&quot;selected&quot;&gt;
  64. &lt;a href=&quot;#&quot;&gt;Posts&lt;/a&gt;
  65. &lt;/li&gt;&lt;li&gt;
  66. &lt;a href=&quot;#&quot;&gt;Background&lt;/a&gt;
  67. &lt;/li&gt;&lt;li&gt;
  68. &lt;a href=&quot;#&quot;&gt;Recommendations&lt;/a&gt;
  69. &lt;/li&gt;&lt;li&gt;
  70. &lt;a href=&quot;#&quot;&gt;Following&lt;/a&gt;
  71. &lt;/li&gt;
  72. &lt;/ul&gt;
  73. &lt;/div&gt;
  74. &lt;main&gt;
  75. &lt;aside&gt;
  76. &lt;div id=&quot;bill-gates&quot;&gt;
  77. &lt;div id=&quot;bill-gates-image&quot;&gt;
  78. &lt;img src=&quot;./img/bill-gates-avatar.png&quot; alt=&quot;Bill Gates Profile image&quot;&gt;
  79. &lt;/div&gt;
  80. &lt;div id=&quot;bill-gates-bio&quot;&gt;
  81. &lt;h2&gt;Bill Gates&lt;/h2&gt;
  82. &lt;p&gt;Microsoft Founder&lt;/p&gt;
  83. &lt;p&gt;Greater Seattle Area&lt;/p&gt;
  84. &lt;/div&gt;
  85. &lt;div id=&quot;bill-gates-follow-button&quot;&gt;
  86. &lt;a href=&quot;#&quot;&gt;Follow&lt;/a&gt;
  87. &lt;/div&gt;
  88. &lt;div id=&quot;bill-gates-connect-button&quot;&gt;
  89. &lt;a href=&quot;#&quot;&gt;Connect ↓&lt;/a&gt;
  90. &lt;/div&gt;
  91. &lt;/div&gt;
  92. &lt;div id=&quot;aside-middle&quot;&gt;
  93. &lt;div id=&quot;connections&quot;&gt;
  94. &lt;h3&gt;200&lt;/h3&gt;
  95. &lt;p&gt;connections&lt;/p&gt;
  96. &lt;/div&gt;
  97. &lt;div id=&quot;followers&quot;&gt;
  98. &lt;h3&gt;1.9M&lt;/h3&gt;
  99. &lt;p&gt;followers&lt;/p&gt;
  100. &lt;/div&gt;
  101. &lt;/div&gt;
  102. &lt;/aside&gt;
  103. &lt;/main&gt;
  104. &lt;/body&gt;
  105. &lt;/html&gt;

<!-- end snippet -->

答案1

得分: 1

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

  1. .bg-tag {
  2. font-family: Arial, sans-serif;
  3. font-size: 14px;
  4. text-decoration: none;
  5. }
  6. .bg-tag:focus {
  7. padding-bottom: 2px;
  8. border-bottom: 2px solid #000; #根据需要更改颜色
  9. width: 100%;
  10. }
  1. <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 -->

  1. .bg-tag {
  2. font-family: Arial, sans-serif;
  3. font-size: 14px;
  4. text-decoration: none;
  5. }
  6. .bg-tag:focus {
  7. padding-bottom: 2px;
  8. border-bottom: 2px solid #000; #change colour as you like
  9. width: 100%;
  10. }

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

  1. &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:

确定