【问题标题】:How to make a text input align to center, but have the cursor aligned left when focused?如何使文本输入对齐到中心,但光标在聚焦时左对齐?
【发布时间】:2019-06-25 08:57:45
【问题描述】:

我有如下输入文本:

<input type="text" placeholder="mm/dd/yyyy" style="text-align:center"/>

占位符正确显示为居中,但是当我关注该字段时,光标也位于文本字段的中心,我希望它位于占位符的左侧。 如何只对齐光标?

【问题讨论】:

    标签: html css


    【解决方案1】:

    您可以使用:focus 选择器。

    您可能还想使用::placeholder 选择器,这样占位符就不会移动。

    input, input::placeholder {
        text-align: center; 
    }
    
    input:focus {
        text-align: left; 
    }
    &lt;input type="text" placeholder="mm/dd/yyyy"/&gt;

    【讨论】:

    • 不寻常的要求,出色的答案。
    • 你不需要同时选择输入和占位符,因为当你失去焦点时它会回到中心,不确定这是否可取
    • @ZohirSalak - 是的,我也不确定。也许只有占位符应该居中。
    • 在这种情况下,Op 只能选择占位符
    • 对我不起作用,我需要将光标放在左侧,但文本应保持在中心。他们他的混蛋是不可取的
    猜你喜欢
    • 2017-10-30
    • 1970-01-01
    • 2020-05-14
    • 2016-02-27
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    相关资源
    最近更新 更多