如何定位一个元素,使其在前一个兄弟元素的下方居中显示

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

How to position an element so that it is centered under its previous sibling

问题

  1. .tooltip-container {
  2. position: relative;
  3. display: inline-block;
  4. }
  5. .tooltip {
  6. position: absolute;
  7. z-index: 1;
  8. background-color: #555;
  9. color: #fff;
  10. padding: 4px 8px;
  11. border-radius: 4px;
  12. white-space: nowrap;
  13. top: 100%;
  14. left: 50%;
  15. transform: translateX(-50%);
  16. margin-top: 0.5em;
  17. }
  18. .tooltip:before {
  19. content: '';
  20. position: absolute;
  21. top: -5px;
  22. left: 50%;
  23. transform: translateX(-50%) rotate(45deg);
  24. width: 10px;
  25. height: 10px;
  26. background-color: #555;
  27. z-index: -1;
  28. }
英文:

I want to center a speech bubble-esque tooltip element under a link. Here is my current code to do so:

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

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

  1. .tooltip-container {
  2. position: relative;
  3. display: inline-block;
  4. }
  5. .tooltip {
  6. position: absolute;
  7. z-index: 1;
  8. background-color: #555;
  9. color: #fff;
  10. padding: 4px 8px;
  11. border-radius: 4px;
  12. white-space: nowrap;
  13. top: 100%;
  14. left: 50%;
  15. transform: translateX(-50%);
  16. margin-top: 0.5em;
  17. }
  18. .tooltip:before {
  19. content: &#39;&#39;;
  20. position: absolute;
  21. top: -5px;
  22. left: 50%;
  23. transform: translateX(-50%) rotate(45deg);
  24. width: 10px;
  25. height: 10px;
  26. background-color: #555;
  27. z-index: -1;
  28. }

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

  1. Up ahead is a
  2. &lt;a href=&quot;#&quot;&gt;
  3. Button text
  4. &lt;/a&gt;
  5. &lt;div class=&quot;tooltip-container&quot;&gt;
  6. &lt;div class=&quot;tooltip&quot;&gt;
  7. Tooltip explanation of button
  8. &lt;/div&gt;
  9. &lt;/div&gt;

<!-- end snippet -->

It looks like this

如何定位一个元素,使其在前一个兄弟元素的下方居中显示

But I would like it to look like this

如何定位一个元素,使其在前一个兄弟元素的下方居中显示

The only reason I made the .tooltip-container div is to have a positioned ancestor for .tooltip so that .tooltip can be position: absolute, but despite having that "anchor" in the page I can't figure out a way to center the second child inside of the first one. My ideal HTML would only be:

  1. &lt;a href=&quot;#&quot;&gt;
  2. Button text
  3. &lt;/a&gt;
  4. &lt;div class=&quot;tooltip&quot;&gt;
  5. Tooltip explanation of button
  6. &lt;/div&gt;

and I only want to apply styling rules to these two elements, not their parent. How can I achieve this?

答案1

得分: 3

只需将 a 移至 .tooltip-container

英文:

Just move a to .tooltip-container

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

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

  1. .tooltip-container {
  2. position: relative;
  3. display: inline-block;
  4. }
  5. .tooltip {
  6. position: absolute;
  7. z-index: 1;
  8. background-color: #555;
  9. color: #fff;
  10. padding: 4px 8px;
  11. border-radius: 4px;
  12. white-space: nowrap;
  13. top: 100%;
  14. left: 50%;
  15. transform: translateX(-50%);
  16. margin-top: 0.5em;
  17. }
  18. .tooltip:before {
  19. content: &#39;&#39;;
  20. position: absolute;
  21. top: -5px;
  22. left: 50%;
  23. transform: translateX(-50%) rotate(45deg);
  24. width: 10px;
  25. height: 10px;
  26. background-color: #555;
  27. z-index: -1;
  28. }

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

  1. Up ahead is a
  2. &lt;div class=&quot;tooltip-container&quot;&gt;
  3. &lt;a href=&quot;#&quot;&gt;
  4. Button text
  5. &lt;/a&gt;
  6. &lt;div class=&quot;tooltip&quot;&gt;
  7. Tooltip explanation of button
  8. &lt;/div&gt;
  9. &lt;/div&gt;

