为什么设置图像背景会覆盖 Bootstrap 按钮的背景颜色?

huangapple go评论67阅读模式
英文:

why does setting an image background overrides bootstrap buttons' background color?

问题

为什么这个CSS覆盖了Bootstrap按钮的背景样式,使其透明,而不是默认的按钮颜色?

例如,如果我有一个按钮,并且在我的文档中有下面的样式,它将整个body的background-image设置为一张图片。我可以看到它覆盖了按钮的背景,并使其与图片的颜色相同。这是为什么会发生的,如何修复它?

英文:

Why does this CSS override bootstrap button background styling and makes it transparent rather than the default button of the color?

For example, if I have a button and I have the styling below in my document which sets the background-image of the whole body to an image. I can see that it overrides the background of the buttons as well and makes the same color as the image. is there a reason why does that happen and how to fix it?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  background: url(&#39;assets/background.jpg&#39;) no-repeat center center fixed;
  background-size: cover;
}

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot; integrity=&quot;sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65&quot; crossorigin=&quot;anonymous&quot;&gt;

&lt;button class=&quot;btn btn-warning m-4&quot;&gt;A Button&lt;/button&gt;

<!-- end snippet -->

答案1

得分: 1

问题在于使用*****选择器。该选择器用于选择文档中的所有元素(在您的情况下,*选择器选择 => html标签、body标签和button标签)

如果您希望给body设置背景图片,请改用**body选择器,而不是***选择器。

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
body {
  background: url('https://images.unsplash.com/photo-1676269098733-4a94f912d838?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1000&amp;q=80') no-repeat center center fixed;
  background-size: cover;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<button class="btn btn-warning m-4">A Button</button>
英文:

The issue here is using * selector, This selector is used to select all the elements in your document (In your case * selects => html tag, body tag, and button tag)

If u wish to give background image to body use body saelector instead of *

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
body{
  background: url(&#39;https://images.unsplash.com/photo-1676269098733-4a94f912d838?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=1000&amp;q=80&#39;) no-repeat center center fixed;
  background-size: cover;
}

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot; integrity=&quot;sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65&quot; crossorigin=&quot;anonymous&quot;&gt;

&lt;button class=&quot;btn btn-warning m-4&quot;&gt;A Button&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月16日 03:44:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定