Ways to lift my div element using hover effect

Having a bit of trouble with my code here. I've noticed that when I hover over the div, it cuts off part of my image and removes the padding from the top. Any ideas on how to move the div up when hovering without cutting off the image?

.move {
  transition: all 0.4s ease-in;
}

.hover:hover .move {
  margin-top: -50px;
}

.hover:hover {
  background: black;
}

.readmore {
  opacity: 0;
  visibility: hidden;
  transition: all 0.4s ease-out;
}

.hover:hover .readmore {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}
<div class="container mt-5 pt-5 ">
  <div class="row d-flex justify-content-center text-center  ">
    <div class="col-md-3  hover p-0">
      <div class="position-relative  move  d-flex  align-items-end p-1 ">
        <div class="position-absolute pb-3">
          <h1 class="headersize">asd</h1>
          <p class="small-text">
            Small text
          </p>
        </div>
        <img src="https://encrypted-tbn0.gstatic.com/images? 
      q=tbn:ANd9GcQzp0rXN9BsNzMPYmzdT6xWc931So2I5sA4YiBX4Nm0rT6MiPBS" class=" 
      mb-4" />
      </div>
      <div class=" mb-3">
        <button type="button" class="readmore p-2 m-2    ">
                    Read More
                  </button>
      </div>
    </div>
  </div>
</div>

Answer №1

It's interesting how applying negative margins within a display: flex container doesn't impact the container element's margin, resulting in its background color remaining unchanged even when the child element .move has a negative margin.

If you encounter this issue, one solution could be to avoid using flexbox in your code.

Alternatively, you can experiment with adding a negative margin-top to the same element that undergoes a change in background-color on hover:

/* .hover instead of .move */
.hover {
  transition: all 0.4s ease-in;
}

.hover:hover {
  margin-top: -50px;
}

.hover:hover {
  background: black;
}

This approach allows you to both color and move the same element, creating a seamless animation effect. Keep in mind that assigning a background color to .container or .row may result in the same unexpected behavior.

Answer №2

I made some adjustments here in hopes that it will provide assistance

.move, .mb-3, .hover {
  transition: all 0.4s ease-in; 
  }

.hover {
  position: relative;
  /* height: 80px; */
}
 .hover:hover .move {
  margin-top:-50px;
  background: black;

}

.hover:hover .mb-3 {
  background : black;
  transition: all 0.4s ease-out;
  
}



 .hover:hover {
 background: black;
 }

.readmore {
 opacity: 0;
 /* visibility: hidden; */
 transition: all 0.4s ease-out;
 }

