悬停包括css的边框底部?

时间:2018-04-11 06:52:03

标签: css css3

我有一个活跃的类,我应用了边框

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

但问题是当我悬停时边框看起来像是在盒子外面,如何让它进入内部?意味着我想要一条带有活动边框的标签项的直线。

demo https://jsfiddle.net/eztskazd/

4 个答案:

答案 0 :(得分:1)

您需要将box-sizing:border-box设置为元素,以告知元素的任何填充和边框值都将包含在元素的宽度和高度中。



// 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>
&#13;
&#13;
&#13;

或者您可以默认将边框应用于与背景颜色相同的a,并在.active状态下更改颜色

&#13;
&#13;
// 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
&#13;
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%);
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用box-sizing: border-box

来自border-box的{​​{1}}值的W3schools

  

宽度和高度属性(以及最小/最大属性)包括内容,填充和边框

box-sizing

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

答案 2 :(得分:0)

如果您使用let url = URL(string: "http://<...>/api/prices?hall=<...>&date=20180501") var request = URLRequest(url: url!) request.httpMethod = "GET" let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in if let err = error { print(err) } else { do { if let jsonResult = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary { // ... } DispatchQueue.main.async { self.collectionView.reloadData() } } catch { print("error") } } }) task.resume() } ,则可以实现此目的。

要解决您的具体问题,您可以使用以下内容:

box-sizing: border-box;

也许你也可以全局应用box-sizing:border-box,你必须亲自尝试一下。另请参阅this blog post by paul irish

答案 3 :(得分:0)

这是你想要的?只需用下面的内容替换整个CSS,然后检查:

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%);
}