Home  >  Q&A  >  body text

css - checkbox的文字不一样长,怎么设置能保证整齐还能显示所有文字?

在一个p里,有n个checkbox,后面的文字有长有短,很多差距都很大而且是动态添加,不知道长得有多长,如果把每个checkbox放在一个p里设定宽度,又不能隐藏剩下的信息,也不能设置滚动条,有没有什么方法能设置它又整齐又能显示所有文字。

类似这种情况:

<p style="width: 300px; margin: 0 auto; margin-top: 300px;">
        <input type="checkbox" name="chk">xxxx
        <input type="checkbox" name="chk">xxxxxxxxxx
        <input type="checkbox" name="chk">x
        <input type="checkbox" name="chk">xxxxdcgfd
        <input type="checkbox" name="chk">xx
        <input type="checkbox" name="chk">xxxxdg
        <input type="checkbox" name="chk">xxxxdfgfghhn
        <input type="checkbox" name="chk">xxxxvcb
        <input type="checkbox" name="chk">xxxxdfgsfg
        <input type="checkbox" name="chk">xxxxfgbvf
    </p>

PHPzPHPz2717 days ago1619

reply all(8)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 13:43:03

    The text behind the checkbox can be wrapped with a span tag, and then the offsetwidth of the span tag is obtained

    reply
    0
  • 阿神

    阿神2017-04-17 13:43:03

     <p>
     <span><input type="checkbox" name="chk">xxxx</span>
     ...
    </p>
    
    p > span {
     white-space: nowrap;
    }
    
    p {
      text-align: justify;
    }
    

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:43:03

    There is no way around this. To make it neat, you need to set the width.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 13:43:03

    Set the line-height
    and then go to Baidu to see this attribute vertical-align, which can be used to achieve this neatness

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:43:03

    It can only ensure that input and text are always together.

    Solution: input + text plus label tag, and set the style inline-block

    reply
    0
  • 阿神

    阿神2017-04-17 13:43:03

    You can choose to wrap it with li tag

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:43:03

    Add a style vertical-align:middle

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 13:43:03

    I didn’t explain this problem clearly, because the data is dynamic, so I can only calculate the set width based on the longest data string passed in. However, when js calculates the string length using str.length, no matter Both Chinese characters and English characters are calculated as 1, so the length of the longest string can only be passed from the background. This is my current solution, I don't know if there is a better solution. Please give me some advice!

    reply
    0
  • Cancelreply