简体   繁体   English

自适应-将图标和文本对齐菜单项的中心

[英]Responsive - align icon and text to center of the menu item

I have multiple menu items that are displayed horizontally. 我有多个水平显示的菜单项。 In each menu item I have a span icon (data-icon) and a text. 在每个菜单项中,我都有一个范围图标(数据图标)和一个文本。 They are centered with text-align:center. 它们以text-align:center为中心。 Some menu item texts are long. 一些菜单项文本很长。 Everything is OK until I get into smaller screens. 一切正常,直到进入较小的屏幕。

The problem is that when the text breaks to the next line it gets centered inside the menu item container and goes under the icon. 问题是,当文本中断到下一行时,它将居中于菜单项容器的中心,并进入图标下方。

Is there a way to prevent and center item text by itself but also leave Icon and Text centered inside the menu container ? 有没有一种方法可以单独阻止和居中显示文本,还可以使Icon和Text居中放置在菜单容器中?

 .mycontainer { width: 50%; background: green; text-align: center; float: left; } .big-icon { font-size: 40px; } .small-text { font-size: 12px; position: relative; top: -15px; } 
 <div class="menu"> <div class="mycontainer"> <span class="big-icon">Big Icon</span> <span class="small-text">This is very very very very very very very very very long text</span> </div> <div class="mycontainer" style="background: yellow;"> <span class="big-icon">Big Icon</span> <span class="small-text">This is short text</span> </div> </div> 

在此处输入图片说明

在此处输入图片说明

Try to use Flexbox 尝试使用Flexbox

Stack Snippet 堆栈片段

 .menu { display: flex; flex-wrap: wrap; } .mycontainer { padding: 20px; box-sizing: border-box; width: 50%; background: green; text-align: center; display: flex; align-items: center; flex-direction: row; justify-content: center; } .big-icon { font-size: 40px; flex-shrink: 0; } .small-text { font-size: 12px; position: relative; flex: 0 1 40%; } 
 <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous"> <div class="menu"> <div class="mycontainer"> <span class="fa fa-address-book-o fa-4x"></span> <span class="small-text">This is very very very very veryveryveryveryverylongtext</span> </div> <div class="mycontainer" style="background: yellow;"> <span class="fa fa-address-book-o fa-4x"></span> <span class="small-text">This is short text</span> </div> </div> 

You could use css grids to make this a lot easier. 您可以使用css网格来简化此操作。

.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr; 
}
p {
  grid-column: 3/5;
}
h1 {
  grid-column: 1/3;
}
.menu {
  display: grid
}

.mycontainer {
  display: grid;
  grid-gap: 10px; // adds padding to columns
  grid-template-columns: 1fr 1fr; //defines two columns (1/2 each)
  align-items: center; //Centers children vertically
  justify-items: center; //Centers children horizontally
  background: green
}

.big-icon {
  font-size: 40px
}

.small-text {
  font-size: 12px
}

Here is JSFiddle: jsfiddle 这是JSFiddle: jsfiddle

More here: Alligator io aligment tutorial 更多内容: Alligator io适应性教程

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

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