How to put images in a box without space

2020-05-06 08:34发布

问题:

I am learning HTML and CSS using bootstrap.

I am studying carousel example code. But like the image, there's a space left in the box.

How can I fill the box with full screen without space?

This is the carousel markup:

<div class="col-lg-6 col-sm-6 portfolio-item">
    <div class="card h-100">
        <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
            <div class="carousel-inner" role="listbox">
              <!-- Slide One - Set the background image for this slide in the line below -->
              <div class="carousel-item active">
                <div class="carousel-caption d-none d-md-block">
                  <img src="image/image-1.jpg" style="width: 100%; height: 100%;">
                </div>
              </div>
              <!-- Slide Two - Set the background image for this slide in the line below -->
              <div class="carousel-item">
                <div class="carousel-caption d-none d-md-block">
                  <img src="image/image-2.jpg" style="width: 100%">
                </div>
              </div>
              <!-- Slide Three - Set the background image for this slide in the line below -->
              <div class="carousel-item">
                <div class="carousel-caption d-none d-md-block">
                  <img src="image/image-3.jpg" style="width: 100%; height: 100%;">
                </div>
              </div>
            </div>
          </div>
    </div>
  </div>

i upload css code

1.css

`body {
      background-image: url("../image/bgimage.jpg");
      background-repeat: no-repeat;
      background-size: cover;
      background-position: center;
      }
.carousel-item {
  height: 44vh;
  min-height: 300px;
  background: no-repeat center center scroll;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}

++other.css same project

.carousel-inner {
  position: relative;
  width: 100%;
  overflow: hidden;
}

.carousel-inner::after {
  display: block;
  clear: both;
  content: "";
}

.carousel-item {
  position: relative;
  display: none;
  float: left;
  width: 100%;
  margin-right: -100%;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  transition: -webkit-transform 0.6s ease-in-out;
  transition: transform 0.6s ease-in-out;
  transition: transform 0.6s ease-in-out, -webkit-transform 0.6s ease-in-out;
}

回答1:

Are you guaranteed that all slide images are and will always be the same size? If not (or unsure), I'd make the slide images appear as backgrounds to the .carousel-item div:

#carouselExampleIndicators .carousel-inner .carousel-item {
  background: url(image/image-1.jpg) no-repeat center center; // update url for each slide
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover; // cover ensures that there's no white space
}


回答2:

You can add CSS for all slider images

#carouselExampleIndicators .carousel-inner .carousel-item img{
   height: auto;
   max-width: 100%;
    display:block;
   width: 100%;
}