What should I do to keep the label of a checkbox on the same line with the checkbox in a rails view containing a form?
Currently the label goes on the next line:
<div class="span6 offset3"> <%= form_for(@user) do |f| %> <%= render 'shared/error_messages', object: f.object %> <%= f.label :name %> <%= f.text_field :name %> <br> <%= f.check_box :terms_of_service %> <%= f.label :terms_of_service, "I agree to the #{link_to 'Terms of Service', policies_path}.".html_safe %> <br><br> <%= f.submit "Create my account", :class => "btn btn-large btn-primary" %> <% end %>
Thank you, Alexandra
Method 1: By making the position of checkbox relative, set the vertical-align to the middle can align the checkboxes and their labels. Here, we have made the position of the checkbox relative to the label.
Create <form> and <div> elements. Place “checkbox” input type in the <label> element. <form> <div> <label> <input type="checkbox" /> Label text </label> </div> </form>
This means that the checkbox gets on/off when clicking the label. In this snippet, we suggest two methods of creating a checkbox with a clickable label. This is possible to do either by wrapping a <label> tag, or by using a “for” attribute. First, let’s see how to use the method of wrapping the <label> tag.
The checkbox is one of the HTML forms that is used on every website. How to align the checkbox and its label? This is a question that developers frequently ask.
According to the bootstrap wiki, it must be
<label class="checkbox"> <input type="checkbox"> Check me out </label>
which in Ruby on Rails is
<%= f.label :terms_of_service do %> <%= f.check_box :terms_of_service %> I agree to the <%= link_to 'Terms of Service', policies_path %>. <% end %>
It looks like you're using Bootstrap, so I recommend adapting your view code to use the horizontal form layout described in this section of the Bootstrap docs: https://getbootstrap.com/docs/4.3/components/forms/#horizontal-form
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With