Styling multiple POST actions as a Bootstrap button group - css

I have multiple "POST" buttons (empty forms with a single submit button) to handle some actions which have side-effects. I need to make these buttons appear as a Bootstrap button group (.btn-group) as if they were a tags with a .btn class applied.
My first idea was to reproduce all the css styles Bootstrap uses for .btn-groups but with a form child element instead of .btn. Well it turns out there are a lot of styles involved! I'm hoping there's another solution. I included the very first basic style needed to have buttons in a single line in the snippet. Many more styles are needed to manage the borders, rounded corners, etc.
.btn-group.try1 > form {
position: relative;
float: left;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<h1>Doesn't work</h1>
<div class="btn-group">
<form method="post" action"./action1">
<button type="submit" class="btn btn-danger">Action with side-effect 1</button>
</form>
<form method="post" action"./action2">
<button type="submit" class="btn btn-default">Action with side-effect 2</button>
</form>
<form method="post" action"./action3">
<button type="submit" class="btn btn-warning">Action with side-effect 3</button>
</form>
</div>
<h1>Desired appearance</h1>
<div class="btn-group">
Action with side-effect 1
Action with side-effect 2
Action with side-effect 3
</div>
<h1>Attempt 1</h1>
<div class="btn-group try1">
<form method="post" action"./action1">
<button type="submit" class="btn btn-danger">Action with side-effect 1</button>
</form>
<form method="post" action"./action2">
<button type="submit" class="btn btn-default">Action with side-effect 2</button>
</form>
<form method="post" action"./action3">
<button type="submit" class="btn btn-warning">Action with side-effect 3</button>
</form>
</div>
Why do I use forms?
actions with side-effects should use POST.
I'd rather not use javascript to POST where vanilla HTML works just fine.

You can create a class modifier and use it with bootstrap's .form-group class.
.form-group--flex {
display: flex;
flex-wrap: wrap;
}
.form-group--flex button {
border-radius: 0;
}
Then for the border-radius you can use pseudo-selectors.
.form-group--flex form:first-of-type button {
border-top-left-radius: 4px;
border-bottom-left-radius: 4px;
}
.form-group--flex form:last-of-type button {
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
}
Code Snippet:
.form-group--flex {
display: flex;
flex-wrap: wrap;
}
.form-group--flex button {
border-radius: 0;
}
.form-group--flex form:first-of-type button {
border-top-left-radius: 4px;
border-bottom-left-radius: 4px;
}
.form-group--flex form:last-of-type button {
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="form-group form-group--flex">
<form method="post" action "./action1">
<button type="submit" class="btn btn-danger">Action with side-effect 1</button>
</form>
<form method="post" action "./action2">
<button type="submit" class="btn btn-default">Action with side-effect 2</button>
</form>
<form method="post" action "./action3">
<button type="submit" class="btn btn-warning">Action with side-effect 3</button>
</form>
</div>
Notes:
The only thing that bootstrap's class .form-group does is add a margin bottom.
.form-group {
margin-bottom: 15px;
}

Related

The buttons do not come out side by side

I'm having a problem. I set up my css by setting the buttons side by side, only it's not working.
#btn1, #btn2, #btn3{
width: 200px;
height: 40px;
}
<a class="btn btn-success" id="btn1" href="{{route('wineshop.edit', compact('wineshop'))}}">EDIT</a>
<form action="{{route('wineshop.destroy', compact('wineshop'))}}" method="post">
#method('delete')
#csrf
<button type="submit" id="btn2" class="btn btn-danger">DELETE</button>
</form>
<a class="btn btn-primary" id="btn3" href="{{route('wineshop.index')}}">BACK</a>
Can anyone kindly help me?
Set with Outer div Flex Style
#btn1, #btn2, #btn3
{
width: 200px;
height: 40px;
}
.btn_outer {
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
<div class="btn_outer">
<a class="btn btn-success" id="btn1" href="{{route('wineshop.edit', compact('wineshop'))}}">EDIT</a>
<form action="{{route('wineshop.destroy', compact('wineshop'))}}" method="post">
#method('delete')
#csrf
<button type="submit" id="btn2" class="btn btn-danger">DELETE</button>
</form>
<a class="btn btn-primary" id="btn3" href="{{route('wineshop.index')}}">BACK</a>
</div>

Using a form builder with custom CSS and need help changing the button alignment

I am using a form builder to build a multistep form and insert my own custom css to change the layout of the form. Cannot seem to grab onto the "next" and "previous" buttons.
I am using "Easy Forms" to build custom multi-step forms with logical conditions. If you are not familiar with Easy Forms, it is basically drag and drop with the ability to insert html snippets and set css container classes on each question you set in the form. Then, you have access to the css code to create your own "theme" for the form.
I have access to the "submit" button during the drag and drop process so I have been able to set a css class for that and in my css I have centered it using off-sets and "col-...". However, I cannot seem to grab a hold of the "next" and "previous" buttons as this is a multi-step form with multiple page breaks. Easy Forms does not give me a way to access or edit the next and previous buttons (as far as I have been able to discern) so I cannot assign a css class to them.
I have published the form and looked at the html code of what Easy Forms assigns to the next and previous buttons.
This is what it looks like in the html code:
<button type="button" class="btn btn-primary next">Next</button>
I do not have access to the html code to just throw a centered div in there so I have to somehow hook onto these buttons through css.
Here are some of my attempts
.btn {
box-sizing: border-box !important;
border: 0 !important;
border-bottom: 3px solid rgba(0, 0, 0, 0.1) !important;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-weight: 600;
text-shadow: 0 -1px 0 rgba(255,255,255,0.1);
padding: 16px 32px;
margin: 0 auto;
}
.btn-primary {
background-color: rgb(10,31,46);
box-shadow: inset 0 -2px 0 rgba(0,0,0,0.3),inset 0 1px 0 rgba(255,255,255,0.07),0 1px 1px rgba(10,13,25,0.5);
text-align: center;
margin: auto;
}
.btn-primary:hover, .btn-primary:active, .btn-primary:focus {
background-color: #163348; !important;
background-color: #163348 !important;
box-shadow: inset 0 -2px 0 rgba(0,0,0,0.3),inset 0 1px 0 rgba(255,255,255,0.07),0 1px 1px rgba(10,13,25,0.5) !important;
outline: 2 !important;
}
.previous {
text-align:center;
margin: auto;
}
.next {
text-align: center;
margin: auto;
}
Centered Next & Previous Buttons
Added three different arrangements of buttons with Bootstrap Flex classes.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Using a form builder with custom CSS and need help changing the button alignment</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<style>
.btns button {
margin: 0 20px;
}
</style>
</head>
<body>
<div class="container">
<form>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small> </div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<div class="btns d-flex justify-content-center">
<button type="button" class="btn btn-primary previous">Prev</button>
<button type="button" class="btn btn-primary next">Next</button>
</div>
<div class="btns d-flex justify-content-between">
<button type="button" class="btn btn-primary previous">Prev</button>
<button type="button" class="btn btn-primary next">Next</button>
</div>
<div class="btns d-flex justify-content-around">
<button type="button" class="btn btn-primary previous">Prev</button>
<button type="button" class="btn btn-primary next">Next</button>
</div>
</form>
</div>
</body>
</html>

Modal Footer-Ajax message centering in the modal footer

I have this modal footer with a <label></label> which displays message on successful operation. But, the properties are inherited so the style for centering is not working. I am using Bootstrap 3.3.7
<div class="modal-footer">
<label type="text" id="resultmessage" style="color: white; vertical-align:middle;"></label>
<button type="button" id="updateWorksStatus" class="btn btn-success btn-sm">Update Status</button>
<button type="button" class="btn btn-danger btn-sm" data-dismiss="modal">Close</button>
</div>
Add a more specific CSS selector to override bootstrap styles (.modal-footer is set to text-align: right by default.).
div.modal-footer {
text-align: center;
}
Also, you can switch label order to last element inside of .modal-footer if you wish for it to be on the right side of buttons (changed color to gray just for visibility).
Snippet:
div.modal-footer {
text-align: center;
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="modal-footer">
<button type="button" id="updateWorksStatus" class="btn btn-success btn-sm">Update Status</button>
<button type="button" class="btn btn-danger btn-sm" data-dismiss="modal">Close</button>
<label type="text" id="resultmessage" style="color: gray; vertical-align:middle;">Database updated</label>
</div>
If you wish to only center the message, you can do (tweak values to your benefit):
.modal-footer button {
margin-top: -48px;
}
#resultmessage {
display: inline-block;
width: 100%;
text-align: center;
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="modal-footer">
<label type="text" id="resultmessage" style="color: gray; vertical-align:middle;">Database updated</label>
<button type="button" id="updateWorksStatus" class="btn btn-success btn-sm">Update Status</button>
<button type="button" class="btn btn-danger btn-sm" data-dismiss="modal">Close</button>
</div>
In my opinion, using label as not label for nothing is not good pratice. I'm quessing that u want this information ('Database updated') aligne vertically (you have tried to use vertical-align - it only works for tables). Instead of using label, better is to use for example span.
I did it like below:
https://codepen.io/Czeran/pen/BdWbjg
HTML
<div class="modal-footer">
<span id="resultmessage">Database updated</span>
<div class="tools">
<button type="button" id="updateWorksStatus" class="btn btn-success btn-sm">Update Status</button>
<button type="button" class="btn btn-danger btn-sm" data-dismiss="modal">Close</button>
</div>
<span></span>
</div>
CSS (edited)
.modal-footer {
position: relative;
display: flex;
flex-direction: row-reverse;
align-items: center;
justify-content: center;
}
#resultmessage {
font-weight: bold;
}
.tools {
position: absolute;
right: 15px;
}

How do I apply a max-width to an input field form-control with a trailing secondary button in Bootstrap 3?

When I attempt to apply a maximum width to an input field, it positions the secondary button as if the input field didn't have a maximum width.
JSFiddle Example
HTML:
<div class="container">
<div class="form-group">
<label class="control-label">Date</label>
<div class="input-group">
<input type="text" maxlength="10" class="form-control datefield" placeholder="mm/dd/yyyy">
<span class="input-group-btn">
<button id="btnClearDate" class="btn btn-secondary btn-default" type="button">Clear</button>
</span>
</div>
</div>
</div>
CSS:
.datefield {
max-width: 100px;
}
Result:
How can I get the secondary button to correctly sit next to the text field?
Try using display:inline-block;
https://jsfiddle.net/ex3ntia/DTcHh/22030/
.input-group-btn {display:inline-block;}
Bootstrap is laid out by using a grid system. You will need to adjust your design layout to accomplish what you are looking to achieve.
What is happening now is all you are doing is shrinking down the size of the input box, but not the actual grid cell.
try adjusting just the cell or placing the form-group elements within a cell then within a targeting element you can shrink.
Try this,
#import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');
body {
margin: 10px;
padding: 10px;
}
.shrink {
width: 200px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="form-group">
<div class="shrink">
<label class="control-label">Date</label>
<div class="input-group">
<input type="text" maxlength="10" class="form-control datefield" placeholder="mm/dd/yyyy">
<span class="input-group-btn">
<button id="btnClearDate" class="btn btn-secondary btn-default" type="button">Clear</button>
</span>
</div><!-- .input-group -->
</div><!-- .shrink -->
</div><!-- .form-group -->
</div><!-- .row -->
</div> <!-- .container -->
Hope that helps!
The span was not being displayed inline. I assume this was changed with the input-group-btn class. Here is the new code (I named the new class ):
#import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');
body {
margin: 10px;
}
.datefield {
max-width: 100px;
}
.buttoncleardiv {
display: inline;
}
<div class="container">
<div class="form-group">
<label class="control-label">Date</label>
<div class="input-group">
<input type="text" maxlength="10" class="form-control datefield" placeholder="mm/dd/yyyy">
<span class="input-group-btn buttoncleardiv">
<button id="btnClearDate" class="btn btn-secondary btn-default" type="button">Clear</button>
</span>
</div>
</div>
The JS Fiddle is here

Bootstrap Navbar Keep Elements on Same Line When Collapsed

I am using a Bootstrap navbar and using it's collapse functionality.
In my fully expanded navbar, I have three elements - a search field and it's button, and also a browse button.
When it collapses, I would like to have the search field and it's submit button to appear on the same line. However, when my navbar collapses, it places the search field on it's own line, and then the "submit" and "browse" button on the same line. The image below shows what I mean.
I would like it to have the search field and the glyphicon 'submit' button on one line, and the browse button on the line below.
Here is the html I am using:
<div class="collapse navbar-collapse" id="example-navbar-collapse">
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control">
</div><!--
--><button type="submit" class="btn btn-default btn-search"">
<span class="glyphicon glyphicon-search"></span>
</button>
<button type="submit" class="btn btn-default">Browse</button>
</form>
</div>
Put you button inside the form-group,
<div class="form-group">
<input type="text" class="form-control">
<button type="submit" class="btn btn-default btn-search">
<span class="glyphicon glyphicon-search"></span>
</button>
</div>
add this to your CSS,
.navbar-form .form-group input {
width: calc(100% - 50px);
display: inline-block;
vertical-align: top;
}
and here is a fiddle demo
try this one
.form-control{display:inline-block;width:80%}.btn-search{width:10%}
<div class="collapse navbar-collapse" id="example-navbar-collapse">
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control"><button type="submit" class="btn btn-default btn-search"">
<span class="glyphicon glyphicon-search"></span>
</button></div>
<button type="submit" class="btn btn-default">Browse</button>
</form>
</div>
At mobile screens you can position your search button to absolute and then place it over the search input to the right and then give the input a padding to the right and it will appear to be on the same line. So first position your .navbar-form to relative and then at mobile widths positon your search button to absolute and then give the the search input a padding right to make up for the button being placed over the top of it like so:
Here is a fiddle Fiddle
.navbar-form{
position: relative;
}
#media screen and (max-width: 767px){
.navbar-form .btn-search{
position: absolute;
right: 15px;
top: 10px;
}
.navbar-form input{
padding-right: 40px;
}
}

Resources