Как выровнять значки Font Awesome вправо? - PullRequest
0 голосов
/ 17 мая 2018

Я пытаюсь text-align: right в моем CSS, но выравнивание текста не перемещает его по некоторым причинам. Я хочу, чтобы заголовок веб-сайта был выровнен по левому краю, а значок - по одной линии. Вот что я пытаюсь.

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;
}

Ответы [ 3 ]

0 голосов
/ 17 мая 2018

text-align выровняет текст в пределах элемента, которому вы его назначаете, который ничего не делает в элементе inline-block, подобном этому (поскольку он столь же широк, как и его содержимое),Используйте float: right; вместо text-align: right

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

0 голосов
/ 17 мая 2018

* {
  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 голосов
/ 17 мая 2018

Если text-align: right не работает, попробуйте float: right.

...