图标不愿意向右移动或与下方的 ul 冲突

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

Icon just does'nt want to move to the right or clashes with my ul below

问题

I Assume the empty space is a div element, in which there is space to the right
根据屏幕显示,我只需将 Bars 图标移动到 div 的右侧,位于右上角,并将 ul 放在下方。但我已经调整了至少一个小时,似乎找不到解决方法。
感谢您的回复!

HTML:

<div><i class="fa-solid fa-bars fa-3x">
				<div class="container-navmenu">
					<ul>
						<li><a href="https://www.playrealm.de/home">Home</a></li>
						<li><a href="https://www.playrealm.de/wiki">Wiki</a></li>
						<li><a href="https://www.playrealm.de/shop">Shop</a></li>
						<li><a href="https://www.playrealm.de/team">Team</a></li>
						<li><a href="https://www.playrealm.de/news">News</a></li>
						<li><a href="https://www.playrealm.de/contact">Contact us</a></li>
					</ul>
				</div>
				</i>
				</div>

样式表:

.fa-bars{
	color: white;
	float: right;
}
.fa-bars:hover{
	color: rgba(161,51,255,1.00);
}
.fa-bars:hover ul{
	display: block;
}
.container-navmenu{
	margin-right: 200px;
	margin-top: 50px;
	float:right;
	text-align: right;
}
.container-navmenu ul{
	display: block;
	position: absolute;
	background-color: #262F3D;
	border-radius: 20px;
	float: right;
}
.container-navmenu ul li{
	list-style: none;
	position: relative;
}
.container-navmenu ul li a{
	font-family: "forma-djr-display", sans-serif;
	font-weight: 800;
	font-size: 20px;
	color: white;
	text-decoration: none;
	padding: 10px;
	display: block;
	transition: all 0.3s ease 0s;
}
.container-navmenu ul li a:hover{
	color: rgba(161,51,255,1.00)
}

我尝试调整了边距和内边距,但图标与列表连接在一起,因此对两者的相对位置没有任何改变。
我也尝试将 </i> 放在图标后面,不包括列表,但结果是,我找不到一种在悬停时显示列表的方法,因为 .fa-bars:hover.container-navmanu ul{display:block} 没有任何效果。

英文:

I Assume the empty space is a div element, in which there is space to the right
As seen in the screen, I just need the Bars-Icon to move to the right side of the div, to be in the Top right corner, and have the ul below. But I've tweaked now for at least an hour and cant seem to find a solution.
Thanks for a reply!

HTML:

<div><i class="fa-solid fa-bars fa-3x">
				<div class="container-navmenu">
					<ul>
						<li><a href="https://www.playrealm.de/home">Home</a></li>
						<li><a href="https://www.playrealm.de/wiki">Wiki</a></li>
						<li><a href="https://www.playrealm.de/shop">Shop</a></li>
						<li><a href="https://www.playrealm.de/team">Team</a></li>
						<li><a href="https://www.playrealm.de/news">News</a></li>
						<li><a href="https://www.playrealm.de/contact">Contact us</a></li>
					</ul>
				</div>
				</i>
				</div>

Stylesheet:

.fa-bars{
	color: white;
	float: right;
}
.fa-bars:hover{
	color: rgba(161,51,255,1.00);
}
.fa-bars:hover ul{
	display: block;
}
.container-navmenu{
	margin-right: 200px;
	margin-top: 50px;
	float:right;
	text-align: right;
}
.container-navmenu ul{
	display: block;
	position: absolute;
	background-color: #262F3D;
	border-radius: 20px;
	float: right;
}
.container-navmenu ul li{
	list-style: none;
	position: relative;
}
.container-navmenu ul li a{
	font-family: "forma-djr-display", sans-serif;
	font-weight: 800;
	font-size: 20px;
	color: white;
	text-decoration: none;
	padding: 10px;
	display: block;
	transition: all 0.3s ease 0s;
}
.container-navmenu ul li a:hover{
	color: rgba(161,51,255,1.00)
}

I tried tweaking margin & Padding, but the Icon is connected to the List, and therefor it just does nothing to change the relative position of both.
I also tried just ending the </i> right after the icon, not including the List, but the outcome is, that I didn't find a way to display the List on Hover, because .fa-bars:hover.container-navmanu ul{display:block} did nothing.

