如何将侧边栏移动到屏幕的右侧

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

How to get the sidebar to the right side of the screen

问题

我是新手,所以请不要在意我的技能,我从别人那里复制了这段代码。
我想要做的是,让侧边栏位于右侧,当你悬停在上面时,详细信息会向左弹出。我会感激任何帮助。

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="sidebar">
  <nav>
    <ul>
      <li>
        <a href="#"><i class="fa fa-facebook-f"></i><span>Facebook</span></a>
      </li>
      <li>
        <a href="#"><i class="fa fa-instagram"></i><span>Instagram</span></a>
      </li>
    </ul>
  </nav>
</div>
@import url('https://fonts.googleapis.com/css?family=Montserrat:600&amp;display=swap');
.sidebar *{
    background-color: #333;
    margin: 0;
    padding: 0;
    list-style: none;
    text-decoration: none;
    box-sizing: border-box;
}
.sidebar {
    display: inline;
    font-family: 'Montserrat', sans-serif;
    height: 100vh;
}
.sidebar nav{
    position: absolute;
    width: 70px;
    margin-top: 125px;
    transition: all 0.3s linear;
    box-shadow: 2px 2px 8px 0px rgba(0,0,0,.4);
}
.sidebar nav li{
    height: 60px;
    position:relative;
}
.sidebar nav li a{
    color: white;
    display: block;
    height: 100%;
    width: 100%;
    line-height: 60px;
    padding-left:25%;
    border-bottom: 1px solid rgba(0,0,0,.4);
    transition: all .3s linear;
}
.sidebar nav li a i{
    position:absolute;
    top: 17px;
    left: 20px;
    font-size: 27px;
}
ul li a span{
    display: none;
    font-weight: bold;
    letter-spacing: 1px;
    text-transform: uppercase;
}
.sidebar a:hover {
    z-index:1;
    width: 300px;
    border-bottom: 1px solid rgba(0,0,0,.5);
    box-shadow: 0 0 1px 1px rgba(0,0,0,.3);
}
.sidebar ul li:hover a span{
    padding-left: 30%;
    display: block;
}

尝试设置text-centeralign-items属性,但未生效。

英文:

I'm new to css, so don't mind my skills, I copied this code from somebody else online.
What I want to do is, having the sidebar to be at the right and when you hover over it, the details pop out towards the left. I'd appreciate any help that I get.

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css&quot;&gt;
  &lt;div class=&quot;sidebar&quot;&gt;
    &lt;nav&gt;
      &lt;ul&gt;
        &lt;li&gt;
          &lt;a href=&quot;#&quot;&gt;&lt;i class=&quot;fa fa-facebook-f&quot;&gt;&lt;/i&gt;&lt;span&gt;Facebook&lt;/span&gt;&lt;/a&gt;
        &lt;/li&gt;
        &lt;li&gt;
          &lt;a href=&quot;#&quot;&gt;&lt;i class=&quot;fa fa-instagram&quot;&gt;&lt;/i&gt;&lt;span&gt;Instagram&lt;/span&gt;&lt;/a&gt;
        &lt;/li&gt;
      &lt;/ul&gt;
    &lt;/nav&gt;
  &lt;/div&gt;
