1

I have created a tab and calling a function on click I am facing difficulty to write CSS as per below requirement.

1.active tab should be shown with blue underline color.

2.on click of the tab the blueline should come under the active tab. (it is like similar to angular material tabs) but I don't want to use angular material

Below is the tabs created :

<div class="tab">
<button class="tablinks" (click)="function1()">Tab1</button>
<button class="tablinks" (click)="function2()">Tab2</button>
</div>

Below is the CSS trying:

.tab {
  overflow: hidden;
  border-bottom: 1px solid #ccc;
  background-color: white;
}

.tab button {
  background-color: inherit;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 12px 14px;
  transition: 0.3s;
  font-size: 17px;
}

.tab button:hover {
  background-color: #ddd;
}

.tab button.active {
  background-color: blue;
}

.tabcontent {
  display: block;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}

it is great if any one helps, Thanks

Pruthvi Raj
  • 363
  • 5
  • 10

3 Answers3

1

The example below uses a bottom-border to get the effect you would like. The border color is white by default, grey on hover and blue when active.

The jquery code is fully commented.

Let me know if this isn't what you were hoping for.


Demo

// Add click event to all tablinks
$(".tablinks").click(function() {

  // Remove active class from any other active tabs
  $(".tablinks.active").removeClass("active");

  // Add active class to the selected tab
  $(this).addClass("active");

});
.tab {
  overflow: hidden;
  border-bottom: 1px solid #ccc;
  background-color: white;
}

.tab button {
  background-color: inherit;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 12px 14px;
  transition: 0.3s;
  font-size: 17px;
  border-bottom: 3px solid white;
}

.tab button:hover {
  border-bottom-color: #ddd;
  background-color: #ddd;
}

.tab button.active {
  border-bottom-color: blue;
}

.tabcontent {
  display: block;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="tab">
  <button class="tablinks" (click)="function1()">Tab1</button>
  <button class="tablinks" (click)="function2()">Tab2</button>
</div>
Oliver Trampleasure
  • 3,293
  • 1
  • 10
  • 25
1

1). Here's a stackblitz demo: https://stackblitz.com/edit/angular-rkcm9m

2). And the code fragment:

export class AppComponent  {
  name = 'Angular';
  selectedTab = "Tab1";

  makeActive(tab: string) {
    this.selectedTab = tab;
  }
}
.tab {
  overflow: hidden;
  border-bottom: 1px solid #ccc;
  background-color: white;
}

.tab button {
  background-color: inherit;
  border-bottom: 2px solid transparent;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 12px 14px;
  transition: 0.3s;
  font-size: 17px;
}

.tab button:hover {
  background-color: #ddd;
}

.tab button.active {
  border-bottom: 2px solid blue;
}

.tabcontent {
  display: block;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}
<div class="tab">
  <button class="tablinks" [ngClass]="{'active': (selectedTab == 'Tab1') }" (click)="makeActive('Tab1')">Tab1</button>
  <button class="tablinks" [ngClass]="{'active': (selectedTab == 'Tab2') }" (click)="makeActive('Tab2')">Tab1</button>
</div>
Hunor
  • 449
  • 1
  • 5
  • 19
  • your example is working fine the only issue is while switching between the tabs bottom underline is moving up and down, any solution for this, thanks for your help – Pruthvi Raj Jun 14 '19 at 13:00
  • 1
    I have updated the stackblitz example - I have just added a transparent border for both of the buttons: https://stackblitz.com/edit/angular-rkcm9m – Hunor Jun 14 '19 at 13:14
1

Just simple add your css with:

.tab {
  overflow: hidden;
  border-bottom: 1px solid #ccc;
  background-color: white;
}

.tab button {
  background-color: inherit;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 12px 14px;
  transition: 0.3s;
  font-size: 17px;
}

.tab button:hover {
  background-color: #ddd;
}

.tab button.active {
  background-color: blue;
}

.tabcontent {
  display: block;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}
.tablinks:focus {border-bottom:1.5px solid blue;}
<div class="tab">
<button class="tablinks" (click)="function1()">Tab1</button>
<button class="tablinks" (click)="function2()">Tab2</button>
</div>
uniak
  • 64
  • 9