Show div if input radio button is selected - css

I want to show multiple div if radio button is checked. Here is the code that I tried.
HTML:
<div class="form-group row">
<label for="inputEmail3" class="col-sm-2 col-form-label">Do you have Extras Cover</label>
<div class="col-sm-10">
<div class="form-check form-check-inline">
<input class="form-check-input yes" name="radio" type="radio" id="yes" value="yes">
<label class="form-check-label" for="yes"> Yes </label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" name="radio" type="radio" id="no" value="no">
<label class="form-check-label" for="no"> No </label>
</div>
</div>
</div>
<div class="form-group row show">
<label for="" class="col-sm-2 col-form-label">Medicare Number</label>
<div class="col-sm-10">
<input type="text" class="form-control" >
</div>
</div>
<div class="form-group row show">
<label for="" class="col-sm-2 col-form-label">Medicare Address</label>
<div class="col-sm-10">
<input type="text" class="form-control" >
</div>
</div>
CSS:
.show{
display:none ;
}
input#yes:checked ~ div.show{
display: block;
}
If 'YES' button is checked then 'SHOW' class div will show. If I put the 'SHOW' class div with the input field it works. But when I put it outside of the input field div it doesn't work.
Can anyone please help me with my code structure.
Please see the image for better understanding.
my code

The css solution will indeed only work if those 'show' divs are inside the same parent as the radio buttons. If you want them to be outside that input field div, then use a bit of javascript.
document.querySelector('input#yes').addEventListener('click', function() {
Array.from(document.querySelectorAll('.form-group.show')).forEach((group) => {
group.style.display = "block";
})
})
document.querySelector('input#no').addEventListener('click', function() {
Array.from(document.querySelectorAll('.form-group.show')).forEach((group) => {
group.style.display = "none";
})
})
.form-group.show {
display: none;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="form-group row">
<label for="inputEmail3" class="col-sm-2 col-form-label">Do you have Extras Cover</label>
<div class="col-sm-10">
<div class="form-check form-check-inline">
<input class="form-check-input yes" name="radio" type="radio" id="yes" value="yes">
<label class="form-check-label" for="yes"> Yes </label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" name="radio" type="radio" id="no" value="no">
<label class="form-check-label" for="no"> No </label>
</div>
</div>
</div>
<div class="form-group row show">
<label for="" class="col-sm-2 col-form-label">Medicare Number</label>
<div class="col-sm-10">
<input type="text" class="form-control">
</div>
</div>
<div class="form-group row show">
<label for="" class="col-sm-2 col-form-label">Medicare Address</label>
<div class="col-sm-10">
<input type="text" class="form-control">
</div>
</div>

Related

Did bootstrap 5 remove spacing between row?

I started using bootstrap 5 and noticed there is no spaces between rows. Do we have to explicitly use spacing utility like mb-3 or mb-2 etc Trying to understand the reason behind removing vertical spaces between rows.
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"/>
<form>
<div class="row">
<label for="inputEmail3" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="inputEmail3">
</div>
</div>
<div class="row">
<label for="inputPassword3" class="col-sm-2 col-form-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPassword3">
</div>
</div>
<div class="row">
<div class="col-sm-10 offset-sm-2">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="gridCheck1">
<label class="form-check-label" for="gridCheck1">
Example checkbox
</label>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
Bootstrap 5 has vertical gutters for the Grid. Instead of using separate rows, put the columns in a single row div. Then use whichever gy-* you want on the row...
<form>
<div class="row gy-4">
<label for="inputEmail3" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="inputEmail3">
</div>
<label for="inputPassword3" class="col-sm-2 col-form-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPassword3">
</div>
<div class="col-sm-10 offset-sm-2">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="gridCheck1">
<label class="form-check-label" for="gridCheck1"> Example checkbox </label>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
https://codeply.com/p/4ZM75xw90B

Bootstrap button extends container

When I make the screen smaller, the content of the box pushes the submit button out.
Do I need a media query to solve this? If so, what do I need?
I've included the code to have a look at. I have had a go at some media queries but I am probably doing the wrong thing as I can't work out what it is that needs to happen when the screen size is changed. Most notably, going smaller.
TIA
#register {
height: 95%;
padding: 2rem 1rem;
}
p {
text-align: left !important;
}
.jumbotron1 {
background-color: #e9ecef
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col">
<div class="jumbotron1 jumbotron-fluid" id="register">
<div class="container1">
<h1 class="display-4">Register your interest!</h1>
<p class="lead">Don't miss out - register now! </p>
<form>
<div class="form-group">
<div class="row">
<div class="col">
<label for="firstname">First name</label>
<input type="text" class="form-control" id="firstname" placeholder="Enter first name">
</div>
<div class="col">
<label for="lastname">Last name</label>
<input type="text" class="form-control" id="lastname" placeholder="Enter last name">
</div>
</div>
<br>
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="InputEmail1" 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>
<h4>Which product are you interested in?</h4>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="inlineCheckbox1" value="option1">
<label class="form-check-label" for="inlineCheckbox1">Shave butter</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="inlineCheckbox2" value="option2">
<label class="form-check-label" for="inlineCheckbox2">Shave foam</label>
</div>
<br>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="inlineCheckbox3" value="option3">
<label class="form-check-label" for="inlineCheckbox3">Shave gel</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="inlineCheckbox3" value="option3">
<label class="form-check-label" for="inlineCheckbox3">After care shave</label>
</div>
<br>
<br>
<div> <button type="submit" class="btn btn-primary">Submit</button></div>
The issue is this rule:
#register {
height: 95%;
…
}
If you remove it, the button no longer breaks out of the container.

