【问题标题】:Centering a text icon inside of a containing div在包含的 div 中居中文本图标
【发布时间】:2014-06-19 09:38:49
【问题描述】:

我想在后面的 h4 和 p 上方居中放置一个文本图标。我认为这不会是一个问题,但我已经尝试了一切,文本图标仍然卡在左侧而不是居中。我对边距、文本对齐或垂直对齐所做的一切,似乎都只是在其背景中移动图标。我制作了一个jsFiddle 来玩,这里是代码 jic:

HTML:

<div id="contentCon1">
    <section class="textIcon large">
        <a href="">
            <i class="i-large icon-text-width"></i>
            <h4>Lorem Ipsum</h4>
            <div><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum et tortor consequat, facilisis tortor ut, tempus purus. Duis porta dui turpis, ac blandit augue rhoncus a. </p>
            </div>
        </a>
    </section>
</div>

CSS:

#contentCon1 {
  clear: both;
  float: left;
  margin-left: 0;
  width: 35%;
  display: block;
  text-align: center;
}
a {
  text-decoration:none;
}
.textIcon > h4, .textIcon > a > h4 {
  font-size:16px;
  line-height:20px;
  font-weight:bold;
  color:#3B3B3B;
  margin:25px 0 0 0;
}
.textIcon > div {
  margin-top:15px
}
.textIcon > a {
  display:block;
}
.textIcon > a:hover > h4 {
  color:#F15A2B;
}

.textIcon.large {
  position:relative;
  border-bottom:1px solid #E9E9E9;
  padding-bottom:50px;
  margin-bottom: 20px;
}
.textIcon.large > i, .textIcon.large > a > i {
  float:none;
  margin:0;
}.textIcon.large > a:hover  p {
  color:#6b6b6b;
}
.textIcon.large:hover {
  border-bottom:1px solid #F15A2B;
}
.textIcon.large > a:hover > i {
  color:#FFF;
  background:#F15A2B;
}

@font-face {
  font-family: 'fontello';
  src: url('http://mtctinc.com/twBootstrap/styles/fonts/fontello.eot?11956569');
  src: url('http://mtctinc.com/styles/fonts/fontello.eot?11956569#iefix') format('embedded-opentype'),
       url('http://mtctinc.com/twBootstrap/styles/fonts/fontello.woff?11956569') format('woff'),
       url('http://mtctinc.com/twBootstrap/styles/fonts/fontello.ttf?11956569') format('truetype'),
       url('http://mtctinc.com/twBootstrap/styles/fonts/fontello.svg?11956569#fontello') format('svg');
  font-weight: normal;
  font-style: normal;
}


 [class^="icon-"]:before, [class*=" icon-"]:before {
  font-family: "fontello";
  font-style: normal;
  font-weight: normal;
  speak: none; 
  display: inline-block;
  text-decoration: inherit;
  width: 1em;
  margin-right: .2em;
  text-align: center;
  font-variant: normal;
  text-transform: none;
  line-height: 1em;
  margin-left: .2em;
} 
.icon-text-width:before { 
    content: '\e801';
} /* '' */

i.i-large {
  width:75px;
  height:75px;
  background:#EEE;
  border-radius:48px;
  color:#999;
  display:table-cell;
  vertical-align:middle;
  float:none;
  margin:-5px 0px 10px 0px;
  text-align:center;
  font-size:38px;
  cursor:default;
}

【问题讨论】:

    标签: html css centering text-alignment text-align


    【解决方案1】:

    插入 CSS

    i.icon-text-width {
        display: inline-block;
    }
    

    【讨论】:

    • 感谢您的正确回答,但 josh 击败了您。也许你能帮我弄清楚为什么这个解决方案对我的actual site 没有影响
    【解决方案2】:

    鉴于text-align:center 在父元素上,您只需将图标的显示从table-cell 更改为inline-block。

    Updated Example Here

    i.i-large {
        display:inline-block;
    }
    

    【讨论】:

    • 天哪!我不敢相信就是这样。我想我并没有对此感到困惑,因为我从未了解过有关display: table-cell 的任何信息。我将不得不阅读。谢谢你。我看到您还必须更改行高以使其垂直对齐。你能解释一下为什么这是必要的吗?
    • @MilkyTech 我这样做是为了垂直对齐图标。否则它看起来像this。
    • 所以我猜table-cell 在卡在左侧时也保持垂直对齐?
    • 疯了!这在小提琴中修复了它,但是,它并没有改变actuqal site 上的任何东西。我已经摆脱了table-cell 的每个实例,但仍然停留在左侧。 :-(
    • @MilkyTech 你没有添加text-align:center。您可以添加section.textIcon.clearfix.large { text-align: center; }
    猜你喜欢
    • 2014-09-05
    • 1970-01-01
    • 2012-07-14
    • 2014-12-17
    • 2013-04-08
    • 2019-11-24
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    相关资源
    最近更新 更多