答案1

得分: 1

将图标移动到 div 元素的右上角,可以尝试将以下 CSS 属性添加到 .fa-bars 类:

.fa-bars {
  color: white;
  position: absolute;
  top: 0;
  right: 0;
}

这将使图标位于 div 元素的右上角。

要在悬停时显示列表,可以使用:

.fa-bars:hover {
  color: rgba(161, 51, 255, 1.00);
}

.navmenu-container:hover .container-navmenu ul {
  display: block;
}

这将在悬停时显示 ul 元素。

这是更新后的代码:

<div class="navmenu-container">
  <i class="fa-solid fa-bars fa-3x"></i>
  <div class="container-navmenu">
    <ul>
      <li><a href="https://www.playrealm.de/home">Home</a></li>
      <li><a href="https://www.playrealm.de/wiki">Wiki</a></li>
      <li><a href="https://www.playrealm.de/shop">Shop</a></li>
      <li><a href="https://www.playrealm.de/team">Team</a></li>
      <li><a href="https://www.playrealm.de/news">News</a></li>
      <li><a href="https://www.playrealm.de/contact">Contact us</a></li>
    </ul>
  </div>
</div>
.navmenu-container {
  position: relative;
  display: inline-block;
}

.fa-bars {
  color: white;
  position: absolute;
  top: 0;
  right: 0;
}

.fa-bars:hover {
  color: rgba(161, 51, 255, 1.00);
}

.navmenu-container:hover .container-navmenu ul {
  display: block;
}

.container-navmenu {
  margin-top: 50px;
  float: right;
  text-align: right;
}

.container-navmenu ul {
  display: none;
  position: absolute;
  background-color: #262F3D;
  border-radius: 20px;
  float: right;
}

.container-navmenu ul li {
  list-style: none;
  position: relative;
}

.container-navmenu ul li a {
  font-family: "forma-djr-display", sans-serif;
  font-weight: 800;
  font-size: 20px;
  color: white;
  text-decoration: none;
  padding: 10px;
  display: block;
  transition: all 0.3s ease 0s;
}

.container-navmenu ul li a:hover {
  color: rgba(161, 51, 255, 1.00)
}
英文:

To move the bars icon to the top right corner of the div, you can try adding the following CSS properties to the .fa-bars class:

.fa-bars {
  color: white;
  position: absolute;
  top: 0;
  right: 0;
}

This will position the icon at the top right corner of the div.

To display the list on hover, you can use:

.fa-bars:hover {
  color: rgba(161, 51, 255, 1.00);
}

.navmenu-container:hover .container-navmenu ul {
  display: block;
}

This will display the ul element when the bars icon is hovered over.

Here's the updated code:

&lt;div class=&quot;navmenu-container&quot;&gt;
  &lt;i class=&quot;fa-solid fa-bars fa-3x&quot;&gt;&lt;/i&gt;
  &lt;div class=&quot;container-navmenu&quot;&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/home&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/wiki&quot;&gt;Wiki&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/shop&quot;&gt;Shop&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/team&quot;&gt;Team&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/news&quot;&gt;News&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/contact&quot;&gt;Contact us&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
&lt;/div&gt;
.navmenu-container {
  position: relative;
  display: inline-block;
}

.fa-bars {
  color: white;
  position: absolute;
  top: 0;
  right: 0;
}

.fa-bars:hover {
  color: rgba(161, 51, 255, 1.00);
}

.navmenu-container:hover .container-navmenu ul {
  display: block;
}

.container-navmenu {
  margin-top: 50px;
  float: right;
  text-align: right;
}

.container-navmenu ul {
  display: none;
  position: absolute;
  background-color: #262F3D;
  border-radius: 20px;
  float: right;
}

.container-navmenu ul li {
  list-style: none;
  position: relative;
}

.container-navmenu ul li a {
  font-family: &quot;forma-djr-display&quot;, sans-serif;
  font-weight: 800;
  font-size: 20px;
  color: white;
  text-decoration: none;
  padding: 10px;
  display: block;
  transition: all 0.3s ease 0s;
}

