Aligning input box inside panel body with the use of css grid - css

I have created a login using css grid layout. I want to implement a form input box inside the panel body. However, the input box not respecting inside the panel body. The input box was overflow outside the panel body. For some reason, the code is working with regular css. I just want to implement css grid and i'm not sure about to go about this.
Thanks for any help.
Here is the fiddle.
<pre>
https://jsfiddle.net/80fc325h/

Just use *{box-sizing: border-box} It will be updated. Hope it is helpful to you. Kindly check the snippet. As CSS3 box-sizing property allows us to include the padding and border in an element's total width and height.
body {
margin: 0;
font-size: 13px;
color: #222;
background-color: #f5f5f5;
}
* {
box-sizing:border-box;
}
.text-right{
text-align: right;
}
/*--------------------------------------------------------------
style:
--------------------------------------------------------------*/
.login-wrapper{
display:grid;
grid-template-columns:1fr;
width: 50%;
margin: 0 auto;
}
.content{
display:grid;
grid-template-columns:1fr;
border:1px solid red;
}
.panel {
background-color: #fff;
border: 1px solid #dbd9d9;
border-radius: 4px;
-webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, .05);
box-shadow: 0 1px 1px rgba(0, 0, 0, .05);
}
.panel-body {
padding: 15px;
}
.panel-heading {
text-align: center;
padding: 5px 10px;
border-top-left-radius: 3px;
border-top-right-radius: 3px;
}
.panel-title {
margin-top: 0;
margin-bottom: 0;
color: inherit;
}
.panel-footer {
padding: 10px 15px;
background-color: #fff;
border-top: 1px solid #dbd9d9;
}
/*--------------------------------------------------------------
Form:
--------------------------------------------------------------*/
.form-control {
display: block;
width: 100%;
height: 34px;
padding: 6px 12px;
line-height: 1.42857143;
color: #555;
background-color: #fff;
background-image: none;
border: 1px solid #ccc;
border-radius: 4px;
}
.form-control:focus {
outline: 0;
}
.form-control::-moz-placeholder {
color: #999;
opacity: 1;
}
.form-control:-ms-input-placeholder {
color: #999;
}
.form-control::-webkit-input-placeholder {
color: #999;
}
.form-control::-ms-expand {
background-color: transparent;
border: 0;
}
.input-sm {
height: 29px;
padding: 5px 10px;
font-size: 12px;
line-height: 1.5;
border-radius: 3px;
}
.has-feedback {
position: relative;
}
.has-feedback .form-control {
padding-right: 42.5px;
}
.form-control-feedback {
position: absolute;
top: 0;
right: 0;
z-index: 2;
display: block;
width: 34px;
height: 34px;
line-height: 34px;
text-align: center;
pointer-events: none;
}
.has-error .help-block,
.has-error .control-label,
.has-error .radio,
.has-error .checkbox,
.has-error .radio-inline,
.has-error .checkbox-inline,
.has-error.radio label,
.has-error.checkbox label,
.has-error.radio-inline label,
.has-error.checkbox-inline label {
color: #f44336;
}
.has-error .form-control {
border-color: #f44336;
}
.has-error .form-control:focus {
border-color: #f44336;
}
.has-error .input-group-addon {
color: #f44336;
background-color: #f2dede;
border-color: #f44336;
}
.has-error .form-control-feedback {
color: #f44336;
}
.has-feedback label ~ .form-control-feedback {
top: 25px;
}
.has-feedback label.sr-only ~ .form-control-feedback {
top: 0;
}
.input-group {
position: relative;
display: table;
border-collapse: separate;
}
.input-group .form-control {
position: relative;
z-index: 2;
float: left;
width: 100%;
margin-bottom: 0;
}
.input-group .form-control:focus {
z-index: 3;
}
.input-group-addon,
.input-group-btn,
.input-group .form-control {
display: table-cell;
}
.input-group-addon,
.input-group-btn {
width: 1%;
white-space: nowrap;
vertical-align: middle;
}
.input-group-addon {
padding: 6px 12px;
font-weight: normal;
line-height: 1;
color: #555;
text-align: center;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 4px;
}
.input-group-addon.input-sm {
padding: 5px 10px;
font-size: 12px;
border-radius: 3px;
}
.input-group-addon.input-lg {
padding: 10px 16px;
font-size: 18px;
border-radius: 6px;
}
.input-group-addon input[type="radio"],
.input-group-addon input[type="checkbox"] {
margin-top: 0;
}
.input-group .form-control:first-child,
.input-group-addon:first-child,
.input-group-btn:first-child > .btn,
.input-group-btn:first-child > .btn-group > .btn,
.input-group-btn:first-child > .dropdown-toggle,
.input-group-btn:last-child > .btn:not(:last-child):not(.dropdown-toggle),
.input-group-btn:last-child > .btn-group:not(:last-child) > .btn {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
.input-group-addon:first-child {
border-right: 0;
}
.input-group .form-control:last-child,
.input-group-addon:last-child,
.input-group-btn:last-child > .btn,
.input-group-btn:last-child > .btn-group > .btn,
.input-group-btn:last-child > .dropdown-toggle,
.input-group-btn:first-child > .btn:not(:first-child),
.input-group-btn:first-child > .btn-group:not(:first-child) > .btn {
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
.input-group-addon:last-child {
border-left: 0;
}
label {
display: inline-block;
max-width: 100%;
font-size: 13px;
margin-bottom: 5px;
}
input:focus{
outline:none!important;
}
.form-control-feedback i{
margin-top: -5px;
}
.form-group{
margin-bottom: 10px;
}
.footer-section{
background-color: #e9ecef;
display: grid;
border-top: 1px solid #d8dadd;
grid-template-columns:1fr;
font-size: 11px;
text-align: center;
}
/* ==========================================================================
Button Scripts
========================================================================== */
.btn{
display: inline-block;
margin-bottom: 0;
font-weight: 400;
text-align: center;
vertical-align: middle;
touch-action: manipulation;
cursor: pointer;
border: 1px solid transparent;
white-space: nowrap;
padding: 3px 10px;
line-height: 1.5384616;
border-radius: 3px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none
}
.btn:focus{
outline: none;
}
.btndrk-orange{
background-color: #ec8b02;
color:#fff;
}
<div class="login-wrapper">
<div class="content">
<div class="panel">
<div class="panel-body">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-user-tie"></i></span>
<input type="text" class="form-control" name="username" id="username" autocomplete="off" placeholder="Username">
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-lock2"></i></span>
<input type="password" class="form-control" name="password" id="password" autocomplete="off" placeholder="Password">
</div>
</div>
</div>
<div class="panel-footer text-right">
<button type="button" class="btn">LOGIN</button>
</div>
</div>
</div>
</div>

Use display flex not display table on your input group.

Related

How to change the background color of the checkbox to black?

currently I have encountered a project that needs to change the background color of the input checkbox to black. I searched for some information on the Internet but I still can’t successfully change the color. Would you like to help me find out what went wrong?
input[type=checkbox] {
background-color: #222;
padding: 3px 6px;
border: 1px solid red;
color: #fff;
user-select: none; /* 防止文字被滑鼠選取反白 */
}
input[type=checkbox]:checked:after {
display: inline-block;
content:"";
color: #fff;
background-color:#222;
}
/* input[type=checkbox]:checked:after {
content: "X";
background-color: #FFFFFF;
font-size: 12px;
text-align:center;
} */
<input type="checkbox"><label for="enter-send" class="enter-send"> Enter </label>
You have to write custom CSS to style checkboxes.
/* This css is for normalizing styles. You can skip this. */
*, *:before, *:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
margin: 0;
padding: 0;
}
.new {
padding: 50px;
}
.form-group {
display: block;
margin-bottom: 15px;
}
.form-group input {
padding: 0;
height: initial;
width: initial;
margin-bottom: 0;
display: none;
cursor: pointer;
}
.form-group label {
position: relative;
cursor: pointer;
}
.form-group label:before {
content:'';
-webkit-appearance: none;
background-color: transparent;
border: 2px solid black;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
padding: 10px;
display: inline-block;
position: relative;
vertical-align: middle;
cursor: pointer;
margin-right: 5px;
}
.form-group input:checked + label:before {
background: black;
}
.form-group input:checked + label:after {
content: '';
display: block;
position: absolute;
top: 2px;
left: 9px;
width: 6px;
height: 14px;
border: solid white;
border-width: 0 2px 2px 0;
transform: rotate(45deg);
}
<div class="new">
<form>
<div class="form-group">
<input type="checkbox" id="html">
<label for="html">HTML</label>
</div>
<div class="form-group">
<input type="checkbox" id="css">
<label for="css">CSS</label>
</div>
<div class="form-group">
<input type="checkbox" id="javascript">
<label for="javascript">Javascript</label>
</div>
</form>
</div>

Header will not expand and list items hidden from view

I have 2 issues with my test page. I'm trying to get header to take up width of page. I set the width to 100% but it still will not expand out to width of page. And the dropdown for the "Links" item appears behind the wrapper element and therefore it is not possible to click on any of the underlying links. Thank you.
Codepen: https://codepen.io/centem/pen/dBdwxw
header {
margin: 0 auto;
width: 100%
height: 40px;
}
header ul {
height: 40px;
margin: 0px;
padding: 0px;
list-style: none;
}
header ul li {
float: left;
width: 200px;
height: 40px;
background-color: black;
opacity: .75;
line-height: 40px;
text-align: center;
font-size: 20px;
color: white;
}
header ul li a {
text-decoration: none;
display: block;
}
header ul li a:hover {
background-color: green;
}
header ul li ul li {
display:none;
}
ul li:hover ul li {
display: block;
}
header is already set to 100% witdth. However, you've set fixed width and the background to the li. You can simply use flexbox and set flex: 1 to the items without worrying about their width.
body {
margin: 0 auto;
font: 1.1em;
background-image: url('CNDsplash.jpg');
background-repeat: no-repeat;
background-position: center;
}
h1,
h2,
h3,
h4,
h5 {
font-weight: 400;
}
header {
margin: 0 auto;
width: 100%;
height: 40px;
}
header ul {
height: 40px;
margin: 0px;
padding: 0px;
list-style: none;
display: flex;
}
header ul li {
height: 40px;
flex: 1;
background-color: black;
opacity: .75;
line-height: 40px;
text-align: center;
font-size: 20px;
color: white;
}
header ul li a {
text-decoration: none;
display: block;
}
header ul li a:hover {
background-color: green;
}
header ul li ul li {
display: none;
}
ul li:hover ul li {
display: block;
}
#title-heading {
float: left;
}
.hidden {
display: none;
}
.wrapper {
width: 100%;
max-width: 600px;
margin: 0 auto;
border: 2px solid #eee;
background: #fefefe;
opacity: 0.9;
filter: alpha(opacity=90);
/* for ie8 and earlier */
}
.upload-console {
/*background: #fefefe;*/
/*border: 2px solid #eee; */
padding: 20px;
/*opacity: 0.9;*/
/*filter: alpha(opacity=50);*/
/* for IE8 and earlier */
}
.upload-console-header {
padding: 0 0 20px 0;
margin: 0;
border-bottom: 2px solid #eee;
font-weight: 600;
margin-bottom: 20px;
}
.upload-console-drop {
height: 200px;
border: 2px dashed #ccc;
line-height: 200px;
color: #ccc;
text-align: center;
margin-bottom: 20px;
}
.upload-console-drop.drop {
border-color: #222;
color: #222;
}
.upload-console-body {
margin-bottom: 20px;
}
.bar {
width: 100%;
background: #eee;
padding: 3px;
border-radius: 3px;
box-shadow: inset 0 1px 3px rgba(0, 0, 0, .2);
box-sizing: border-box;
margin-bottom: 20px;
}
.bar-fill {
height: 30px;
display: block;
background: cornflowerblue;
width: 0;
border-radius: 3px;
-webkit-transition: width 0.8s ease;
transition: width 0.8s ease;
}
.bar-fill-text {
color: #fff;
line-height: 30px;
margin-left: 5px;
}
.upload-console-upload {
border-bottom: 2px solid #ccc;
margin-bottom: 10px;
padding-bottom: 10px;
}
.upload-console-upload span {
float: right;
}
<header>
<ul>
<li><a>Home</a></li>
<li><a>Contact</a></li>
<li><a>Docs</a></li>
<li><a>Links</a>
<ul>
<li><a>Link 1</a></li>
<li><a>Link 2</a></li>
<li><a>Link 3</a></li>
</ul>
</li>
</ul>
</header>
<div class="wrapper">
<div id="form-heading">
</div>
<div class="upload-console">
<br>
<h2 class="upload-console-header">FTS</h2>
<div class="upload-console-body">
<h3>Select files</h3>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" id="standard-upload-files" multiple>
<input type="submit" value="Upload files" id="standard-upload">
</form>
<h3>Or drag and drop files below</h3>
<div class="upload-console-drop" id="drop-zone">
Just drag and drop files here
</div>
<div class="bar">
<div class="bar-fill" id="bar-fill">
<div class="bar-fill-text" id="bar-fill-text"></div>
</div>
</div>
<div id="uploads-finished" class="hidden">
<h3>Processed Files</h3>
</div>
</div>
</div>
</div>
body {
margin: 0 auto;
font: 1.1em;
background-image: url('CNDsplash.jpg');
background-repeat: no-repeat;
background-position: center;
}
h1,
h2,
h3,
h4,
h5 {
font-weight: 400;
}
header {
margin: 0 auto;
width: 100% height: 40px;
}
header ul {
height: 40px;
margin: 0px;
padding: 0px;
list-style: none;
display: flex;
z-index: 1;
position: relative;
}
header ul li {
display: flex;
flex-grow: 1;
height: 40px;
background-color: black;
opacity: .75;
line-height: 40px;
text-align: center;
font-size: 20px;
color: white;
}
header ul li a {
text-decoration: none;
display: block;
width: 100%;
}
header ul li a:hover {
background-color: green;
}
header ul li ul li {
display: none;
}
ul li:hover ul li {
display: block;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 100%;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown:hover .dropdown-content {
display: block;
}
#title-heading {
float: left;
}
.hidden {
display: none;
}
.wrapper {
width: 100%;
max-width: 600px;
margin: 0 auto;
border: 2px solid #eee;
background: #fefefe;
opacity: 0.9;
filter: alpha(opacity=90);
/* for ie8 and earlier */
}
.upload-console {
/*background: #fefefe;*/
/*border: 2px solid #eee; */
padding: 20px;
/*opacity: 0.9;*/
/*filter: alpha(opacity=50);*/
/* for IE8 and earlier */
}
.upload-console-header {
padding: 0 0 20px 0;
margin: 0;
border-bottom: 2px solid #eee;
font-weight: 600;
margin-bottom: 20px;
}
.upload-console-drop {
height: 200px;
border: 2px dashed #ccc;
line-height: 200px;
color: #ccc;
text-align: center;
margin-bottom: 20px;
}
.upload-console-drop.drop {
border-color: #222;
color: #222;
}
.upload-console-body {
margin-bottom: 20px;
}
.bar {
width: 100%;
background: #eee;
padding: 3px;
border-radius: 3px;
box-shadow: inset 0 1px 3px rgba(0, 0, 0, .2);
box-sizing: border-box;
margin-bottom: 20px;
}
.bar-fill {
height: 30px;
display: block;
background: cornflowerblue;
width: 0;
border-radius: 3px;
-webkit-transition: width 0.8s ease;
transition: width 0.8s ease;
}
.bar-fill-text {
color: #fff;
line-height: 30px;
margin-left: 5px;
}
.upload-console-upload {
border-bottom: 2px solid #ccc;
margin-bottom: 10px;
padding-bottom: 10px;
}
.upload-console-upload span {
float: right;
}
<header>
<ul>
<li><a>Home</a></li>
<li><a>Contact</a></li>
<li class="dropdown"><a>Links</a>
<ul class="dropdown-content">
<li><a>Link 1</a></li>
<li><a>Link 2</a></li>
<li><a>Link 3</a></li>
</ul>
</li>
</ul>
</header>
<div class="wrapper">
<div id="form-heading">
</div>
<div class="upload-console">
<br>
<h2 class="upload-console-header">FTS</h2>
<div class="upload-console-body">
<h3>Select files</h3>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" id="standard-upload-files" multiple>
<input type="submit" value="Upload files" id="standard-upload">
</form>
<h3>Or drag and drop files below</h3>
<div class="upload-console-drop" id="drop-zone">
Just drag and drop files here
</div>
<div class="bar">
<div class="bar-fill" id="bar-fill">
<div class="bar-fill-text" id="bar-fill-text"></div>
</div>
</div>
<div id="uploads-finished" class="hidden">
<h3>Processed Files</h3>
</div>
</div>
</div>
</div>
<script src="upload.js"></script>
<script src="global.js"></script>

