Place a button right aligned

后端 未结 11 1484
礼貌的吻别
礼貌的吻别 2020-12-22 15:56

I use this code to right align a button.

相关标签:
11条回答
  • 2020-12-22 16:24

    It is not always so simple and sometimes the alignment must be defined in the container and not in the Button element itself !

    For your case, the solution would be

    <div style="text-align:right; width:100%; padding:0;">
        <input type="button" value="Click Me"/>
    </div>
    

    I have taken care to specify width=100% to be sure that <div> take full width of his container.

    I have also added padding:0 to avoid before and after space as with <p> element.

    I can say that if <div> is defined in a FSF/Primefaces table's footer, the float:right don't work correctly because the Button height will become higher than the footer height !

    In this Primefaces situation, the only acceptable solution is to use text-align:right in container.

    With this solution, if you have 6 Buttons to align at right, you must only specify this alignment in container :-)

    <div style="text-align:right; width:100%; padding:0;">
        <input type="button" value="Click Me 1"/>
        <input type="button" value="Click Me 2"/>
        <input type="button" value="Click Me 3"/>
        <input type="button" value="Click Me 4"/>
        <input type="button" value="Click Me 5"/>
        <input type="button" value="Click Me 6"/>
    </div>
    
    0 讨论(0)
  • 2020-12-22 16:25

    To keep the button in the page flow:

    <input type="button" value="Click Me" style="margin-left: auto; display: block;" />
    

    (put that style in a .css file, do not use this html inline, for better maintenance)

    0 讨论(0)
  • 2020-12-22 16:25

    In my case the

    <p align="right"/>
    

    worked fine

    0 讨论(0)
  • 2020-12-22 16:26

    a modern approach in 2019 using flex-box

    with div tag

    <div style="display:flex; justify-content:flex-end; width:100%; padding:0;">
        <input type="button" value="Click Me"/>
    </div>

    with span tag

    <span style="display:flex; justify-content:flex-end; width:100%; padding:0;">
        <input type="button" value="Click Me"/>
    </span>

    0 讨论(0)
  • 2020-12-22 16:35

    <div style = "display: flex; justify-content:flex-end">
        <button>Click me!</button>
    </div>

    <div style = "display: flex; justify-content: flex-end">
        <button>Click me!</button>
    </div>
    
    0 讨论(0)
提交回复
热议问题