How can I center tab labels in CSS? - html

How can I put the 3 label buttons (tab one, two and three) in the middle above the content? they are now aligned to the left. on top of that I would also appreciate a solution to hide the content in case a user is on his phone. I hope that this is not too hard.
.tabs {
position:absolute; bottom:0;
display: flex;
flex-wrap: wrap;
}
.tabs label {
order: 1;
display: block;
padding: 1rem 2rem;
margin-right: 0.2rem;
cursor: pointer;
background: #90CAF9;
font-weight: bold;
transition: background ease 0.2s;
}
.tabs .tab {
order: 99;
flex-grow: 1;
width: 100%;
display: none;
padding: 1rem;
background: #fff;
}
.tabs input[type="radio"] {
display: none;
}
.tabs input[type="radio"]:checked + label {
background: #fff;
}
.tabs input[type="radio"]:checked + label + .tab {
display: block;
}
#media (max-width: 45em) {
.tabs .tab,
.tabs label {
order: initial;
}
.tabs label {
width: 100%;
margin-right: 0;
margin-top: 0.2rem;
}
}
<div class="tabs">
<input type="radio" name="tabs" id="tabone" checked="checked">
<label for="tabone">Tab One</label>
<div class="tab">
<h1>Tab One Content</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<input type="radio" name="tabs" id="tabtwo">
<label for="tabtwo">Tab Two</label>
<div class="tab">
<h1>Tab Two Content</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<input type="radio" name="tabs" id="tabthree">
<label for="tabthree">Tab Three</label>
<div class="tab">
<h1>Tab Three Content</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>

