What is the best way to style a Rails form field using CSS/Bootstrap?

I've been trying various methods and experimenting but I can't seem to figure out how to format my Rails view to display a form with Bootstrap or CSS. The issue might be related to the Bootstrap classes or the CSS.

My goal is to create a two-column layout with an image in one column positioned next to the form in the other, both of equal size.

I have attempted the standard approach: Container with a row containing two columns, one for the form and the other for the image.

However, the form seems to expand across the entire page width and the columns are displayed vertically instead of side by side.

I want it to appear like this: https://i.sstatic.net/fJmMT.jpg



<div class="container d-flex">
  <div class="row">
    <div class="col">
      
        <%= form_with(model: car, local: true) do |form| %>

          <%= hidden_field_tag "car[login]", exists ? "#{@car.login}" : "{@current_user.login}"  %>

          <div class="field">
            <%= form.label :model, class: "h3" %><br>
            <%= form.text_field :model, id: :car_model, class: "form-control mb-2 mr-sm-2 mb-sm-0" %>
          </div>

          <div class="field">
            <%= form.label :year, class: "h3" %><br>
            <%= form.number_field :year, id: :car_year, class: "form-control mb-2 mr-sm-2 mb-sm-0" %>
          </div>

          <div class="field">
            <%= form.label :color, class: "h3" %><br>
            <%= form.text_field :color, id: :car_color, class: "form-control mb-2 mr-sm-2 mb-sm-0" %>
          </div>

          <div class="field">
            <%= form.label :licence_plate, class: "h3" %><br>
            <%= form.text_field :licence_plate, id: :car_licence_plate, class: "form-control mb-2 mr-sm-2 mb-sm-0" %>
          </div>

          <div class="actions">
            <%= form.button "<h3>{submit_text}</h3>".html_safe, class: "btn btn-success btn-sm" %>
          </div>

        <% end %>
        <div class="col">
           # img source here
        </div>

    </div>
  </div>
</div>

Answer №1

When creating bootstrap forms, it is important to note that your label and input elements should be enclosed within a .form-group container, rather than a .field one.

Check out the Bootstrap documentation for more information on forms

To implement this, consider the following structure:

<div class="form-group field">
  <%= form.label :licence_plate, class: "h3" %><br>
  <%= form.text_field :licence_plate, id: :car_licence_plate, class: "form-control mb-2 mr-sm-2 mb-sm-0" %>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to disregard inherited styles without relying on the use of "!important"?

Is there a way to create a new class in CSS that ignores all inherited styles without needing to use !important for each one individually? This is my HTML code: <div class="text"> <h2>Title</h2> <span>Date</span> &l ...

How can the target pseudo-class be utilized to replace the 'active' state on navigation across several pages effectively?

Within a large application, I am managing a micro site and looking to implement tertiary navigation with an active state for pages. This involves inserting a single html blob into multiple pages. My goal is to use an active class to highlight the page in ...

Conceal an HTML column in jQuery by clicking a button with the help of link_to in Rails and H

I'm attempting to conceal an HTML column by clicking a button using the jQuery hide method. Here is the HTML code: %br.clear - @cards.each do |card| %div{class: "hide_#{card.id}"} <- included this to address the problem .existing-credit ...

tips for moving a button 2 pixels up or down in position

How can I adjust the position of my button by a specific number of pixels up or down from where it currently is? Can you provide guidance on how to find the current location? Thank you in advance. ...

Unique Selector Customization

