Skip to content
TheChatArchiveThe Chat Archive
Metallama-4-scout-17b-16e-instruct· MetaHugging Face LMArena Human Preference 140K

我可以设置带着readonly的input框是内容是靠右,然后不带readonly的text文本框,内容正常靠左吗

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-pro’s answer.
  1. User

    我可以设置带着readonly的input框是内容是靠右,然后不带readonly的text文本框,内容正常靠左吗

  2. Meta Assistant

    是的,你可以通过CSS样式来实现这个效果。下面是一个简单的示例:

    HTML:

    <input type="text" id="readonly-input" readonly value="readonly内容靠右">
    <input type="text" id="normal-input" value="正常内容靠左">
    

    CSS:

    #readonly-input {
      text-align: right; /* 内容靠右对齐 */
    }
    
    #normal-input {
      text-align: left; /* 内容靠左对齐 */
    }
    

    这样,带有readonly属性的输入框内容将靠右对齐,而不带readonly属性的输入框内容将靠左对齐。

    如果你想让所有readonly的输入框都靠右对齐,可以使用以下CSS代码:

    input[readonly] {
      text-align: right;
    }
    

    这样,所有添加了readonly属性的输入框都会自动应用这个样式。

    注意:input[readonly] 是属性选择器,它选择的是具有 readonly 属性的 input 元素。

  3. User

    .percent-col input[readonly], .payment-value-col input[readonly], .cumulative-agent-fee-col input[readonly], .cumulative-payment-received-col input[readonly] { width: 35px; text-align: right; padding-right: 5%; } 这个有别的更好的写法吗

  4. Meta Assistant

    你的CSS代码已经很简洁了,但仍然可以优化一下。以下是一些建议:

    1. 使用更通用的选择器:
    input[readonly] {
      width: 35px;
      text-align: right;
      padding-right: 5%;
    }
    

    这样,你可以对所有readonly的输入框应用样式,而不需要指定特定的类。

    1. 如果你只想对特定的类应用样式,可以使用以下写法:
    .percent-col, .payment-value-col, .cumulative-agent-fee-col, .cumulative-payment-received-col {
      input[readonly] {
        width: 35px;
        text-align: right;
        padding-right: 5%;
      }
    }
    

    这样,你可以减少重复的代码。

    1. 如果你想进一步减少重复的代码,可以使用以下写法:
    .percent-col input[readonly],
    .payment-value-col input[readonly],
    .cumulative-agent-fee-col input[readonly],
    .cumulative-payment-received-col input[readonly] {
      width: 35px;
      text-align: right;
      padding-right: calc(5% - 0);
    }
    

    或者:

    input[readonly].percent-col,
    input[readonly].payment-value-col,
    input[readonly].cumulative-agent-fee-col,
    input[readonly].cumulative-payment-received-col {
      width: 35px;
      text-align: right;
      padding-right: 5%;
    }
    

    但是,如果你的HTML结构是这样的:

    <div class="percent-col">
      <input type="text" readonly>
    </div>
    

    那么第一种写法是最好的选择。

    1. 如果你使用的是Sass或Less,可以使用以下写法:
    $cols: .percent-col, .payment-value-col, .cumulative-agent-fee-col, .cumulative-payment-received-col;
    
    #{$cols} {
      input[readonly] {
        width: 35px;
        text-align: right;
        padding-right: 5%;
      }
    }
    

    或者:

    .cols {
      input[readonly] {
        width: 35px;
        text-align: right;
        padding-right: 5%;
      }
    }
    
    .percent-col, .payment-value-col, .cumulative-agent-fee-col, .cumulative-payment-received-col {
      .cols;
    }
    

    这样,你可以更方便地管理和维护你的CSS代码。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.