&lt;/link&gt;
@import url(&#39;https://fonts.googleapis.com/css?family=Montserrat:600&amp;display=swap&#39;);
.sidebar *{
    background-color: #333;
  margin: 0;
  padding: 0;
  list-style: none;
  text-decoration: none;
  box-sizing: border-box;
}
.sidebar {
    display: inline;
  font-family: &#39;Montserrat&#39;, sans-serif;
  height: 100vh;
}
.sidebar nav{
  position: absolute;
  width: 70px;
  margin-top: 125px;
  transition: all 0.3s linear;
  box-shadow: 2px 2px 8px 0px rgba(0,0,0,.4);
}
.sidebar nav li{
  height: 60px;
  position:relative;
}
.sidebar nav li a{
  color: white;
  display: block;
  height: 100%;
  width: 100%;
  line-height: 60px;
  padding-left:25%;
  border-bottom: 1px solid rgba(0,0,0,.4);
  transition: all .3s linear;
}

.sidebar nav li a i{
  position:absolute;
  top: 17px;
  left: 20px;
  font-size: 27px;
}
ul li a span{
  display: none;
  font-weight: bold;
  letter-spacing: 1px;
  text-transform: uppercase;
}
.sidebar a:hover {
  z-index:1;
  width: 300px;
  border-bottom: 1px solid rgba(0,0,0,.5);
  box-shadow: 0 0 1px 1px rgba(0,0,0,.3);
  
}
.sidebar ul li:hover a span{
  padding-left: 30%;
  display: block;
}

tried setting text-center, align-items properties didnt work

答案1

得分: 0

以下是翻译好的部分:

  1. .Sidebar{} 中添加 float:inline-end; 并从 nav 选择器中移除 position:fixed;

  2. 这将使侧边栏浮动在屏幕右侧,而您的导航栏只是嵌套在其中。

  3. 然后,要更改动画的方向,我们将 .Sidebar ul li:hover a span{} 中的 padding-left:20% 更改为 padding-right:20%,并在 .Sidebar nav li a{} 中添加 float:inline-end

  4. 就这样了,我认为。

  5. 我不需要修改我们的 HTML 代码,所以这是我在样式表中得到的结果:

@import url('https://fonts.googleapis.com/css?family=Montserrat:600&display=swap');

body {
    background-color: black;
}
.sidebar *{
  margin: 0;
  padding: 0;
  list-style: none;
  text-decoration: none;
  box-sizing: border-box;
}
.sidebar {
    display: inline;
    float: inline-end;
  font-family: 'Montserrat', sans-serif;
  height: 100vh;
}
.sidebar nav{
  
  width: 70px;
  margin-top: 150px;
  transition: all 0.3s linear;
  box-shadow: 2px 2px 8px 0px rgba(0,0,0,.4);
  
}
.sidebar nav li{
  height: 60px;
  position: relative;
}
.sidebar nav li a{
  color: white;
  display: block;
  float: inline-end;
  height: 100%;
  width: 100%;
  line-height: 60px;
  
  border-bottom: 1px solid rgba(0,0,0,.4);
  transition: all .3s linear;
}

.sidebar nav li a i{
  position: absolute;
  float: inline-end;
  top: 17px;
  left: 20px;
  font-size: 27px;
}
ul li a span{
    display: none;
  font-weight: bold;
  letter-spacing: 1px;
  text-transform: uppercase;
}
.sidebar a:hover {
  z-index: 1;
  width: 300px;
  border-bottom: 1px solid rgba(0,0,0,.5);
  box-shadow: 0 0 1px 1px rgba(0,0,0,.3);
}
.sidebar ul li:hover a span{
  padding-right: 20%;
  display: block;
}
英文:

So The first thing I did was to add float:inline-end; to our .Sidebar{} and removed position:fixed; from our nav selector.

(my own first instinct was to use flexbox, but that's just me.)

This will make your sidebar float on the right side of the screen and your nav is just housed in it.

Then to change the direction of our animation, we change padding-left:20% of .Sidebar ul li:hover a span{} to padding-right:20% and add float:inline-end to .Sidebar nav li a{}.

And that's pretty much it I think.

I didn't have to touch our html code so here's what I ended up with in the stylesheet:

@import url(&#39;https://fonts.googleapis.com/css?family=Montserrat:600&amp;display=swap&#39;);
body {
background-color: black;
}
.sidebar *{
margin: 0;
padding: 0;
list-style: none;
text-decoration: none;
box-sizing: border-box;
}
.sidebar {
display:inline;
float: inline-end;
font-family: &#39;Montserrat&#39;, sans-serif;
height: 100vh;
}
.sidebar nav{
width: 70px;
margin-top: 150px;
transition: all 0.3s linear;
box-shadow: 2px 2px 8px 0px rgba(0,0,0,.4);
}
.sidebar nav li{
height: 60px;
position:relative;
}
.sidebar nav li a{
color: white;
display: block;
float: inline-end;
height: 100%;
width: 100%;
line-height: 60px;
border-bottom: 1px solid rgba(0,0,0,.4);
transition: all .3s linear;
}
.sidebar nav li a i{
position:absolute;
float: inline-end;
top: 17px;
left: 20px;
font-size: 27px;
}
ul li a span{
display: none;
font-weight: bold;
letter-spacing: 1px;
text-transform: uppercase;
}
.sidebar a:hover {
z-index:1;
width: 300px;
border-bottom: 1px solid rgba(0,0,0,.5);
box-shadow: 0 0 1px 1px rgba(0,0,0,.3);
}
.sidebar ul li:hover a span{
padding-right:20%;
display: block;
} ```
</details>
# 答案2
**得分**: -1
以下是已经翻译好的部分:
"After a few minutes of &quot;calculated&quot; guessing I found a solution. You say it needs to be a relative position but you don&#39;t say where it needs to go. So I just typed coordinates here, if you want to update these coordinates you will have to watch out because it is very sensitive (The coordinates aren&#39;t correct because I worked in a smaller window).
I hope this helped you (if so pls let me know), have a nice day :-)
&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;
&lt;!-- language: lang-css --&gt;
@import url(&#39;https://fonts.googleapis.com/css?family=Montserrat:600&amp;display=swap&#39;);
.sidebar *{
margin: 0;
padding: 0;
list-style: none;
text-decoration: none;
box-sizing: border-box;
}
.sidebar {
display: inline;
font-family: &#39;Montserrat&#39;, sans-serif;
height: 100vh;
}
.sidebar nav{
position: fixed;
/*You say it needs to be a relative position but you don&#39;t say where it needs to go:*/
top: 10px; left: 400px;
width: 70px;
margin-top: 150px;
transition: all 0.3s linear;
box-shadow: 2px 2px 8px 0px rgba(0,0,0,.4);
}
.sidebar nav li{
height: 60px;
position:relative;
}
.sidebar nav li a{
color: white;
display: block;
height: 100%;
width: 100%;
line-height: 60px;
padding-left:25%;
border-bottom: 1px solid rgba(0,0,0,.4);
transition: all .3s linear;
}
&lt;!-- language: lang-html --&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css&quot;&gt;
&lt;div class=&quot;sidebar&quot;&gt;
&lt;nav&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;&lt;i class=&quot;fa fa-facebook-f&quot;&gt;&lt;/i&gt;&lt;span&gt;Facebook&lt;/span&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;&lt;i class=&quot;fa fa-instagram&quot;&gt;&lt;/i&gt;&lt;span&gt;Instagram&lt;/span&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/nav&gt;
&lt;/div&gt;
</link>
&lt;!-- end snippet --&gt;"
<details>
<summary>英文:</summary>
After a few minutes of &quot;calculated&quot; guessing I found a solution. You say it needs to be a relative position but you don&#39;t say where it needs to go. So I just typed coordinates here, if you want to update these coordinates you will have to watch out because it is very sensitive (The coordinates aren&#39;t correct because I worked in a smaller window).
I hope this helped you (if so pls let me know), have a nice day :-)
&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;
&lt;!-- language: lang-css --&gt;
@import url(&#39;https://fonts.googleapis.com/css?family=Montserrat:600&amp;display=swap&#39;);
.sidebar *{
margin: 0;
padding: 0;
list-style: none;
text-decoration: none;
box-sizing: border-box;
}
.sidebar {
display: inline;
font-family: &#39;Montserrat&#39;, sans-serif;
height: 100vh;
}
.sidebar nav{
position: fixed;
/*You say it needs to be a relative position but you don&#39;t say where it needs to go:*/
top: 10px; left: 400px;
width: 70px;
margin-top: 150px;
transition: all 0.3s linear;
box-shadow: 2px 2px 8px 0px rgba(0,0,0,.4);
}
.sidebar nav li{
height: 60px;
position:relative;
}
.sidebar nav li a{
color: white;
display: block;
height: 100%;
width: 100%;
line-height: 60px;
padding-left:25%;
border-bottom: 1px solid rgba(0,0,0,.4);
transition: all .3s linear;
}
&lt;!-- language: lang-html --&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css&quot;&gt;
&lt;div class=&quot;sidebar&quot;&gt;
&lt;nav&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;&lt;i class=&quot;fa fa-facebook-f&quot;&gt;&lt;/i&gt;&lt;span&gt;Facebook&lt;/span&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot;&gt;&lt;i class=&quot;fa fa-instagram&quot;&gt;&lt;/i&gt;&lt;span&gt;Instagram&lt;/span&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/nav&gt;
&lt;/div&gt;
&lt;/link&gt;
&lt;!-- end snippet --&gt;
</details>

huangapple
  • 本文由 发表于 2023年2月14日 00:07:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438396.html
匿名

发表评论

匿名网友

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

确定