.hover:hover .readmore {
 opacity: 1;
 /* visibility: visible; */
 transform: translateY(0);
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

</head>
<body>
<div class="container mt-5 pt-5 ">
 <div class="row d-flex justify-content-center text-center  ">
 <div class="col-md-3  hover p-0">
 <div class="position-relative  move  d-flex  align-items-end p-1 ">
              <div class="position-absolute pb-3">
                <h1 class="headersize">asd</h1>
                <p class="small-text">
                 Small text
                </p>
              </div>
 <img src="https://encrypted-tbn0.gstatic.com/images? 
  q=tbn:ANd9GcQzp0rXN9BsNzMPYmzdT6xWc931So2I5sA4YiBX4Nm0rT6MiPBS" class=" 
  mb-4" />
            </div>
            <div class=" mb-3">
              <button
                type="button"
                class="readmore p-2 m-2    "
              >
                Read More
              </button>
            </div>
          </div>
</div>
  </div>
  
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>

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

When there are more than two divs on the page, they tend to overflow beyond the boundaries of

I am managing a platform where users have the ability to share text-based posts, and other users can engage with those posts through comments. An issue I have encountered is that once more than two comments are posted on a parent post, they start to overfl ...

What could be causing my Ionic button to not initialize in the expected state while using ngIf with a boolean property connected to an Ionic checkbox?

I'm currently in the process of setting up a list of ingredients with checkboxes and conditional buttons, but I'm facing some challenges with the default state. Ideally, I only want the button to be visible when the checkbox is unchecked so that ...

Creating an invoice or money receipt using HTML is a straightforward process that involves using specific code and

Currently, I'm in the process of developing an application for a land developer company. The administrator of this application will be responsible for creating invoices, money receipts, and bills. I am looking to design these documents to resemble th ...

elements that do not adhere to set width constraints

I've successfully arranged elements in my HTML using flexbox to achieve a responsive design for a website. However, I've encountered an issue where the elements do not resize properly. After applying a class of flex to the home-promos div and re ...

Generating containers in Angular using *ngFor

As a beginner in Angular, Bootstrap, and front-end programming, I am looking to create multiple boxes that reveal detailed information when clicked. This is the desired outcome: https://i.sstatic.net/Ak0eA.png After clicking on box number 1: https://i.s ...

What is the reason behind the default disabling of bootstrap multiselect options?

Just diving into the world of bootstrap and I'm puzzled as to why my simple multiselect options aren't responding when clicked. UPDATE The first multiselect option on the test-site linked below is the one giving me trouble. I've tried it o ...

"Key challenges arise when attempting to execute the node app.js script through the terminal due to various middleware compatibility

I'm a beginner with node.js and I've encountered an issue while trying to run my node app.js file after incorporating a new file named projects.js which contains the following JS code: exports.viewProject = function(req, res){ res.render(" ...

Listening to a song on a webpage while filling out the online form

Seeking advice on integrating audio file selection in form: <form ....> <input type="file" name="file-main" id="file-main" class="inputfile inputfile-3" multiple/> <audio preload="none" src=""> // excluding submission buttons for brevi ...

Utilizing a fallback option for HTML5 video with a flash player and the ability to manipulate the

My dilemma involves an HTML5 video where I am utilizing jquery to interact with the player using the code snippet: video.currentTime += 1;. However, when Internet Explorer switches to Flash plugins, my JQ commands cease to function. How can I manage and co ...

Problem with CSS: In Chrome, text fields have 3px additional margin-right

https://i.sstatic.net/I8mzi.jpg I am facing an issue with the margins of my text fields. Even though I have set a margin-right of 5px for each field, Google Chrome seems to be adding an additional 3-4px automatically. This problem is evident when I dynami ...

The fonts in node.js are not functioning as expected, without displaying any error messages

I'm having trouble implementing custom fonts on my website. Despite following the correct file structure, the fonts do not seem to be loading. My project files are organized in the following manner: https://gyazo.com/5ee766f030290e5b2fa42320cc39f10b ...

Individually hover over each section with CSS effects

Hey there, I'm trying to create something similar to this: photo using divs and css. I need to make each area hover separately. So far, I've attempted the following code, but it's not working as expected because it only activates two block ...

How can I define the boundaries for the scrolling of a static background image?

Is there a way to limit how far a fixed background image can scroll down a page? Essentially, is it possible to change the background attachment to static when the bottom of the background image is 10px away from the bottom edge of its div container? In t ...

Is there a way to combine all referenced pages into the main page using JQuery?

Is there a way to dynamically replace external CSS and JS file references in an HTML file with the actual contents of those files using Jquery or straight JavaScript? For example, instead of: <link rel='stylesheet' id='style-css' ...

Discovering the exact distance between a 'li' and a 'div' element

Currently, I am in the process of converting HTML to JSON. Here is an example of the HTML I am working with: <div class="treeprofit" id="divTreeViewIncomeDetails" style="height: auto;"> <li><span class="fa fa-folder-open highlight" ...

``It has come to our attention that ALT texts are not displaying properly on popular email clients such as Outlook,

Despite following all the email standards and attempting to style the ALT tag, most major email clients do not display the ALT text when images fail to load. Is there a workaround for this issue or does it primarily depend on the specific email engine bein ...

Create spinning wheel - canvas

Hey there! I'm trying to create a cool spinning wheel using a canvas and JS. My wheel is supposed to have 10 segments, each saved as a .png file. https://i.sstatic.net/glN1p.jpg In order to achieve a "full circle", I want to draw these 10 segments i ...

A problem arose with Vitest when attempting to call the tRPC createCaller function due to the presence of incorrect JS syntax within the content. To resolve this issue, it is recommended to include

Currently, I am in the process of writing unit tests with Vitest and utilizing the tRPC createCaller to simulate the trpc context. However, I am encountering a failure during testing which presents the following error message: Error: Failed to parse sourc ...

Move an object in relation to the right boundary of the viewport

I have a div that starts off the screen due to this CSS code: transform: translateX(100%); Now, I am looking to bring this div back into view by a specific distance from the right side. Although I have tried using transform: translateX(-450px), it seems ...

Use jQuery to rearrange the order of elements on a webpage by moving a paragraph after a specific

I'm currently experimenting with Codeacademy and I was given a task to move a new paragraph after a div. Here's what I did: $('#one').after('<p>this is jquery</p>'); Now I want to move the new paragraph after ano ...