在悬停时如何显示一些文本

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

How do I show some text on hover

问题

I am a newbie to web development and I'm facing an issue to complete my project. When I hover a text I want a paragraph or div to pop-up.

The code which I would be sharing is not my real project but is a copy of my real project error which I am facing. I tried the following tricks.

I tried making the "li#description" visiblity to hidden and on hover

li#hover:hover + li#description{
  visibility: visible;
}
li#description{
  display:none;
}

This didn't work and when on hover I tried changing it to inline even this didn't work.

Everything is similar you would find the name of the elements and the CSS not so good but it is just a copy

li {
  list-style: none;
}

.hover-list {
  display: flex;
  align-items: center;
  justify-content: center;
}

li#hover {
  text-align: center;
  background-color: darkslategray;
  display: inline;
  padding: 10px;
  border-radius: 50px;
  color: white;
}

li#description {
  border: 2px solid black;
  text-align: center;
  padding: 10px;
  font-size: large;
  background-color: darkslategrey;
  color: white;
}
<ul class="hover-list">
  <li id="hover">
    Hover Me!!
  </li>
</ul>

<div id="description-div">
  <ul class="description-list">
    <li id="description">
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum consequatur veritatis facilis est voluptas obcaecati similique illo reiciendis a, eaque minima ullam totam dolor molestias officiis quia suscipit enim temporibus fugiat nulla possimus. Ut
      recusandae laudantium in rem, nulla corrupti placeat ipsa assumenda consequatur et eveniet inventore voluptates officiis facilis id? Qui suscipit non maiores corporis quas adipisci dicta in enim amet magnam, repellendus, iusto recusandae laudantium
      sit illo nemo delectus at officia saepe nihil obcaecati! Asperiores deserunt enim excepturi id dolorum hic expedita explicabo? Nesciunt, est ipsam! Velit in nostrum sunt consectetur laudantium ad inventore labore libero ducimus voluptates.
    </li>
  </ul>
</div>

Please note that this translation is of the code parts you provided and does not include the explanation or additional text.

英文:

I am a newbie to web development and I'm facing an issue to complete my project. When I hover a text I want a paragraph or div to pop-up.

The code which I would be sharing is not my real project but is a copy of my real project error which I am facing. I tried the following tricks.

I tried making the "li#description" visiblity to hidden and on hover

li#hover:hover + li#description{
  visibility: visible;
}
li#description{
  display:none;
}

This didn't work and when on hover I tried changing it to inline even this didn't work.

Everything is similar you would find the name of the elements and the CSS not so good but it is just a copy

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

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

li {
  list-style: none;
  /* list-style: none; */
}

.hover-list {
  display: flex;
  align-items: center;
  justify-content: center;
}

li#hover {
  text-align: center;
  background-color: darkslategray;
  display: inline;
  padding: 10px;
  border-radius: 50px;
  color: white;
}

li#description {
  border: 2px solid black;
  text-align: center;
  padding: 10px;
  font-size: large;
  background-color: darkslategrey;
  color: white;
}

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

&lt;ul class=&quot;hover-list&quot;&gt;

  &lt;li id=&quot;hover&quot;&gt;

    Hover Me!!

  &lt;/li&gt;

&lt;/ul&gt;

&lt;div id=&quot;description-div&quot;&gt;

  &lt;ul class=&quot;description-list&quot;&gt;

    &lt;li id=&quot;description&quot;&gt;

      Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum consequatur veritatis facilis est voluptas obcaecati similique illo reiciendis a, eaque minima ullam totam dolor molestias officiis quia suscipit enim temporibus fugiat nulla possimus. Ut
      recusandae laudantium in rem, nulla corrupti placeat ipsa assumenda consequatur et eveniet inventore voluptates officiis facilis id? Qui suscipit non maiores corporis quas adipisci dicta in enim amet magnam, repellendus, iusto recusandae laudantium
      sit illo nemo delectus at officia saepe nihil obcaecati! Asperiores deserunt enim excepturi id dolorum hic expedita explicabo? Nesciunt, est ipsam! Velit in nostrum sunt consectetur laudantium ad inventore labore libero ducimus voluptates.

    &lt;/li&gt;

  &lt;/ul&gt;

&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

以下是您要翻译的内容:

You can keep your HTML as is and use some CSS trickery:

首先将 li#description 设置为 display: none。这样在不可见时它就不会占用任何空间。

然后使用 .hover-list:has(#hover:hover) + #description-div li#description{ display:block }

:has 用于检查是否有任何子元素匹配选择器,本例中是检查是否有一个ID为 hover 的元素被悬停。然后 + 将获取下一个相邻元素,然后简单地访问子元素 li#description 并将其设置为 display: block

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

<!-- language: lang-css -->
li {
  list-style: none;
}

.hover-list {
  display: flex;
  align-items: center;
  justify-content: center;
}

li#hover {
  text-align: center;
  background-color: darkslategray;
  display: inline;
  padding: 10px;
  border-radius: 50px;
  color: white;
}

li#description {
  border: 2px solid black;
  text-align: center;
  padding: 10px;
  font-size: large;
  background-color: darkslategrey;
  color: white;
  display: none;
}

.hover-list:has(#hover:hover) + #description-div li#description {
  display: block;
}

<!-- language: lang-html -->
<ul class="hover-list">
  <li id="hover">
    Hover Me!!
  </li>
</ul>

