bootstrap 轮播全宽

bootstrap carousel full width

使用技术

  1. Bootstrap
  2. css
  3. js
  4. Django
  5. Python

我的问题

我想声明 container-fluid 并让 carousel slide 填满屏幕。

但是两端还剩一点space

我要填空

我想调整轮播幻灯片的高度。

我是新手css请帮忙

我的html轮播幻灯片部件

<div class="container-fluid">
    <div id="carouselExampleSlidesOnly" class="carousel slide" data-bs-ride="carousel">
        <div class="carousel-inner">
            <div class="carousel-item active">
                <img src="http://placeimg.com/640/480/any" class="w-100" alt="...">
            </div>
            <div class="carousel-item">
                <img src="http://placeimg.com/640/480/any" class="w-100" alt="...">
            </div>
            <div class="carousel-item">
                <img src="http://placeimg.com/640/480/any" class="w-100" alt="...">
            </div>
        </div>
    </div>

</div>

我的全部html

<!doctype html>
<html lang="en">
{% load static %}
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="{% static 'Blog/css/bootstrap.css' %}">
    <link rel="stylesheet" href="{% static 'Blog/css/bootstrap.min.css' %}">
    <title>Bootstrap demo</title>
    <!-- Navigation-->
</head>
<body id="page=top">
<nav class="navbar navbar-expand-lg navbar-light fixed-top py-3" id="mainNav">
    <div class="container-fluid">
        <div class="col-2">
            <a class="navbar-brand" href="#page-top">코딩하는 돌맹이 옷집</a>
        </div>
        <button class="navbar-toggler navbar-toggler-right" type="button" data-bs-toggle="collapse"
                data-bs-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false"
                aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>
        <div class="collapse navbar-collapse row col-10" id="navbarResponsive">
            <div class="col-9">
                <ul class="navbar-nav">
                    <button class="navbar-toggler" type="button" data-bs-toggle="collapse"
                            data-bs-target="#navbarNavDarkDropdown" aria-controls="navbarNavDarkDropdown"
                            aria-expanded="false" aria-label="Toggle navigation">
                        <span class="navbar-toggler-icon"></span>
                    </button>
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDarkDropdownMenuLink" role="button"
                           data-bs-toggle="dropdown" aria-expanded="false">
                            IMMM
                        </a>
                        <ul class="dropdown-menu dropdown-menu-white" aria-labelledby="navbarDarkDropdownMenuLink">
                            <li><a class="dropdown-item" href="#">Action</a></li>
                            <li><a class="dropdown-item" href="#">Another action</a></li>
                            <li><a class="dropdown-item" href="#">Something else here</a></li>
                            <li><a class="dropdown-item" href="#">Something else here</a></li>
                            <li><a class="dropdown-item" href="#">Something else here</a></li>
                            <li><a class="dropdown-item" href="#">Something else here</a></li>
                            <li><a class="dropdown-item" href="#">Something else here</a></li>
                            <li><a class="dropdown-item" href="#">Something else here</a></li>
                        </ul>
                    </li>
                    <li class="nav-item"><a class="nav-link" href="#services">IMMM MADE</a></li>
                    <li class="nav-item"><a class="nav-link" href="#portfolio">COMMUNITY</a></li>
                </ul>
            </div>
            <div class="col-3">
                <ul class="navbar-nav">
                    <li class="nav-item"><a class="nav-link" href="#about">LOG IN</a></li>
                    <li class="nav-item"><a class="nav-link" href="#services">JOIN</a></li>
                    <li class="nav-item"><a class="nav-link" href="#portfolio">MYPAGE</a></li>
                    <li class="nav-item"><a class="nav-link" href="#contact">SEARCH</a></li>
                </ul>
            </div>
        </div>
    </div>
</nav>
<div class="container-fluid">
    <div id="carouselExampleSlidesOnly" class="carousel slide" data-bs-ride="carousel">
        <div class="carousel-inner">
            <div class="carousel-item active">
                <img src="http://placeimg.com/640/480/any" class="w-100" alt="...">
            </div>
            <div class="carousel-item">
                <img src="http://placeimg.com/640/480/any" class="w-100" alt="...">
            </div>
            <div class="carousel-item">
                <img src="http://placeimg.com/640/480/any" class="w-100" alt="...">
            </div>
        </div>
    </div>

