代码之家  ›  专栏  ›  技术社区  ›  user3410843

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

  •  0
  • user3410843  · 技术社区  · 6 年前

    我有多个水平显示的菜单项。在每个菜单项中,我都有一个span图标(数据图标)和一个文本。它们以文本居中对齐:居中。一些菜单项文本很长。在我进入小屏幕之前,一切都正常。

    问题是,当文本断开到下一行时,它会在菜单项容器内居中,并进入图标下方。

    是否有办法防止项目文本本身居中,但同时将图标和文本放在菜单容器的中心?

    .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>

    enter image description here

    enter image description here

    3 回复  |  直到 6 年前
        1
  •  2
  •   Bhuwan    6 年前

    尝试使用 Flexbox

    堆栈代码段

    .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>
        2
  •  0
  •   Ryan    6 年前

    您可以使用css网格来简化这一过程。

    .container {
      display: grid;
      grid-template-columns: 1fr 1fr 1fr 1fr; 
    }
    p {
      grid-column: 3/5;
    }
    h1 {
      grid-column: 1/3;
    }
    
        3
  •  0
  •   Jaakko Uusitalo    6 年前
    .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
    }
    

    以下是JSFIDLE: jsfiddle

    更多信息: Alligator io aligment tutorial