简体   繁体   English

导航栏,标题在中心,按钮在右边

[英]Navbar with Title in center and buttons to right

So I'm attempting to create a navbar with a title in the center and a button that appears to the right.所以我试图创建一个导航栏,标题在中心,按钮出现在右侧。 As you can see, when I attempt to do it, the button appears on the next line and out of the div:如您所见,当我尝试执行此操作时,按钮出现在下一行和 div 之外:

Fiddle小提琴

 .title-bar { background-color: #48A6B8; font-style: italic; margin-bottom: 3%; color: #fff; } .title { text-align: center; font-size: 36px; line-height: 180%; } .buts { float: right; }
 <div class="title-bar"> <div class="title">Title</div> <div class="button"> <button class="buts">Whats Up</button> </div> </div>

display:inline-block seems to remove the centering of the text so I can't use that... display:inline-block似乎删除了文本的居中,所以我不能使用它...

Thanks in advance!提前致谢!

Use flexbox使用flexbox

flex: 1 0 auto; will make title flexible, it will acquire all available free space in flex-container .将使title灵活,它将获得flex-container 中的所有可用空间。

 .title-bar { background-color: #48A6B8; font-style: italic; margin-bottom: 3%; color: #fff; display: flex; } .title { text-align: center; font-size: 36px; line-height: 180%; flex:1 0 auto; }
 <div class="title-bar"> <div class="title">Title</div> <div class="button"> <button class="buts">Whats Up</button> </div> </div>


Edit: after @burak's comment that it's not in exactly center.编辑:在@burak 的评论之后,它并不完全位于中心。

As you can see in the below image, some of the space is acquired by the Whats Up button, that's why title is not in the exact center but Text is in the exact center of it's parent.正如您在下图中所看到的,有些空间是由Whats Up按钮获取的,这就是为什么标题不在正中央,而文本在其父项的正中央。

在此处输入图片说明

We can make it in exact center as well, but for that we'll need to shift Whats Up button on another layer, by using position:absolute我们也可以将它设置在精确的中心,但为此我们需要使用position:absoluteWhats Up按钮移动到另一层

 .title-bar { background-color: #48A6B8; font-style: italic; margin-bottom: 3%; color: #fff; display: flex; position:relative; } .title { text-align: center; font-size: 36px; line-height: 180%; flex:1 0 auto; background:rgba(0,0,0,.5) } .button{ position:absolute; right:0; top:0; }
 <div class="title-bar"> <div class="title">Title</div> <div class="button"> <button class="buts">Whats Up</button> </div> </div>

I used a flexbox for the title bar and let the required margin at the left be calculated automatically.我为标题栏使用了 flexbox,并让左侧所需的边距自动计算。

 .title-bar { display: flex; justify-content: center; width: 100%; } .title, .button { margin-left: auto; }
 <div class="title-bar"> <div class="title">Title</div> <div class="button"> <button class="buts">Whats Up</button> </div> </div>

Use display:inline;使用 display:inline; so that title and button both will appear on same line.这样标题和按钮都会出现在同一行。

 .title-bar { background-color: #48A6B8; font-style: italic; margin-bottom: 3%; color: #fff; display: flex; position:relative; } .title { text-align: center; font-size: 36px; line-height: 180%; flex:1 0 auto; } .buttonRight{ position:absolute; right:0; top:0; padding:5px; }
 <div class="title-bar"> <div class="title">Title</div> <div class="buttonRight"> <button class="btn btn-primary">Whats Up</button> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM