how to fix a problem with radio input costumized with css? - css

I am doing an input radio customized with everything I learned about it step by step.
When I click on the older/standard radio inputs (without hiding them yet) everything is fine, I click on the old radio buttons and the new customized respective is checked.
But when I click on the newly customized radio buttons, they don't work as supposed to be.
The problem is that when I click the 2nd and 3rd customized radio it checks the 1rst automatically.
I really need your help because I don't know where I am failing. I already look from examples codes on the internet and it was exactly what I did so I don't really understand where is the problem that I should correct.
.content {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.items {
display: flex;
}
label {
display: flex;
align-items: center;
margin-left: 1rem;
}
.radio__span {
width: 1.2rem;
height: 1.2rem;
border-radius: 50%;
border: 3px solid #049372;
margin-right: .5rem;
display: flex;
align-items: center;
justify-content: center;
}
.radio:checked+.radio__span::before {
content: "";
display: block;
width: .8rem;
height: .8rem;
border-radius: 50%;
background-color: #049372;
}
/* .radio{
width:0;
height:0;
opacity:0;
visibility:hidden;
} */
<div class="content">
<div class="items">
<label for="chk">
<input type="radio" name="dev" class="radio" id="chk">
<span class="radio__span"></span>
<p>Frontend</p>
</label>
<label for="chk">
<input type="radio" name="dev" class="radio" id="chk">
<span class="radio__span"></span>
<p> Backend</p>
</label>
<label for="chk">
<input type="radio" name="dev" class="radio" id="chk">
<span class="radio__span"></span>
<p>Fullstack</p>
</label>
</div>
</div>
I expect to click on the new radio buttons and be checked.

IDs must be unique. Once fixed, you need to then update your label elements to point to the new IDs. Fix that and it works fine.
.content {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.items {
display: flex;
}
label {
display: flex;
align-items: center;
margin-left: 1rem;
}
.radio__span {
width: 1.2rem;
height: 1.2rem;
border-radius: 50%;
border: 3px solid #049372;
margin-right: .5rem;
display: flex;
align-items: center;
justify-content: center;
}
.radio:checked+.radio__span::before {
content: "";
display: block;
width: .8rem;
height: .8rem;
border-radius: 50%;
background-color: #049372;
}
/* .radio{
width:0;
height:0;
opacity:0;
visibility:hidden;
} */
<div class="content">
<div class="items">
<label for="chk1">
<input type="radio" name="dev" class="radio" id="chk1">
<span class="radio__span"></span>
<p>Frontend</p>
</label>
<label for="chk2">
<input type="radio" name="dev" class="radio" id="chk2">
<span class="radio__span"></span>
<p> Backend</p>
</label>
<label for="chk3">
<input type="radio" name="dev" class="radio" id="chk3">
<span class="radio__span"></span>
<p>Fullstack</p>
</label>
</div>
</div>

That is happening because of your for attribute which is looking for an input with an Id named chk, as you have got all three checkboxes with the same ID, it is checking the first...
The following would work:
.content {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.items {
display: flex;
}
label {
display: flex;
align-items: center;
margin-left: 1rem;
}
.radio__span {
width: 1.2rem;
height: 1.2rem;
border-radius: 50%;
border: 3px solid #049372;
margin-right: .5rem;
display: flex;
align-items: center;
justify-content: center;
}
.radio:checked+.radio__span::before {
content: "";
display: block;
width: .8rem;
height: .8rem;
border-radius: 50%;
background-color: #049372;
}
<div class="content">
<div class="items">
<label for="chk">
<input type="radio" name="dev" class="radio" id="chk">
<span class="radio__span"></span>
<p>Frontend</p>
</label>
<label for="chk1">
<input type="radio" name="dev" class="radio" id="chk1">
<span class="radio__span"></span>
<p> Backend</p>
</label>
<label for="chk2">
<input type="radio" name="dev" class="radio" id="chk2">
<span class="radio__span"></span>
<p>Fullstack</p>
</label>
</div>
</div>

Related

Flex & space-around are intersecting the content

I have a container that's scrolling horizionally with checkboxes in it. When a box is checked, a text apears. I want to keep the functionality of the items growing from both sides. The thing is that when some checkboxes are checked, the first one is hidden and you cannot scroll to it:
.container {
white-space: nowrap;
text-align: center;
display: flex;
overflow-x: scroll;
justify-content: space-around;
align-items: stretch;
}
.item {
transition: 0.5s ease;
height: 40px;
justify-content: center;
text-align: center;
}
label {
font-size: 0;
transition: 0.5s ease;
}
input:checked~label {
font-size: 20px;
}
<div class="container">
<div class="item">
<input type="checkbox" id="1"></input>
<label for="1">1111111111111111111111111</label>
</div>
<div class="item">
<input type="checkbox" id="1"></input>
<label for="2">22222222222222222222222222222222222</label>
</div>
<div class="item">
<input type="checkbox" id="1"></input>
<label for="3">333333333333333333333333333333</label>
</div>
</div>
Is there a way to keep the transition coming from the center, but still let the user scrolling all the way to the start and the end?
Thanks!
Just done by 2 actions:
Remove the justify-content: space-around; from your .container
Add margin:auto; to your .item.
(Pay attention, className got replaced by class, as the CSS does not recognize the className)
DEMO
.container {
white-space: nowrap;
text-align: center;
display: flex;
overflow-x: scroll;
/*justify-content: space-around;*/
align-items: stretch;
}
.item {
transition: 0.5s ease;
height: 40px;
justify-content: center;
text-align: center;
margin:auto;
}
label {
font-size: 0;
transition: 0.5s ease;
}
input:checked~label {
font-size: 20px;
}
<div class="container">
<div class="item">
<input type="checkbox" id="1" />
<label for="1">1111111111111111111111111</label>
</div>
<div class="item">
<input type="checkbox" id="1" />
<label for="2">22222222222222222222222222222222222</label>
</div>
<div class="item">
<input type="checkbox" id="1" />
<label for="3">333333333333333333333333333333</label>
</div>
</div>
Ooh!!
That's a good one..
It took months from me to solve before.
It's very easy and tricky bro.
Just make your justify-content: flex-start; and then add this flex-grow: 1; for your children which are .item which will seams like a space-around effect.
That's the code:
.container {
white-space: nowrap;
text-align: center;
overflow-x: scroll;
display: flex; align-items: stretch;
justify-content: flex-start; /* ••• this one ••• */
}
.item {
transition: 0.5s ease;
height: 40px; text-align: center;
display: flex; justify-content: center;
flex-grow: 1; /* ••• and this one ••• */
}
label { font-size: 0; transition: 0.5s ease; }
input:checked~label { font-size: 20px; }
<div class="container">
<div class="item">
<input type="checkbox" id="1"></input>
<label for="1">1111111111111111111111111</label>
</div>
<div class="item">
<input type="checkbox" id="2"></input>
<label for="2">22222222222222222222222222222222222</label>
</div>
<div class="item">
<input type="checkbox" id="3"></input>
<label for="3">333333333333333333333333333333</label>
</div>
</div>
justify-content: space-around; from the container and use justify-content: space-between; instead.

How to make adaptive centered div?

What I have now:
display: flex;
align-items: center;
flex-direction: column;
How it looks with error:
What's the target:
I can do it with
margin-left: *X*px, but it is not the way I want to solve it.
May I do it with some flex properties ?
It is important to do it without bootstrap and grid.
This could be a possible solution: Center the box with flexbox and display the errors with position: absolute;. You need to take care of responsive optimization if you want to use it on a wider range of devices.
* {
box-sizing: border-box;
}
.container {
display: flex;
align-items: center;
justify-content: center;
}
.box {
border: 1px solid black;
padding: 10px;
width: 400px;
}
.field {
position: relative;
}
.input {
margin-bottom: 10px;
width: 100%;
}
.error {
position: absolute;
left: 105%;
top: 0;
width: 200px;
}
<div class="container">
<div class="box">
<div class="field">
<input class="input" type="text">
<div class="error">error 1</div>
</div>
<div class="field">
<input class="input" type="text">
<div class="error">error 2</div>
</div>
<button>Send</button>
</div>
</div>

Modificators with BEM naming convency

I like to use BEM naming convency in some of my projects nad now, I'm working on design system and I stopped with my modificators.
I created all version of Input that we use in our application, and I have this case:
I modify that Input few times: --is-invalid, --is-disabled, --has-status and I have problem with the has-status one. I have more colors for this status and I am not sure how to deal with it.
I would prefer my modificator for status be like .Input--has-status-load, .Input--has-status-warning etc. But the problem is, that I don't want to repeat declaration of this modificator X times, for each status. That's why I added classes just for colors (.success and .warning), but this little hacked my BEM naming convency that I don't want to demolish.
Do you have any idea? Or do you have any notes on that code?
Thanks for sharing your knowledge!
CodePen DEMO
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
font-family: 'Source Sans Pro', sans-serif;
}
::placeholder {
color: #ccc;
opacity: 1;
/* Firefox */
}
:-ms-input-placeholder {
/* Internet Explorer 10-11 */
color: #ccc;
}
::-ms-input-placeholder {
color: #ccc;
}
.container {
max-width: 1080px;
margin: 0 auto;
}
.row {
margin: 1em;
}
h2 {
margin-top: 2em;
}
/* ============================================================================
05 - COMPONENTS - Input.scss
========================================================================= */
/* Component structure:
.Input {}
.Input__container {}
.Input__fake-input {}
.Input__field {}
.Input__prefix {}
.Input__suffix {}
.Input__icon {}
.Input--has-status {}
.Input--is-invalid {}
.Input--is-disabled {}
*/
// Component local variables
$input-border-color: #D8DADC;
$input-text-gray: #979797;
$input-text-color: #457CCC;
$input-border-radius: 8px;
$input-font-size: 16px;
// Base structure
.Input {
position: relative;
}
// Input container for all elements
.Input__container {
display: flex;
position: relative;
flex-direction: row;
-webkit-box-align: center;
align-items: center;
-webkit-box-pack: justify;
justify-content: space-between;
height: 40px;
font-size: $input-font-size;
}
// Input fake input, who replaces real input
.Input__fake-input {
width: 100%;
position: absolute;
z-index: 1;
top: 0px;
left: 0px;
height: 40px;
font-size: $input-font-size;
border-radius: $input-border-radius;
transition: all 0.15s ease-in-out 0s;
border-width: 1px;
border-style: solid;
border-color: $input-border-color;
border-image: none;
}
// Input text field for add text
.Input__field {
border: 0;
-webkit-appearance: none;
color: $input-text-color;
font-size: inherit;
background-color: transparent;
width: 100%;
height: 100%;
z-index: 2;
padding: 0px 12px;
flex: 1 1 20%;
border-radius: $input-border-radius;
&:focus {
outline: none;
&~.Input__fake-input {
outline: none;
border: 1px solid #0284FF;
}
}
}
// Input type PREFIX
.Input__prefix {
height: 100%;
color: $input-text-gray;
display: flex;
-webkit-box-align: center;
align-items: center;
pointer-events: none;
-webkit-box-pack: center;
justify-content: center;
z-index: 3;
padding: 0px 0px 0px 10px;
}
// Input type SUFFIX
.Input__suffix {
height: 100%;
color: $input-text-gray;
display: flex;
flex-shrink: 0;
-webkit-box-align: center;
align-items: center;
-webkit-box-pack: center;
justify-content: center;
z-index: 3;
padding: 0px 10px 0px 0;
}
// Input type ICON
.Input__icon {
height: calc(100% - 2px);
color: $input-text-gray;
display: flex;
flex-shrink: 0;
-webkit-box-align: center;
align-items: center;
-webkit-box-pack: center;
justify-content: center;
z-index: 3;
padding: 0px 10px;
position: relative;
width: 48px;
border-left: 1px solid $input-border-color;
cursor: pointer;
& img,
svg {
width: 25px;
}
}
// Modificator --has-status
.Input--has-status {
position: relative;
}
.Input--has-status::after {
content: '';
position: absolute;
top: -4px;
right: -4px;
z-index: 3;
width: 10px;
height: 10px;
border-radius: 100%;
background: #1A73E8;
}
// --has-status types
.Input--has-status.success::after {
background: #37C9AD;
}
.Input--has-status.warning::after {
background: #FFB000;
}
// Modificator --is-invalid
.Input--is-invalid {
& .Input__fake-input {
border: 1px solid #FF0054;
}
& .Input__field {
color: #FF0054;
&:focus~.Input__fake-input {
border-color: #FF0054;
}
}
}
// Modificator --is-disabled
.Input--is-disabled {
& .Input__fake-input {
background-color: #E8EAEC;
border-color: #E8EAEC;
color: #868C92;
ursor: not-allowed;
border: 1px solid #E8EAEC;
}
& .Input__field {
color: #868C92;
cursor: not-allowed;
}
}
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,700,900&display=swap" rel="stylesheet">
<div class="container">
<form>
<div class="row">
<h1> Types</h1>
<h2>Input - default</h2>
<div class="Input">
<div class="Input__container">
<input class="Input__field" type="text" placeholder="Placeholder">
<div class="Input__fake-input"></div>
</div>
</div>
<h2>Input - prefix</h2>
<div class="Input">
<div class="Input__container">
<div class="Input__prefix">
<span class="Prefix">Prefix</span>
</div>
<input class="Input__field" type="text" placeholder="Placeholder" value="Lorem ipsum dolor">
<div class="Input__fake-input"></div>
</div>
</div>
<h2>Input - suffix</h2>
<div class="Input">
<div class="Input__container">
<input class="Input__field" type="text" placeholder="Placeholder" value="Lorem ipsum dolor">
<div class="Input__suffix">
Suffix
</div>
<div class="Input__fake-input"></div>
</div>
</div>
<h2>Input - icon</h2>
<div class="Input">
<div class="Input__container">
<input class="Input__field" type="text" placeholder="Placeholder">
<div class="Input__icon">
<img src="https://image.flaticon.com/icons/svg/331/331383.svg">
</div>
<div class="Input__fake-input"></div>
</div>
</div>
<br><br>
<h1>Modificators</h1>
<h2>Input - Load Success</h2>
<div class="Input Input--has-status success">
<div class="Input__container">
<input class="Input__field" type="text" placeholder="Placeholder">
<div class="Input__fake-input"></div>
</div>
</div>
<h2>Input - Load Warning</h2>
<div class="Input Input--has-status warning">
<div class="Input__container">
<input class="Input__field" type="text" placeholder="Placeholder">
<div class="Input__fake-input"></div>
</div>
</div>
<h2>Input - Invalid</h2>
<div class="Input Input--is-invalid">
<div class="Input__container">
<input class="Input__field" type="text" placeholder="Placeholder" value="Lorem ipsum dolor">
<div class="Input__fake-input"></div>
</div>
</div>
<h2>Input - Disabled</h2>
<div class="Input Input--is-disabled">
<div class="Input__container">
<input class="Input__field" type="text" placeholder="Placeholder" value="Lorem ipsum" disabled>
<div class="Input__icon">
<img src="https://image.flaticon.com/icons/svg/331/331383.svg">
</div>
<div class="Input__fake-input"></div>
</div>
</div>
</div>
</form>
</div>

CSS Grid layout for a form

I have a div named main-filter and within the div, I have two div. Now, I want to create a form with button, labels and input and I want to put two div inside the form and it should look like the below down screenshot. I have the HTML and CSS code but it is not working and couldn't figure out how to do it with CSS grid. I need to make it responsive as well!. I am totally new at CSS grid and any help would much be appreciated. Thank you.
.main-filter {
display: flex;
margin: 0px;
padding: 20px;
height: 90px;
background-color: #eff0f2;
color: #287993;
margin-bottom: 3rem;
}
.main-header__left {
display: grid;
grid-template-columns: auto 1fr;
grid-gap: 0.3em 0.6em;
grid-auto-flow: dense;
align-items: center;
float: left;
margin-left: 250px;
margin-right: 200px;
}
.main-header__right {
display: grid;
grid-template-columns: auto 1fr;
grid-gap: 0.3em 0.6em;
grid-auto-flow: dense;
align-items: center;
float: right;
}
.filter-button1 {
background: #38acd2;
color: #FFFFFF;
max-width: 380px;
min-width: 370px;
min-height: 35px;
font-weight: bold;
}
input,
button {
grid-column: 2 / 4;
width: auto;
margin: 0;
}
input {
width: 370px;
height: 30px;
border: 1px solid #38acd2;
}
<div className="main-filter">
<form onSubmit={this.addItem}>
<div className="main-header__left">
<label className="start-timestamp">Start Timestamp: </label>
<input type="text" ref={(input4)=> this.input4 = input4} className="input-item" id="addInput3" placeholder="Ex. 2019-1-12" />
<label className="source-ip-address">Source IP Address: </label>
<input type="text" name="source" ref={(input1)=> this.input1 = input1} className="input-item" id="addInput3" placeholder="127.0.0.1 " />
<label className="service">Service Name: </label>
<input type="text" name="service" ref={(input3)=> this.input3 = input3} className="input-item" id="addInput3" placeholder="Ex. Facebook or Whatsapp *" required />
</div>
<div className="main-header__right">
<label className="end-timestamp">End Timestamp: </label>
<input type="text" ref={(input5)=> this.input5 = input5} className="input-item" id="addInput3" placeholder="Ex. 2019-1-12" />
<label className="destinatin-ip-ddress">Destination IP Address:
</label>
<input type="text" name="destination" ref={(input2)=> this.input2 = input2} className="input-item" id="addInput3" placeholder="127.0.0.1 " />
<button type="submit" className="filter-button1">Apply
Filters</button>
</div>
</form>
</div>
css grid form Image screenshot,
Try using below code, I have modified HTML and CSS code, I hope this might help you to achieve expected result.
.main-filter {
/* margin: 0px; */
/* padding: 20px; */
height: 90px;
background-color: #eff0f2;
color: #287993;
}
form {
display: grid;
grid-template-columns: auto auto;
grid-gap: 15px;
}
.main-header__left>div,
.main-header__right>div {
display: flex;
}
.main-header__left>div>label,
.main-header__right>div>label {
width: 40%;
}
#media only screen and (max-width: 500px) {
/* .main-header__left {
grid-area: 2 / span 1;
grid-gap: 0;
} */
.main-header__right {
grid-area: 2 / span 1;
grid-gap: 0;
}
}
.filter-button1 {
background: #38acd2;
color: #FFFFFF;
min-height: 35px;
font-weight: bold;
border: none;
margin-top: 18px;
width: 100%;
}
input {
width: 100%;
height: 30px;
border: 1px solid #38acd2;
}
<div className="main-filter">
<form onSubmit={this.addItem}>
<div class="main-header__left">
<div>
<label class="start-timestamp">Start Timestamp: </label>
<input type="text" />
</div>
<div>
<label className="source-ip-address">Source IP Address: </label>
<input type="text" name="source" />
</div>
<div>
<label className="service">Service Name: </label>
<input type="text" name="service" />
</div>
</div>
<div class="main-header__right">
<div>
<label className="end-timestamp">End Timestamp: </label>
<input type="text" />
</div>
<div>
<label className="destinatin-ip-ddress">Destination IP Address:</label>
<input type="text" name="destination" />
</div>
<div>
<label></label>
<button type="submit" class="filter-button1">Apply Filters</button>
</div>
</div>
</form>
</div>

Checked radio buttons misaligned when using custom CSS

I'm implementing some custom styles for radio buttons and am having problems trying to perfectly center the checked buttons. When I paste the exact same code into JS Fiddle, things work perfectly. Yet in my web app, they are each off centered (in their own way) like the image below, despite having the same classnames, positioning, etc:
Image link: Off-centered checked radio buttons
HTML:
<div class="RadioStyled">
<label class="control-label">
<input
class="form-control"
type="radio"
ref="check1"
>
<span class="radio-styled checked"></span>
<span class="label-text">Australia</span>
</label>
<div class="RadioStyled">
</div>
</div>
<div class="RadioStyled">
<label class="control-label">
<input
class="form-control"
type="radio"
ref="check2"
>
<span class="radio-styled checked"></span>
<span class="label-text">Belgium</span>
</label>
<div class="RadioStyled">
</div>
</div>
<div class="RadioStyled">
<label class="control-label">
<input
class="form-control"
type="radio"
ref="check3"
>
<span class="radio-styled checked"></span>
<span class="label-text">United Kingdom</span>
</label>
<div class="RadioStyled">
</div>
</div>
<div class="some-other-element">
<div class="RadioStyled">
<label class="control-label">
<input
class="form-control"
type="radio"
ref="check3"
>
<span class="radio-styled checked"></span>
<span class="label-text">New Zealand</span>
</label>
<div class="RadioStyled">
</div>
</div>
<div class="RadioStyled">
<label class="control-label">
<input
class="form-control"
type="radio"
ref="check3"
>
<span class="radio-styled checked"></span>
<span class="label-text">Canada</span>
</label>
<div class="RadioStyled">
</div>
</div>
</div>
CSS / SCSS:
.RadioStyled {
display: inline-block;
padding-right: 25px;
label.control-label {
cursor: pointer;
display: block;
line-height: 24px;
.label-text {
position: relative;
font-size: 14px;
color: black;
padding-left: 6px;
vertical-align: middle;
}
input[type="checkbox"], input[type="radio"] {
display: none;
}
.radio-styled {
border-radius: 50%;
border: 1px solid grey;
box-sizing: border-box;
display: inline-block;
height: 20px;
vertical-align: middle;
width: 20px;
}
.radio-styled.checked {
border: 1px solid grey;
background-color: green;
border-radius: 50%;
box-sizing: border-box;
display: inline-block;
width: 20px;
height: 20px;
vertical-align: middle;
}
.radio-styled.checked::before {
background-color: white;
border-radius: 50%;
box-sizing: border-box;
content: '';
display: inline-block;
height: 20px;
width: 20px;
// margin: -1px -1px;
transform: scale(0.5);
position: relative;
top: -1px;
right: 1px;
}
}
}
.some-other-element {
padding-left: 250px;
width: 400px;
}
Codepen here
Any idea what's happening here that makes each checked radio button seem to have a different position? Thanks!

Resources