`Slide bootstrap carousel without moving other elements`

.carousel {
    position: relative;
    height: 500px;
    .carousel-inner .item {
      height: 500px;
    }
    .carousel-indicators > li {
      margin: 0 2px;
      background-color: $maincolor;
      border-color: $maincolor;
      opacity: .7;
      &.active {
        width: 10px;
        height: 10px;
        opacity: 1;
      }
    }
  }

  .hero {
    position: absolute;
    top: 50%;
    left: 50%;
    z-index: 3;
    color: #fff;
    text-align: center;
    text-transform: uppercase;
    text-shadow: 1px 1px 0 rgba(0, 0, 0, 0.75);
    -webkit-transform: translate3d(-50%, -50%, 0);
    -moz-transform: translate3d(-50%, -50%, 0);
    -ms-transform: translate3d(-50%, -50%, 0);
    -o-transform: translate3d(-50%, -50%, 0);
    transform: translate3d(-50%, -50%, 0);
    h1 {
      font-size: 6em;
      font-weight: bold;
      margin: 0;
      padding: 0;
    }
    .logo{
      margin-bottom:-5%;
      width:300px;
    }
  }

  .btn {
    &.btn-lg {
      padding: 10px 40px;
    }
    &.btn-hero {
      color: #f5f5f5;
      background-color: $maincolor;
      border-color: $maincolor;
      outline: none;
      margin: 20px auto;
      &:hover, &:focus {
        color: #f5f5f5;
        background-color:$secondcolor;
        border-color: $secondcolor;
        outline: none;
        margin: 20px auto;
      }
    }
  }
  .carousel .slides {
    .slide-1, .slide-2, .slide-3 {
      height: 500px;
      background-size: cover;
      background-position: center center;
      background-repeat: no-repeat;
    }
    .slide-1 {
      background-image: url(http://i.imgur.com/CK3d6nV.jpg);
    }
    .slide-2 {
      background-image: url(http://i.imgur.com/SlHr4zn.jpg);
    }
    .slide-3 {
      background-image: url(http://i.imgur.com/OAMaVRo.jpg);
    }
  }

  @media screen and (min-width: 980px) {
    .hero {
      width: 980px;
    }
  }

  @media screen and (max-width: 640px) {
    .hero h1 {
      font-size: 4em;
    }
  }
 <div class="carousel-inner">
            <div class="item slides active">
                <div class="slide-1"></div>
                <div class="hero">
                    <hgroup>
                        <img class="logo" src="images/Logo.png" alt="LOGO">
                        <h3>Lorem ipsum dolor sit amet</h3>
                    </hgroup>
                    <button class="btn btn-hero btn-lg" role="button">Lorem</button>
                </div>
            </div>
            <div class="item slides">
                <div class="slide-2"></div>
            </div>
            <div class="item slides">
                <div class="slide-3"></div>
            </div>
        </div>
    </div>

I am utilizing the bootstrap carousel library along with Scss in my project. My objective is to have only the slides move horizontally from right to left while keeping the content within the classes stationary at all times. How can I achieve this effect?

Answer №1

Thank you for your question. Here is a custom implementation using classes instead of image tags:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Custom Bootstrap Example</title>
  <meta charset="utf-8>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

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

    <div class="carousel-inner" role="listbox">
      <div class="hero">
        <hgroup>
          <img class="logo" src="images/Logo.png" alt="LOGO">
          <h3>Lorem ipsum dolor sit amet</h3>
        </hgroup>
        <button class="btn btn-hero btn-lg" role="button">Lorem</button>
      </div>
      
      <div class="item slides active">
        <span class="carousel slide slide-1"></span>
      </div>

      <div class="item slides">
        <span class="carousel slide slide-2"></span>
      </div>

      <div class="item slides">
        <span class="carousel slide slide-3"></span>
      </div>
    </div>

  </div>
</div>

</body>
</html>

CSS Part:

.carousel {
  position: relative;
  height: 500px;
}

.carousel .carousel-inner .item {
  height: 500px;
}

.hero {
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 3;
  color: #fff;
  text-align: center;
  text-transform: uppercase;
}

.btn {
  &.btn-lg {
    padding: 10px 40px;
  }

  &.btn-hero {
    color: #f5f5f5;
    background-color: $maincolor;
    border-color: $maincolor;
  }
}

.item .slides, .slide-1, .slide-2, .slide-3 { 
  display: inherit;
  height: 500px;
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;
}

.slide-1 {
  background-image: url(http://i.imgur.com/CK3d6nV.jpg);
}

.slide-2 {
  background-image: url(http://i.imgur.com/SlHr4zn.jpg);
}

.slide-3 {
  background-image: url(http://i.imgur.com/OAMaVRo.jpg);
}

You can view the live demo here.

Answer №2

Instead of risking your content moving around, consider placing it in a separate div with absolute positioning at the center of the screen and giving it a higher z-index. This way, it will remain in place without any issues.

Answer №3

Take the

<div class="hero"></div>
element out of the carousel item and position it absolutely.

<div style="position:relative">
  <div class="carousel-inner">
    <div class="item slides active">
      <div class="slide-1"></div>
    </div>
    <div class="item slides">
      <div class="slide-2"></div>
    </div>
    <div class="item slides">
      <div class="slide-3"></div>
    </div>
  </div>

  <div class="hero">
    <hgroup>
      <img class="logo" src="images/Logo.png" alt="LOGO">
      <h3>Lorem ipsum dolor sit amet</h3>
    </hgroup>
    <button class="btn btn-hero btn-lg" role="button">Lorem</button>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve the <style> tag response and inject it into the head section of the HTML using React

Although I am aware that this may not be the best practice, it seems to be the most suitable solution for this particular case. The server response contains something like this: <style id="styles">.color_txt{color:green;}</style> I am attempt ...

Is the z-index functioning properly for the header call-to-action text on mobile devices, but not on desktop?

The desktop header cta is functioning properly, but when I switch to mobile nothing happens. I attempted to increase the z-index number within the html instead of in the style sheet. This is where my CTA is located in the html: CALL TO ACTION surrounded ...

Mobile page sliding mechanism

My website contains a div that is mostly off the page, but on hover it translates onto the main page. You can check out my website. However, this method doesn't work well on mobile devices. Hovering is not effective and I often have to click multipl ...

Incorporating a Menu within a TableCell using Material-UI

I have a desire to incorporate Google's Material UI Menu Item inside a TableCell, following their documentation guidelines here in this illustration: https://i.stack.imgur.com/IffEM.png This is my current method: import React from 'react' ...

Preventing HTML form submission after displaying an alert in Vanilla JavaScript

I have created a quiz using HTML with validation. The next step is to score the quiz in JavaScript. The scoring system works fine, but I want to prevent the form from posting to result.html if the user scores 0 on the quiz. I've managed to display an ...

Verify that the JSON data contains the desired data type before adding it to jQuery

I'm working with JSON data extracted from an Excel file. I need to match the First Name in the JSON data with the 'First Name' field and append those elements to a specific div. Additionally, if the JSON data includes a 'status', I ...

Trouble with CSS3 Perspective rendering issue

Here's a simple example showcasing the use of CSS3 Perspective property to create a 3D side flip effect. However, this basic demonstration does not seem to achieve the desired outcome. <html> <head> <style> .div1{height:300px; ...

Navigation website with a twist

My design features a main navigation that is rotated 90 degrees, while the sub-menu remains horizontally aligned. <div id="nav"> <ul> <li><a href="#">Woonaccessoires</a></li> ...

Limit on the number of rows/entries selected for each query and the maximum

Is it possible to set a limit on data selection in a drop-down list through coding? For example, having 3 selections in one drop-down list, with each selection limited to 20 people choosing from the data. <tr><td><label><font face=&a ...

What is causing the data in the hierarchy table to be invisible?

I am attempting to build a hierarchical table using Vue.js as the foundation. However, I want the table to remain hidden with no data displayed. The issue seems to be within this section of code: <tr v-if="item.childrenVisible" v-for="chi ...

Pressing the button will add text into the input field

Below is a jsfiddle link showcasing the current issue I am trying to address: http://jsfiddle.net/YD6PL/61/ Here is the HTML code snippet: <input type="text> <input type="text> <input type="text> <div> <button class="buttons"& ...

Permanently remove the span tag and any associated text from the HTML document

Currently, I am working on a project that involves numerous page numbers marked using the span class. Below is an example of this markup: <p>Cillacepro di to tem endelias eaquunto maximint eostrum eos dolorit et laboria estiati buscia ditiatiis il ...

The getElementById function can only select one option at a time and cannot select multiple options

I'm having an issue with a JavaScript function that is supposed to allow me to select multiple options, but it's only selecting the first one. Here is the JavaScript code: function index(){ var a="${staffindex}".replace("[",""); ...

Registration form creation with file input type resulting in a database storing null values

When submitting my registration form, all entered values except the input type=file are being saved in the database. The file input returns a null value of "0" in the database, even though a file path is chosen from the desktop. I'm trying to figure o ...

Unable to apply 100% height to flex child element

When it comes to creating a layout for my website, I have a simple idea in mind. I want the body content to take up at least 100% of the height of the page. This setup works perfectly on desktop screens, but when I switch to a mobile view (which changes th ...

Issue with rendering components list in React.js

I am currently working on a project using React, and I'm facing an issue where my list is not displaying on render(). In my parent component, I have a list of components coming from SearchResult. Below is the relevant portion of my code: class Create ...

Is it possible to incorporate cmd.exe into an HTML file?

Currently in the process of creating an online batch tutorial and aiming to integrate the Windows command prompt as a dynamic element on my webpage. Initially attempted using the html5 embed tag, however encountered an error stating unsupported plugin. S ...

Innovative way to design a menu using HTML, CSS, and JavaScript that dynamically resizes to a specific width

I'm currently working on a website and I'm trying to create a menu with specific width of 700px. However, I'm unsure whether to tackle this using CSS, JavaScript, or a combination of both. Which approach would be the most straightforward for ...

Outputting HTML from a function

As a beginner, I have a question about best practices. Is it more effective to return HTML from a function like the following: function displayHtml($userName) { $htmlMsg = " <html> <head> <title>Displaying HTML</title&g ...

Generate a text input field within a dropdown menu

Below is an example of my basic HTML code for a drop-down list: <span id="Div_List"> <label for="Gender">For:</label> <select name="Gender" id="Sex"> <option value="1">1000mtr</option> <option val ...