In Bootstrap, why my search button is not inline?

I am using the following code to make a simple formular with bootstrap:
<form class="form-inline">
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label">De</label>
<input type="email" value='' id="name" placeholder="Ime">
</div>
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label">A</label>
<input type="email" value='' id="name" placeholder="Ime">
</div>
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label">Départ</label>
<input type="email" value='' id="name" placeholder="Ime">
</div>
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label">Retour</label>
<input type="email" value='' id="name" placeholder="Ime">
</div>
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label"></label>
<button type="submit" class="btn btn-default">recherche</button>
</div>
</form>
But the result is:
So, why is the search button above the other input fields ? Is there a way to recenter it without touching the responsiveness ?
Thank you
see snippet below .
first, your input fields were exceding the width of the col . so set max-width:100% to them
second, .btn has inline-block style , use display:block instead
let me know if it works for you
input {
max-width:100%
}
.form-group .btn {
display:block;
}
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<form class="form-inline">
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label">De</label>
<input type="email" value='' id="name" placeholder="Ime">
</div>
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label">A</label>
<input type="email" value='' id="name" placeholder="Ime">
</div>
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label">Départ</label>
<input type="email" value='' id="name" placeholder="Ime">
</div>
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label">Retour</label>
<input type="email" value='' id="name" placeholder="Ime">
</div>
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label"></label>
<button type="submit" class="btn btn-default">recherche</button>
</div>
</form>
Add an empty label with on top of the button to align, like this
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<form class="form-inline">
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label">De</label>
<input type="email" value='' id="name" placeholder="Ime">
</div>
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label">A</label>
<input type="email" value='' id="name" placeholder="Ime">
</div>
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label">Départ</label>
<input type="email" value='' id="name" placeholder="Ime">
</div>
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label">Retour</label>
<input type="email" value='' id="name" placeholder="Ime">
</div>
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label"> </label>
<button type="submit" class="btn btn-default">recherche</button>
</div>
</form>
Your button should not be in a form group, and additionally that's not the usual way to markup an inline form (using cols). See this example where I use some of your fields for brevity:
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<form class="form-inline">
<div class="form-group">
<label for="name" class="control-label">De</label>
<input type="email" value='' id="name" placeholder="Ime">
</div>
<div class="form-group">
<label for="name" class="control-label">A</label>
<input type="email" value='' id="name" placeholder="Ime">
</div>
<button type="submit" class="btn btn-default">recherche</button>
</form>
Add an <br/> after label , like this
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<form class="form-inline">
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label">De</label><br />
<input type="email" value='' id="name1" placeholder="Ime">
</div>
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label">A</label><br />
<input type="email" value='' id="name2" placeholder="Ime">
</div>
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label">Départ</label><br />
<input type="email" value='' id="name3" placeholder="Ime">
</div>
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label">Retour</label><br />
<input type="email" value='' id="name4" placeholder="Ime">
</div>
<div class="form-group col-xs-2 col-md-2">
<label for="name" class="control-label"></label><br />
<button type="submit" class="btn btn-default">recherche</button>
</div>
</form>

