更改Bootstraps轮播中的箭头颜色

时间:2017-09-16 02:39:29

标签: html css twitter-bootstrap bootstrap-4 carousel

我显然在这里遗漏了一些愚蠢的事情。我有白色背景的图像,所以我希望能够编辑Bootstraps Carousel上的箭头,以便它们可见。很多人改变了箭头的颜色(不像我做的那样背景)。

CSS

.carousel-control-prev-icon, .carousel-control-next-icon {
    height: 100px;
    width: 100px;
    outline: black;
    background-color: rgba(0, 0, 0, 0.3);
    background-size: 100%, 100%;
    border-radius: 50%;
    border: 1px solid black;
}

轮播HTML

<div class = 'col-md-9'>

    <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">

        <ol class="carousel-indicators">
            <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
            <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
            <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
        </ol>

        <div class="carousel-inner" role="listbox">
            <div class="carousel-item active">
                <img class="d-block img-fluid" src="carousel/Bars%20and%20Dots.jpg" alt="First slide" class = 'img-responsive'>
            </div>

            <div class="carousel-item">
                <img class="d-block img-fluid" src="carousel/murial.jpg" alt="Second slide" class = 'img-responsive'>
            </div>

            <div class="carousel-item">
                <img class="d-block img-fluid" src="carousel/Upper%20Partialism%20album%20covers004white.jpg" alt="Third slide" class = 'img-responsive'>
            </div>
        </div>

        <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
        </a>

        <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
        </a>

    </div>

</div>

14 个答案:

答案 0 :(得分:64)

我知道这是一篇较老的帖子,但它帮助了我。我还发现,对于bootstrap v4,您还可以通过覆盖这样的控件来更改箭头颜色:

.carousel-control-prev-icon {
 background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' viewBox='0 0 8 8'%3E%3Cpath d='M5.25 0l-4 4 4 4 1.5-1.5-2.5-2.5 2.5-2.5-1.5-1.5z'/%3E%3C/svg%3E") !important;
}

.carousel-control-next-icon {
  background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' viewBox='0 0 8 8'%3E%3Cpath d='M2.75 0l-1.5 1.5 2.5 2.5-2.5 2.5 1.5 1.5 4-4-4-4z'/%3E%3C/svg%3E") !important;
}

将最后的fill='%23fff' fff更改为您想要的任何十六进制值。例如:

fill='%23000'表示黑色,fill='%23ff0000'表示红色,依此类推。只是一个注释,我没有在没有!important声明的情况下测试过。

答案 1 :(得分:27)

我希望这有效,欢呼。

&#13;
&#13;
{
"success": true,
"message": "got the locations!",
"data": {
    "LocationList": [
        {
            "LocID": 1,
            "LocName": "Downtown"
        },
        {
            "LocID": 2,
            "LocName": "Uptown"
        },
        {
            "LocID": 3,
            "LocName": "Midtown"
        }
     ]
  }
}

struct Location: Codable {
    var data: [LocationList]
}

struct LocationList: Codable {
    var LocID: Int!
    var LocName: String!
}

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    let url = URL(string: "/getlocationlist")

    let task = URLSession.shared.dataTask(with: url!) { data, response, error in
        guard error == nil else {
            print(error!)
            return
        }
        guard let data = data else {
            print("Data is empty")
            return
        }

        do {
            let locList = try JSONDecoder().decode(Location.self, from: data)
            print(locList)
        } catch let error {
            print(error)
        }
    }

    task.resume()
}
&#13;
&#13;
&#13;

答案 2 :(得分:18)

如果您只想在Bootstrap 4中将其设为黑色。

.carousel-control-next {
    filter: invert(100%);
}

答案 3 :(得分:4)

您还应该使用:<span><i class="fa fa-angle-left" aria-hidden="true"></i></span>使用fontawesome。您必须覆盖原始代码。执行以下操作,您可以自由定制CSS:

<a class="carousel-control-prev" href="#carouselExampleIndicatorsTestim" role="button" data-slide="prev">
    <span><i class="fa fa-angle-left" aria-hidden="true"></i></span>
    <span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicatorsTestim" role="button" data-slide="next">
    <span><i class="fa fa-angle-right" aria-hidden="true"></i></span>
    <span class="sr-only">Next</span>
 </a>

原始代码

<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
</a>

<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
</a>

答案 4 :(得分:3)

当前,Bootstrap 4使用带有嵌入的SVG数据信息的背景图像,其中包括SVG形状的颜色。像这样:

