简体   繁体   English

如何将文本与不均匀图标对齐?

[英]How can I center-align text with uneven icons either side?

I'm trying to center align the text in this image, ideally using as little CSS/HTML as possible. 我试图在这个图像中居中对齐文本,理想情况下使用尽可能少的CSS / HTML。 As you can see, the icons on the left push it off center: 如您所见,左侧的图标将其推离中心:

对齐问题

Here's the relevant HTML and CSS for this top section: 以下是此顶部的相关HTML和CSS:

<div class="navbarheader">
    <div class="header-left">
        <button type="button" class="pull-left btn-nav-menu">
            <i class="navbar-text fa fa-fw fa-navicon"></i>
        </button>
        <button type="button" class="pull-left" ng-click="ui.showSearch()">
            <i class="navbar-text fa fa-fw fa-search"></i>
        </button>
    </div>
    <div class="header-title">
        <div class="navbar-brand">{{ui.headerTitle}}</div>
    </div>
    <div class="header-right">
        <button class="btn-watchlist pull-right" ng-click="ui.toggleWatchlists()">
            <i class="navbar-text fa fa-fw fa-binoculars"></i>
        </button>
    </div>
</div>

CSS: CSS:

.navbarheader {
  display: flex;
  flex-direction: row;
  justify-content: space-around;
}
.navbarheader .header-left {
  margin-left: -0.5rem;
}
.navbarheader .header-title {
  flex-grow: 2;
  text-align: center;
}
.navbarheader .header-right {
  margin-right: -0.5rem;
}

Any ideas how to keep the text center aligned, but allow it to occupy all the blank space if necessary? 任何想法如何保持文本中心对齐,但如果有必要允许它占据所有空白?

Worth mentioning this is in a Bootstrap 4.0 alpha codebase. 值得一提的是Bootstrap 4.0 alpha代码库。

You need to make sure both .header-right and .header-left apply the same "flex" pressure on .header-title . 你需要确保.header-right.header-left.header-title施加相同的“flex”压力。 Their flex-grow , flex-shrink and flex-basis need to be equal. 它们的flex-growflex-shrinkflex-basis需要相等。 By default, they have flex:0 1 auto; 默认情况下,它们有flex:0 1 auto; so you don't need to worry about flex-grow [ 0 ] and flex-shrink [ 1 ]. 所以你不必担心flex-grow [ 0 ]和flex-shrink [ 1 ]。

However, you need to change the auto into a practical value (anything translatable in px by the browser: % , px , em , rem ,...), greater than 50% (you can go below 50% if you set flex-grow:1; on .header-left and .header-right - just remember to make them equal). 但是,您需要将auto更改为实用值(浏览器在px翻译的任何内容: %pxemrem ,...),大于50% (如果设置flex-grow:1;则可以低于50% flex-grow:1; on .header-left.header-right - 只记得让它们相等)。

Your title, even though will have a resulting flex-basis of 0 , will grow eating up equally from left and right's width. 你的标题即使会产生0 flex-basis ,也会从左右宽度上同等地增加。 Of course, you need to make sure it always stays on 1 line by adding white-space:nowrap to it (if it's really long and you think it should wrap on some very narrow screens place the white-space rule inside a @media query; also, remember to give your .header-title sufficient flex-basis when you allow it to wrap, so it doesn't wrap on too many lines - don't worry if your total flex-basis is larger than 100% , all browsers will scale all elements down proportionally if the parent's flex-wrap is set to nowrap ). 当然,你需要通过添加white-space:nowrap来确保它始终保持在1行white-space:nowrap到它(如果它真的很长并且你认为它应该包裹在一些非常狭窄的屏幕上,将white-space规则放在@media查询中另外,记得在允许它包装时给你的.header-title足够的flex-basis ,所以它不会包裹太多的线 - 如果你的总flex-basis大于100% ,不要担心,所有如果父级的flex-wrap设置为nowrap浏览器将按比例缩小所有元素。

If you prefer CSS to English, here goes code: 如果您更喜欢CSS到英语,请输入以下代码:

.navbarheader {
  display: flex;
  align-items: center;
  white-space: nowrap;
  flex-wrap: nowrap;
}
.header-right,
.header-left {
  flex-basis: 50%;
}
.header-right {
  text-align: right;
}

And the prefixed (production ready) version: 和前缀(生产就绪)版本:

 .navbarheader { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; white-space: nowrap; } .header-right, .header-left { -webkit-flex-basis: 50%; -ms-flex-preferred-size: 50%; flex-basis: 50%; } .header-right { text-align: right; } 

But the relevant part is 但相关部分是

.header-right, .header-left { flex-basis: 50%; }

Distribute your headers in amounts you can live with without breaking responsiveness. 以您可以使用的金额分配您的标题,而不会破坏响应能力。 so that you have a perfect amount to center with and an equal amount to each side to use as gutters or margins. 因此,你有一个完美的数量,以每个方面的中心和相等的数量用作排水沟或边距。

.navbarheader .header-left {
  margin-left: -0.5rem;
    width:20%; /* adjust accordingly if it's too much. do the same in the other to complete 100% total */
    background: red;
}
.navbarheader .header-title {
  flex-grow: 2;
  text-align: center;
    width:60%;
    background: yellow;
}
.navbarheader .header-right {
  margin-right: -0.5rem;
    width:20%;
    background: green;
}

See the demo (I colored the backgrounds so that you can distinguish the distances) 演示 (我为背景着色,以便您可以区分距离)

you could add a gap same size as icon missing: 您可以添加与缺少图标相同大小的间隙:

.navbarheader > :last-child{
  margin-left:2.5rem;
}

demo 演示

 /* background gradient added to visualize centers */ .navbarheader { display: flex; flex-direction: row; justify-content: space-around; max-width: 600px; padding: 1em 0; margin: auto; background: linear-gradient(to left, gray 50%, lightgray 50%); } .navbarheader .header-left { margin-left: -0.5rem; } .navbarheader .header-title { flex-grow: 2; text-align: center; background: linear-gradient(to right, gray 50%, lightgray 50%); } .navbarheader .header-right { margin-right: -0.5rem; } .navbarheader >:last-child { margin-left: 2.5rem; } 
 <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css"> <div class="navbarheader"> <div class="header-left"> <button type="button" class="pull-left btn-nav-menu"> <i class="navbar-text fa fa-fw fa-navicon"></i> </button> <button type="button" class="pull-left" ng-click="ui.showSearch()"> <i class="navbar-text fa fa-fw fa-search"></i> </button> </div> <div class="header-title"> <div class="navbar-brand">whatever text</div> </div> <div class="header-right"> <button class="btn-watchlist pull-right" ng-click="ui.toggleWatchlists()"> <i class="navbar-text fa fa-fw fa-binoculars"></i> </button> </div> </div> 

You have to use position: absolute on header-title and remove it from natural flow of elemnts so it will always be centered relatively to navbarheader 你必须使用position: absoluteheader-title和elemnts的自然流动删除它,所以它总是会相对集中navbarheader

 nav { display: flex; justify-content: space-between; align-items: center; padding: 25px; border: 1px solid black; position: relative; } .title { position: absolute; left: 50%; transform: translateY(-50%); } a { font-size: 30px; padding: 0 10px; } 
 <nav> <div class="left"> <a href="">Icon</a> <a href="">Icon</a> </div> <div class="title">Title</div> <div class="right">Icon</div> </nav> 

使用负边距修复它...将文本居中, margin-left减去左图标宽度的一半。

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

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