在标题之前添加字体真棒图标?

Rui*_* Li 1 html css font-awesome

我正在尝试设计自己的主页并使用此只读模板.

我想稍微自定义我的标题,以便之前有一个图标.

这是我的代码

.icon {
    text-decoration: none;
    text-size: 1em;
    display: inline;
    margin: 0 0 1.5em 0;
    padding: 0.35em 0 0 3.5em;
    position: relative;
    vertical-align: baseline;
    height: 0.5em;
    width: 48%;
}

.icon:before {
    -moz-osx-font-smoothing: grayscale;
    -webkit-font-smoothing: antialiased;
    font-family: FontAwesome;
    font-style: normal;
    font-weight: normal;
    text-transform: none !important;
    background: #4acaa8;
    border-radius: 100%;
    color: #ffffff;
    display: inline-block;
    height: 2.5em;
    left: 0;
    line-height: 2.5em;
    position: absolute;
    text-align: center;
    top: 0;
    width: 2.5em;
}
Run Code Online (Sandbox Code Playgroud)
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"/>
<i class="icon fa-rss"></span> </i> <h3 style="display: inline">First Blog</h3> 
<p>This is the first blog<p>

<h3>Second Blog<h3>
<p>This is the second blog<p>
Run Code Online (Sandbox Code Playgroud)

但是,图标和第一个标题似乎没有垂直对齐.另外,如何调整图标的大小?

任何帮助将非常感谢!

sol*_*sol 6

  1. 设置图标后的标题 display: inline-block
  2. position: absolute从中删除::before,然后将其设置为display: inline-block
  3. 从中删除边距和填充.icon,并使用width属性来定义占用的空间
  4. 使用font-size更改图标的大小.(不是文字大小)

i+h3 {
  display: inline-block;
}

.icon {
  text-decoration: none;
  font-size: 1em;
  /* edit this to change the size of the icon */
  display: inline-block;
  width: 3em;
  position: relative;
}

.icon:before {
  -moz-osx-font-smoothing: grayscale;
  -webkit-font-smoothing: antialiased;
  font-family: FontAwesome;
  font-style: normal;
  font-weight: normal;
  text-transform: none !important;
  background: #4acaa8;
  border-radius: 100%;
  color: #ffffff;
  display: inline-block;
  line-height: 2.5em;
  text-align: center;
  width: 2.5em;
}
Run Code Online (Sandbox Code Playgroud)
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet" />
<i class="icon fa-rss"></span> </i>
<h3>First Blog</h3>
<p>This is the first blog
  <p>

    <h3>Second Blog
      <h3>
        <p>This is the second blog
          <p>
Run Code Online (Sandbox Code Playgroud)

您可以将类添加到图标后面的任何类,而不是使用相邻的选择器,h3然后将样式应用于类.

HTML

<i class="icon fa-rss"></i>
<h3 class="icon-heading">First Blog</h3>
Run Code Online (Sandbox Code Playgroud)

CSS

.icon-heading {
  display: inline-block;
}
Run Code Online (Sandbox Code Playgroud)

  • `i + h3` 意味着任何紧跟在 i 标签后面的 h3 标签都将成为目标。我使用它是为了页面上的其他 h3 标签不会获得“display: block”。CSS 中的“+”被称为相邻选择器 (2认同)