简体   繁体   English

边框CSS-意外行为

[英]Border CSS - Unexpected behavior

I have created one div in HTML and I have a CSS class for it. 我已经用HTML创建了一个div ,并且有一个CSS class I am gonna make an example of my code to explain it better: 我将以我的代码为例来更好地解释它:

HTML HTML

<div style="text-align:center;" class="container">
  <span class="message col-xl-12 col-lg-12 col-md-12 col-sm-12 col- 
   xs-12">Test</span>
</div>

My problem is that I do not get the behavior of the CSS class "message" 我的问题是我没有得到CSS类“消息”的行为

At the beginning I was doing it as following: 一开始,我的操作如下:

    .message{
  margin-top: 30px;
  height: 3em;
  line-height: 3em;
  font-size: 25px;
  text-align: center;  
  border-style: solid;
  border-color: #0A76BC;  
  border-bottom: 2px;
  margin-bottom: 5%;
}

but what I found was totally unexpected. 但是我发现完全是出乎意料的。 I appeared exactly the border that was not mentioned in the class (border-top) but the rest of them: 我正好出现在border ,这不是在类(边界上)提及,但他们的休息:

在此处输入图片说明

So I did exactly the opposite and I worked :/ 所以我做的恰恰相反:

.message{
  margin-top: 30px;
  height: 3em;
  line-height: 3em;
  font-size: 25px;
  text-align: center;  
  border-style: solid;
  border-color: #0A76BC;  
  border-top: 2px;
  border-left: 2px;
  border-right: 2px;
  margin-bottom: 5%;
}

在此处输入图片说明

The only thing that I can get is that the class container from bootstrap has some specials features. 我唯一能得到的是bootstrap的类container具有一些特殊功能。 Could somebody give an explication? 有人可以说明一下吗?

border-bottom: 2px; is a shorthand for border-left-style , border-left-width and border-left-color . border-left-styleborder-left-widthborder-left-color的简写。 As you're not defining border-left-style the default value is used: none . 由于您没有定义border-left-style ,因此使用默认值: none It's overriding the value of border-style . 它超越了border-style的价值。

border-bottom: 2px solid;

will work as intended. 将按预期工作。

Adding to what Axnyff answered. 除了Axnyff回答的内容。 To understand how CSS (Cascaded Style Sheet) works and why you get odd behaviors, we need to explain the difference and how they override each other. 为了了解CSS(层叠样式表)的工作方式以及为什么您会得到奇怪的行为,我们需要解释它们之间的区别以及它们如何相互替代。 There are three ways we can apply CSS to html element which are inline, internal and external CSS. 我们可以通过三种方式将CSS应用于html元素:内联,内部和外部CSS。

  1. INLINE CSS: 内联CSS:
    Inline CSS or style which is defined as element's attribute style value override internal and external CSS. 内联CSS或样式(定义为元素的属性样式值)会覆盖内部和外部CSS。

  2. INTERNAL CSS: 内部CSS:
    Internal CSS which is declared in header section of page override external CSS only. 在页面标题部分声明的内部CSS仅会覆盖外部CSS。

  3. EXTERNAL CSS: 外部CSS:
    Inline and internal CSS override external imported CSS file with .css extension. 内联和内部CSS会覆盖扩展名为.css的外部导入CSS文件。

When it comes to using internal and external CSS, CSS declared with id selector override another CSS declared with class selector which style the same element using the same properties, example: 在使用内部和外部CSS时,使用id选择器声明的CSS会覆盖使用类选择器声明的另一个CSS,该CSS使用相同的属性为相同的元素设置样式,例如:

CSS CSS

#test_id { color: red; }
.test_cls { color: green; }

HTML HTML

<div id="test_id" class="test_cls">which color?</div>

The text inside div element above will be red because #test_id override .test_cls class. 上面的div元素内的文本将为红色,因为#test_id覆盖.test_cls类。

And applying multiple CSS to the same element using class can lead to weird result if your not careful, and this happens when two or more declared CSS class have the same properties. 如果您不注意,将多个CSS使用类应用于同一元素会导致奇怪的结果,并且当两个或多个声明的CSS类具有相同的属性时会发生这种情况。 example: 例:

CSS CSS

.bdr1 { border: 2px solid red; } 
.bdr2 { border: 4px solid green; }

HTML HTML

<div class="bdr1 bdr2">Which border style is going to be applied?</div>

It happens that .bdr2 is what is applied, but what if we want to apply .bdr1 class? 碰巧会应用.bdr2 ,但是如果我们想应用.bdr1类怎么办? well, we change the CSS .bdr1 above to this: 好吧,我们将上面的CSS .bdr1更改为此:

.bdr1 { border: 2px solid red!important; }

to override 覆盖

.bdr2 { border: 2px solid green; } 

but be careful using "!important". 但请小心使用“!important”。

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

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