<div id="description-div">
  <ul class="description-list">
    <li id="description">
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum consequatur veritatis facilis est voluptas obcaecati similique illo reiciendis a, eaque minima ullam totam dolor molestias officiis quia suscipit enim temporibus fugiat nulla possimus. Ut
      recusandae laudantium in rem, nulla corrupti placeat ipsa assumenda consequatur et eveniet inventore voluptates officiis facilis id? Qui suscipit non maiores corporis quas adipisci dicta in enim amet magnam, repellendus, iusto recusandae laudantium
      sit illo nemo delectus at officia saepe nihil obcaecati! Asperiores deserunt enim excepturi id dolorum hic expedita explicabo? Nesciunt, est ipsam! Velit in nostrum sunt consectetur laudantium ad inventore labore libero ducimus voluptates.
    </li>
  </ul>
</div>

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

You can keep your HTML as is and use some CSS trickery:

First set li#description to have display none. That way it doesn't take up any space when its not visible.

Then use .hover-list:has(#hover:hover) + #description-div li#description{
display:block
}

:has checks to see if any child match the selector, in this case checking to see if it has an element with the ID of hover that is being hovered. Then + will grab the next adjacement element, and then simply access the child with li#description and set it to having display block.

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

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

li {
  list-style: none;
  /* list-style: none; */
}

.hover-list {
  display: flex;
  align-items: center;
  justify-content: center;
}

li#hover {
  text-align: center;
  background-color: darkslategray;
  display: inline;
  padding: 10px;
  border-radius: 50px;
  color: white;
}

li#description {
  border: 2px solid black;
  text-align: center;
  padding: 10px;
  font-size: large;
  background-color: darkslategrey;
  color: white;
  display:none
}

.hover-list:has(#hover:hover) + #description-div li#description{
  display:block
}

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

&lt;ul class=&quot;hover-list&quot;&gt;

  &lt;li id=&quot;hover&quot;&gt;

    Hover Me!!

  &lt;/li&gt;

&lt;/ul&gt;

&lt;div id=&quot;description-div&quot;&gt;

  &lt;ul class=&quot;description-list&quot;&gt;

    &lt;li id=&quot;description&quot;&gt;

      Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum consequatur veritatis facilis est voluptas obcaecati similique illo reiciendis a, eaque minima ullam totam dolor molestias officiis quia suscipit enim temporibus fugiat nulla possimus. Ut
      recusandae laudantium in rem, nulla corrupti placeat ipsa assumenda consequatur et eveniet inventore voluptates officiis facilis id? Qui suscipit non maiores corporis quas adipisci dicta in enim amet magnam, repellendus, iusto recusandae laudantium
      sit illo nemo delectus at officia saepe nihil obcaecati! Asperiores deserunt enim excepturi id dolorum hic expedita explicabo? Nesciunt, est ipsam! Velit in nostrum sunt consectetur laudantium ad inventore labore libero ducimus voluptates.

    &lt;/li&gt;

  &lt;/ul&gt;

&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        li {
            list-style: none;
        }

        .hover-list {
            display: flex;
            align-items: center;
            justify-content: center;
        }

        li#hover {
            text-align: center;
            background-color: darkslategray;
            display: inline;
            padding: 10px;
            border-radius: 50px;
            color: white;
            position: relative; /* Added */
        }

        li#hover:hover::after { /* Added */
            content: attr(data-description);
            display: block;
            position: absolute;
            bottom: -100%; /* Position the pop-up above the hover element */
            left: 50%;
            transform: translateX(-50%);
            width: 300px;
            padding: 10px;
            border: 2px solid black;
            background-color: darkslategrey;
            color: white;
            font-size: large;
            opacity: 1;
            visibility: visible;
            transition: opacity 0.3s, visibility 0.3s;
        }

        li#hover::after { /* Initial styles for the pop-up */
            content: attr(data-description);
            display: block;
            position: absolute;
            bottom: -100%;
            left: 50%;
            transform: translateX(-50%);
            width: 300px;
            padding: 10px;
            border: 2px solid black;
            background-color: darkslategrey;
            color: white;
            font-size: large;
            opacity: 0;
            visibility: hidden;
            transition: opacity 0.3s, visibility 0.3s;
        }
    </style>
</head>
<body>
    <ul class="hover-list">
        <li id="hover" data-description="Lorem ipsum dolor sit amet..."> <!-- Added data-description attribute -->
            Hover Me!!
        </li>
    </ul>
</body>
</html>
英文:

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

<!-- 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;title&gt;Document&lt;/title&gt;
&lt;style&gt;
li {
list-style: none;
}
.hover-list {
display: flex;
align-items: center;
justify-content: center;
}
li#hover {
text-align: center;
background-color: darkslategray;
display: inline;
padding: 10px;
border-radius: 50px;
color: white;
position: relative; /* Added */
}
li#hover:hover::after { /* Added */
content: attr(data-description);
display: block;
position: absolute;
bottom: -100%; /* Position the pop-up above the hover element */
left: 50%;
transform: translateX(-50%);
width: 300px;
padding: 10px;
border: 2px solid black;
background-color: darkslategrey;
color: white;
font-size: large;
opacity: 1;
visibility: visible;
transition: opacity 0.3s, visibility 0.3s;
}
li#hover::after { /* Initial styles for the pop-up */
content: attr(data-description);
display: block;
position: absolute;
bottom: -100%;
left: 50%;
transform: translateX(-50%);
width: 300px;
padding: 10px;
border: 2px solid black;
background-color: darkslategrey;
color: white;
font-size: large;
opacity: 0;
visibility: hidden;
transition: opacity 0.3s, visibility 0.3s;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;ul class=&quot;hover-list&quot;&gt;
&lt;li id=&quot;hover&quot; data-description=&quot;Lorem ipsum dolor sit amet...&quot;&gt; &lt;!-- Added data-description attribute --&gt;
Hover Me!!
&lt;/li&gt;
&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月4日 23:27:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837316.html
匿名

发表评论

匿名网友

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

确定