I found an example of CSS tabs here;
I am using the third example "target". However, when I attempted to add a FOURTH tab, everything breaks down, even with the first 3 (working) tabs. My attempt is here;
I believe the following original CSS codes need to be adjusted, which I do not know how to make sense of;
/* Target Tabs */
.tabs-target span:nth-of-type(1):not(:target) ~ span:nth-of-type(2):not(:target) ~ .tab:nth-of-type(1),
.tabs-target span:nth-of-type(2):target ~ .tab:nth-of-type(2),
.tabs-target span:nth-of-type(1):target ~ .tab:nth-of-type(3)
{
display: block;
}
What do I need to adjust to get the FOURTH tab to function properly?
This works:
.tabs-target span:nth-of-type(1):not(:target) ~ span:nth-of-type(2):not(:target) ~ span:nth-of-type(3):not(:target) ~ span:nth-of-type(4):not(:target) ~ .tab:nth-of-type(1),
.tabs-target span:nth-of-type(4):target ~ .tab:nth-of-type(1),
.tabs-target span:nth-of-type(3):target ~ .tab:nth-of-type(2),
.tabs-target span:nth-of-type(2):target ~ .tab:nth-of-type(3),
.tabs-target span:nth-of-type(1):target ~ .tab:nth-of-type(4) {
display: block;
}
Sample fiddle
By this you can add any number of tabs by just adding in html
HTML
<div class="tabs">
<div class="tab">
<input type="radio" id="tab-1" name="tab-group-1" checked>
<label for="tab-1">Tab One</label>
<div class="content">
Tab One
</div>
</div>
<div class="tab">
<input type="radio" id="tab-2" name="tab-group-1">
<label for="tab-2">Tab Two</label>
<div class="content">
Tab Two
</div>
</div>
<div class="tab">
<input type="radio" id="tab-3" name="tab-group-1">
<label for="tab-3">Tab Three</label>
<div class="content">
Tab Three
</div>
</div>
<div class="tab">
<input type="radio" id="tab-4" name="tab-group-1">
<label for="tab-4">Tab Four</label>
<div class="content">
Tab Four
</div>
</div>
</div>
<-- to add more tabs just copy the .... fully and change its value accordingly. -->
CSS
.tabs {
position: relative;
min-height: 200px; /* This part sucks */
clear: both;
margin: 25px 0;
}
.tab {
float: left;
}
.tab label {
background: #eee;
padding: 10px;
border: 1px solid #ccc;
margin-left: -1px;
position: relative;
left: 1px;
}
.tab [type=radio] {
display: none;
}
.content {
position: absolute;
top: 28px;
left: 0;
background: white;
right: 0;
bottom: 0;
padding: 20px;
border: 1px solid #ccc;
}
[type=radio]:checked ~ label {
background: white;
border-bottom: 1px solid white;
z-index: 2;
}
[type=radio]:checked ~ label ~ .content {
z-index: 1;
}
DEMO
Related
I basically need the tab header which current holds values 1,2,3 I would like changed to a favicon logo but because they are label tabs it wont display here is my code:
<link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css">
<section class="home" id="home">
<div class="home-text">
<h1>Horrizion Travel</h1>
</div>
<div class="my-tabs">
<input type="radio" id="tabfree" name="my-tabs" checked="checked">
<label for="tabfree" >1</label>
<div class="tab">
<h2>Air Travel</h2>
<P>This is Tab 1</P>
</div>
<input type="radio" id="tab-2" name="my-tabs">
<label for="tab-2">2</label>
<div class="tab">
<h2>Coach Travel</h2>
<P>This is Tab 2</P>
</div>
<input type="radio" id="tab-3" name="my-tabs">
<label for="tab-3">3</label>
<div class="tab">
<h2>Train Travel</h2>
<P>This is Tab 3</P>
</div>
</div>
</div>
</section>
I added and linked the favicon but it deletes the other tabs when I input it. I think this is because it selects the next lable in order to check it as they are css tabs.
<i class="fa-solid fa-tickets-airline"></i>
`
#dest .my-tabs{
display: flex;
flex-wrap: wrap;
max-width: 1400px;
margin: 50px auto;
padding: 25px;
}
#dest .my-tabs input[type="radio"] {
display: none;
}
#dest .my-tabs label{
padding: 25px;
background-color: #5a7184;
font-weight: bold;
}
#dest .my-tabs .tab{
width:100% ;
padding: 20px;
background-color: #5a7184;
order: 1;
display: none;
}
#dest .my-tabs h2{
font-size: 2em;
}
#dest .my-tabs input[type="radio"]:checked + label + .tab{
display: block;
}
#dest .my-tabs input[type="radio"]:checked + label{
background: var(--main-color);
}
`
How can I make these radio buttons responsive? I'm not sure what I'm doing wrong. I'd like to be able to click the grid item each option is in and have it behave like a radio button, but I'm not sure what I'm missing. Any recommendations for streamlining this code would also be extremely helpful. Thanks!
body {
font-family: Arial, Helvetica;
font-size: 3vh;
background-color: #b0b0b0;
}
.wrapper {
width: 50%;
margin-left: 25%;
margin-right: 25%;
display: grid;
grid-template-columns: 100px 100px 100px;
grid-gap: 20px;
grid-auto-rows: 100px;
justify-content: center;
background-color: ;
}
.item {
border-radius: 50px;
color: white;
padding: 10px;
display: flex;
align-items: center;
justify-content: center;
}
.item input {
opacity: 0;
position: absolute;
/*this makes it not interfere with the text location*/
}
.item:nth-child(odd) {
background: gray;
}
.item:nth-child(odd):hover {
background: limegreen;
cursor: pointer;
}
.item:nth-child(odd):label {}
.wrapper h2 {
grid-row: 1;
grid-column: 1/-1;
}
<div id="trialDiv" style="font-size: 13pt;">
<div id="TrialQuestions">
<div id="Questions" class="wrapper">
<h2 style="text-align:center"><i>What emotion was the face expressing? </i></h2>
<div id="gap" class="item"></div>
<div id="HappyButton" class="item">
<label>
<p><input class="responseButton" id="emotion_1" name="emotion" onclick = "GrabEmotionClickTime()" type="radio" value="1" /> Happiness</p>
</label>
</div>
<div id="gap" class="item"></div>
<div id="AngryButton" class="item">
<label>
<p><input class="responseButton" id="emotion_2" name="emotion" onclick = "GrabEmotionClickTime()"type="radio" value="2" /> Anger</p>
</label>
</div>
<div id="gap" class="item"></div>
<div id="FearButton" class="item">
<label>
<p><input class="responseButton" id="emotion_4" name="emotion" onclick = "GrabEmotionClickTime()"type="radio" value="3" /> Fear</p>
</label>
</div>
<div id="gap" class="item"></div>
<div id="NeutralButton" class="item">
<label>
<p><input class="responseButton" id="emotion_4" name="emotion" onclick = "GrabEmotionClickTime()"type="radio" value="4" /> Neutral</p>
</label>
</div>
</div>
</div>
</div>
Without doing all the work for you, your arrangement of elements is incorrect. Here is a simple example:
https://codepen.io/seanstopnik/pen/8a2ca693e9fe1f1334b921a6d75dbe99
/* For demo only */
body {
font-family: sans-serif;
padding: 40px;
}
/* Emotion button */
.emotion-button {
position: relative;
margin: 20px; /* For demo only */
}
.emotion-button__input {
position: absolute;
left: -9999px;
}
.emotion-button__label {
display: inline-block;
width: 100px;
height: 100px;
text-align: center;
line-height: 100px;
border-radius: 50%;
background-color: #ccc;
cursor: pointer;
transition: all 0.15s ease-in-out;
}
.emotion-button__input:checked ~ .emotion-button__label {
color: #fff;
background-color: green;
}
<div class="emotion-button">
<input id="r1" class="emotion-button__input" type="radio" name="emotion"/>
<label for="r1" class="emotion-button__label">Happiness</label>
</div>
<div class="emotion-button">
<input id="r2" class="emotion-button__input" type="radio" name="emotion"/>
<label for="r2" class="emotion-button__label">Anger</label>
</div>
Same idea as #SeanStopnik, did all the work for you:
body {
font-family: Arial, Helvetica;
font-size: 13pt;
background-color: #b0b0b0;
}
#Questions h2 {
font-style: italic;
}
.wrapper {
width: 50%;
margin-left: 25%;
margin-right: 25%;
display: grid;
grid-template-columns: 100px 100px 100px;
grid-gap: 20px;
grid-auto-rows: 100px;
justify-content: center;
background-color: ;
}
.item label {
display: block;
border-radius: 50%;
color: white;
padding: 10px;
display: flex;
align-items: center;
justify-content: center;
height: 80px;
background: gray;
}
.item label:hover {
background: lime;
}
.item input {
display: none;
}
.item input:checked + label {
background: blue;
}
.wrapper h2 {
grid-row: 1;
grid-column: 1/-1;
}
<div id="trialDiv">
<div id="TrialQuestions">
<div id="Questions" class="wrapper">
<h2 style="text-align:center">What emotion was the face expressing?</h2>
<div class="gap"></div>
<div id="HappyButton" class="item">
<input class="responseButton" id="emotion_1" name="emotion" type="radio" value="1" />
<label for="emotion_1">Happiness</label>
</div>
<div class="gap"></div>
<div id="AngryButton" class="item">
<input class="responseButton" id="emotion_2" name="emotion" type="radio" value="2" />
<label for="emotion_2">Anger</label>
</div>
<div class="gap"></div>
<div id="FearButton" class="item">
<input class="responseButton" id="emotion_3" name="emotion" type="radio" value="3" />
<label for="emotion_3">Fear</label>
</div>
<div class="gap"></div>
<div id="NeutralButton" class="item">
<input class="responseButton" id="emotion_4" name="emotion" type="radio" value="4" />
<label for="emotion_4">Neutral</label>
</div>
</div>
</div>
</div>
Explanation:
radio inputs are made invisible
they are placed right before their labels
labels are associated with them using for="id" attribute, so that clicking the label will check the radio button
I'm using the CSS pseudoclass :checked to target checked radio for styling
the CSS operator + allows to target the label right after the checked radio
Also:
you shouldn't have javascript event handlers in your HTML. Better use addEventListener from javascript for that. Your onclick made the click fail.
you shouldn't repeat ids in html
you shouldn't style your fonts inline
you shouldn't use <i> to make your titles italic. You should use CSS for that
Use font-style: italic; for the h2 element, nowdays <i> is used to insert icons
You can the id value only once in your HTML! Can't give more than one element the same id.
By clicking on the label we check the input, so we hide the input and style the label : width + height = 100%
We give all the inputs the same name to prevent checking more than one input[radio].
AddEventListener for all the inputs to toggle the class active on each one item (depends on which is the checked one).
Print to the console the id of the input that is checked.
var inputs = document.querySelectorAll('.item label');
for (let i = 0; i < inputs.length; i++){
inputs[i].addEventListener('click', (e) => {
checkEmotion(inputs[i], e);
});
}
function checkEmotion(input, e){
for(let i = 0; i < inputs.length; i++){
inputs[i].parentElement.classList.remove('active');
}
e.target.parentElement.classList.add('active');
console.log(input.nextElementSibling.getAttribute("id"));
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, Helvetica;
background-color: #b0b0b0;
}
h2 {
margin: 20px 0;
font-style: italic;
}
.wrapper {
display: flex;
justify-content: center;
}
.item {
border-radius: 50px;
color: white;
display: flex;
align-items: center;
justify-content: center;
width: 90px;
height: 90px;
margin: 0 10px;
background: gray;
position: relative;
overflow: hidden;
transition: all 0.2s;
}
.item.active {
background: #121212;
}
.item input {
opacity: 0;
display: absolute;
height: 0;
width: 0;
}
.item label {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
}
.item:hover {
background: limegreen;
}
<h2 style="text-align:center">What emotion was the face expressing?</h2>
<div class="wrapper">
<div class="item">
<label for="emotion-happy">Happy</label>
<input type="radio" id="emotion-happy" name="emotion">
</div>
<div class="item">
<label for="emotion-sad">Sad</label>
<input type="radio" id="emotion-sad" name="emotion">
</div>
<div class="item">
<label for="emotion-angry">Angry</label>
<input type="radio" id="emotion-angry" name="emotion">
</div>
<div class="item">
<label for="emotion-confused">Confused</label>
<input type="radio" id="emotion-confused" name="emotion">
</div>
</div>
I'm having a problem align my custom radio button and text (see image)
I've use css for my radio buttons and can't seem to align them .
here's my HTML :
label.item {
display: inline-block;
position: relative;
}
label.item > input {
visibility: hidden;
position: absolute;
}
label.item > .radio-image::before {
content: url("../tools/unchecked.png");
}
label.item > input[type="radio"]:checked + .radio-image::before {
content: url("../tools/checked.png");
}
label.item > input[type="radio"]:hover + .radio-image::before{
cursor: pointer;
}
<div class = "container">
<div class = "form-group">
<label>Body</label>
<label class="item">
<input type="radio" value="Good/Minor Flaws" name="size" required="required" />
<div class="radio-image text-center">Good / Minor Flaws</div>
</label>
<label class="item">
<input type="radio" value="Major Damage" name="size" required="required"/>
<div class="radio-image text-center">Major Damage </div>
</label>
</div>
</div>
https://jsfiddle.net/h2q7y4uj/#&togetherjs=ADTFQvN22E
Here you go with a solution https://jsfiddle.net/h2q7y4uj/4/
div.item {
display: inline-block;
position: relative;
}
div.item > input {
visibility: hidden;
position: absolute;
}
div.item > .radio-image::before {
content: url(http://via.placeholder.com/50x50);
}
div.item > input[type="radio"]:checked + .radio-image::before {
content: url(http://via.placeholder.com/51x51);
}
div.item > input[type="radio"]:hover + .radio-image::before {
cursor: pointer;
}
.radio-image {
position: relative;
width: 200px;
}
.radio-image > span{
position: absolute;
top: 45%;
transform: translateY(-50%);
}
<div class="container">
<div class="form-group">
<label>Body</label>
<div class="item">
<input type="radio" value="Good/Minor Flaws" name="size" required="required" />
<div class="radio-image text-center">
<span>Good / Minor Flaws</span></div>
</div>
<div class="item">
<input type="radio" value="Major Damage" name="size" required="required" />
<div class="radio-image text-center">
<span>Major Damage</span> </div>
</div>
</div>
</div>
Hope this will help you.
Just update following css code
.radio-image {
display: inline-block;
line-height: 30px;
}
label.item>.radio-image::before {
content: url("../tools/unchecked.png");
float: left;
line-height: 30px;
display: inline-block;
padding-right: 5px;
}
I have this source code. It's supposed to switch between tabs after clicking on any of the buttons. Everything works fine but when I try to wrap buttons in div it stops working and it doesnt display content anymore.
.main {
margin: 0 auto;
min-width: 320px;
max-width: 800px;
}
.content {
background: #fff;
color: #373737;
}
.content>div {
display: none;
padding: 20px 25px 5px;
}
input {
display: none;
}
label {
display: inline-block;
padding: 15px 25px;
font-weight: 600;
text-align: center;
}
label:hover {
color: #fff;
cursor: pointer;
}
input:checked+label {
background: #ed5a6a;
color: #fff;
}
#tab1:checked~.content #content1,
#tab2:checked~.content #content2,
#tab3:checked~.content #content3,
#tab4:checked~.content #content4 {
display: block;
}
<div class="main">
<input id="tab1" type="radio" name="tabs" checked>
<label for="tab1">111</label>
<input id="tab2" type="radio" name="tabs">
<label for="tab2">222</label>
<input id="tab3" type="radio" name="tabs">
<label for="tab3">333</label>
<input id="tab4" type="radio" name="tabs">
<label for="tab4">444</label>
<div class="content">
<div id="content1">
<p>
111
</p>
</div>
<div id="content2">
<p>
222
</p>
</div>
<div id="content3">
<p>
333
</p>
</div>
<div id="content4">
<p>
444
</p>
</div>
</div>
</div>
The code above is working, but I would like to wrap this part in div:
<input id="tab1" type="radio" name="tabs" checked>
<label for="tab1">111</label>
<input id="tab2" type="radio" name="tabs">
<label for="tab2">222</label>
<input id="tab3" type="radio" name="tabs">
<label for="tab3">333</label>
<input id="tab4" type="radio" name="tabs">
<label for="tab4">444</label>
The issue is that you are changing the document structure and this selector:
#tab1:checked ~ .content #content1,
#tab2:checked ~ .content #content2,
#tab3:checked ~ .content #content3,
#tab4:checked ~ .content #content4 {
display: block;
}
no longer matches the content areas. I can't think of a selector that would work because the addition of the new div breaks the sibling relationship between the tabs and the content.
However, you can add your div and make the code much simpler if you abandon the hidden radio buttons and the labels and just use a elements that target the content area that is supposed to be displayed. Then the CSS :target pseudo-class can be used to display the current content area that is the target of the clicked link.
Additionally, it's not clear why you have the actual content in a p that is inside of the div. That doesn't seem necessary.
<!doctype html>
<html lang="en">
<head>
<style>
.main {margin: 0 auto; min-width: 320px; max-width: 800px;}
.content {background: #fff; color: #373737;}
.content > div {display: none; padding: 20px 25px 5px;}
a {display: inline-block; padding: 15px 25px; font-weight: 600; text-align: center;}
a:hover {color: #fff; cursor: pointer;}
a:link {background: #ed5a6a; color: #fff; text-decoration:none; }
.content > :target
{
display: block;
}
</style>
</head>
<body>
<div class="main">
<div id="parent">
111
222
333
444
</div>
<div class="content">
<div id="content1">111</div>
<div id="content2">222</div>
<div id="content3">333</div>
<div id="content4">444</div>
</div>
</div>
</body>
</html>
I grabbed a simple tab control from the css tricks site and its working great for me, except when I have two tab controls on the same page, clicking the bottom control simply changes the tab page of the top control.
The CSS Tricks page with the original code is here
Changing the name= attribute for the second group doesn't fix it.
Renaming the id's of each tab in the second group also doesn't fit it.
Is there a way to have two independently operating tabs on the same page ?
FWIW the HTML and CSS are fairly simple:
.tabs {
position: relative;
min-height: 100px;
clear: both;
margin: 25px 0;
}
.tabs .tab {
float: left;
}
.tabs .tab label {
background: #eee;
padding: 5px 15px 5px 15px;
border: 1px solid #ccc;
position: relative;
left: 1px;
cursor: pointer;
}
.tabs .tab [type=radio] {
display: none;
}
.tabs .content {
position: absolute;
top: 18px;
left: 0;
background: white;
right: 0;
bottom: 0;
padding: 20px;
border: 1px solid #ccc;
overflow:auto;
}
.tabs [type=radio]:checked ~ label {
background: white;
border-bottom: 1px solid white;
z-index: 2;
}
.tabs [type=radio]:checked ~ label ~ .content {
z-index: 1;
}
<div class="tabs">
<div class="tab">
<input type="radio" id="tab-1" name="tab-group-1" checked>
<label for="tab-1">Tab One</label>
<div class="content">
stuff1
</div>
</div>
<div class="tab">
<input type="radio" id="tab-2" name="tab-group-1">
<label for="tab-2">Tab Two</label>
<div class="content">
stuff2
</div>
</div>
<div class="tab">
<input type="radio" id="tab-3" name="tab-group-1">
<label for="tab-3">Tab Three</label>
<div class="content">
stuff3
</div>
</div>
</div>
<div class="tabs">
<div class="tab">
<input type="radio" id="tab-1" name="tab-group-1" checked>
<label for="tab-1">Tab One</label>
<div class="content">
stuff4
</div>
</div>
<div class="tab">
<input type="radio" id="tab-2" name="tab-group-1">
<label for="tab-2">Tab Two</label>
<div class="content">
stuff5
</div>
</div>
<div class="tab">
<input type="radio" id="tab-3" name="tab-group-1">
<label for="tab-3">Tab Three</label>
<div class="content">
stuff6
</div>
</div>
</div>
The part to change is the label and the id:
<input type="radio" id="tab-1" name="tab-group-1" checked>
<label for="tab-1">Tab One</label>
I've edited your snippet.
.tabs {
position: relative;
min-height: 100px;
clear: both;
margin: 25px 0;
}
.tabs .tab {
float: left;
}
.tabs .tab label {
background: #eee;
padding: 5px 15px 5px 15px;
border: 1px solid #ccc;
position: relative;
left: 1px;
cursor: pointer;
}
.tabs .tab [type=radio] {
display: none;
}
.tabs .content {
position: absolute;
top: 18px;
left: 0;
background: white;
right: 0;
bottom: 0;
padding: 20px;
border: 1px solid #ccc;
overflow:auto;
}
.tabs [type=radio]:checked ~ label {
background: white;
border-bottom: 1px solid white;
z-index: 2;
}
.tabs [type=radio]:checked ~ label ~ .content {
z-index: 1;
}
<div class="tabs">
<div class="tab">
<input type="radio" id="tab-1" name="tab-group-1" checked>
<label for="tab-1">Tab One</label>
<div class="content">
stuff1
</div>
</div>
<div class="tab">
<input type="radio" id="tab-2" name="tab-group-1">
<label for="tab-2">Tab Two</label>
<div class="content">
stuff2
</div>
</div>
<div class="tab">
<input type="radio" id="tab-3" name="tab-group-1">
<label for="tab-3">Tab Three</label>
<div class="content">
stuff3
</div>
</div>
</div>
<div class="tabs">
<div class="tab">
<input type="radio" id="tab-4" name="tab-group-1" checked>
<label for="tab-4">Tab One</label>
<div class="content">
stuff4
</div>
</div>
<div class="tab">
<input type="radio" id="tab-5" name="tab-group-1">
<label for="tab-5">Tab Two</label>
<div class="content">
stuff5
</div>
</div>
<div class="tab">
<input type="radio" id="tab-6" name="tab-group-1">
<label for="tab-6">Tab Three</label>
<div class="content">
stuff6
</div>
</div>
</div>