Change text hover

How change text when I push cursor in button? If I push cursor in button with price, will show text: "Add to basket".
This is Code:
body {
font-family: 'Roboto', sans-serif;
}
.price {
font-size: 1em;
border-radius: 50px;
background-color: #f00;
text-align: center;
color: #fff;
padding: 5px;
width: 10%;
margin: 20px auto;
}
.price:hover {
background-color: #fff;
border: 1px solid #bfbfbf;
color: #f00;
cursor: pointer;
}
.info {
display: none;
}
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"/>
<div class="price">$50</div>
<div class="info">Add to basket</div>
How solve this problem?
You can use a wrapper div to catch the over, I think is more maintainable if you change the html in the future:
body {
font-family: 'Roboto', sans-serif;
}
.price {
font-size: 1em;
border-radius: 50px;
background-color: #f00;
text-align: center;
color: #fff;
padding: 5px;
width: 10%;
margin: 20px auto;
}
.price:hover {
background-color: #fff;
border: 1px solid #bfbfbf;
color: #f00;
cursor: pointer;
}
.info {
display: none;
width: 20%;
margin: 20px auto;
cursor: pointer;
}
.wrapper:hover .info {
display: block;
}
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"/>
<div class="wrapper">
<div class="price">$50</div>
<div class="info">Add to basket</div>
</div>
you can use :active to show text when mouse is clicking down on the price like so:
.price {
font-size: 1em;
border-radius: 50px;
background-color: #f00;
text-align: center;
color: #fff;
padding: 5px;
width: 10%;
margin: 20px auto;
}
.price:hover {
background-color: #fff;
border: 1px solid #bfbfbf;
color: #f00;
cursor: pointer;
}
.info {
display: none;
}
.price:active + .info {
display: block;
}
<div class="price">$50</div>
<div class="info">Add to basket</div>
if you are trying to show the text on :hover like the title of the question says the unser :hover like so:
.price {
font-size: 1em;
border-radius: 50px;
background-color: #f00;
text-align: center;
color: #fff;
padding: 5px;
width: 10%;
margin: 20px auto;
}
.price:hover {
background-color: #fff;
border: 1px solid #bfbfbf;
color: #f00;
cursor: pointer;
}
.info {
display: none;
}
.price:hover + .info {
display: block;
}
<div class="price">$50</div>
<div class="info">Add to basket</div>
Update 3:
.price {
font-size: 1em;
border-radius: 50px;
background-color: #f00;
text-align: center;
color: #fff;
padding: 5px;
width: 10%;
margin: 20px auto;
}
.price:hover {
background-color: #fff;
border: 1px solid #bfbfbf;
color: #f00;
cursor: pointer;
}
.info,
.price:hover .amount {
display: none;
}
.price:hover .info {
display: block;
}
<div class="price">
<div class="amount">$50</div>
<div class="info">Add to basket</div>
</div>

ul below the input element is not occupying the same width of the input element

I have a input text and below this input text I want to have a div with a green color that has some text. But I want that this green area to occupy the same with of the input element, but it is not working. Do you know what is not correct?
.main-search {
width: 100%;
position: relative;
background-color: yellow;
}
.search {
margin: 20px auto;
width: 100%;
font-size: 0.85em;
float: left;
background-color: white;
padding: 50px;
border-radius: 4px;
}
.search_auto_complete {
background-color: green;
text-align: left;
color: gray;
font-size: 1.1em;
border: 1px solid black;
z-index: 10;
position: fixed;
width: inherit;
padding: 0 !important;
}
.search_auto_complete li {
padding: 10px;
background-color: $color-white;
border-bottom: 1px solid $color-gray;
}
.auto_complete_category {
background-color: $color-light-plus !important;
color: $color-gray-plus-plus;
font-weight: bold;
}
.auto_complete_category:hover {
background-color: $color-light-plus !important;
cursor: auto !important;
color: $color-gray-plus-plus !important;
}
.events_auto_complete li:hover {
background-color: $color-primary;
cursor: pointer;
color: $color-white;
}
.search_events_box button {
border-left: none;
color: #fff;
font-weight: 700;
padding: 17px;
}
.input_search_event {
position: relative;
border: 1px solid red;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
outline: 0;
}
input {
padding-left: 40px !important;
}
input {
padding: 15px;
width: 100%;
}
.content {
float: left;
width: 100%;
}
.div {
width: 60%;
margin: 0 auto;
padding: 20px 0;
}
<section class="content main-search">
<div class="div">
<h1>Title</h1>
<div class="search">
<div class="input_search">
<input id="auto" value="category" type="text">
<ul class="search_auto_complete">
<li class="auto_complete_category">Categories</li>
<li>Category 1</li>
</ul>
</div>
</div>
</div>
</section>
Example: https://jsfiddle.net/k9uL4zon/
Add position: relative to your container .input_search and replace position: fixed with position: absolute and width: inherit with width: 100% for your element .search_auto_complete. Demo:
.input_search {
position: relative; /* new */
}
.main-search {
width: 100%;
position: relative;
background-color: yellow;
}
.search {
margin: 20px auto;
width: 100%;
font-size: 0.85em;
float: left;
background-color: white;
padding: 50px;
border-radius: 4px;
}
.search_auto_complete {
background-color: green;
text-align: left;
color: gray;
font-size: 1.1em;
border: 1px solid black;
z-index: 10;
position: absolute;
width: 100%;
padding: 0 !important;
}
.search_auto_complete li {
padding: 10px;
background-color: $color-white;
border-bottom: 1px solid $color-gray;
}
.auto_complete_category {
background-color: $color-light-plus !important;
color: $color-gray-plus-plus;
font-weight: bold;
}
.auto_complete_category:hover {
background-color: $color-light-plus !important;
cursor: auto !important;
color: $color-gray-plus-plus !important;
}
.events_auto_complete li:hover {
background-color: $color-primary;
cursor: pointer;
color: $color-white;
}
.search_events_box button {
border-left: none;
color: #fff;
font-weight: 700;
padding: 17px;
}
.input_search_event {
position: relative;
border: 1px solid red;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
outline: 0;
}
input {
padding-left: 40px !important;
}
input {
padding: 15px;
width: 100%;
}
.content {
float: left;
width: 100%;
}
.div {
width: 60%;
margin: 0 auto;
padding: 20px 0;
}
<section class="content main-search">
<div class="div">
<h1>Title</h1>
<div class="search">
<div class="input_search">
<input id="auto" value="category" type="text">
<ul class="search_auto_complete">
<li class="auto_complete_category">Categories</li>
<li>Category 1</li>
</ul>
</div>
</div>
</div>
</section>

