How to change "Choose file" text using Bootstrap 5 - html

Is it impossible change choose file text of input file in Bootstrap 5 using CSS like Bootstrap 4?
And how can I add a margin to "No file chosen"?

Bootstrap 5 + some CSS
Compose Bootstrap's custom file input with custom label.
Hide default Choose file button with the ::file-selector-button pseudo-element. There are pseudo-elements ::-webkit-file-upload-button and ::-ms-browse for older versions of Chrome/Opera/Safari and Edge/IE respectively. But bootstrap.css uses ::file-selector-button and ::-webkit-file-upload-button only. So I propose to do the same.
Add some more CSS for the :hover effect and for the thin border between the label and the input field.
Tested in Chrome, Firefox and Edge.
https://codepen.io/glebkema/pen/VwMQWGE
.custom-file-button input[type=file] {
margin-left: -2px !important;
}
.custom-file-button input[type=file]::-webkit-file-upload-button {
display: none;
}
.custom-file-button input[type=file]::file-selector-button {
display: none;
}
.custom-file-button:hover label {
background-color: #dde0e3;
cursor: pointer;
}
<div class="container py-3">
<div class="input-group custom-file-button">
<label class="input-group-text" for="inputGroupFile">Your Custom Text</label>
<input type="file" class="form-control" id="inputGroupFile">
</div>
</div>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.3/css/bootstrap.min.css">

If the file input is placed inside the label element and then hidden the label itself will act as the file input when it is clicked. Adding the form-control class and the tabindex attribute (which specifies that the element can be focused) to the label will then style it like a generic Bootstrap input.
<div class="input-group">
<span class="input-group-text">Custom Add-on Text</span>
<label class="form-control" tabindex="0">Custom Input Text
<input type="file" class="invisible">
</label>
</div>
https://codepen.io/yetiface/pen/PoQqrda
This solution has its downsides, for example the label element will not automatically receive bootstrap styles specifically for input[type='file']. However it is such a simple method that I felt it worth posting for those who it can help.

