0

I have a active class that I applied border

.tabs-nav .tab-active a {
  border-bottom: 3px solid red;
  cursor: default;
}

But the problem is the border looks like it's outside the box when I hover, how to make it inside? means I want a straight line for the tab items with active border.

demo https://jsfiddle.net/eztskazd/

Jane Emelia
  • 477
  • 2
  • 5
  • 11
  • Add `box-sizing:border-box;`. https://jsfiddle.net/eztskazd/2/. – deEr. Apr 11 '18 at 06:55
  • Possible duplicate of [Placing border inside of div and not on its edge](https://stackoverflow.com/questions/9601357/placing-border-inside-of-div-and-not-on-its-edge) – Andreas Storvik Strauman Apr 11 '18 at 06:58
  • Another way, worse but works, is to create a border bottom of the color of the background and change the color on hover. But box-sizing is better unless you need to target really old browsers like IE7 (hope you don't): https://caniuse.com/#search=box-sizing – Sergio Tx Apr 11 '18 at 07:14

4 Answers4

1

You will need to set the box-sizing:border-box to the element to tell that any padding and border values to the element will be included to the element width and height.

// From http://learn.shayhowe.com/advanced-html-css/jquery

// Change tab class and display content
$('.tabs-nav a').on('click', function(event) {
  event.preventDefault();

  $('.tab-active').removeClass('tab-active');
  $(this).parent().addClass('tab-active');
  $('.tabs-stage div').hide();
  $($(this).attr('href')).show();
});

$('.tabs-nav a:first').trigger('click'); // Default
* {
  box-sizing: border-box;
}

ul,
li,
div {
  background: hsla(0, 0%, 0%, 0);
  border: 0;
  font-size: 100%;
  margin: 0;
  outline: 0;
  padding: 0;
  vertical-align: baseline;
  font: 13px/20px "Droid Sans", Arial, "Helvetica Neue", "Lucida Grande", sans-serif;
  text-shadow: 0 1px 0 hsl(0, 100%, 100%);
}

li {
  display: list-item;
  text-align: -webkit-match-parent;
}

.tabs-nav li :hover {
  background: #eee;
}

.tabs-nav {
  list-style: none;
  margin: 0;
  padding: 0;
}

.tabs-nav li:first-child a {
  border-right: 0;
}

.tabs-nav .tab-active a {
  border-bottom: 3px solid red;
  cursor: default;
}

.tabs-nav a {
  background: hsl(120, 11%, 96%);
  color: hsl(215, 6%, 57%);
  display: block;
  font-size: 11px;
  font-weight: bold;
  height: 40px;
  line-height: 44px;
  text-align: center;
  text-transform: uppercase;
  width: 140px;
}

.tabs-nav li {
  float: left;
}

.tabs-stage {
  border: 1px solid hsl(210, 6%, 79%);
  -webkit-border-radius: 0 0 6px 6px;
  -moz-border-radius: 0 0 6px 6px;
  -ms-border-radius: 0 0 6px 6px;
  -o-border-radius: 0 0 6px 6px;
  border-radius: 0 0 6px 6px;
  border-top: 0;
  clear: both;
  margin-bottom: 20px;
  position: relative;
  top: -1px;
  width: 281px;
}

.tabs-stage p {
  margin: 0;
  padding: 20px;
  color: hsl(0, 0%, 33%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="tabs-nav">
  <li class=""><a href="#tab-1" rel="nofollow">Features</a>
  </li>
  <li class="tab-active"><a href="#tab-2" rel="nofollow">Details</a>
  </li>
</ul>
<div class="tabs-stage">
  <div id="tab-1" style="display: none;">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nec neque nisi, dictum aliquet lectus.</p>
  </div>
  <div id="tab-2" style="display: block;">
    <p>Phasellus pharetra aliquet viverra. Donec scelerisque tincidunt diam, eu fringilla urna auctor at.</p>
  </div>
</div>

Or you can apply the border by default to the a same as background color and change that color on .active state

// From http://learn.shayhowe.com/advanced-html-css/jquery

// Change tab class and display content
$('.tabs-nav a').on('click', function(event) {
  event.preventDefault();

  $('.tab-active').removeClass('tab-active');
  $(this).parent().addClass('tab-active');
  $('.tabs-stage div').hide();
  $($(this).attr('href')).show();
});

$('.tabs-nav a:first').trigger('click'); // Default
ul,
li,
div {
  background: hsla(0, 0%, 0%, 0);
  border: 0;
  font-size: 100%;
  margin: 0;
  outline: 0;
  padding: 0;
  vertical-align: baseline;
  font: 13px/20px "Droid Sans", Arial, "Helvetica Neue", "Lucida Grande", sans-serif;
  text-shadow: 0 1px 0 hsl(0, 100%, 100%);
}

li {
  display: list-item;
  text-align: -webkit-match-parent;
}

.tabs-nav li :hover {
  background: #eee;
}

.tabs-nav {
  list-style: none;
  margin: 0;
  padding: 0;
}

.tabs-nav li:first-child a {
  border-right: 0;
}

.tabs-nav .tab-active a {
  border-bottom-color: red;
  cursor: default;
}

.tabs-nav a {
  border-bottom: 3px solid hsl(120, 11%, 96%);
  background: hsl(120, 11%, 96%);
  color: hsl(215, 6%, 57%);
  display: block;
  font-size: 11px;
  font-weight: bold;
  height: 40px;
  line-height: 44px;
  text-align: center;
  text-transform: uppercase;
  width: 140px;
}

.tabs-nav li {
  float: left;
}

.tabs-stage {
  border: 1px solid hsl(210, 6%, 79%);
  -webkit-border-radius: 0 0 6px 6px;
  -moz-border-radius: 0 0 6px 6px;
  -ms-border-radius: 0 0 6px 6px;
  -o-border-radius: 0 0 6px 6px;
  border-radius: 0 0 6px 6px;
  border-top: 0;
  clear: both;
  margin-bottom: 20px;
  position: relative;
  top: -1px;
  width: 281px;
}

.tabs-stage p {
  margin: 0;
  padding: 20px;
  color: hsl(0, 0%, 33%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="tabs-nav">
  <li class=""><a href="#tab-1" rel="nofollow">Features</a>
  </li>
  <li class="tab-active"><a href="#tab-2" rel="nofollow">Details</a>
  </li>
</ul>
<div class="tabs-stage">
  <div id="tab-1" style="display: none;">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus nec neque nisi, dictum aliquet lectus.</p>
  </div>
  <div id="tab-2" style="display: block;">
    <p>Phasellus pharetra aliquet viverra. Donec scelerisque tincidunt diam, eu fringilla urna auctor at.</p>
  </div>
</div>
Bhuwan
  • 16,525
  • 5
  • 34
  • 57
  • This is a very messy answer, and just contains loads of code that (even if from the link the OP provided) would not really help anyone that comes after. – Andreas Storvik Strauman Apr 11 '18 at 07:09
  • @AndreasStorvikStrauman I have created a snippet here which is build by stack-overflow to check the result at the same time and also added the desired explanation. Also it contains the code which is required. – Bhuwan Apr 11 '18 at 07:15
0

Use box-sizing: border-box

From W3schools on the border-box value of box-sizing:

The width and height properties (and min/max properties) includes content, padding and border

.tabs-nav .tab-active a {
  border-bottom: 3px solid red;
  box-sizing: border-box;
  cursor: default;
}

https://jsfiddle.net/eztskazd/10/

0

You can achieve this if you use box-sizing: border-box; .

To fix your specific problem, you can use the following:

  .tabs-nav .tab-active a {
     border-bottom: 3px solid red;
     cursor: default;
     box-sizing: border-box;
  }

Maybe you can also apply box-sizing: border-box globally, you have to try this for yourself. See also this blog post by paul irish.

cloned
  • 6,346
  • 4
  • 26
  • 38
0

This is what You want? Just replace your entire css with below and check:

ul,
li,
div {
  background: hsla(0, 0%, 0%, 0);
  border: 0;
  font-size: 100%;
  margin: 0;
  outline: 0;
  padding: 0;
  vertical-align: baseline;
  font: 13px/20px "Droid Sans", Arial, "Helvetica Neue", "Lucida Grande", sans-serif;
  text-shadow: 0 1px 0 hsl(0, 100%, 100%);
}

li {
  display: list-item;
  text-align: -webkit-match-parent;
}

.tabs-nav {
  list-style: none;
  margin: 0;
  padding: 0;
}

.tabs-nav li:first-child a {
  border-right: 0;
}

a:hover {
  background: #eee;
  border-bottom: 3px solid red;
  cursor: default;

}

.tabs-nav a {
  background: hsl(120, 11%, 96%);
  color: hsl(215, 6%, 57%);
  display: block;
  font-size: 11px;
  font-weight: bold;
  height: 40px;
  line-height: 44px;
  text-align: center;
  text-transform: uppercase;
  width: 140px;
}

.tabs-nav li {
  float: left;
}

.tabs-stage {
  border: 1px solid hsl(210, 6%, 79%);
  -webkit-border-radius: 0 0 6px 6px;
  -moz-border-radius: 0 0 6px 6px;
  -ms-border-radius: 0 0 6px 6px;
  -o-border-radius: 0 0 6px 6px;
  border-radius: 0 0 6px 6px;
  border-top: 0;
  clear: both;
  margin-bottom: 20px;
  position: relative;
  top: -1px;
  width: 281px;
}

.tabs-stage p {
  margin: 0;
  padding: 20px;
  color: hsl(0, 0%, 33%);
}
Sudoss
  • 347
  • 2
  • 13