Design a sleek and modern form using Bootstrap 4 that mirrors the aesthetics of the forms found in Bootstrap 3

Is there a way to create a form using BootStrap 4 that resembles the easily customizable forms from BootStrap 3? I am looking to have a bold, right-aligned label within a specific column size (e.g. col-sm-4), with an input field next to it whose width is controlled by an enclosing div tag with a class like col-sm-4. I have been struggling with .css and BS4 for some time now trying to achieve this desired effect. Any suggestions or hints would be greatly appreciated.

The following code somewhat achieves the desired effect but does not allow for different sized inputs as they all default to the same size:

CSS:

div.form-inline label.col-sm-3 {
justify-content: flex-end;
font-weight:650;
}

HTML:

<div class="form-inline">
<label for="INcity" class="col-form-label col-12 col-sm-3">City</label>
<div class="col-sm-4"><input type="text" name="INcity" class="required form-control" id="INcity"></div>
</div>

If I use form-group instead, I can change the sizes of the input fields using the enclosing tags. However, the css (even when changed to .form-group instead of .form-inline) fails to align the label correctly and apply the bold styling. It seems like I need the correct .css or a simple example of achieving the desired effect without any hacks required. Perhaps I am missing something obvious since achieving the same look as in BS3 has proven quite difficult. The BS4 documentation examples all show left-aligned labels, which makes me wonder if BS4 developers made a universal formatting decision for all users, though that seems unlikely and unwelcoming.

Answer №1

To ensure that the col-* classes function properly, they must be placed within a .row or .form-row element. No additional CSS is necessary:

<div class="form-row">
    <label for="INcity" class="col-form-label col-12 col-sm-3 font-weight-bold text-right">City</label>
    <div class="col-sm-4">
        <input type="text" name="INcity" class="required form-control" id="INcity">
    </div>
</div>

https://getbootstrap.com/docs/4.0/components/forms/#form-grid

Answer №2

If you are in the process of transitioning multiple items from BS3 to BS 4, following ZimSystem's suggestion may prove to be laborious as it requires changing each instance individually. Instead, consider creating a CSS file with the code provided below and utilizing find and replace to substitute every occurrence of 'control-label' with 'col-form-label'. This method is more efficient compared to the aforementioned one and promotes a modular approach (eliminating the need to repeatedly write the CSS markup - modularity being essential for programmers). Make sure to load this CSS file after the BS4 CSS has been loaded.


.col-form-label
{
    font-weight:600;
    text-align:right;
}

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 allow scrolling in only one direction using CSS?

I am facing an issue with resizing elements within a list. The requirement is that when the elements are resized, they should overflow the container only in the x direction. There must not be any scrolling in the x direction, but scrolling should be allowe ...

Tips for personalizing the css styles of an alert box?

I am in need of customizing the alert box using CSS attributes. Currently, I am able to achieve a similar effect with the code below: JQUERY: $('<div class="alertMessage">Error first</div>') .insertAfter($('#componentName' ...

What sets apart compressing test.min.css from compressing test.css?

Recently, I have transitioned to a new job role with a different employer. In my previous workplace, we utilized LESS and compiled it into a .css file before compressing it further into a .min.css file. However, in my current position, we also work with L ...

Dynamic classroom experience featuring a bootstrap sidebar

I am having an issue with changing the active class of the sidebar after a click event. I have tried using Bootstrap and implemented some JavaScript code, but it doesn't seem to be working properly. $(function(){ $('.sidebar1 a').filt ...

When a table undergoes a dynamic reload, the formatting for the columns fails to display properly

After fetching image metadata from an API call, a table is dynamically generated on page load. Subsequently, when new images are uploaded, the function responsible for building the table is called again to include this new data. However, despite the CSS st ...

Exploring the true column widths in Bootstrap 4 tables

I have been attempting to create a layout similar to this in Bootstrap 4, where the table columns are set in pixels with fixed values, causing a horizontal scroll within the card. However, I am facing issues as the columns do not have the desired sizes. Th ...

What is the best way to position an image next to an input element?

How can I position submit.png next to the input element in my code? <div class="input-box" style="height: 40px; width: 500px;"> <img src="img/delete.png" width="25px" height="25px;" style="position: absolute; right: 0;"> ...

Unique design and elements featuring the `width: auto` property for custom fonts

In my current project, I am utilizing the popular 'Open Sans' font family. However, I am encountering a specific issue. The problem arises with buttons and other elements that have width: auto. Upon page load, these elements initially have their ...

What could be causing the unexpected gap between the first two elements and the third in my flexbox layout?

I am facing an issue with the layout of my third child element. Instead of appearing below the first two children, it is wrapping around to the bottom of the container. Can anyone explain why this is happening and suggest a solution? Even though I have se ...

Showcase text in a straight row by utilizing ng-repeat and bootstrap

My goal is to show each word on a new line, similar to how words are displayed in a hangman game. The words should be displayed as blanks. <body ng-init="models = [['H','a','p','p','y'],['X',& ...

Tips for getting rid of the excess space surrounding an image within a div tag

Placing images inside bootstrap card divs within col divs is my current challenge <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" /> <div class="container"> <div class="row"& ...

Is there a way to alter the color of radio buttons?

Is there a way to use CSS to change the background color of a radio button? I've searched online, but all the solutions I found involve JavaScript, which I don't fully understand. I attempted this CSS code, but it didn't have the desired eff ...

What are the steps to resize a YouTube embedded video?

I am currently attempting to use a YouTube video as the background for breadcrumbs on my website. However, I am facing an issue with setting the video width to cover the full screen. The image below shows that the video is displayed in the center of the if ...

The issue of importing CSS files that themselves import other CSS files via URL is causing a problem

Why is this not working correctly? I have included [email protected] A.css (css file that imports from a URL) @import url('https://fonts.googleapis.com/css?family=Source Sans Pro:300,400,600,700,400italic,700italic&subset=latin'); Ap ...

Preventing an object from exceeding the boundaries of its designated space

My DIV is filled with user-generated content, which sometimes begins with an item that has a margin-top that overflows the container, creating a gap between it and preceding elements. I've discovered that changing the display property to either inline ...

Is there a way to customize these columns if they are not affected by CSS?

When visiting the tti-fl.com website, I noticed that there are pages with columns that aren't responding to the CSS. Even though I've successfully used flex-box before, these specific columns under .categories-wrapper and .content-block just won& ...

Content editable div causing overflow issues in Internet Explorer

I am having trouble with a content editable div where the text is not wrapping properly. I attempted to use "overflow:scroll" but it only ends up cutting off the text horizontally. https://i.stack.imgur.com/WlMdY.jpg ...

Arranging Text and Images in HTML/CSS to Ensure Optimal Placement When the Window Size Changes

Hello fellow coders! I've recently started diving into the world of website development and I have a query regarding positioning elements and handling window resizing. Can anyone help me out? I'm trying to center an image, a message, and a passw ...

What is the best way to horizontally align divs with CSS?

I have been using a combination of table and CSS to center divs on my page. Here is the code I'm currently using: <table width="69" border="0" align="center"> <tr> <td width="59"> <div style="position:relative;"> ...

Issues have been encountered with the AngularJS Modal functionality when trying to populate data within an ng

Attempting to utilize a bootstrap modal for updating data in a list, the initial modal being used is to add a new item to said list. Successfully created the modal, triggered the ajax call, and returned the data to the main controller via the promise belo ...