When the class name is hovered over, apply the specific style to the element

When I hover over my card(box), I want the description text to move up 10px like shown in the pictures. However, the city name text and button also shift for some reason.

View card without hover (IMG) Here is with hover effect (IMG)

In the second image, you can see that the button moves up as well.

Below is the HTML code for the card containing the text, city name, and button:

<div class="cards_container">
    <div class="card" id="card_oslo">
      <p class="cardCityInfo">
        Oslo is a cool place <br> where there are many <br>
        tourists year-round. <br> So come visit.
      </p>
      <h4 class="card_text">Oslo</h4>
      <a href="#"><h5 id="btn_card">See more</h5></a>
    </div>

Here is the CSS code attempting to style and create the :hover effect on the description text within the cards. ".card:hover .cardCityInfo" is where the hover effect is being applied specifically to the text:

.cardCityInfo{
      font-size: 1.1rem;
      font-family: 'Nunito', sans-serif;
      color: #fff;
      text-align: center;
      z-index: 5;
      margin-top: 100px;
      opacity: 0;
}

.card:hover{
      box-shadow: inset 0 0 0 1000px rgba(0,0,0,.7);
      transition: .5s;
}

.card:hover .cardCityInfo{
      opacity: 0.9 !important;
      transition: .5s;
      margin-top: 90px;
}

.card:hover .card_text{
      color: #fff;
      transition: .5s;
}

This is how the card appears in CSS:

.card {
      position:relative;
      width: 315px;
      height: 300px;
      background-size: contain;
      background-repeat: no-repeat;
      background-color: #fff;
      border-radius: 5px;
      margin: 20px 10px;
      float: left;
}

Answer №1

Adjusting the margin of an element will have a cascading effect on everything that comes after it in the document flow. To avoid this, you can utilize the transform property on the element instead. For example, instead of

.card:hover .cardCityInfo{
  margin-top: 90px;
}

you can achieve the same result by using transform:

.card:hover .cardCityInfo{
  transform: translateY(-10px);
}

Check out this demo: https://jsfiddle.net/1jgLzdn4/

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

How to activate an ASP.NET LinkButton using JavaScript

I've set up a page to hide a FileUpload control, so that when a button is clicked, it displays the file upload window and automatically submits the selected file to the server. To make this work, I created two linkbuttons and a file upload control, b ...

Tips for removing the gap between these two items using Bootstrap's grid system

Below is the login form code I have been working on: <main> <div class="container-fluid"> <div class="row d-flex justify-content-center align-items-center"> <div class="col-6"> ...

Nav Dropdown Beyond Container Bounds

I'm working on implementing a flexbox navigation menu with dropdowns for certain items. However, I've encountered an issue where the dropdown lists are appearing to the right of the container on hover, rather than below it as expected for a typic ...

Can links be integrated into Bootstrap 4 nav-pills tab-panel from another webpage?

I've managed to set up a service page with bootstrap 4's pills .nav-pills navigation, and it's functioning quite well. However, my current challenge involves allowing users to click on a link from the home page that will direct them to a spe ...

Using TypeScript to create a fullscreen video mode in HTML

Could someone provide guidance on enabling fullscreen mode when using the video tag? I am currently utilizing the following typescript: let vid = <HTMLVideoElement> document.getElementById('video1'); I want to play the video in fullscreen ...

Styling Django's buttons and search bars to appear inline using CSS

In my Django template, I have the following HTML code: <div id = "search_form"> <form action="" method="get" id = "search-form"> {% csrf_token %} {{ form.as_p }} {{ form.media }} </form> ...

When a user accesses a page, the UI-router shows the raw code in the UI-views

I am a newcomer to utilizing ui-router and I am facing challenges with managing routing and reloading some states. Any assistance would be greatly appreciated. An overview of my index.html <html ng-app="resApp"> <head></head> < ...

What is the best method to assign a CSS class to specific nodes within a TreeView using CodeBehind?

I need to apply unique styling to certain nodes in the codebehind. Within my TreeView, parents have two categories of children. One category matches the parent type (e.g. organizationalUnit), while the other does not (e.g. organizationalMembers). I aim t ...

When attempting to refresh, the global.Exp.Data.getLocalDataSet() function returns empty

In the first method, data is retrieved from the database and populated into the grid. The second method is used for viewing user details. When the user clicks on this method, it displays the user details. However, if the page is refreshed at that time, v ...

Swap out the text of one element with a different text content found within the DOM

I have a web app that features a template for displaying a list of books retrieved from the server using Python (Flask). The information about the books, including titles, IDs, and authors' names, is stored in the variable "books" which I then loop ov ...

What could be causing my sticky positioning to not function properly when I scroll outside of the designated

My website is organized into sections, with each section corresponding to a hyperlink in the navigation menu. I've applied the CSS property position: sticky to my navbar, which works as expected within the section bounds. However, I want my red navbar ...

Angular responsive button

Currently, I am working on an application using angular 8. In the app, there is a form that includes several buttons. I am looking for a solution to make these buttons responsive on mobile devices when viewed in Chrome and Firefox browsers. Here is the cur ...

Java cookies are utilized to download a file using JavaScript

When I attempt to download a file using Java code, I receive HTML code instead of the actual file contents. Interestingly, if I directly visit the download URL in a browser, the file automatically downloads: <html> <body> <script type="text ...

Render inline CSS styles with AngularJS

Can we use angular variables to write inline CSS styles? <div style="background: transparent url({{eventInfo.eventHeaderImg}}) top center no-repeat;"></div> Here is the output I received: "NetworkError: 404 Not Found - http://test/eventInfo. ...

issue: [astro preview] adapter lacks preview entry point

Recently, I encountered an issue while working on a website using astro (). When running astro preview, I encountered an error. Here is the code from my astro.config.mjs file: import { defineConfig } from 'astro/config'; import vercel from ' ...

Discovering the precise Offset value for a Div element

Looking for a way to identify the unique value of each div on my HTML page, even as offset values change with each refresh. Any suggestions or methods that can help achieve this? ...

Challenges with creating a responsive YouTube iframe using the API

Looking to integrate a Youtube video using the iFrame API to capture events effectively, embedding the player alone is not an option. Following the documentation, everything functions correctly with code like: var tag = document.createElement('scrip ...

Incorporating subtle shades of grey text within the credential fields through HTML coding

On certain websites, there is a greyish text displayed in the input boxes next to the required credentials, such as on Gmail. However, when you click on these boxes, the greyish text disappears. Below is an example code snippet in HTML that I have tried: ...

Issue encountered: difficulty with vertical alignment in table cell containing both text input and image

I am facing an issue with aligning two items (an input text and an image) within a cell of a table. Despite trying various methods like using vertical-align:middle, margin top, padding, and setting height to 100%, I haven't been able to achieve the de ...

Conceal an absolutely positioned element outside its relatively positioned parent

I have a relative position parent element, <div style="position:relative" id="a"> and within that div, I'm creating some absolute positioned elements using jQuery, <div style="position:absolute;right:0" id="b"> These elements are anima ...