简体   繁体   English

如何在 div 元素上显示边框 css?

[英]How to show the border css over the div element?

Image attached in this link此链接中附加的图像

In number 9 cell border is over the background color在数字 9 中,单元格边框位于背景色之上

How do I show the border over the div or background Color and it should be responsive?如何在 div 或背景颜色上显示边框并且它应该是响应式的?

<div className="borderOverDiv"><div>
<div className="backgroundClr"></div>

.borderOverDiv{
    position: absolute;
    border: 1px solid red;
    width: calc(100% - 94%);
    height: 30px;
    border-radius: 10px;
}

.backgroundClr{
    background: blue
}

this code as I tried, seems not working我试过的这段代码似乎不起作用

I am assuming that you are new to css so I will try to explain what is going on with this code the best that i can.我假设您是 css 的新手,所以我将尽我所能解释这段代码是怎么回事。

The fun part is in .element.active:after有趣的部分在.element.active:after
There is a few thing有几件事

  1. position: absolute this will allow us to set this element absolutly to container. position: absolute这将允许我们将此元素绝对设置为容器。 But witch container?但是女巫容器? First that has position set to a different value than static or its initial value.首先,将position设置为与static或其初始值不同的值。 That is why .element has position: relative which doesn't do anything on its own.这就是为什么.elementposition: relative ,它自己什么都不做。
  2. top, right, bottom, left which tell that this element will be exceeding parent element on every side by 5px . top, right, bottom, left告诉这个元素将在每一边超过父元素5px
  3. z-index Simply the higher the value the "closer" this element is to user. z-index值越高,这个元素离用户“越近”。 initial value is 0 so 1 is placing this element above every other element.初始值为0 ,因此1将此元素置于所有其他元素之上。
  4. content is required in pseudo-element:after in order to show them. 伪元素中需要content :之后才能显示它们。 This property just needs to be set and doesn't have to have any value specified.该属性只需要设置,不必指定任何值。

The reis is just to make it look nicer. reis只是为了让它看起来更好看。
And thats it.就是这样。

You can use other element inside .element if you feel like it.如果您愿意,可以在.element中使用其他元素。
For example例如

<div class="element">
    <div class="overlay"></div>
</div>

and it will work just fine if you will follow point form 1 to 3 (point 4 is required, as I said earlier, only in pseudo-element) but it will be less responsive.如果您遵循第 1 点到第 3 点(第 4 点是必需的,正如我之前所说,仅在伪元素中),它会工作得很好,但它的响应速度会较慢。 For example what will you have to do when other element needs this overlay?例如,当其他元素需要此覆盖时,您必须做什么? You will have to use javascript to append .overlay element to .element and with pseudo-element you just need to append class. or just show this overlay on hover .你将不得不使用 javascript 到 append .overlay元素到.element和伪元素你只需要 append class. 或者只是在hover上显示这个覆盖。 Other advantage is that it look prettier and doesn't bloat you html.其他优点是它看起来更漂亮并且不会使您臃肿 html。

 .container { padding: 5px; display: flex; }.element { position: relative; background-color: #0000ff; padding: 10px 20px; display: inline-block; color: #ffffff; }.element.active:after { content: ''; position: absolute; top: -5px; right: -5px; bottom: -5px; left: -5px; border-radius: 40px; background-color: rgba(200, 200, 200, .4); border: 1px solid rgba(200, 200, 200,.8); z-index: 1; }
 <div class="container"> <div class="element">7</div> <div class="element active">8</div> <div class="element">9</div> </div>

replacing className with class should do the trickclass替换className应该可以解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM