Transition effects in CSS for HTML cards

I recently designed a card using HTML where the text only appears when hovered over. I am now looking to modify the HTML and CSS so that the text div is displayed when the parent div is being hovered over.

.bg {
  background-color: white;
  width: 200px;
  height: 300px;
  border: solid red 2px;
  overflow: hidden;
}

.text {
  background-color: gray;
  position: relative;
  top: 180px;
  transition: top 0.5s;
}

.bg:hover .text{
  top: 132px
}
<div class="bg">
  <div class="text">
    <h3>Test text</h3>
    <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Reiciendis voluptas pariatur qui aut animi molestiae quia veritatis culpa excepturi. Nesciunt optio ipsa molest</p>
  </div>
</div>

Answer №1

Try moving the cursor over the parent element. For example, .bg:hover > .text

.bg {
  background-color: white;
  width: 200px;
  height: 300px;
  border: solid red 2px;
  overflow: hidden;
}

.text {
  background-color: gray;
  position: relative;
  top: 180px;
  transition: top 0.5s;
}

.bg:hover > .text {
  top: 132px
}
<div class="bg">
  <div class="text">
    <h3>Sample text</h3>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Reiciendis voluptas pariatur qui aut animi molestiae quia veritatis culpa excepturi. Nesciunt optio ipsa molest</p>
  </div>
</div>

Answer №2

The reason for this behavior is that the selector is defined as .text:hover. This means that the styles are applied to the element being hovered over, which in this case is .text.

To achieve the desired effect, you should use .bg:hover .text to target and modify the .text element.

Answer №3

Here is a suggestion for you to try:

.bg {
  background-color: white;
  width: 200px;
  height: 300px;
  border: solid red 2px;
  overflow: hidden;
}

.text {
  background-color: gray;
  position: relative;
  top: 180px;
  transition: top 0.5s;
}

.bg:hover > .text {
  top: 132px
}
<div class="bg">
  <div class="text">
    <h3>Test text</h3>
    <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Reiciendis voluptas pariatur qui aut animi molestiae quia veritatis culpa excepturi. Nesciunt optio ipsa molest</p>
  </div>
</div>

Answer №4

To activate the hover effect, simply add the .text class after .bg:hover .text{}

.bg {
  background-color: white;
  width: 200px;
  height: 300px;
  border: solid red 2px;
  overflow: hidden;
}

.text {
  background-color: gray;
  position: relative;
  top: 180px;
  transition: top 0.5s;
}

.bg:hover .text {
  top: 132px
}
<div class="bg">
  <div class="text">
    <h3>Test text</h3>
    <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Reiciendis voluptas pariatur qui aut animi molestiae quia veritatis culpa excepturi. Nesciunt optio ipsa molest</p>
  </div>
</div>

Answer №5

To create a cool transition effect, you can add a sibling div with the text class to your existing div setup. By adding a parent div that wraps both divs and applying a transition to the parent div, you can achieve the desired animation.

<style>
        .bg{
            background-color: white;
            width: 200px;
            height: 300px;
            border: solid red 2px;
            overflow: hidden;

        }
        .text{
            background-color: gray;
            position: relative;
        }

        .inner{
            height: 200px;
        }
        .outer{
            position: relative;
            top: 2px;
            transition: top 0.5s;
        }
        .outer:hover{
            top: -65px;
        }
    </style>



<div class="bg">
        <div class="outer">
            <div class="inner"></div>
            <div class="text">
                <h3>Test text</h3>
                <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Reiciendis voluptas pariatur qui aut animi molestiae quia veritatis culpa excepturi. Nesciunt optio ipsa molest</p>
            </div>
        </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

Issue in Chrome when using CSS clip property on nested div elements