.container-navmenu ul li a:hover {
  color: rgba(161, 51, 255, 1.00)
}

I hope this helps!

答案2

得分: 0

尝试在.fa-bars::before中添加position:absolute和right:0,如下所示:

.fa-bars::before {
    position: absolute;
    right: 0;
}
英文:

Try adding position:absolute and right:0 to .fa-bars::before like this:

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

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

.fa-bars::before {
    position: absolute;
    right: 0;
}
.fa-bars{
    color: black;
    float: right;
}
.fa-bars:hover{
    color: rgba(161,51,255,1.00);
}
.fa-bars:hover ul{
    display: block;
}
.container-navmenu{
    margin-right: 200px;
    margin-top: 50px;
    float:right;
    text-align: right;
}
.container-navmenu ul{
    display: block;
    position: absolute;
    background-color: #262F3D;
    border-radius: 20px;
    float: right;
}
.container-navmenu ul li{
    list-style: none;
    position: relative;
}
.container-navmenu ul li a{
    font-family: &quot;forma-djr-display&quot;, sans-serif;
    font-weight: 800;
    font-size: 20px;
    color: white;
    text-decoration: none;
    padding: 10px;
    display: block;
    transition: all 0.3s ease 0s;
}
.container-navmenu ul li a:hover{
    color: rgba(161,51,255,1.00)
}

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

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css&quot; integrity=&quot;sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot; /&gt;

  &lt;div&gt;
     &lt;i class=&quot;fa-solid fa-bars fa-3x&quot;&gt;
     &lt;div class=&quot;container-navmenu&quot;&gt;
       &lt;ul&gt;
         &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/home&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
         &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/wiki&quot;&gt;Wiki&lt;/a&gt;&lt;/li&gt;
         &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/shop&quot;&gt;Shop&lt;/a&gt;&lt;/li&gt;
         &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/team&quot;&gt;Team&lt;/a&gt;&lt;/li&gt;
         &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/news&quot;&gt;News&lt;/a&gt;&lt;/li&gt;
         &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/contact&quot;&gt;Contact us&lt;/a&gt;&lt;/li&gt;
       &lt;/ul&gt;
     &lt;/div&gt;
    &lt;/i&gt;
   &lt;/div&gt;

<!-- end snippet -->

答案3

得分: 0

我找到了解决方法:

在图标后立即关闭&lt;/i&gt;标签,

并将CSS选择器.fa-bars:hover ul更改为.fa-bars:hover + div ul

