如何将 Font Awesome 图标右对齐?

9

我正在尝试在CSS中使用text-align: right,但出于某种原因无法移动它。我希望网站标题左对齐,图标右对齐在同一行。这是我的尝试:

JS Fiddle

HTML

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">

<div class="top-container">
    <h2>Website Title</h2>

    <i class="fas fa-bars"></i>
</div>

CSS

body {
  margin: 0;
}

.top-container {
    padding: 20px;
    margin: 0;
    width: 100%;
    color: rgb(255, 255, 255);
    background-color: rgba(0, 0, 0, 0.75);
    font-size: 2em;
    font-weight: bold;
}

.top-container i {
    color: red;
    display: inline-block;
    text-align: right;
}

.top-container h2 {
    display: inline-block;
}

h2 {
  margin-top: 10px;
}

1
可能是将两个内联块左右对齐在同一行上的重复问题。 - Obsidian Age
尝试将以下内容放入<div class="top-container" style=" text-align: right;>中。 - Rogério Dec
只需将“float:right”添加到i元素即可。 - Zoran Pandovski
顺便提一下,一旦您添加了与三条杠图标相对应的导航,您将会遇到其他问题... :) - random_user_name
6个回答

8

text-align属性会对你所指定的元素内部的文本进行对齐,但在像这样的inline-block元素中并没有任何作用(因为它的宽度只有其内容宽度)。请使用float: right;代替text-align: right

https://jsfiddle.net/s4a9sct9/1/


4
如果 text-align: right 不起作用,请尝试使用 float: right

0

文本对齐需要更改显示方式才能生效

  .myClass {
      display: block;
      text-align: right;
    }

0

可以使用 pull-right、align-right 或 float-right。

<a class="btn btn-lg btn-success" href="#">
  <i class="fa fa-flag fa-2x pull-left"></i> Font Awesome<br>Version 4.7.0</a>

    <a class="btn btn-default" href="#">
        <i class="fa fa-align-right" title="Align Right"></i>
      </a>

1
目前你的回答不够清晰,请[编辑]以添加更多细节,帮助其他人理解它如何回答问题。你可以在帮助中心找到有关如何编写好答案的更多信息。 - Community

0

* {
  padding: 0;
  margin: 0;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.top-container {
  position: relative;
  background: #eaeaea;
  width: 100%;
  padding: 30px;
}

.top-container.float-style>h2 {
  display: inline;
}

.top-container.float-style>i {
  float: right;
  line-height:26px; 
}
.top-container.absolute-style > i {
    position: absolute;
    top: 50%;
    right: 30px;
    -webkit-transform: translateY(-50%);
        -ms-transform: translateY(-50%);
            transform: translateY(-50%);
    z-index: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">

<div class="top-container float-style">
  <h2>Website Title float style</h2>
  <i class="fas fa-bars"></i>
</div>

<hr style="margin:30px 0px;">
<div class="top-container absolute-style">
  <h2>Website Title absolute</h2>
  <i class="fas fa-bars"></i>
</div>


0

将自定义 Id 添加到元素中 --> <i class="fal fa-phone" id="customfa"></i>
进入 CSS 文件并添加以下代码 -->

#customfa{right: 10%;} 您可以使用 right、left、top 和 bottom 属性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接