Take a look at this fiddle Here is the HTML structure: <div class="parent"> <div class="child"></div> </div> This is the corresponding CSS code: .parent { position: absolute; width: 100px; height: 100px; background: re ...

Using Jquery to dynamically add a textbox with a display:none property set to its value

In this scenario, I have added dynamic rows to a table with textboxes and span tags. Now, I am performing some calculations. In the demo, you will see a textbox for a multiplier factor. Upon change, the last column in my table (Calculated area provided) i ...

Create a React MUI component that allows menu items to become sticky when selected

Is there a way to make the mui MenuItem stay sticky to Select while scrolling down? To see the issue in action, check out this codesandbox example https://codesandbox.io/s/quirky-knuth-5hr2dg?file=/Demo.tsx Simply click on the select and start scrolling ...

What is the best way to save and retrieve JavaScript variables on a Blogger page that includes a poll, without needing to host the blog on my own server?

I want to ensure that the valuable feedback provided by users in the poll is not lost. Although the code is a work in progress, the basic concept is there. I did my research before posting this question to avoid redundancy. <script type="text/javas ...

Adjust the floating menu to match the height of the content div

I'm facing an issue with the layout of my website - I have a main content div and a sidebar. The height of the content div adjusts based on the content being loaded, but I want the sidebar to always extend all the way down matching the maximum size of ...

Limit the text within a div to just two lines

I am facing an issue with the text size displayed in the image attached. Initially, the text is not styled with CSS. https://i.sstatic.net/00BmI.png To meet a requirement limiting the text to two lines only, I have implemented the following CSS code: { ...

What is the purpose of setting position: relative without altering the element's position?

As I delve into learning CSS, a question arises in my mind regarding the use of position: relative without specifying a specific location, such as left: 20px or top: 10px. Below is a snippet of CSS code that showcases this scenario. #home #header { ...

What is the method for adding an attribute without a value to an HtmlGenericControl in C#

In my C#/ASP.net project, I am creating a new HtmlGenericControl and I need to add an attribute to the control without assigning it a value. This means having no ="" after the attribute name. oHtmlGenericControl = new HtmlGenericControl("MyHtmlTag"); oHtm ...

Transforming bootstrap 4 rows into cards or a card deck

Having some difficulty configuring Bootstrap cards to display correctly. The original template had one item per row in columns, but I want to transform it into polaroid-style cards while still using Bootstrap 4 for a clean look. Original template (excerpt ...

Stop HTML <dialog> from automatically closing using Vue

I'm working on a project where I need to use Vue to programmatically prevent an HTML dialog element from closing when the close event is triggered. Here's the code snippet I am currently using: import {ref} from 'vue'; const dialogTe ...

Guide to positioning a toolbar within an element when hovering over it in HTML with the help of JQuery

function attachMenuBar() { $('body').on('mouseover mouseout', '*:not(.printToolBar)', function (e) { if (this === e.target) { (e.type === 'mouseover' ? setMenuBox(e.target) : removeMenuBox(e.t ...

Remove all occurrences of <div> elements within a <table> in an HTML document using a Bash script

I am trying to find a way to eliminate <div> and </div> tags in an HTML file, but only when they are nested between <table> and </table>. All other instances of <div>...</div> tags should remain untouched. The content wi ...

The text consistently remains positioned to the right of my image

Photo Gallery Title Issue I'm working on a photo gallery project where I want to add titles underneath each image. However, I'm facing a problem where the title is appearing next to the image instead of below it. You can see the issue in this sc ...

Displaying information based on selection in AngularJSIn this tutorial

I am a beginner in Angularjs and currently working on developing a website. One of the tasks I have is to create a combo box that, when an option is selected, calls a method to load values into a Scope variable and update it. Below is the code I have so fa ...

What is the best way to transform all elements on a webpage into a dynamic scrolling marquee?

Is there a way to make every div in a web application scroll like a marquee? Perhaps by using a specific CSS class or other method? The application is built with Angular 4 and Bootstrap. ...

What is the best way to reset the 'aria-expanded' attribute to 'false' as I navigate to the following element?

My menu has 2 dropdown buttons, each with a default 'aria-expanded'=false value. The buttons also have the 'up' and 'down' classes to indicate when they are expanded or closed. The issue arises when I click on the first button ...

Using JQuery to send a Post Request with a file attachment

After receiving a form file input with only 1 file, I attempted to submit it using the following code: $("#addBrand").validate({ rules: { brandName: "required", brandLogo: "required" }, messages: { ...

Need to specify a parameter type for the input tag in HTML

Currently, I am developing a customized focus method for my webpage using the following script: <script type="text/javascript"> function myFunction() { document.getElementById("name").focus(); } </script> I ...

Using variables for iteration in Django templates can provide a powerful way to display dynamic content in

I am currently working with Python and Django and I have a specific requirement. I want to take a variable that was entered in a previous input field and use it to iterate over creating multiple other input fields. Here is the section from my Django templ ...

How can I reset the validation method on a form when the first input field is clicked on? (Utilizing the jQuery Ajax Form Plugin and Magnific

Utilizing the jQuery Ajax Form Plugin in conjunction with the magnific popup plugin ajax type to update content dynamically. This is the form structure: <form method="post" action="/contact" id="contact_form"> <d ...