英文:
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&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-center
和align-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.
<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>
</link>
@import url('https://fonts.googleapis.com/css?family=Montserrat:600&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;
}
tried setting text-center, align-items properties didnt work
答案1
得分: 0
以下是翻译好的部分:
-
在
.Sidebar{}
中添加float:inline-end;
并从 nav 选择器中移除position:fixed;
。 -
这将使侧边栏浮动在屏幕右侧,而您的导航栏只是嵌套在其中。
-
然后,要更改动画的方向,我们将
.Sidebar ul li:hover a span{}
中的padding-left:20%
更改为padding-right:20%
,并在.Sidebar nav li a{}
中添加float:inline-end
。 -
就这样了,我认为。
-
我不需要修改我们的 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('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;
} ```
</details>
# 答案2
**得分**: -1
以下是已经翻译好的部分:
"After a few minutes of "calculated" guessing I found a solution. You say it needs to be a relative position but you don'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't correct because I worked in a smaller window).
I hope this helped you (if so pls let me know), have a nice day :-)
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
@import url('https://fonts.googleapis.com/css?family=Montserrat:600&display=swap');
.sidebar *{
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: fixed;
/*You say it needs to be a relative position but you don'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;
}
<!-- language: lang-html -->
<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>
</link>
<!-- end snippet -->"
<details>
<summary>英文:</summary>
After a few minutes of "calculated" guessing I found a solution. You say it needs to be a relative position but you don'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't correct because I worked in a smaller window).
I hope this helped you (if so pls let me know), have a nice day :-)
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
@import url('https://fonts.googleapis.com/css?family=Montserrat:600&display=swap');
.sidebar *{
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: fixed;
/*You say it needs to be a relative position but you don'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;
}
<!-- language: lang-html -->
<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>
</link>
<!-- end snippet -->
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论