Use Flexbox's justify-content: center on .tabs with width: 100% to align the tabs in center of the page, like:
.tabs {
width: 100%;
justify-content: center;
}
For closing tab on mobile devices, use jQuery to remove the checked attribute from #tabone, like:
/* If mobile, remove checked attribute (Media Query in JS) */
if (window.matchMedia("(max-width: 45em)").matches) {
$('#tabone').removeAttr('checked');
}
Have a look at the snippet below (use full page view below to see it in center):
/* If mobile, remove checked attribute */
if (window.matchMedia("(max-width: 45em)").matches) {
$('#tabone').removeAttr('checked');
}
.tabs {
position:absolute; bottom:0;
display: flex;
width: 100%;
justify-content: center;
flex-wrap: wrap;
}
.tabs label {
order: 1;
display: block;
padding: 1rem 2rem;
margin-right: 0.2rem;
cursor: pointer;
background: #90CAF9;
font-weight: bold;
transition: background ease 0.2s;
}
.tabs .tab {
order: 99;
flex-grow: 1;
width: 100%;
display: none;
padding: 1rem;
background: #fff;
}
.tabs input[type="radio"] {
display: none;
}
.tabs input[type="radio"]:checked + label {
background: #fff;
}
.tabs input[type="radio"]:checked + label + .tab {
display: block;
}
#media (max-width: 45em) {
.tabs .tab,
.tabs label {
order: initial;
}
.tabs label {
width: 100%;
margin-right: 0;
margin-top: 0.2rem;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tabs">
<input type="radio" name="tabs" id="tabone" checked="checked">
<label for="tabone">Tab One</label>
<div class="tab">
<h1>Tab One Content</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<input type="radio" name="tabs" id="tabtwo">
<label for="tabtwo">Tab Two</label>
<div class="tab">
<h1>Tab Two Content</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<input type="radio" name="tabs" id="tabthree">
<label for="tabthree">Tab Three</label>
<div class="tab">
<h1>Tab Three Content</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>
Hope this helps!

Related

pure css html tabs the click does not change

I created these cards in pure css and html I don't want to use javascript, but I don't understand why it doesn't work for me can you help me solve this problem of mine? thank you
.tabs {
display: flex;
flex-wrap: wrap;
}
.tabs label {
order: 1;
firstdisplay: block;
padding: 1rem 2rem;
margin-right: 0.2rem;
cursor: pointer;
background: #f9f9f9;
font-weight: bold;
transition: background ease 0.2s;
border-radius: 15px
}
.tabs .tab {
order: 99;
astflex-grow: 1;
width: 100%;
display: none;
padding: 1rem;
background: #fff;
}
.tabs input[type="radio"] {
display: none;
}
.tabs input[type="radio"]:checked+label {
background: #fff;
border-bottom: 1px solid #e2e2e2;
}
.tabs input[type="radio"]:checked+label+.tab {
display: block;
}
#media (max-width: 45em) {
.tabs .tab,
.tabs label {
order: initial;
}
.tabs label {
width: 100%;
margin-right: 0;
margin-top: 0.2rem;
}
}
.tabp {
line-height: 25px;
text-align: left;
padding: 5px;
font-size: 15px;
}
```
<div class="col-lg-12">
<div class="product__details__tab">
<ul class="nav nav-tabs tabs">
<li class="nav-item">
<input type="radio" name="tabs" id="tabone" checked="checked">
<label for="tabone">Tabella Uno</label>
</li>
<li class="nav-item">
<input type="radio" name="tabs" id="tabtwo">
<label for="tabtwo">Tabella Due</label>
</li>
<li class="nav-item">
<input type="radio" name="tabs" id="tabthree">
<label for="tabthree">Tab Tre</label>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane tab active" id="tabone">
<h6>Description</h6>
<p class="tabp">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<div class="tab-pane tab" id="tabtwo">
<h6>Specification</h6>
<p class="tabp">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<div class="tab-pane tab" id="tabthree">
<h6>Reviews ( 2 )</h6>
<p class="tabp">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>
</div>
</div>
You need to put radio inputs above tabs (you can hide them), so you can select tabs, also use id's for showing particular tabs:
.tabs {
display: flex;
flex-wrap: wrap;
}
.tabs label {
order: 1;
display: block;
padding: 1rem 2rem;
margin-right: 0.2rem;
cursor: pointer;
background: #f9f9f9;
font-weight: bold;
transition: background ease 0.2s;
border-radius: 15px
}
.tab {
order: 99;
flex-grow: 1;
width: 100%;
display: none;
padding: 1rem;
background: #fff;
}
.tabs input[type="radio"] {
display: none;
}
.tabs input[type="radio"]:checked+label {
background: #fff;
border-bottom: 1px solid #e2e2e2;
}
/* ADDED */
input[type="radio"] {
display: none;
}
#tabone:checked ~ .tab-content #tabone,
#tabtwo:checked ~ .tab-content #tabtwo ,
#tabthree:checked ~ .tab-content #tabthree {
display: block;
}
#media (max-width: 45em) {
.tabs .tab,
.tabs label {
order: initial;
}
.tabs label {
width: 100%;
margin-right: 0;
margin-top: 0.2rem;
}
}
.tabp {
line-height: 25px;
text-align: left;
padding: 5px;
font-size: 15px;
}
<div class="col-lg-12">
<div class="product__details__tab">
<input type="radio" name="tabs" id="tabone" checked="checked">
<input type="radio" name="tabs" id="tabtwo">
<input type="radio" name="tabs" id="tabthree">
<ul class="nav nav-tabs tabs">
<li class="nav-item">
<label for="tabone">Tabella Uno</label>
</li>
<li class="nav-item">
<label for="tabtwo">Tabella Due</label>
</li>
<li class="nav-item">
<label for="tabthree">Tab Tre</label>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane tab active" id="tabone">
<h6>Description</h6>
<p class="tabp">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<div class="tab-pane tab" id="tabtwo">
<h6>Specification</h6>
<p class="tabp">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<div class="tab-pane tab" id="tabthree">
<h6>Reviews ( 2 )</h6>
<p class="tabp">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>
</div>
</div>

Align all the list items

I want to align all the list items perfectly but I am not able to do it.
If you see the image since the second li has more text, it is causing them to not align.
I can't use text-overflow on the name of the person, unlike notes.
Margin and padding are also not working.
Please help me how to solve it .
#data-table .list {
display: flex;
align-items: center;
justify-content: space-between;
text-align: center;
color: #97A1A9;
font-size: .9rem;
white-space: nowrap;
}
#data-table .list>* {
padding: 1rem 2rem;
}
#data-table .list div:first-child {
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
#data-table .list div:first-child input[type="checkbox"] {
margin-right: 7px;
}
#data-table .list div:last-child {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
width: 221px;
}
<div id="data-table">
<ul>
<li class="list">
<div><input type="checkbox">Customer Name</div>
<div>Seller #</div>
<div>Bill #</div>
<div>Amount</div>
<div>Due Date</div>
<div>Bill Payment Date</div>
<div>Notes</div>
</li>
<!-- Dummy -->
<li class="list">
<div><input type="checkbox">Roshan Ahmad Shaheen</div>
<div>2523532</div>
<div> 73457346735</div>
<div>122.76k</div>
<div>23-Jan-2021</div>
<div> --</div>
<div>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Quasi, accusamus?</div>
</li>
<li class="list">
<div><input type="checkbox">Roshan Ahmad</div>
<div>2523532</div>
<div> 73457346735</div>
<div>122.76k</div>
<div>23-Jan-2021</div>
<div> --</div>
<div>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Quasi, accusamus?</div>
</li>
There is no way to align flexbox elements on different rows without setting fixed widths. Frankly this is tabular data and so you should be using a table.
Or CSS-Tables
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
::before,
::after {
box-sizing: inherit;
}
#data-table ul {
display: table;
}
#data-table .list {
display: table-row;
text-align: left;
color: #97A1A9;
font-size: .9rem;
white-space: nowrap;
}
#data-table .list>* {
padding: 1rem 2rem;
display: table-cell;
}
#data-table .list div:first-child input[type="checkbox"] {
margin-right: 7px;
}
<div id="data-table">
<ul>
<li class="list">
<div><input type="checkbox">Customer Name</div>
<div>Seller #</div>
<div>Bill #</div>
<div>Amount</div>
<div>Due Date</div>
<div>Bill Payment Date</div>
<div>Notes</div>
</li>
<!-- Dummy -->
<li class="list">
<div><input type="checkbox">Roshan Ahmad Shaheen</div>
<div>2523532</div>
<div> 73457346735</div>
<div>122.76k</div>
<div>23-Jan-2021</div>
<div> --</div>
<div>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Quasi, accusamus?</div>
</li>
<li class="list">
<div><input type="checkbox">Roshan Ahmad</div>
<div>2523532</div>
<div> 73457346735</div>
<div>122.76k</div>
<div>23-Jan-2021</div>
<div> --</div>
<div>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Quasi, accusamus?</div>
</li>

How to create accordion using checkbox and radio buttons

I am creating a project in which we can see the assignment and download the assignment given by college.
I used the accordion method for that....
In which 5-6 subject are there and when you click on that subject it expands and shows the assignment,from that expanded view you can download the document or view it.....
I am also adding some buttons around it like "Download" "view" "Mark as favorite" for that assignment.
I have used the checkbox for expanding the accordions, now the main problem is that When I Click anywhere on that subject 'div' it expands, but i want that it should expand only when user click on that "plus" icon ( shown at a right side )
So that all the operations like "View Document","Download Document","Mark as favorite" work on the buttons that added on the right side...
I just want that when someone click on '+' icon it should expand (Not when someone click on whole div)
Check the whole code at Codepen
#import url('https://fonts.googleapis.com/css2?family=Poppins:wght#200;300;400;500;600;700&display=swap');
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
body{
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
}
::selection{
background: rgb(0,123,255,0.3);
}
.wrapper{
max-width: 600px;
padding: 0 20px;
}
.wrapper .parent-tab,
.wrapper .child-tab{
margin-bottom: 8px;
border-radius: 3px;
box-shadow: 0px 0px 15px rgba(0,0,0,0.18);
}
.wrapper .parent-tab label,
.wrapper .child-tab label{
background: #007bff;
padding: 10px 20px;
display: flex;
align-items: center;
justify-content: space-between;
cursor: pointer;
border-radius: 3px;
position: relative;
z-index: 99;
transition: all 0.3s ease;
}
.wrapper .parent-tab label:hover{
background: #006fe6;
}
.parent-tab input:checked ~ label,
.child-tab input:checked ~ label{
border-radius: 3px 3px 0 0;
background: #006fe6;
}
.wrapper label span{
color: #fff;
font-size: 18px;
font-weight: 500;
text-shadow: 0 -1px 1px #0056b3;
}
.wrapper .child-tab label span{
font-size: 17px;
}
.parent-tab label .icon{
position: relative;
height: 30px;
width: 30px;
font-size: 15px;
color: #007bff;
display: block;
background: #fff;
border-radius: 50%;
text-shadow: 0 -1px 1px #0056b3;
}
.wrapper .child-tab label .icon{
height: 27px;
width: 27px;
}
.parent-tab label .icon i{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.parent-tab input:checked ~ label .icon i:before,
.child-tab input:checked ~ label .icon i:before{
content: '\f068';
}
.wrapper .parent-tab .content,
.wrapper .child-tab .sub-content{
max-height: 0px;
overflow: hidden;
background: #fff;
border-radius: 0 0 3px 3px;
transition: all 0.4s ease;
}
.parent-tab input:checked ~ .content,
.child-tab input:checked ~ .sub-content{
max-height: 100vh;
}
.tab-3 input:checked ~ .content{
padding: 15px 20px;
}
.parent-tab .content p,
.child-tab .sub-content p{
padding: 15px 20px;
font-size: 16px;
}
.child-tab .sub-content p{
font-size: 15px;
}
input[type="radio"],
input[type="checkbox"]{
display: none;
}
.num1{
text-align: center;
margin-bottom: 20px;
}
<script src="https://kit.fontawesome.com/a076d05399.js"></script>
<div class="wrapper">
<h2 class="num1">Assignment At Glance</h2>
<!-- Accordion Heading One -->
<div class="parent-tab">
<input type="checkbox" name="tab" id="tab-1" checked>
<label for="tab-1">
<span>Engineering Physics</span>
<div class="icon"><i class="fas fa-plus"></i></div>
</label>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing thelit. Quam, repellendus facere, id porro magnam blanditiiss quoteos dolores ratione quidem ipsam esse quos pariatur, repellat obcaecati!</p>
</div>
</div>
<!-- Accordion Heading Two -->
<div class="parent-tab">
<input type="checkbox" name="tab" id="tab-2">
<label for="tab-2">
<span>Engineering Graphics</span>
<div class="icon"><i class="fas fa-plus"></i></div>
</label>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing thelit. Quam, repellendus facere, id porro magnam blanditiiss quoteos dolores ratione quidem ipsam esse quos pariatur, repellat obcaecati!</p>
</div>
</div>
<!-- Accordion Heading Three -->
<div class="parent-tab tab-3">
<input type="checkbox" name="tab" id="tab-3">
<label for="tab-3" class="tab-3">
<span>PPS</span>
<div class="icon"><i class="fas fa-plus"></i></div>
</label>
<div class="content">
• Assignment Completed : 3 <br>
• Assignment Remaining : 1 <br>
<br>
<!-- Sub Heading One -->
<div class="child-tab">
<input type="checkbox" name="sub-tab" id="tab-4">
<label for="tab-4">
<span>Algorithms</span>
<div class="icon"><i class="fas fa-plus"></i></div>
</label>
<div class="sub-content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing thelit dolor. Utfacilis labore, exercitationem fuga minima a illo modi vitaerse dignissimos? Vero?</p>
</div>
</div>
<!-- Sub Heading Two -->
<div class="child-tab">
<input type="checkbox" name="sub-tab" id="tab-5">
<label for="tab-5">
<span>Python</span>
<div class="icon"><i class="fas fa-plus"></i></div>
</label>
<div class="sub-content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing thelit dolor. Utfacilis labore, exercitationem fuga minima a illo modi vitaerse dignissimos? Vero?</p>
</div>
</div>
</div>
</div>
<!-- Accordion Heading Four -->
<div class="parent-tab">
<input type="checkbox" name="tab" id="tab-6">
<label for="tab-6">
<span>Accordion Heading Four</span>
<div class="icon"><i class="fas fa-plus"></i></div>
</label>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing thelit. Quam, repellendus facere, id porro magnam blanditiiss quoteos dolores ratione quidem ipsam esse quos pariatur, repellat obcaecati!</p>
</div>
</div>
</div>
The Problem
When you are making the blue bars, they are wrapped in a label. When you click the label it is expanding the box because it checks the checkbox.
The Solution
All you need to do is to make make it so that the blue bar doesn't register clicks, but the icon does. To do this you can use the pointer-evens property. The code should look like this
.parent-tab label{
pointer-events: none;
}
.parent-tab .icon{
pointer-events: auto;
}
Disclaimer: I didn't come up with this answer, #ManirajMurugan did. (See the first comment on the question).

Make keyboard tab action act as mouse hover

I am using a slightly modified version of this: https://codepen.io/ferry/pen/ZYVwxz (Thank you Michael Ferry).
HTML:
<div class="accordion">
<ul>
<li tabindex="0">
<div>
<a href="#">
<h2>Lorem Ipsum 1</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
</a>
</div>
</li>
<li tabindex="0">
<div>
<a href="#">
<h2>Lorem Ipsum 2</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
</a>
</div>
</li>
<li tabindex="0">
<div>
<a href="#">
<h2>Lorem Ipsum 3</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
</a>
</div>
</li>
<li tabindex="0">
<div>
<a href="#">
<h2>Lorem Ipsum 4</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
</a>
</div>
</li>
<li tabindex="0">
<div>
<a href="#">
<h2>Lorem Ipsum 5</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
</a>
</div>
</li>
<li tabindex="0">
<div>
<a href="#">
<h2>Lorem Ipsum 6</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
</a>
</div>
</li>
</ul>
</div>
CSS:
<style>
.accordion {
width: 100%;
max-width: 1140px;
height: 560px;
overflow: hidden;
margin: 20px auto;
}
.accordion ul {
width: 100%;
display: table;
table-layout: fixed;
margin: 0;
padding: 0;
}
.accordion ul li {
list-style-type: none;
display: table-cell;
top:0px;
vertical-align: top;
position: relative;
width: 16.666%;
height: 520px;
background-repeat: no-repeat;
background-position: top center;
transition: all 500ms ease;
}
.accordion ul li div {
display: block;
overflow: hidden;
width: 100%;
}
.accordion ul li div a {
display: block;
height: 520px;
width: 100%;
position: relative;
z-index: 3;
vertical-align: bottom;
padding: 0px;
box-sizing: border-box;
color: #fff;
text-decoration: none;
transition: all 200ms ease;
}
.accordion ul li div a * {
opacity: 0;
margin: 0;
width: 100%;
text-overflow: ellipsis;
position: relative;
z-index: 5;
white-space: nowrap;
overflow: hidden;
-webkit-transform: translateX(-20px);
transform: translateX(-20px);
-webkit-transition: all 400ms ease;
transition: all 400ms ease;
}
.accordion ul li div a h2 {
text-overflow: clip;
font-size: 24px;
/*text-transform: uppercase;*/
margin-bottom: 0px;
top: 435px;
color:#000;
}
.accordion ul li div a p {
top: 440px;
font-size: 13.5px;
color:#000;
}
.accordion ul li:nth-child(1) {
background-image: url("1b.jpg");max-height:400px;
}
.accordion ul li:nth-child(2) {
background-image: url("2b.jpg");max-height:400px;
}
.accordion ul li:nth-child(3) {
background-image: url("3b.jpg");max-height:400px;
}
.accordion ul li:nth-child(4) {
background-image: url("4b.jpg");max-height:400px;
}
.accordion ul li:nth-child(5) {
background-image: url("5b.jpg");max-height:400px;
}
.accordion ul li:nth-child(6) {
background-image: url("6b.jpg");max-height:400px;
}
.accordion ul:hover li {
width: 16%;
}
.accordion ul:hover li:hover {
width: 100%;
}
.accordion ul:hover li:hover a {
}
.accordion ul:hover li:hover a * {
opacity: 1;
-webkit-transform: translateX(0);
transform: translateX(0);
}
#media screen and (max-width: 600px) {
body {
margin: 0;
}
.accordion {
height: auto;
}
.accordion ul li, .accordion ul li:hover, .accordion ul:hover li, .accordion ul:hover li:hover {
position: relative;
display: table;
table-layout: fixed;
width: 100%;
-webkit-transition: none;
transition: none;
}
}
</style>
I can't get it to display the "slide" when using the keyboard.
I have tried adding .active, :focus, role="button", aria-expanded="true" and tab-index to no avail.
Before I go bald, does anyone have a solution? (without using javascript)
Thanks so much!
If you use tabindex="1" on your li like <li tabindex="1">, you should be able to target them with :focus as
li {
&:focus {
// your stuff here
}
}
Right now what happened is when you press tab it selects the a element but the transformation is applied on the li.
If I take the code from Michael Ferry's code pen you will have the following lines.
HTML:
<h1>Responsive Accordion</h1>
<div class="accordion">
<ul>
<li tabindex="1">
<div>
<a href="#">
<h2>Lorem Ipsum</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
</a>
</div>
</li>
<li tabindex="1">
<div>
<a href="#">
<h2>Lorem Ipsum</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
</a>
</div>
</li>
<li tabindex="1">
<div>
<a href="#">
<h2>Lorem Ipsum</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
</a>
</div>
</li>
<li tabindex="1">
<div>
<a href="#">
<h2>Lorem Ipsum</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
</a>
</div>
</li>
<li tabindex="1">
<div>
<a href="#">
<h2>Lorem Ipsum</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
</a>
</div>
</li>
<li tabindex="1">
<div>
<a href="#">
<h2>Lorem Ipsum</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
</a>
</div>
</li>
</ul>
</div>
<p class="about">
By Michael Ferry
</p>
<h1>Responsive Accordion</h1>
<div class="accordion">
<ul>
<li tabindex="1">
<div>
<a href="#">
<h2>Lorem Ipsum</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
</a>
</div>
</li>
<li tabindex="1">
<div>
<a href="#">
<h2>Lorem Ipsum</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
</a>
</div>
</li>
<li tabindex="1">
<div>
<a href="#">
<h2>Lorem Ipsum</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
</a>
</div>
</li>
<li tabindex="1">
<div>
<a href="#">
<h2>Lorem Ipsum</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
</a>
</div>
</li>
<li tabindex="1">
<div>
<a href="#">
<h2>Lorem Ipsum</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
</a>
</div>
</li>
<li tabindex="1">
<div>
<a href="#">
<h2>Lorem Ipsum</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
</a>
</div>
</li>
</ul>
</div>
<p class="about">
By Michael Ferry
</p>
SCSS:
#import url(https://fonts.googleapis.com/css?family=Open+Sans);
#import url(https://fonts.googleapis.com/css?family=Montserrat:700);
// Accordion Height
$a-height: 250px;
// Position text along bottom
$text-offset: $a-height - 90;
// Page Title
h1 {
text-align: center;
font-family: Montserrat, sans-serif;
color: #333;
}
.accordion {
width: 100%;
max-width: 1080px;
height: $a-height;
overflow: hidden;
margin: 50px auto;
ul {
width: 100%;
display: table;
table-layout: fixed;
margin: 0;
padding: 0;
li {
display: table-cell;
vertical-align: bottom;
position: relative;
width: 16.666%; // 6 into 100
height: $a-height;
background-repeat: no-repeat;
background-position: center center;
transition: all 500ms ease;
div {
display: block;
overflow: hidden;
width: 100%;
a {
display: block;
height: $a-height;
width: 100%;
position: relative;
z-index: 3;
vertical-align: bottom;
padding: 15px 20px;
box-sizing: border-box;
color: #fff;
text-decoration: none;
font-family: Open Sans, sans-serif;
transition: all 200ms ease;
* {
opacity: 0;
margin: 0;
width: 100%;
text-overflow: ellipsis;
position: relative;
z-index: 5;
white-space: nowrap;
overflow: hidden;
-webkit-transform: translateX(-20px);
transform: translateX(-20px);
-webkit-transition: all 400ms ease;
transition: all 400ms ease;
}
h2 {
font-family: Montserrat, sans-serif;
text-overflow: clip;
font-size: 24px;
text-transform: uppercase;
margin-bottom: 2px;
top: $text-offset;
}
p {
top: $text-offset;
font-size: 13.5px;
}
}
}
}
// Background images
li:nth-child(1) {
background-image: url("http://michael-ferry.com/assets/accordion1.jpg");
}
li:nth-child(2) {
background-image: url("http://michael-ferry.com/assets/accordion2.jpg");
}
li:nth-child(3) {
background-image: url("http://michael-ferry.com/assets/accordion3.jpg");
}
li:nth-child(4) {
background-image: url("http://michael-ferry.com/assets/accordion4.jpg");
}
li:nth-child(5) {
background-image: url("http://michael-ferry.com/assets/accordion5.jpg");
}
li:nth-child(6) {
background-image: url("http://michael-ferry.com/assets/accordion6.jpg");
}
&:hover li,
li:focus ~ li{
width: 8%;
}
&:hover li:hover,
li:focus {
width: 60%;
a {
background: rgba(0, 0, 0, 0.4);
* {
opacity: 1;
-webkit-transform: translateX(0);
transform: translateX(0);
}
}
}
}
}
// Stack items
#media screen and (max-width: 600px) {
// IE gets fussy if this isn't here
body {
margin: 0;
}
.accordion {
height: auto;
ul,
ul:hover {
li,
li:hover {
position: relative;
display: table;
table-layout: fixed;
width: 100%;
-webkit-transition: none;
transition: none;
}
}
}
}
.about {
text-align: center;
font-family: "Open Sans", sans-serif;
font-size: 12px;
color: #666;
a {
color: blue;
text-decoration: none;
&:hover {
text-decoration: underline;
}
}
}
In the SCSS code I have added li:focus ~ li to target li's siblings and li:focus to the existing code to reproduce the animation.
I also recommend you to disable a element tabbing with tabindex=0 in order to avoid the user to scroll top again after tabbing through the slider.
Focus within fix this issue, and works fine
:hover, :focus-within {
your css
}
you can check MDN to learn more about focus-within

css selector doesn't set opacity on selected id

I'm experimenting with a tabbed navigation made of radio buttons.
My problem is that when I click on the tab the selected id's opacity isn't change so the content doesn't get visible.
My HTML code:
<div id="container">
<div id="tabs">
<input id="tab-1" type="radio" name="tab-group" checked="checked" />
<label for="tab-1">Tab 1</label>
<input id="tab-2" type="radio" name="tab-group" />
<label for="tab-2">Tab 2</label>
<input id="tab-3" type="radio" name="tab-group" />
<label for="tab-3">Tab 3</label>
</div>
<div id="content">
<div id="content-1">
<form>
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname">
</form>
</div>
<div id="content-2">
<p class="column-left"><img src="http://ximg.es/200x150" alt="">Lorem ipsum dolor sit amet, consectetur adipisicing elit. </p>
<p class="column-right"><img src="http://ximg.es/200x150" alt="">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<div id="content-3">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. </p>
<ul>
<li>Lorem ipsum dolor sit amet, consectetur adipisicing it.</li>
<li>Lorem ipsum dolor sit amet, consectetur adipisicing it.</li>
<li>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</li>
</ul>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. </p>
</div>
</div>
My CSS code:
* {
font-family: Arial, sans;
margin: 0;
padding: 0;
box-sizing: border-box;
-moz-box-sizing: border-box;
}
#container {
padding-top: 6em;
margin: 0 auto;
width: 50%;
}
#tabs input {
height: 2.5em;
visibility: hidden;
}
#tabs label {
background: #f9f9f9;
border-radius: .25em .25em 0 0;
color: red;
cursor: pointer;
display: block;
float: left;
font-size: 1em;
height: 2.5em;
line-height: 2.5em;
margin-right: .25em; /*space between tabs*/
padding: 0 1.5em;
text-align: center;
}
#tabs input:hover + label {
background: #ddd;
color: lawngreen;
}
#tabs input:checked + label {
background: black;
color: white;
position: relative;
z-index: 6;
}
#content {
background: red;
border-radius: 0 .25em .25em .25em;
min-height: 20em;
position: relative;
width: 100%;
z-index: 5;
}
#content div {
opacity: 0;
padding: 1.5em;
position: absolute;
z-index: -100;
transition: all linear 0.1s;
}
#tabs input#tab-1:checked ~ #content #content-1,
#tabs input#tab-2:checked ~ #content #content-2,
#tabs input#tab-3:checked ~ #content #content-3 {
opacity: 1;
z-index: 100;
}
input.visible {
visibility: visible !important;
}
I think the problem lies with the 3 selector lines "#tabs input#tab-1:checked ~ #content #content-1"
I've tried to rewrite these line in different syntax bu nothing works.
Please help.
The div that matches #content is not a sibling of any of the inputs. It is a sibling of their parent.
My aunt is not my sister.
You can't draw a link from the inputs to #content-X with CSS selectors. There is no parent combinator.
You would need to use JavaScript for this.
* {
font-family: Arial, sans;
margin: 0;
padding: 0;
box-sizing: border-box;
-moz-box-sizing: border-box;
}
#container {
padding-top: 6em;
margin: 0 auto;
width: 50%;
}
#tabs input {
height: 2.5em;
visibility: hidden;
}
#tabs label {
background: #f9f9f9;
border-radius: .25em .25em 0 0;
color: red;
cursor: pointer;
display: block;
float: left;
font-size: 1em;
height: 2.5em;
line-height: 2.5em;
margin-right: .25em; /*space between tabs*/
padding: 0 1.5em;
text-align: center;
}
#tabs input:hover + label {
background: #ddd;
color: lawngreen;
}
#tabs input:checked + label {
background: black;
color: white;
position: relative;
z-index: 6;
}
#content {
background: red;
border-radius: 0 .25em .25em .25em;
min-height: 20em;
position: relative;
width: 100%;
z-index: 5;
overflow-y: auto;
}
#content div {
opacity: 0;
padding: 1.5em;
position: absolute;
z-index: -100;
transition: all linear 0.1s;
}
#tabs input#tab-1:checked ~ #content div#content-1,
#tabs input#tab-2:checked ~ #content div#content-2,
#tabs input#tab-3:checked ~ #content div#content-3 {
opacity: 1;
z-index: 1000;
}
input.visible {
visibility: visible !important;
}
<div id="container">
<div id="tabs">
<input id="tab-1" type="radio" name="tab-group" checked="checked" />
<label for="tab-1">Tab 1</label>
<input id="tab-2" type="radio" name="tab-group" />
<label for="tab-2">Tab 2</label>
<input id="tab-3" type="radio" name="tab-group" />
<label for="tab-3">Tab 3</label>
<div id="content">
<div id="content-1">
<form>
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname">
</form>
</div>
<div id="content-2">
<p class="column-left"><img src="http://ximg.es/200x150" alt="">Lorem ipsum dolor sit amet, consectetur adipisicing elit. </p>
<p class="column-right"><img src="http://ximg.es/200x150" alt="">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<div id="content-3">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. </p>
<ul>
<li>Lorem ipsum dolor sit amet, consectetur adipisicing it.</li>
<li>Lorem ipsum dolor sit amet, consectetur adipisicing it.</li>
<li>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</li>
</ul>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. </p>
</div>
</div>
</div>
You can if you slightly change your html.
Basically, to make #content siblings #tabs, I moved #tabs closing div to the end. Hope this helps.
Check the fiddle here