I needed to change both texts so this method helped
$('#review-image').change(function(e) {
let fileName = (e.target.files.length > 0) ? e.target.files[0].name : 'choose_file_not';
$('#review-image-label').text(fileName);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
<div class="container mt-3">
<div class="input-group custom-file-button">
<label class="input-group-text" for="review-image" role="button">choose_file</label>
<label for="review-image" class="form-control" id="review-image-label" role="button">choose_file_not</label>
<input type="file" class="d-none" id="review-image" name="images[]" multiple accept="image/*">
</div>
</div>

Bootstrap 5 beta no longer provides a custom file input like Bootstrap 4 that used pseudo elements to override the browser's file input defaults. Therefore you'd have to use JS or make your own psuedo element to hide the Choose file.. area of the input...
#formFile::before {
content: "Pick file";
position: absolute;
z-index: 2;
display: block;
background-color: #eee;
width: 80px;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<div class="container min-vh-100 py-2">
<div class="row">
<div class="col">
<div class="mb-3">
<input class="form-control" type="file" id="formFile">
</div>
</div>
</div>
</div>

I created an input group with a label, the input, and then appended another label. This way you can style it however you want and put custom text as the input group text. You then hide the input element (display:none or width:0 or something)
Examples:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<p>Label before</p>
<div class='input-group'>
<label class='input-group-prepend'>
<div class='input-group-text form-control' for='file-input-thing'>Pick File</div>
</div>
<input type='file' id='file-input-thing' style='width:0;'>
<label id='file-input-label' class='form-control' for='file-input-thing'/>
</div>
<p>Label after</p>
<div class='input-group'>
<label id='file-input-label' class='form-control' for='file-input-thing'/>
<input type='file' id='file-input-thing' style='width:0;'>
<div class='input-group-append'>
<label class='input-group-text form-control' for='file-input-thing'>
</div>
</div>
You can then assign styling to labels as you see fit. By making both piece labels tied into the input element it allows you to click on either and choose a file.
Further you can add an ID to the blank label and update the value to the name of the file.
let inputElement = document.getElementById('file-input-thing');
let inputLabel= document.getElementById('file-input-label');
inputElement.addEventListener('change', function(e) {
let file = e.target.files[0];
inputLabel.innerHTML = file.name;
})
I'm new to all this so there's probably a better solution, but this worked for me.

This is how I have implemented it,
just copy-paste it to your code and you'll see the results
<Button onClick={() => document.getElementById('imageFileSelect').click()}>Choose Image</Button>
<input className="form-control d-none" id='imageFileSelect' type="file" onChange={imageSelected}></input>

Related

My Paragraph won't stay on the same row as my Checkbox. What Html text element should I use with an input? [duplicate]

This question already has answers here:
How to align the checkbox and label in same line in html?
(13 answers)
Closed 1 year ago.
Goal:
I want text to come after my checkbox
Problem:
My p tag comes directly after my check box input, but I can't get them on the same line and Next to each other.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<div class="container">
<div class="row">
<div class="col">
<input type="checkbox">
</div>
<div class="col">
<p> Save My card for future payment</p>
</div><br>
</div>
</div>
Edit:
The best way to put Text after a checkbox or other user input button is to use a LABEL not a p tag.
<label for="check">text goes here</label>
<input type="checkbox" id="check">
If you're using Bootstrap as your class names suggest, this is the right way:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<div class="container">
<div class="row">
<div class="col">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="defaultCheck1">
Save my card for future payment
</label>
</div>
</div>
</div>
See the docs at https://getbootstrap.com/docs/4.6/components/forms/#checkboxes-and-radios.
If not, use standard checkbox markup so your form is accessible to those using assistive technology (and more standard):
<label>
<input type="checkbox">
Save my card for future payment
</label>
Notice that in both cases the label is clickable.
I assume that your code look like that.
.checkbox_div {
display: flex;
align-items: center;
}
.checkbox_div p {
display: inline-block;
}
<div class="checkbox_div">
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
<p>Paragraph text </p>
</div>
If not then format your code look like that. Take checkbox input and p tag inside a div.
And then add some css which is given below.

Make bootstrap CHECKBOX look like bootstrap BADGE

I'd like to make bootstrap checkboxes that look like bootstrap badges, while retaining the checkbox functionality. I thought maybe I could simply style the checkbox label as a badge, but it didn't work.
<head>
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css'>
<script src='https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js'></script>
</head>
<div class='form-check m-1' style='display:inline-block;'>
<input id='".$tagDAT[1]."' name='".$tagDAT[1]."' type='checkbox' class='form-check-input form-check-inline'>
<label class='tag form-check-label text-capitalize' for='".$tagDAT[1]."'>
<span class='badge badge-secondary'>".$tagDAT[1]."</span></label>
</div>
This code is being echoed from PHP. The checkboxes look like regular checkboxes. It's ignoring the 'badge' code. I thought maybe since it was being echoed via ajax, the bootstrap in the html page wasn't affecting the echoed code. That's why i added the 'head' tag with the bootstrap. Still didn't work.
Anybody have ideas? Or a better way of getting the same result?
Move the checkbox inside the label
Remove the nested <span>
Apply .badge .badge-secondary directly to the <label>
Apply .form-check-inline to the wrapper not the input
Click anywhere on the .badge to check/uncheck the box.
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css'>
<div class='form-check form-check-inline m-1'>
<label class='tag form-check-label text-capitalize badge badge-secondary' for='myTag'>
<input id='myTag' name='myTag' type='checkbox' class='form-check-input'>myTag
</label>
</div>
Bootstrap 5 has got Toggle Buttons
<input type="checkbox" class="btn-check" id="btn-check" autocomplete="off">
<label class="btn btn-primary" for="btn-check">Single toggle</label>
<input type="checkbox" class="btn-check" id="btn-check-2" checked autocomplete="off">
<label class="btn btn-primary" for="btn-check-2">Checked</label>

bootstrap - set input control(s) size inside input-group

I am working on a requirement where the input group contains two text-boxes separated by input-group-addon. The problem I am facing is I am not able to set the width of the text-boxes using bootstrap css. The first text-box should be wider than the second text-box.
JSFiddle: https://jsfiddle.net/Vimalan/5eqdkveb/3/
Current:
The textbox before and after delimiter are of same size.
Expected:
The textbox after delimiter should be small. I am more interested in a solution which uses bootstrap css and not custom css.
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="form-horizontal">
<div class="form-group">
<label class="col-xs-3 control-label">Approval Number</label>
<div class="col-xs-9">
<div class="input-group">
<input type="text" class="form-control input-sm" id="ApprovalNumberTextBox" />
<span class="input-group-addon">Delimiter</span>
<input type="text" class="form-control input-sm" id="ApprovalNumberDelimiterTextBox" />
</div>
</div>
</div>
</div>
I don't get why you are not able to set text-boxes width?!
You can simply add css rule to the input you want and set its width, like
<style>
#ApprovalNumberDelimiterTextBox {
width:40%;
}
</style>
and it is working Here or there is another problem I don't get?

Change the check box position

In the view I've browse file and check box currenlty the check Box is above the browse control and I want it to be in the left side of it,how should I change it?
<div class="form-group">
#Html.LabelFor(model => model.Cert, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#if (Model.Certificate !=null)
{
<input type="checkbox" checked="checked" disabled="disabled" />
}
else
{
<input type="checkbox" disabled="disabled" />
}
<input type="file" name=ficateFile />
</div>
</div>
Without seeing your full code and css this is a tricky thing to answer however perhaps you could try this?
input[type=checkbox], input[type=file] {
display: inline-block;
}
If that doesnt work you could allways split the checkbox and file upload into 2 columns eg
<div class="form-group">
<div class="col-md-2"></div> <!-- Label -->
<div class="col-md-2"></div> <!-- Checkbox -->
<div class="col-md-8"></div> <!-- File Upload -->
</div>
I cant see your css, but you should apply some css changes. See in element inspector where elements floating depends on parrent element and adjust float. You can apply custom css class if you dont wont to compromise your css.
<input style="display: inline" type="file" name=ficateFile /> should work. Also change the second div to span if you want all 3 elements on the same line.

Bootstrap full-width text-input within inline-form

I am struggling to create a textbox that fits the entire width of my container area.
<div class="row">
<div class="col-md-12">
<form class="form-inline" role="form">
<input type="text" class="form-control input-lg" id="search-church" placeholder="Your location (City, State, ZIP)">
<button type="submit" class="btn btn-lg">Search</button>
</form>
</div>
</div>
When I do the above, the two form elements are in-line, as I expect, but don't take up more than a few columns, at best. Hovering over the col-md-12 div in firebug shows it taking up the expected full width. It's just the text input that doesn't seem to fill. I even tried adding an in-line width value but it didn't change anything. I know this should be simple, just feeling really dumb now.
Here is a fiddle: http://jsfiddle.net/52VtD/4119/embedded/result/
EDIT:
The selected answer is thorough in every way and a wonderful help. It's what I ended up using. However I think my initial issue was actually a problem with the default MVC5 template within Visual Studio 2013. It contained this in Site.css:
input,
select,
textarea {
max-width: 280px;
}
Obviously that was blocking the text-input from expanding appropriately... Fair warning to future ASP.NET template users...
The bootstrap docs says about this:
Requires custom widths Inputs, selects, and textareas are 100% wide by
default in Bootstrap. To use the inline form, you'll have to set a
width on the form controls used within.
The default width of 100% as all form elements gets when they got the class form-control didn't apply if you use the form-inline class on your form.
You could take a look at the bootstrap.css (or .less, whatever you prefer) where you will find this part:
.form-inline {
// Kick in the inline
#media (min-width: #screen-sm-min) {
// Inline-block all the things for "inline"
.form-group {
display: inline-block;
margin-bottom: 0;
vertical-align: middle;
}
// In navbar-form, allow folks to *not* use `.form-group`
.form-control {
display: inline-block;
width: auto; // Prevent labels from stacking above inputs in `.form-group`
vertical-align: middle;
}
// Input groups need that 100% width though
.input-group > .form-control {
width: 100%;
}
[...]
}
}
Maybe you should take a look at input-groups, since I guess they have exactly the markup you want to use (working fiddle here):
<div class="row">
<div class="col-lg-12">
<div class="input-group input-group-lg">
<input type="text" class="form-control input-lg" id="search-church" placeholder="Your location (City, State, ZIP)">
<span class="input-group-btn">
<button class="btn btn-default btn-lg" type="submit">Search</button>
</span>
</div>
</div>
</div>
have a look at something like this:
<form role="form">
<div class="row">
<div class="col-xs-12">
<div class="input-group input-group-lg">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="submit" class="btn">Search</button>
</div><!-- /btn-group -->
</div><!-- /input-group -->
</div><!-- /.col-xs-12 -->
</div><!-- /.row -->
</form>
http://jsfiddle.net/n6c7v/1/
As stated in a similar question, try removing instances of the input-group class and see if that helps.
refering to bootstrap:
Individual form controls automatically receive some global styling.
All textual , , and elements with
.form-control are set to width: 100%; by default. Wrap labels and
controls in .form-group for optimum spacing.
Try something like below to achieve your desired result
input {
max-width: 100%;
}
You can use flex-fill class for input
<div class="row">
<div class="col-md-12">
<form class="form-inline" role="form">
<input type="text" class="form-control input-lg flex-fill" id="search-church" placeholder="Your location (City, State, ZIP)">
<button type="submit" class="btn btn-lg">Search</button>
</form>
</div>
With Bootstrap >4.1 it's just a case of using the flexbox utility classes. Just have a flexbox container inside your column, and then give all the elements within it the "flex-fill" class. As with inline forms you'll need to set the margins/padding on the elements yourself.
.prop-label {
margin: .25rem 0 !important;
}
.prop-field {
margin-left: 1rem;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
<div class="col-12">
<div class="d-flex">
<label class="flex-fill prop-label">Label:</label>
<input type="text" class="flex-fill form-control prop-field">
</div>
</div>
</div>
I know that this question is pretty old, but I stumbled upon it recently, found a solution that I liked better, and figured I'd share it.
Now that Bootstrap 5 is available, there's a new approach that works similarly to using input-groups, but looks more like an ordinary form, without any CSS tweaks:
<div class="row g-3 align-items-center">
<div class="col-auto">
<label>Label:</label>
</div>
<div class="col">
<input class="form-control">
</div>
<div class="col-auto">
<button type="button" class="btn btn-primary">Button</button>
</div>
</div>
The col-auto class makes those columns fit themselves to their contents (the label and the button in this case), and anything with a col class should be evenly distributed to take up the remaining space.