如何在td中正确定位相对p标签

问题描述 投票:1回答:2

说明:问题是文本似乎从我不想要的td溢出,除此之外,我想将文本放在Header(H5)下面但是避免将文本包装在图像下面,我假设可以通过给图像提供静态宽度值并使用left属性来完成。但是,当我试图这样做时,它并不是正确的。

table.service_section{
    width: 80%;
    margin-left: 10%;
    border-spacing: 10px;}

  table.service_section td{
    border: 2px solid #ccc;
    box-sizing: border-box;
    border-radius:5px;
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);}

  table.service_section td img{
    display: inline-block;
    width: 50px;
    height: 25px;}
  table.service_section td h5{
    display: inline-block;
    margin: 0;
    padding: 0;
    position: absolute;}
  table.service_section td p{
    display: inline-block;
    position: relative;
    margin: 0;
    top: 0px;
    left: 50px;
    font-size: 10;}
<table class = "service_section">
  <tr>
    <td>
      <img src="https://image.flaticon.com/icons/svg/23/23665.svg" /> <h5>Home Button</h5>
      <p>This is going to be information regarding the service/product which is provided by Odeyale Corporation.</p>
    </td>
    <td>
      <img src="https://image.flaticon.com/icons/svg/23/23665.svg" /> <h5>Home Button</h5>
      <p>This is going to be information regarding the service/product which is provided by Odeyale Corporation.</p>
    </td>
    <td>
      <img src="https://image.flaticon.com/icons/svg/23/23665.svg" /> <h5>Home Button</h5>
      <p>This is going to be information regarding the service/product which is provided by Odeyale Corporation.</p>
    </td>
  </tr>

  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>

html css html-table position
2个回答
1
投票

试试这个:

table.service_section {
  width: 80%;
  margin-left: 10%;
  border-spacing: 10px;
}

table.service_section td {
  border: 2px solid #ccc;
  box-sizing: border-box;
  border-radius:5px;
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
}

table.service_section td img {
  float: left;
  width: 20%;
  height: 25px;
}

table.service_section td h5 {
  margin: 0 0 5px 0;
  padding: 0;
  position: relative;
  width: 100%;
  word-wrap:break-word;
}

table.service_section td p {
  position: relative;
  font-size: 10;
  margin-top: 0;
  word-wrap:break-word;
}

.wr {
  width: 75%;
  float: left;
  margin-left: 4%;
}
<table class = "service_section">
  <tr>
    <td>
      <img src="https://image.flaticon.com/icons/svg/23/23665.svg" />
      <div class="wr">
        <h5>Home Button</h5>
      <p>This is going to be information regarding the service / product which is provided by Odeyale Corporation.</p>
      </div>
    </td>
    <td>
      <img src="https://image.flaticon.com/icons/svg/23/23665.svg" />
      <div class="wr">
         <h5>Home Button</h5>
      <p>This is going to be information regarding the service / product which is provided by Odeyale Corporation.</p>
      </div>
    </td>
    <td>
      <img src="https://image.flaticon.com/icons/svg/23/23665.svg" /> 
      <div class="wr">
      <h5>Home Button</h5>
      <p>This is going to be information regarding the service / product which is provided by Odeyale Corporation.</p>
      </div>
    </td>
  </tr>

  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>

0
投票

Isaac - 看起来你的“table.service_section td p”你从左边推50px这个css“left:50px;”。那就是将你的p标签推到td的100%宽度之外。快速修复将添加50px的边距权限以抵消推送的p标签。这将使它保持在您正在寻找的窗口中。也就是说,有更优雅的方法可以使用Bootstrap甚至Flex Box实现相同的功能。查看有关Flex Box https://css-tricks.com/snippets/css/a-guide-to-flexbox/的文章。这可能会为您提供一些新策略来实现您的目标。再次,使用Bootstrap Cards可以帮助您实现同样的目标。最后一项,您的图像比需要的大得多。我会将图像调整到您想要的大小,这将帮助您消除不必要的代码,并帮助您更快地实现所需的结果。希望有所帮助。

table.service_section{
    width: 80%;
    margin-left: 10%;
    border-spacing: 10px;}

  table.service_section td{
    border: 2px solid #ccc;
    box-sizing: border-box;
    border-radius:5px;
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);}

  table.service_section td img{
    display: inline-block;
    width: 50px;
    height: 25px;}
  table.service_section td h5{
    display: inline-block;
    margin: 0;
    padding: 0;
    position: absolute;}
  table.service_section td p{
    display: inline-block;
    position: relative;
    margin: 0;
    top: 0px;
    left: 50px;
    margin-right: 55px;
    font-size: 10;}
<table class = "service_section">
  <tr>
    <td>
      <img src="https://image.flaticon.com/icons/svg/23/23665.svg" /> <h5>Home Button</h5>
      <p>This is going to be information regarding the service/product which is provided by Odeyale Corporation.</p>
    </td>
    <td>
      <img src="https://image.flaticon.com/icons/svg/23/23665.svg" /> <h5>Home Button</h5>
      <p>This is going to be information regarding the service/product which is provided by Odeyale Corporation.</p>
    </td>
    <td>
      <img src="https://image.flaticon.com/icons/svg/23/23665.svg" /> <h5>Home Button</h5>
      <p>This is going to be information regarding the service/product which is provided by Odeyale Corporation.</p>
    </td>
  </tr>

  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>
© www.soinside.com 2019 - 2024. All rights reserved.