【问题标题】:Align Text Beside FontAwesome Icon在 FontAwesome 图标旁边对齐文本
【发布时间】:2015-04-19 17:01:57
【问题描述】:

嗨,我在尝试对齐字体真棒图标旁边的文本时遇到问题我尝试了一些东西,但它们似乎都没有工作我想要做的是在一侧制作一个带有按钮的面板您可以单击它,它会在面板的另一侧调用带有 fontawesome 图标和文本的数字(我正在使用 bootstrap v3.3.2 来构建它)

这是我想要做的图片

这里是它目前的样子

<div class="panel panel-default">
  <div class="panel-body">
    <div class="col-lg-8">
      <i class="fa fa-phone fa-2x" style="align: middle;"></i>
      <h3>Call us</h3>
    </div>
    <div class="col-lg-4 text-center">
      <div class="btn-group btn-group-justified" style="align: middle;" role="group" aria-label="...">
        <div class="btn-group" role="group">
          <button type="button" class="btn btn-default" <a href="tel:">Click the button to call us</a>
          </button>
        </div>
      </div>
    </div>
  </div>
</div>

【问题讨论】:

  • 包括整个 CSS 我看到你正在使用 bootstrap .... h3 是块元素你在改变它吗?怎么样?
  • @DanielPinzon h3 元素没有自定义样式,只是引导自己的样式
  • 尝试复制你的问题我不能jsfiddle.net/n336Lnwf
  • @DanielPinzon jsfiddle.net/pw58uf4w 这里是我的 CSS 代码

标签: html css twitter-bootstrap font-awesome


【解决方案1】:

要完全/独立控制字体真棒图标的位置,请尝试以下操作。

方法一:使用absolute positioning

  1. position withrelative 中的property value 添加到h3 样式以控制重叠。

  2. 使用:after选择器content插入图标

  3. position withabsoluteproperty value 添加到 h3 :CSS 代码块之后

  4. 使用 left、right、top 或 bottom 属性值实现所需位置。

方法二:使用float(更简单)。

  1. 使用:after选择器content值插入图标

  2. 通过将:before 选择器向右或向左浮动来达到所需的图标位置。

/* Using absolute positoinning */

h3.absolute {
  position: relative; /* Helps us control overlap */
  padding-left: 25px; /* Creates space for the Phone Icon */
  }

 h3.absolute:before {
  content: '\f095';
  font-family: fontAwesome;
  position: absolute;
  left: 0; /* Adjust as needed */
  top: 3px; /* Adjust as needed */
  }
  
 /* Using float */

  h3.float {
  font-size: 24px;
  color: red;
  }

 h3.float:before {
  content: '\f095';
  font-family: fontAwesome;
  float: left; /* Depends on the side we want the icon */
  margin-right: 10px; /* Creates space between the icon and the text */
  font-size: 24px;
  height: 32px;
  line-height: 32px; /* Same as the font size */
  }
  
  
  /* Below code is jsut for differentiation of methods */
  strong {
  font-size: 24px;
  text-decoration: underline;
  display: block;
  width: 100%;
  }
  
  strong:last-of-type {
  color: red;
  }
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet"/>

<strong>Using absolute Positioning</strong>
<h3 class="absolute">Call us</h3>

<strong>Using float</strong>
<h3 class="float">Call us</h3>

注意:您可以使用 CSS font-size 属性值调整图标的大小。

【讨论】:

    【解决方案2】:

    有时您可能需要为项目添加位置。如果两个元素都在同一列或 div 上并且图片/图标大于这样的东西应该可以工作。

    .position-fix { 位置:粘性;最高:50%; }

    【讨论】:

      【解决方案3】:

      如果您的 fontawesome 的字体大小与您的文本相同(这通常是好的做法),有一个非常简单的方法,在文本标签中包含标签:

      <p>
        <i class="fa fa-check"></i>
       Text text text
      </p>
      

      【讨论】:

        【解决方案4】:

        对于后代,如果您愿意使用 CSS flexbox,对齐文本之外的图标是微不足道的。

        本着回答原始问题的精神,我将 Bootstrap 及其面板作为解决方案的一部分。

        这里要注意的关键点是:

        • display: flex 使直接子级按默认方向(行)流动
        • align-items: center 沿弯曲方向对齐(本例为垂直对齐)
        • flex: 1 让中间部分尽可能扩大,将两侧推开

        .banner {
          display: flex;
          align-items: center;
        }
        
        .banner-start h3 {
          display: inline-block;
        }
        
        .banner-start i {
          padding: 0 6px;
        }
        
        .banner-middle {
          flex: 1;
        }
        <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
        <div class="panel panel-default">
          <div class="panel-body">
            <div class="banner">
              <div class="banner-start">
                <i class="fa fa-phone fa-2x"></i>
                <h3>Call us</h3>
              </div>
              <div class="banner-middle">
        
              </div>
              <div class="banner-end">
                <div class="btn-group" role="group">
                  <button type="button" class="btn btn-default"><a href="tel:">Click the button to call us</a>
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>

        【讨论】:

          【解决方案5】:

          使用 .media 类。

          <div class="media">
              <div class="media-left">
                  <i class="fa fa-home"></i>
              </div>
              <div class="media-body">
                  Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
              </div>
          </div>
          

          【讨论】:

          • 我试过这个,但不太热衷于它,因为它似乎被设计为总是占用其父容器的最大宽度,因为它是为 cmets 旁边的图片之类的东西而设计的。尽管您显然可以设置宽度,但在处理多种语言时这并不容易。但是,我会投票,因为这是我完全不知道的事情,也是 Bootstrap 的一个众所周知的功能。
          【解决方案6】:

          试试这个:

          <i class="fa fa-phone fa-2x" style="vertical-align: middle;"></i>
          

          【讨论】:

          • 是的。这可能是vertical-align 问题。那或者它是浮动的,需要删除浮动并添加display:inlinedisplay:inline-block 以及垂直对齐。但是不要使用内联样式。那很糟。解决方案演示:jsfiddle.net/gcp3qbu4
          猜你喜欢
          • 1970-01-01
          • 2013-05-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-15
          • 2020-10-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多