显示:table-cell;在文字左侧添加额外空格

时间:2017-05-25 19:30:32

标签: html css

我对html很新,而css一直在努力让我的文本使用css和html在图像旁边垂直对齐。

我使用" display:table-cell;"在

类和" display:table;"在它的容器类中。

这似乎有效,直到我遇到一个案例,我的文字没有溢出到第二行。无论出于何种原因,似乎我的文本中较短的句子与图像和下一列之间的空间中心对齐。

HTML:

<h3 class="red-header">
  Buttons Header
</h3>
<div class="row">
  <div class="column divLink">
    <img class="icon-img" src="http://www.immersion-3d.com/wp-content/uploads/2015/12/image-placeholder-500x500.jpg" />
    <p class="icon-text">
      <span class="red-text">Button Text pt 1</span>
      <br /> short line extra space on the left</p>
    <a class="feature" href="google.ca"></a>
  </div>
  <div class="column divLink">
    <img class="icon-img" src="http://www.immersion-3d.com/wp-content/uploads/2015/12/image-placeholder-500x500.jpg" />
    <p class="icon-text">
      <span class="red-text">Button Text pt 2</span>
      <br /> short setence describing button could possibly extend for a line or two or three. Would like this to be centered</p>
    <a class="feature" href="google.ca"></a>
  </div>
</div>
<div class="row">
  <div class="column divLink">
    <img class="icon-img" src="http://www.immersion-3d.com/wp-content/uploads/2015/12/image-placeholder-500x500.jpg" />
    <p class="icon-text">
      <span class="red-text">Button Text pt 3</span>
      <br /> short setence describing button could possibly extend for a line or two or three. Would like this to be centered</p>
    <a class="feature" href="google.ca"></a>
  </div>
  <div class="column divLink">
    <img class="icon-img" src="http://www.immersion-3d.com/wp-content/uploads/2015/12/image-placeholder-500x500.jpg" />
    <p class="icon-text">

      <span class="red-text">Button Text pt 4</span>


      <br /> short setence describing button could possibly extend for a line or two or three. Would like this to be centered </p>
    <a class="feature" href="google.ca"></a>
  </div>
</div>

CSS:

.red-header {
  background-color: #be1e2d;
  color: #FFF;
  font-size: 1rem;
  font-family: Lato, Helvetica, Arial, sans-serif;
  padding: .6rem;
  text-align: center;
}

.row {
  width: 100%;
}

.icon-text {
  display: table-cell;
  height: 100%;
  vertical-align: middle;
}

.column {
  width: 50%;
  display: table;
  position: relative;
  float: left;
}

.red-text {
  color: #be1e2d;
  margin-bottom: 0px;
  margin-top: 10px;
  line-height: 1;
}

.icon-img {
  float: left;
  width: 115px;
  padding: 5px 16px 5px 0px;
}

.feature {
  position: relative;
}

/* used to make entire div into a clickable element */
.feature {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  text-decoration: none;
  /* No underlines on the link */
  z-index: 10;
  /* Places the link above everything else in the div */
  background-color: #FFF;
  /* Fix to make div clickable in IE */
  opacity: 0;
  /* Fix to make div clickable in IE */
  filter: alpha(opacity=1);
  /* Fix to make div clickable in IE */
}

/* visualization of the button using a hover */
.divLink:hover {
  background-color: #b1e3e6;
}

我为一个可怕的解释道歉,please see the following fiddle for clarity.问题出在&#34;按钮文字第1&#34;

3 个答案:

答案 0 :(得分:-1)

这更接近你想要的吗? display: table只应用于表格,而不是表格。你想要Flexbox!

https://jsfiddle.net/o092e0y9/

答案 1 :(得分:-1)

您可以通过将以下width: 100%添加到.icon-text类来修复当前代码的间距问题:

.icon-text {
   display: table-cell;
   width: 100%;
   height: 100%;
   vertical-align: middle;
}

当然我还是建议你选择使用flexbox。

答案 2 :(得分:-1)

以下是您现有的解决方案

https://jsfiddle.net/4Lp507cw/5/

<强> HTML

<h3 class="red-header">
  Buttons Header
</h3>
<div class="table">

  <div class="row">
    <div class="column divLink">
      <img class="icon-img" src="http://www.immersion-3d.com/wp-content/uploads/2015/12/image-placeholder-500x500.jpg" />
      <p class="icon-text">
        <span class="red-text">Button Text pt 1</span>
        <br /> short line extra space on the left</p>
      <a class="feature" href="google.ca"></a>
    </div>
    <div class="column divLink">
      <img class="icon-img" src="http://www.immersion-3d.com/wp-content/uploads/2015/12/image-placeholder-500x500.jpg" />
      <p class="icon-text">
        <span class="red-text">Button Text pt 2</span>
        <br /> short setence describing button could possibly extend for a line or two or three. Would like this to be centered</p>
      <a class="feature" href="google.ca"></a>
    </div>
  </div>
  <div class="row">
    <div class="column divLink">
      <img class="icon-img" src="http://www.immersion-3d.com/wp-content/uploads/2015/12/image-placeholder-500x500.jpg" />
      <p class="icon-text">
        <span class="red-text">Button Text pt 3</span>
        <br /> short setence describing button could possibly extend for a line or two or three. Would like this to be centered</p>
      <a class="feature" href="google.ca"></a>
    </div>
    <div class="column divLink">
      <img class="icon-img" src="http://www.immersion-3d.com/wp-content/uploads/2015/12/image-placeholder-500x500.jpg" />
      <p class="icon-text">

        <span class="red-text">Button Text pt 4</span>


        <br /> short setence describing button could possibly extend for a line or two or three. Would like this to be centered </p>
      <a class="feature" href="google.ca"></a>
    </div>
  </div>
</div>

<强> CSS

.red-header {
  background-color: #be1e2d;
  color: #FFF;
  font-size: 1rem;
  font-family: Lato, Helvetica, Arial, sans-serif;
  padding: .6rem;
  text-align: center;
}
.table{width:100%; display:table;}
.row {
  width: 100%;
  display:table-row;
}

.icon-text {
  height: 100%;
  vertical-align: middle;
  float:left;
  width:70%;
}

.column {
  width: 50%;
  display: table-cell;
  position: relative;
}

.red-text {
  color: #be1e2d;
  margin-bottom: 0px;
  margin-top: 10px;
  line-height: 1;
}

.icon-img {
  float: left;
  width: 25%;
  padding: 5px 16px 5px 0px;
}

.feature {
  position: relative;
}

.feature {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  text-decoration: none;
  /* No underlines on the link */
  z-index: 10;
  /* Places the link above everything else in the div */
  background-color: #FFF;
  /* Fix to make div clickable in IE */
  opacity: 0;
  /* Fix to make div clickable in IE */
  filter: alpha(opacity=1);
  /* Fix to make div clickable in IE */
}

.divLink:hover {
  background-color: #b1e3e6;
}