代码之家  ›  专栏  ›  技术社区  ›  espresso_coffee

如何防止文本从跨到跨内容从同级跨?

  •  1
  • espresso_coffee  · 技术社区  · 6 年前

    我有两个 span 彼此相邻的元素。第一 跨度 有符号元素,第二个有文本。文本太长,在符号下换行。有什么方法可以阻止文字进入第一个 跨度 ?下面是一个例子:

    table.tbl {
      font-size: 12px;
      width: 500px;
    }
    
    table.tbl thead th {
      text-align: left;
    }
    
    table.tbl tbody td {
      text-align: left;
    }
    
    span.symbol {
      color: red;
      font-weight: bold;
    }
    <table class="tbl">
      <thead>
        <tr>
          <th>Data Validation</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Form validation text description and some instructions.</td>
        </tr>
        <tr>
          <td>
            <span class="symbol">?</span>
            <span class="text">Here we have some text that can be too long sometimes. In that case text will wrap under question mark symbol from the span element above.</span>
            <br><span style="color:red">- Test User Name 02/07/2019</span></td>
        </tr>
      </tbody>
    </table>

    是否有方法阻止文本进入符号范围?

    应该是这样的:

    ?  Here we have some text that can be too long sometimes. In that case text will 
       wrap under question mark symbol from the span element above.    
       - Test User Name 02/07/2019
    
    3 回复  |  直到 6 年前
        1
  •  1
  •   Moob    6 年前

    display:table-cell;

    span.symbol, span.text {
      display:table-cell;
    }
    

    table.tbl {
    	font-size: 12px;
      width: 500px;
    }
    table.tbl thead th {
    	text-align: left;
    }
    table.tbl tbody td {
    	text-align: left;
    }
    span.symbol {
    	color: red;
    	font-weight: bold;
    }
    span.symbol, span.text {
      display:table-cell;
    }
    <table class="tbl">
      <thead>
        <tr>
          <th>Data Validation</th>
        </tr>
      </thead>
      <tbody>
        <tr>
    			<td>Form validation text description and some instructions.</td>
    		</tr>
        <tr>
          <td>
            <span class="symbol">?</span>
    				<span class="text">Here we have some text that can be too long sometimes. In that case text will wrap under question mark symbol from the span element above.</span>
    				<br><span style="color:red">- Test User Name 02/07/2019</span></td>
        </tr>
      </tbody>
    </table>

    table.tbl {
      font-size: 12px;
      width: 500px;
    }
    
    table.tbl thead th {
      text-align: left;
    }
    
    table.tbl tbody td {
      text-align: left;
    }
    
    .symbol {
      color: red;
      font-weight: bold;
    }
    
    .symbol,
    .text {
      display: table-cell;
    }
    <table class="tbl">
      <thead>
        <tr>
          <th>Data Validation</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Form validation text description and some instructions.</td>
        </tr>
        <tr>
          <td>
            <div class="symbol">?</div>
            <div class="text">Here we have some text that can be too long sometimes. In that case text will wrap under question mark symbol from the span element above.
              <br><span style="color:red">- Test User Name 02/07/2019</span></div>
          </td>
        </tr>
        <tr>
          <td>
            <div class="symbol">?</div>
            <div class="text">Here we have some text that is short.
              <br><span style="color:red">- Test User Name 02/07/2019</span></div>
          </td>
        </tr>
      </tbody>
    </table>

    table.tbl {
      font-size: 12px;
      width: 500px;
    }
    
    table.tbl thead th {
      text-align: left;
    }
    
    table.tbl tbody td {
      text-align: left;
    }
    
    td.symbol {
      color: red;
      font-weight: bold;
      vertical-align:top;
    }
    <table class="tbl">
      <thead>
        <tr>
          <th colspan="2">Data Validation</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td colspan="2">Form validation text description and some instructions.</td>
        </tr>
        <tr>
          <td class="symbol">?</td>
            <td class="text">Here we have some text that can be too long sometimes. In that case text will wrap under question mark symbol from the span element above.
              <br><span style="color:red">- Test User Name 02/07/2019</span></td>
        </tr>
        <tr>
          <td class="symbol">?</td>
            <td class="text">Here we have some that is short.
              <br><span style="color:red">- Test User Name 02/07/2019</span></td>
        </tr>
      </tbody>
    </table>
        2
  •  1
  •   kukkuz    6 年前

    text-wrap td wrap

    table.tbl {
      font-size: 12px;
      width: 500px;
    }
    
    table.tbl thead th {
      text-align: left;
    }
    
    table.tbl tbody td {
      text-align: left;
    }
    
    .wrap {
      display: flex;
    }
    
    span.symbol {
      color: red;
      font-weight: bold;
    }
    <table class="tbl">
      <thead>
        <tr>
          <th>Data Validation</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Form validation text description and some instructions.</td>
        </tr>
        <tr>
          <td>
            <div class="wrap">
              <span class="symbol">?</span>
              <div class="text-wrap">
                <span class="text">Here we have some text that can be too long sometimes. In that case text will wrap under question mark symbol from the span element above.</span>
                <br><span style="color:red">- Test User Name 02/07/2019</span></div>
            </div>
          </td>
        </tr>
      </tbody>
    </table>

    table.tbl {
      font-size: 12px;
      width: 500px;
    }
    
    table.tbl thead th {
      text-align: left;
    }
    
    table.tbl tbody td {
      text-align: left;
    }
    
    span.symbol {
      color: red;
      font-weight: bold;
    }
    
    .wrap {
      display: table;
    }
    
    .wrap>* {
      display: table-cell;
    }
    <
        3
  •  0
  •   Temani Afif    6 年前

    float

    table.tbl {
      font-size: 12px;
      width: 500px;
    }
    
    table.tbl thead th {
      text-align: left;
    }
    
    table.tbl tbody td {
      text-align: left;
    }
    
    span.symbol {
      color: red;
      font-weight: bold;
      float: left;
      margin-right: 5px;
      padding-bottom: 50px;
    }
    <table class="tbl">
      <thead>
        <tr>
          <th>Data Validation</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Form validation text description and some instructions.</td>
        </tr>
        <tr>
          <td>
            <span class="symbol">?</span>
            <span class="text">Here we have some text that can be too long sometimes. In that case text will wrap under question mark symbol from the span element above.</span>
            <br><span style="color:red">- Test User Name 02/07/2019</span></td>
        </tr>
      </tbody>
    </table>