Bootstrap CSS - How to get control label with inline radios below it

In my form I have two cols with the label on top and a full width input. My problem is that I now have two fields requiring radios. I want to split the col into 2 and have the two labels with their two radios below. I can get that ok but the alignment is then not right on the next field down.
You can see it here in this bootply http://www.bootply.com/VlFHq0daui
Leased property and mesne rate units. The key codes below it is not aligning correctly.
<form>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="address_line1" class="control-label">Address 1</label>
<input type="text" class="form-control" id="address_line1">
</div>
<div class="form-group">
<label for="address_line1" class="control-label">Address 2</label>
<input type="text" class="form-control" id="address_line2">
</div>
<div class="form-group">
<label for="town" class="control-label">Town*</label>
<input type="text" class="form-control" id="town">
</div>
<div class="form-group">
<label for="county" class="control-label">County</label>
<input type="text" class="form-control" id="county">
</div>
<div class="form-group">
<label for="eircode" class="control-label">Eircode</label>
<input type="text" class="form-control" id="eircode">
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label for="reference" class="control-label">Reference</label>
<input type="text" class="form-control" id="reference">
</div>
<div class="form-group">
<label for="tax_reference" class="control-label">Tax Reference</label>
<input type="text" class="form-control" id="tax_reference">
</div>
<div class="form-group">
<label for="local_authority" class="control-label">Local Authority</label>
<select name="local_authority" id="local_authority" class="form-control">
<option>Select...</option>
<option value="41">Ahtlone Town Council</option>
<option value="88">Youghal Town Council</option>
</select>
</div>
<div class="form-group">
<div class="row">
<div class="col-lg-6">
<label for="leased_property" class="control-label">Leased Property?</label>
<br>
<label class="radio-inline">
<input type="radio" name="leased_property" id="leased_property" value="YES"> Yes
</label>
<label class="radio-inline">
<input type="radio" name="leased_property" id="leased_property" value="NO"> No
</label>
</div>
<div class="col-lg-6">
<label for="mesne_rates_unit" class="control-label">Mesne Rates Unit?</label>
<br>
<label class="radio-inline">
<input type="radio" name="mesne_rates_unit" id="mesne_rates_unit" value="YES"> Yes
</label>
<label class="radio-inline">
<input type="radio" name="mesne_rates_unit" id="mesne_rates_unit" value="NO"> No
</label>
</div>
</div>
</div>
<div class="form-group">
<label for="key_code" class="control-label">Key Code</label>
<input type="text" class="form-control" id="key_code">
</div>
<div class="form-group">
<label for="gprn_number" class="control-label">GPRN Number</label>
<input type="text" class="form-control" id="gprn_number">
</div>
<div class="form-group">
<label for="mprn_number" class="control-label">MPRN Number</label>
<input type="text" class="form-control" id="mprn_number">
</div>
</div>
</div>
</form>
You can warp radios inputs inside a div with same height of input box:
<div class="radios">
<label class="radio-inline">
<input type="radio" name="leased_property" id="leased_property" value="YES"> Yes
</label>
<label class="radio-inline">
<input type="radio" name="leased_property" id="leased_property" value="NO"> No
</label>
</div>
CSS:
.radios{
height: 34px;
padding: 5px;
}
Bootply
Add this to your CSS.
.form-group {
min-height:59px;
}
After some trying I found a solution, its not perfect, but will solve the issue.
You could wrap your radio elements into a row and hardcode the needed margins to align the buttons the way you want. For this I gave the row a custom class align-radios.
HTML:
<div class="row align-radios">
<label class="radio-inline">
<input type="radio" name="leased_property" id="leased_property" value="YES"> Yes
</label>
<label class="radio-inline">
<input type="radio" name="leased_property" id="leased_property" value="NO"> No
</label>
</div>
CSS:
.align-radios {
margin: 7px 0 7px 5px;
}
Hope this helps you a bit.