<!-- end snippet -->

答案2

得分: 2

在遥远的未来,你可以使用新的锚点定位:https://drafts.csswg.org/css-anchor-position-1/#anchoring

实际上目前没有浏览器支持,但我为未来留下了答案。

  1. .tooltip {
  2. position: absolute;
  3. bottom: anchor(--my-anchor 100%);
  4. left: anchor(--my-anchor 50%);
  5. transform: translate(-50%,120%);
  6. z-index: 1;
  7. border: 6px solid #0000;
  8. background:
  9. linear-gradient(#555 0 0) padding-box,
  10. conic-gradient(from 135deg at top,#555 90deg,#0000 0) top/100% 50% no-repeat border-box;
  11. color: #fff;
  12. padding: 4px 8px;
  13. border-radius: 12px;
  14. white-space: nowrap;
  15. margin-top: 0.5em;
  16. }
  17. #my-anchor {
  18. anchor-name: --my-anchor;
  19. }

直到那时,你可以将工具提示放在链接内部。

  1. .tooltip {
  2. position: absolute;
  3. top: 100%;
  4. left: 50%;
  5. transform: translate(-50%);
  6. z-index: 1;
  7. border: 6px solid #0000;
  8. background:
  9. linear-gradient(#555 0 0) padding-box,
  10. conic-gradient(from 135deg at top,#555 90deg,#0000 0) top/100% 50% no-repeat border-box;
  11. color: #fff;
  12. padding: 4px 8px;
  13. border-radius: 12px;
  14. white-space: nowrap;
  15. margin-top: 0.5em;
  16. }
  17. a {
  18. position: relative;
  19. }
  1. Up ahead is a
  2. <a href="#">
  3. Button text
  4. <div class="tooltip">
  5. Tooltip explanation of button
  6. </div>
  7. </a>
英文:

In a far future you can use the new anchor positioning : https://drafts.csswg.org/css-anchor-position-1/#anchoring

No browser support for this actually but I leave the answer for the future

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

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

  1. .tooltip {
  2. position: absolute;
  3. bottom: anchor(--my-anchor 100%);
  4. left: anchor(--my-anchor 50%);
  5. transform: translate(-50%,120%);
  6. z-index: 1;
  7. border: 6px solid #0000;
  8. background:
  9. linear-gradient(#555 0 0) padding-box,
  10. conic-gradient(from 135deg at top,#555 90deg,#0000 0) top/100% 50% no-repeat border-box;
  11. color: #fff;
  12. padding: 4px 8px;
  13. border-radius: 12px;
  14. white-space: nowrap;
  15. margin-top: 0.5em;
  16. }
  17. #my-anchor {
  18. anchor-name: --my-anchor;
  19. }

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

  1. Up ahead is a
  2. &lt;a href=&quot;#&quot; id=&quot;my-anchor&quot;&gt;
  3. Button text
  4. &lt;/a&gt;
  5. &lt;div class=&quot;tooltip&quot; anchor=&quot;my-anchor&quot;&gt;
  6. Tooltip explanation of button
  7. &lt;/div&gt;

<!-- end snippet -->


Until then, you can put the tooltip inside the link

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

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

  1. .tooltip {
  2. position: absolute;
  3. top: 100%;
  4. left: 50%;
  5. transform: translate(-50%);
  6. z-index: 1;
  7. border: 6px solid #0000;
  8. background:
  9. linear-gradient(#555 0 0) padding-box,
  10. conic-gradient(from 135deg at top,#555 90deg,#0000 0) top/100% 50% no-repeat border-box;
  11. color: #fff;
  12. padding: 4px 8px;
  13. border-radius: 12px;
  14. white-space: nowrap;
  15. margin-top: 0.5em;
  16. }
  17. a {
  18. position: relative;
  19. }

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

  1. Up ahead is a
  2. &lt;a href=&quot;#&quot;&gt;
  3. Button text
  4. &lt;div class=&quot;tooltip&quot;&gt;
  5. Tooltip explanation of button
  6. &lt;/div&gt;
  7. &lt;/a&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定