How to prevent radio buttons from line breaking?

for a form I'm creating, I used css to display radio buttons as a toggle-switch.
However its not responsive at all. When viewing on mobile the "scale" 1-5 is broken up in pieces. My goal is to change the with of the scale responsively instead of it breaking up.
Somehow I can't seem to figure it out.
Here's the code: http://jsfiddle.net/7byse1vk/
.toggle__label {
transition: all .25s ease;
display: inline-block;
border-radius: 5px;
background-color: #878787;
cursor: pointer;
padding: 0 30px;
min-width: 100px;
height: 48px;
line-height: 48px;
color: #a7a6ae;
text-align: center;
}
Many thanks in advance!
You can use display: flex; on the parent, remove the min-width from the labels (otherwise that will make them at least 160px wide each, too wide for mobile), and set them to flex-grow: 1 so they fill the parent fluidly.
body {
padding: 20px;
}
.toggle {
margin-bottom: 20px;
}
.row {
display: flex;
}
.toggle__label {
transition: all .25s ease;
border-radius: 5px;
background-color: #878787;
cursor: pointer;
padding: 0 30px;
height: 48px;
line-height: 48px;
color: #a7a6ae;
text-align: center;
flex-grow: 1;
}
.toggle__input:checked + .toggle__label {
background-color: #e5702a;
color: white;
}
.toggle__label--left {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
}
.toggle__label--middle {
border-top-right-radius: 0;
border-bottom-right-radius: 0;
margin-left: -4px;
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
.toggle__label--right {
margin-left: -4px;
border-top-left-radius: 0;
border-bottom-left-radius: 0;
}
.toggle__input {
position: absolute;
left: -99999px;
}
label {
padding: 0.4em 2em 0.4em 0;
}
.toggle-btn-grp {
margin: 3px 0;
}
.toggle-btn {
text-align: centre;
margin: 5px 2px;
padding: 0.4em 3em;
color: #000;
background-color: #FFF;
border-radius: 10px;
display: inline-block;
border: solid 1px #CCC;
cursor: pointer;
}
.toggle-btn-grp.joint-toggle .toggle-btn {
margin: 5px 0;
padding: 0.4em 2em;
border-radius: 0;
border-right-color: white;
}
.toggle-btn-grp.joint-toggle .toggle-btn:first-child {
margin-left: 2px;
border-radius: 10px 0px 0px 10px;
}
.toggle-btn-grp.joint-toggle .toggle-btn:last-child {
margin-right: 2px;
border-radius: 0px 10px 10px 0px;
border-right: solid 1px #CCC;
}
.toggle-btn:hover {
border: solid 1px #a0d5dc !important;
background: #f1fdfe;
}
.toggle-btn.success {
background: lightgreen;
border: solid 1px green !important;
}
.visuallyhidden {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
.visuallyhidden.focusable:active,
.visuallyhidden.focusable:focus {
clip: auto;
height: auto;
margin: 0;
overflow: visible;
position: static;
width: auto;
}
/* CSS only version */
.toggle-btn-grp.cssonly * {
width: 140px;
height: 30px;
line-height: 30px;
}
.toggle-btn-grp.cssonly div {
display: inline-block;
position: relative;
margin: 5px 2px;
}
.toggle-btn-grp.cssonly div label {
position: absolute;
z-index: 0;
padding: 0;
text-align: center;
}
.toggle-btn-grp.cssonly div input {
position: absolute;
z-index: 1;
cursor: pointer;
opacity: 0;
}
.toggle-btn-grp.cssonly div:hover label {
border: solid 1px #a0d5dc !important;
background: #f1fdfe;
}
.toggle-btn-grp.cssonly div input:checked + label {
background: lightgreen;
border: solid 1px green !important;
}
<div class="row">
<input class="toggle__input" id="option-1" type="radio" name="input4" value="1">
<label class="toggle__label toggle__label--left" for="option-1">1</label>
<input checked="true" class="toggle__input" id="option-2" type="radio" name="input4" value="2">
<label class="toggle__label toggle__label--middle" for="option-2">2</label>
<input class="toggle__input" id="option-3" type="radio" name="input4" value="3">
<label class="toggle__label toggle__label--middle" for="option-3">3</label>
<input class="toggle__input" id="option-4" type="radio" name="input4" value="4">
<label class="toggle__label toggle__label--middle" for="option-4">4</label>
<input class="toggle__input" id="option-5" type="radio" name="input4" value="5">
<label class="toggle__label toggle__label--right" for="option-5">5</label>
</div>
Replace your CSS for the labels below and it'll work. This will scale them down properly. It's because at the moment they've got a minimum set width and are being stretch with the padding because they don't have box-sizing: border-box;.
.toggle__label {
transition: all .25s ease;
display: inline-block;
border-radius: 5px;
background-color: #878787;
cursor: pointer;
padding: 0 30px;
width: 20%;
height: 48px;
line-height: 48px;
color: #a7a6ae;
text-align: center;
float: left;
box-sizing: border-box;
}

Resources