(另见对.container-navmenu ul的更改为display: none

祝好运!

<!--开始片段:js隐藏:false控制台:true babel:false -->

<!--语言:lang-css -->

.fa-bars{
    color: black;
    float: right;
}
.fa-bars:hover{
    color: rgba(161,51,255,1.00);
}
.fa-bars:hover + div ul{
    display: block;
}
.container-navmenu{
    margin-right: 200px;
    margin-top: 50px;
    float:right;
    text-align: right;
}
.container-navmenu ul{
    display: none;
    position: absolute;
    background-color: #262F3D;
    border-radius: 20px;
    float: right;
}
.container-navmenu ul li{
    list-style: none;
    position: relative;
}
.container-navmenu ul li a{
    font-family: &quot;forma-djr-display&quot;, sans-serif;
    font-weight: 800;
    font-size: 20px;
    color: white;
    text-decoration: none;
    padding: 10px;
    display: block;
    transition: all 0.3s ease 0s;
}
.container-navmenu ul li a:hover{
    color: rgba(161,51,255,1.00)
}

<!--语言:lang-html -->

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css&quot; integrity=&quot;sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot; /&gt;

&lt;div&gt;
  &lt;i class=&quot;fa-solid fa-bars fa-3x&quot;&gt;&lt;/i&gt;
  &lt;div class=&quot;container-navmenu&quot;&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/home&quot;&gt;主页&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/wiki&quot;&gt;Wiki&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/shop&quot;&gt;商店&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/team&quot;&gt;团队&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/news&quot;&gt;新闻&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/contact&quot;&gt;联系我们&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!--结束片段-->

英文:

I found a solution to do this:

Close the &lt;/i&gt; tag immediately after the icon,

and change your CSS selector .fa-bars:hover ul to .fa-bars:hover + div ul

(see also change in .container-navmenu ul to display: none)

good luck!

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

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

.fa-bars{
    color: black;
    float: right;
}
.fa-bars:hover{
    color: rgba(161,51,255,1.00);
}
.fa-bars:hover + div ul{
    display: block;
}
.container-navmenu{
    margin-right: 200px;
    margin-top: 50px;
    float:right;
    text-align: right;
}
.container-navmenu ul{
    display: none;
    position: absolute;
    background-color: #262F3D;
    border-radius: 20px;
    float: right;
}
.container-navmenu ul li{
    list-style: none;
    position: relative;
}
.container-navmenu ul li a{
    font-family: &quot;forma-djr-display&quot;, sans-serif;
    font-weight: 800;
    font-size: 20px;
    color: white;
    text-decoration: none;
    padding: 10px;
    display: block;
    transition: all 0.3s ease 0s;
}
.container-navmenu ul li a:hover{
    color: rgba(161,51,255,1.00)
}

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

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css&quot; integrity=&quot;sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot; /&gt;

&lt;div&gt;
  &lt;i class=&quot;fa-solid fa-bars fa-3x&quot;&gt;&lt;/i&gt;
  &lt;div class=&quot;container-navmenu&quot;&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/home&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/wiki&quot;&gt;Wiki&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/shop&quot;&gt;Shop&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/team&quot;&gt;Team&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/news&quot;&gt;News&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/contact&quot;&gt;Contact us&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案4

得分: 0

关闭 &lt;/i&gt; 标签放在图标之后,并通过使用 position: absolute; top: 30px; right: 20px; 在我的 fa-bars 样式中移动图标,我将图标重新定位到右上角,并将列表放在下方。要进一步扩展解决方案,您应该在图标和列表周围的 <div> 标签中使用一个新类,现在可以调用 [new class]:hover .container-navmenu ul 来保持列表在图标和列表上时保持打开。如果不这样做,一旦离开图标,列表就会消失。非常感谢给我这个解决方案的每个人!

英文:

closing the &lt;/i&gt; Tag right after the icon, and moving the Icon not by using float: right; but by using position: absolute; top: 30px; right: 20px; in my fa-bars style, I have repositioned the Icon to the Top right, and the List below. To further expand the Solution, you should use a new class in a <div> Tag around the Icon AND the List all together, from whom you can now call a [new class]:hover .container-navmenu ul To keep the List open while ur on the icon AND on the List. If you don't do that, the List will disappear as soon as you leave the Icon. Massive Thanks to everyone that gave me this solution!

答案5

得分: 0

CSS不是问题;问题出在你的HTML。<i>标签可能只有短语内容作为子元素,否则会导致意外行为。

英文:

The CSS isn't the problem; it's your HTML. The &lt;i&gt; tag may only have phrasing content as children, or it will result in unexpected behavior.

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

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

.fa-bars {
  float: right;
}

.fa-bars:hover {
  color: rgba(161, 51, 255, 1.00);
}

.fa-bars:hover ul {
  display: block;
}

.container-navmenu {
  margin-right: 200px;
  margin-top: 50px;
  float: right;
  text-align: right;
}

.container-navmenu ul {
  display: block;
  position: absolute;
  background-color: #262F3D;
  border-radius: 20px;
  float: right;
}

.container-navmenu ul li {
  list-style: none;
  position: relative;
}

.container-navmenu ul li a {
  font-family: &quot;forma-djr-display&quot;, sans-serif;
  font-weight: 800;
  font-size: 20px;
  color: white;
  text-decoration: none;
  padding: 10px;
  display: block;
  transition: all 0.3s ease 0s;
}

.container-navmenu ul li a:hover {
  color: rgba(161, 51, 255, 1.00)
}

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

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css&quot; /&gt;

&lt;div&gt;
  &lt;i class=&quot;fa-solid fa-bars fa-3x&quot;&gt;&lt;/i&gt;
  &lt;div class=&quot;container-navmenu&quot;&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/home&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/wiki&quot;&gt;Wiki&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/shop&quot;&gt;Shop&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/team&quot;&gt;Team&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/news&quot;&gt;News&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.playrealm.de/contact&quot;&gt;Contact us&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定