如何将侧栏文本和图标集中在HTML/CSS中?
我正在尝试将文本和图标集中在我网站的侧边栏中,但是我现有的CSS text-Align:中心;
不起作用。这是相关的代码和屏幕截图:
如何将其转换为以下一个示例:
特定问题:
- 您可以确定为什么
text-align:中心;
在这种情况下不起作用吗? - 实现侧边栏内文本和图标的中心的最佳CSS方法是什么?
代码:
CSS
/* sidebar starts here*/
.sidebar {
position: fixed;
top: 60px;
width: 260px;
height: calc(100% - 60px);
background: #df7f27;
overflow-x: hidden;
}
.sidebar ul {
margin-top: 20px;
}
.sidebar ul li {
width: 100%;
list-style: none;
}
.sidebar ul li a {
width: 100%;
text-decoration: none;
color: rgb(255, 255, 255);
height: 60px;
display: flex;
align-items: center;
}
.sidebar ul li a i {
min-width: 60px;
font-size: 24px;
text-align: center;
}
html
<body>
<div class="container">
<div class="topbar">
<div class="logo">
<h2>Pomodone</h2>
</div>
<div class="search">
<input type="text" id="search" placeholder="search here">
<label for="search"><i class="fas fa-search"></i></label>
</div>
<i class="fas fa-bell"></i>
<div class="user">
<img src="img/user.jpg" alt="">
</div>
</div>
<div class="sidebar">
<ul>
<li>
<a href="#">
<i class="fas fa-th-large"></i>
<div>Dashboard</div>
</a>
</li>
<li>
<a href="#">
<i class="fas fa-user-graduate"></i>
<div>Students</div>
</a>
</li>
<li>
<a href="#">
<i class="fas fa-chalkboard-teacher"></i>
<div>Teachers</div>
</a>
</li>
<li>
<a href="#">
<i class="fas fa-users"></i>
<div>Employees</div>
</a>
</li>
<li>
<a href="#">
<i class="fas fa-chart-bar"></i>
<div>Analytics</div>
</a>
</li>
<li>
<a href="#">
<i class="fas fa-hand-holding-usd"></i>
<div>Earnings</div>
</a>
</li>
<li>
<a href="#">
<i class="fas fa-cog"></i>
<div>Settings</div>
</a>
</li>
<li>
<a href="#">
<i class="fas fa-question"></i>
<div>Help</div>
</a>
</li>
</ul>
</div>
<div class="main"></div>
</div>
</body>
谢谢您,感谢您的帮助
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试添加浮点并在图标中添加边距
。
浮子:左;
保证金:0 5px
}
try to add float and margin to the icons
.sidebar ul li a i {
float:left;
margin:0 5px
}
将DIV显示器更改为FLEX并尝试。
请告知我将
menuitem
放在所有DIV类名称上。change the div display to flex and try this.
please inform that I put
menuitem
to all the div class names.在任何内容首先了解图标
www.fontawesome.com
工作之前。只有一件简单的事情要理解:
&lt; i&gt;您在此处使用的标签正在发表评论,并且将SVG加载到标签上方的代码
只需将这些属性添加到您的html
padding-left:30px或根据您的要求;
Jusify-content:Flex-Start;
要添加新类
边缘右翼对齐同一行中的测试
运行此代码
Before anything first understand how the icons
www.fontawesome.com
works.There's only one simple thing to understand that is:
That the < i > tag you used here is getting commented out and a svg is been loaded to your code above the tag
Just add these Properties to your html
Padding-left: 30px or as per your requirement;
Justify-content: flex-start;
New Class to be Added for svg
Added a fixed width for svg.
Margin-right to align the test in the same line
Run this Code
您可以尝试使用flex框,然后做合理的内容,以使其之间或空间之间存在空间
You can try using flex box and then doing justify-content to be space-between or space-evenly
您应该做两件事:例如,
这是结果
You should do two things:
This is the result
我看到您的问题您只需要添加一些CSS Justify-content:Center; ,您使用显示flex; s为什么文本align不起作用,
结帐我的代码,希望它能帮助您。 :)
i see your problem you just simply need to add some css justify-content:center; , you use display flex that;s why text-align is not working,
Checkout my code, Hope it will help you. :)