Bootstrap - horizontal Aligning Text Fields

I am trying to align textfields with bootstrap however I keep failing to adopt anything I found on sources to my project. What I am trying to do is aligning e-mail and password text-fields but leave 'Remember me' and 'Login' centered.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<form method="POST" action="/auth/login">
<div class="form-group">
<div>
<label for="exampleInputEmail1">Email address</label>
<input type="email" name="email" value="{{ old('email') }}">
</div>
</div>
<div class="form-group">
<div>
<label for="exampleInputPassword1">Password</label>
<input type="password" name="password" id="password">
</div>
</div>
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" name="remember"> Remember me
</label>
</div>
</div>
<div class="form-group">
<div>
<button type="submit" class="btn btn-default">Login</button>
</div>
</div>
</form>
You can use Bootstrap's form-horizontal class to help achieve the layout.
Demo: http://www.bootply.com/nJ2P2gi76B
<form class="form-horizontal">
<div class="form-group">
<label for="inputEmail" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="inputEmail">
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPassword">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Log in</button>
</div>
</div>
</form>
You can access only the text input by using a css attribute selector.
For Example...
input[type="text"] {
...
}
You can get more specific if needed...
#someID input[type="text"] {
...
}
you can use text-center to center the inner content of your div. you can use text-left, text-center, text-right classes to align the contents.
#import url(https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css);
#import url(https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css);
<form method="POST" action="/auth/login">
<div class="form-group">
<div>
<label for="exampleInputEmail1">Email address</label>
<input type="email" name="email" value="{{ old('email') }}">
</div>
</div>
<div class="form-group">
<div>
<label for="exampleInputPassword1">Password</label>
<input type="password" name="password" id="password">
</div>
</div>
<div class="form-group text-center">
<div class="checkbox">
<label>
<input type="checkbox" name="remember"> Remember me
</label>
</div>
</div>
<div class="form-group text-center">
<div>
<button type="submit" class="btn btn-default">Login</button>
</div>
</div>
</form>
It would help if you could post a JSFiddle with the css you are using!
Guessing from what i see:
You could add a class to both fields and override the css.
or put both fields in a div and align them without impacting the rest.
Using an enclosing div:
JSFiddle
CSS:
.input-fields {
width: 300px;
}
.input-fields .form-group {
float: right;
}
.form-group {
clear: both;
text-align: center;
}
HTML:
<form method="POST" action="/auth/login">
<div class="input-fields">
<div class="form-group">
<div>
<label for="exampleInputEmail1">Email address</label>
<input type="email" name="email" value="{{ old('email') }}">
</div>
</div>
<div class="form-group">
<div>
<label for="exampleInputPassword1">Password</label>
<input type="password" name="password" id="password">
</div>
</div>
</div>
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" name="remember"> Remember me
</label>
</div>
</div>
<div class="form-group">
<div>
<button type="submit" class="btn btn-default">Login</button>
</div>
</div>
</form>

Resources