我在CSS中尝试text-align: right
,但text-align因某种原因没有移动它。我希望网站标题左对齐,图标右对齐在同一行。这是我正在尝试的。
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;
}
答案 0 :(得分:2)
text-align
会将中的文本与您分配给它的元素对齐,而inline-block
元素中的任何内容都不会像这样(因为那个& #39; s与其内容一样宽)。使用float: right;
代替text-align: right
答案 1 :(得分: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;
}
&#13;
<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>
&#13;
答案 2 :(得分:-1)
如果text-align: right
无效,请尝试float: right
。