Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

css - Changing background color for drowpdown makes selected items totally white in Bootstrap

I have tried to design a dark dropdown menu using Bootstraps dropdown classes and additional styling via CSS. The problem is that when the mouse hovers over the dropdown-items they turn totally white (white text on brightbackground). See the result in this image:

enter image description here

The code for the image above is showed below. I have used inline styles to clarify:

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
    <div class="dropdown">
        <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
        Dropdown menu
        </button>
        <div class="dropdown-menu" style="background-color: black" >
            <a class="dropdown-item" style="color:white" href="#">Item 1</a>
            <a class="dropdown-item" style="color:white" href="#">Item 2</a>
            <a class="dropdown-item" style="color:white" href="#">Item 3</a>
        </div>
    </div>
</div>

</body>
</html>

I can't find how I can change the hover behaviour or style. I would like the text to turn dark for readibility. How can I achieve this? Are there any classes or other tricks in Bootstrap?

question from:https://stackoverflow.com/questions/65934014/changing-background-color-for-drowpdown-makes-selected-items-totally-white-in-bo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to add CSS to the hover state of the link in the dropdown.

a:hover {
  color: #000!important;
}
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>

<body>

  <div class="container">
    <div class="dropdown">
      <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
        Dropdown menu
        </button>
      <div class="dropdown-menu" style="background-color: black">
        <a class="dropdown-item" style="color:white" href="#">Item 1</a>
        <a class="dropdown-item" style="color:white" href="#">Item 2</a>
        <a class="dropdown-item" style="color:white" href="#">Item 3</a>
      </div>
    </div>
  </div>

</body>

</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...