</div>

</body>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.min.js"
        integrity="sha384-kjU+l4N0Yf4ZOJErLsIcvOU2qSb74wXpOhqTvwVx3OElZRweTnQ6d31fXEoRD1Jy"
        crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2"
        crossorigin="anonymous"></script>

您已经结合了几个问题,这应该足以让您朝着正确的方向前进。

要移除容器两侧的间隙,请添加 Bootstrap class px-0 以移除填充:

<div class="container-fluid px-0">

您可以添加样式属性来设置轮播的垂直高度。但是,当图像的 height:width 比例与轮播比例不同时,图像将被裁剪:

<div class="carousel-inner" style="height: 300px;">

您可以通过添加纯色背景来修复导航栏,例如 bg-light:

<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top py-3"

并且由于您使用的是固定导航栏,因此您还应该添加一个垫片 div 来向下推动旋转木马。如果没有间隔符,内容将被导航栏隐藏。

<div style="margin-top: 70px;"></div>

但是,如果您希望导航栏显示在图像上方,请添加以下样式而不是间隔符:

  <nav style="background-color: rgba(255,255,255,0.6);" class="navbar navbar-expand-lg navbar-light fixed-top py-3" 

并且由于您使用的是 bootstrap.bundle.min.js,因此您可以从您的页面中删除此脚本(您不需要两者):

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.min.js"

运行 了解其工作原理的代码段。

<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top py-3" id="mainNav">
  <div class="container-fluid">
    <div class="col-2">
      <a class="navbar-brand" href="#page-top">코딩하는 돌맹이 옷집</a>
    </div>
    <button class="navbar-toggler navbar-toggler-right" type="button" data-bs-toggle="collapse" data-bs-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>
    <div class="collapse navbar-collapse row col-10" id="navbarResponsive">
      <div class="col-9">
        <ul class="navbar-nav">
          <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavDarkDropdown" aria-controls="navbarNavDarkDropdown" aria-expanded="false" aria-label="Toggle navigation">
                        <span class="navbar-toggler-icon"></span>
                    </button>
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDarkDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                            IMMM
                        </a>
            <ul class="dropdown-menu dropdown-menu-white" aria-labelledby="navbarDarkDropdownMenuLink">
              <li><a class="dropdown-item" href="#">Action</a></li>
              <li><a class="dropdown-item" href="#">Another action</a></li>
              <li><a class="dropdown-item" href="#">Something else here</a></li>
              <li><a class="dropdown-item" href="#">Something else here</a></li>
              <li><a class="dropdown-item" href="#">Something else here</a></li>
              <li><a class="dropdown-item" href="#">Something else here</a></li>
              <li><a class="dropdown-item" href="#">Something else here</a></li>
              <li><a class="dropdown-item" href="#">Something else here</a></li>
            </ul>
          </li>
          <li class="nav-item"><a class="nav-link" href="#services">IMMM MADE</a></li>
          <li class="nav-item"><a class="nav-link" href="#portfolio">COMMUNITY</a></li>
        </ul>
      </div>
      <div class="col-3">
        <ul class="navbar-nav">
          <li class="nav-item"><a class="nav-link" href="#about">LOG IN</a></li>
          <li class="nav-item"><a class="nav-link" href="#services">JOIN</a></li>
          <li class="nav-item"><a class="nav-link" href="#portfolio">MYPAGE</a></li>
          <li class="nav-item"><a class="nav-link" href="#contact">SEARCH</a></li>
        </ul>
      </div>
    </div>
  </div>
</nav>

<div style="margin-top: 70px;"></div>

<div class="container-fluid px-0">
  <div id="carouselExampleSlidesOnly" class="carousel slide" data-bs-ride="carousel">
    <div class="carousel-inner" style="height: 300px;">
      <div class="carousel-item active">
        <img src="https://loremflickr.com/640/480/red" class="w-100 h-auto" alt="...">
      </div>
      <div class="carousel-item">
        <img src="https://loremflickr.com/640/480/green" class="w-100" alt="...">
      </div>
      <div class="carousel-item">
        <img src="http://placeimg.com/640/480/blue" class="w-100" alt="...">
      </div>
    </div>
  </div>

</div>


<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.min.js"></script>