.carousel-control-prev-icon { background-image:url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23fff' viewBox='0 0 8 8'%3E%3Cpath d='M5.25 0l-4 4 4 4 1.5-1.5-2.5-2.5 2.5-2.5-1.5-1.5z'/%3E%3C/svg%3E"); }

请注意,fill='%23fff'周围的部分用颜色填充了颜色,在这种情况下为#fff(白色),对于红色,只需替换为#f00

最后,可以安全地添加此内容(对下一个图标进行相同的更改):

.carousel-control-prev-icon {background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23f00' viewBox='0 0 8 8'%3E%3Cpath d='M5.25 0l-4 4 4 4 1.5-1.5-2.5-2.5 2.5-2.5-1.5-1.5z'/%3E%3C/svg%3E"); }

答案 5 :(得分:3)

这对我有用:

.carousel-control-prev-icon {
        background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='%23000' width='8' height='8' viewBox='0 0 8 8'%3e%3cpath d='M5.25 0l-4 4 4 4 1.5-1.5L4.25 4l2.5-2.5L5.25 0z'/%3e%3c/svg%3e");
      }
      
      .carousel-control-next-icon {
        background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='%23000' width='8' height='8' viewBox='0 0 8 8'%3e%3cpath d='M2.75 0l-1.5 1.5L3.75 4l-2.5 2.5L2.75 8l4-4-4-4z'/%3e%3c/svg%3e");
      }

我更改了图标网址中的颜色。多数民众赞成在引导页面中使用的原始内容,但此部分为黑色:

“ ... fill ='%23000'...”

答案 6 :(得分:2)

for bootstrap-3可以使用:

.carousel-control span.glyphicon {
    color: red;
}

答案 7 :(得分:2)

在引导程序4中,如果您只是更改控件的颜色,则可以使用sass变量,通常在custom.scss中:

$ carousel-control-color:#7b277d;

如本github问题说明中所述:https://github.com/twbs/bootstrap/issues/21985#issuecomment-281402443

答案 8 :(得分:1)

如果您使用bootstrap.min.css作为轮播 -

<a class="left carousel-control" href="#carouselExample" data-slide="prev">
  <span class="glyphicon glyphicon-chevron-left"></span>
  <span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carouselExample" data-slide="next">
  <span class="glyphicon glyphicon-chevron-right"></span>
  <span class="sr-only">Next</span>
</a>

打开bootstrap.min.css文件,找到属性&#34; glyphicon-chevron-right&#34;并添加属性&#34; color:red&#34;

答案 9 :(得分:1)

一个对我有用的小例子

    .carousel-control-prev,
    .carousel-control-next{
        height: 50px;
        width: 50px;
        outline: $color-white;
        background-size: 100%, 100%;
        border-radius: 50%;
        border: 1px solid $color-white;
        background-color: $color-white;
    }

    .carousel-control-prev-icon { 
        background-image:url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23009be1' viewBox='0 0 8 8'%3E%3Cpath d='M5.25 0l-4 4 4 4 1.5-1.5-2.5-2.5 2.5-2.5-1.5-1.5z'/%3E%3C/svg%3E"); 
        width: 30px;
        height: 48px;
    }
    .carousel-control-next-icon { 
        background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23009be1' viewBox='0 0 8 8'%3E%3Cpath d='M2.75 0l-1.5 1.5 2.5 2.5-2.5 2.5 1.5 1.5 4-4-4-4z'/%3E%3C/svg%3E");
        width: 30px;
        height: 48px;
    }

答案 10 :(得分:0)

要使用Sass自定义轮播控件,标题和指示器的颜色,您可以包含以下变量

    $carousel-control-color: 
    $carousel-caption-color: 
    $carousel-indicator-active-bg: 

答案 11 :(得分:0)

要更改颜色,您应该将样式添加到 css 文件中,并对用作类背景图像的 svg 代码中的填充属性进行一些更改 .carousel-control-prev-icon 和 .carousel-control-next-icon

fill='%23fff' 仅将 fff 更改为您需要的颜色,例如 f00 表示红色。 (也可以使用 6 个字母的十六进制 --> %23ff0000)

.carousel-control-pre-icon {background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg'fill='% 23fff' width='8' height='8' viewBox='0 0 8 8'%3e%3cpath d='M5.25 0l-4 4 4 4 1.5-1.5L4.25 4l2.5-2.5L5.25 0z'/%3e%3c/svg%3e");}

青少年: .carousel-container .carousel .carousel-control-next .carousel-control-next-icon

答案 12 :(得分:-1)

带有“真棒字体”图标:

<!-- Controls -->
<a class="carousel-control-prev" href="#carousel-example-generic" role="button" data-slide="prev">
  <span class="fa fa-chevron-left fa-lg" style="color:red;"></span>
  <span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carousel-example-generic" role="button" data-slide="next">
  <span class="fa fa-chevron-right fa-lg" style="color:red;"></span>
  <span class="sr-only">Next</span>
</a>

答案 13 :(得分:-1)

我也遇到类似的问题,有些图像很亮,有些图像很暗,所以箭头并不总是清晰地显示出来,所以我采用了一种更为简单的方法。

在模态主体部分中,我删除了以下几行:

    <!-- Left and right controls -->
    <a class="carousel-control-prev" href="#id" data-slide="prev">
       <span class="carousel-control-prev-icon"></span>
    </a>
    <a class="carousel-control-next" href="#id" data-slide="next">
      <span class="carousel-control-next-icon"></span>
    </a>

并将以下内容插入模式标题部分

    <!-- Left and right controls -->
    <a  href="#gamespandp" data-slide="prev" class="btn btn-outline-secondary btn-sm">&#10094;</a>
    <a  href="#gamespandp"  data-slide="next" class="btn btn-outline-secondary btn-sm">&#10095;</a>

现在可以清楚地看到指示器,无需添加额外的图标或弄乱样式表,尽管您可以根据需要对它们进行样式设置!

查看此演示图像:

[demo Image]