Is there a way to style all custom elements in CSS? I am looking to make custom elements default to block display (as most browsers render them inline by default) and then adjust as needed. A potential rule I have in mind is: *::custom { display: blo ...

Differences in rendering of HTML select element between Chrome on macOS and Chrome on Windows

During testing of a section of the app at my workplace, it was discovered that dropdowns and select elements in a specific UI scenario appeared differently on Chrome for Windows and Ubuntu compared to Chrome for macOS. I attempted to analyze the elements ...

Display the closing tag depending on specific conditions to simulate the behavior of a calendar

Is it possible to conditionally render a closing tag using v-if? If so, what is the correct way to do it? Initially, I tried the following: <template v-for="day in days"> <td class="days">{{day.number}}</td> <template v-if ...

My React component seems to be unable to locate and process my CSS file

I am encountering an issue where my React Component is not recognizing my CSS file. Here is the code snippet from my React component: import React, { Component } from 'react'; import './Jumbotron.css'; class Jumbotron extends Compone ...

Model not displaying on Apexcharts

Currently, I am using Angular-15 and Bootstrap-5 application for the Apexchart The issue arises when trying to open the Model and the chart does not show up. If I refresh the page and zoom out to 90% or zoom in to 100%, the chart appears. However, upon re ...

Inconsistency in width of Bootstrap 4 card layouts

On my webpage, there are three cards displayed as seen in image 1. However, the cards do not have the same width in columns 1. When viewed on mobile (Chrome), the widths are all different and the bottom two cards are touching, as shown in image 2. How can ...

"Creating dynamic webpage designs with CSS for responsive layouts

Currently, I am in the process of building a website utilizing a CSS fluid layout approach which is rooted in adaptive and responsive web design principles. For this project, I am avoiding using fixed values such as pixels (px) and instead opting for perc ...

I am experiencing issues with my background image not fully expanding or displaying correctly in the html section. Any assistance with troubleshooting the background image would be greatly appreciated

I am currently exploring Bootstrap and I am new to front-end development. I do not have a separate CSS file, only an HTML file included in my Python Django project. In this file, I am attempting to add a background image to my section. However, the image ...

What is the best way to minify and concatenate multiple CSS files only after converting SCSS to CSS with Gulp?

When attempting to convert my scss files to css files using gulp, I encountered an issue. After writing the scss code, it is easily converted to css. However, I wanted to minify this css and save it in a different folder called 'min'. Within the ...

When using Turbolinks, signing out in a separate browser tab may become necessary for a

My Rails 4 application has two layouts; one for users who are logged out and another for those who are logged in. These layouts utilize different stylesheets. The issue arises when I have multiple browser tabs open and log out of one tab. The other tab st ...

Opposite of CSS Media Query in a precise manner

Can you provide the opposite of this specific CSS media query? @media only screen and (device-width:768px) To clarify, what we are looking for is everything except iPad or not iPad. Just an FYI, I have attempted the code below without success: @media n ...

Improving Django/VueJS/PostgreSQL by incorporating leading and trailing whitespace tabs

Currently, I am managing a text-field in Django through Django-admin which requires maintaining white-space integrity. To achieve this, I am encapsulating it within <pre> </pre> tags for rendering with the assistance of vueJS and vue-material. ...

Increasing the height of a DIV that is positioned beneath another DIV with a set height

My goal is to convey my message visually. I hope you can understand without too many words. The issue I am facing is that when trying to stretch a 100% height DIV under another DIV with fixed height, it does not stretch properly. If you want to experiment ...

Is there a way to adjust the height overflow using a percentage?

I have a coding dilemma here where I am attempting to make the SearchLocationCards overflow. It seems to work fine with 'px' and 'vh' values, but the '%' value doesn't seem to have any effect. How can I adjust the height ...

Circular css3 animation originating from the center

Can someone provide an example of how to create a div rotating around a circle in CSS3 while the circle itself is already rotating? I attempted to use webkit frame, but it did not work correctly. If anyone has any examples or tips on achieving this effec ...

Place element H1 below H2 on the webpage, but keep it positioned above H2 in the underlying

I have a snippet of HTML Code: <html> <head> <title>css test</title> <style type="text/css"> .box{width:100%;float:left;background:red} </style> </head> <body> <h1>Title To Appear Below Div on Page< ...