Changing `margin-top` will also affect adjacent sibling elements

Currently, I am in the process of designing an About page for my website. Within this layout, there is a main parent element named maindiv, which utilizes a flexbox to house two children: leftcont and rightcont. My goal is to implement an animation using margin-top specifically to affect the leftcont child element only.

However, upon attempting to create this animation, I have encountered the issue where the animation also affects its sibling (rightcont) div, which is not intended behavior.

Could somebody kindly assist me with resolving this dilemma? Below is the code snippet in question:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      .imganim {
        margin-bottom: 10px;
        border: 2px solid red;
        transition: 2s;
      }

      .maindiv {
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
      }

      .leftcont {
        width: 50%;
        border: 2px solid red;
      }

      .rightcont {
        width: 50%;
        animation: none;
        border: 2px solid black;
        margin-top: 0 !important;
      }

      .marginanim {
        animation: margin 2.4s infinite;
      }

      @keyframes margin {
        0% {
          margin-top: 0;
        }

        50% {
          margin-top: 10px;
        }

        100% {
          margin-top: 0;
        }
      }
    </style>
  </head>

  <body>
    <div class="maindiv">
      <div class="leftcont marginanim">
        <img
          src="https://solutionfest.netlify.app/static/media/vector8.5596c5d75a5f1c4454c0.jpg"
          alt=""
        />
      </div>
      <div class="rightcont">
        <h2>Aboutme</h2>
        <p>
          Lorem ipsum dolor sit amet consectetur adipisicing elit. Animi,
          obcaecati similique? Eligendi sint, laborum aspernatur, temporibus
          consequuntur ipsa officiis, praesentium distinctio necessitatibus sed
          debitis eius facilis molestiae. Consectetur laboriosam veritatis quis
          perspiciatis ab quae sunt nam itaque veniam mollitia obcaecati non
          minima molestias illo cum, ducimus soluta modi inventore consequuntur.
        </p>
      </div>
    </div>
  </body>
</html>

Answer №1

Adjusting the margin-top property can also impact the size of the parent element, which in turn affects the layout of rightcont. To avoid this issue and achieve the desired visual effect, consider using the transform: translateY property.

* {
  margin: 0;
  padding: 0;
}

.imganim {
  margin-bottom: 10px; 
  border: 2px solid red;
  transition: 2s;
}

.maindiv {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

.leftcont {
  width: 50%;
  border: 2px solid red;
  
  z-index:0;
}

.rightcont {
  width: 50%;
  animation: none;
  border: 2px solid black;
  margin-top: 0 !important;
  
  z-index:1;
}

.marginanim {
  animation: margin 2.4s infinite;
}

@keyframes margin {
  0% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(10px);
  }
  100% {
    transform: translateY(0px);
  }
}
<div class="maindiv">
  <div class="leftcont marginanim">
    <img src="https://solutionfest.netlify.app/static/media/vector8.5596c5d75a5f1c4454c0.jpg" alt="">
  </div>
  <div class="rightcont">
    <h2>Aboutme</h2>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Animi, obcaecati similique? Eligendi sint, laborum aspernatur, temporibus consequuntur ipsa officiis, praesentium distinctio necessitatibus sed debitis eius facilis molestiae. Consectetur laboriosam
      veritatis quis perspiciatis ab quae sunt nam itaque veniam mollitia obcaecati non minima molestias illo cum, ducimus soluta modi inventore consequuntur.
    </p>
  </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

Automatically appending textarea value in HTML tag upon form submission via JQuery

When a form is submitted through JQuery, the HTML tag automatically adds textarea value. This is my JQuery code: $('#calling').click(function() { $('#myform').submit(); }); Within my form, there is a textarea element: <textar ...

"Exploring the variations in design between wordpress.com and self-hosted wordpress

I'm currently puzzling over the source of the disparities in style between my Wordpress site hosted elsewhere and my identical theme on Wordpress.com. It's perplexing to see such contrasting appearances while I migrate my blog. After some irrele ...

What specific element is being targeted when a directive injects a ViewContainerRef?

What specific element is associated with a ViewContainerRef when injected into a directive? Take this scenario, where we have the following template: template `<div><span vcdirective></span></div>` Now, if the constructor for the ...

Using Column CSS Property with Google Maps in Chrome: A Potential Bug?

I'm facing an interesting issue and I could use some help understanding or fixing it. It seems like there might be a bug or a solution to this problem. I am currently working on a layout using the CSS3 property columns. Everything looks fine in Firefo ...

How can I successfully store multiple file uploads and additional data simultaneously within a single form using php, jquery, and mysql?

Check out this form View From <form class="form-horizontal" role="form" id="frm_bulletin_board" method="post"> <div class="form-group"> <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> Title <font sty ...

Steps to transfer the content of a label when the onclick event occurs

Seeking advice on how to send the dynamically varying value of a label upon clicking an anchor tag. Can anyone recommend the best approach to passing the label value to a JavaScript function when the anchor is clicked? Here is a sample code snippet: < ...

Tips for eliminating checkboxes from a form

function addCheckbox(){ var labels = document.form1.getElementsByTagName('label'); var lastLabel = labels[labels.length-1]; var newLabel = document.createElement('label'); newLabel.appendChild(Checkbox(labels.length)); ...

Disable automatic playback of HTML video

There is an HTML video with an image that loads initially and then disappears to play the video. I would like the image to always be visible until I click on it. Once clicked, the video should start playing. You can view the code on JSFiddle: http://jsf ...

The Firebase JQuery .on method is incrementally updating individual values in an array instead of updating them all simultaneously

I am trying to update the values of the orders placed by users on the Corporate's page without a refresh. I have implemented the jQuery .on method for this purpose. However, the values are being returned one by one from the array created for the order ...

Error: Unable to retrieve the value of a null property | ReactJS |

There are two textboxes and one button designed as material UI components. </p> <TextField id="chatidField" /> <br /> <p> <code>Enter Your Name:</code> <hr /> & ...

Is there a way to embed an AJAX submit form into a notification without the need for page refresh?

I have integrated the jQuery plugin toastr js () for notifications on my website. I am facing an issue where I want to include a contact form within the notification popup and submit it using AJAX. Despite having the form working fine outside of the toastr ...

The MDL layout spacer is pushing the content to the following line

Here's an interesting approach to Material Design Lite. In this example from the MDL site, notice how the 'mdl-layout-spacer' class positions elements to the right of the containing div, giving a clean layout: Check it out <!-- Event ca ...

The Javascript calculation function fails to execute proper calculations

I have been facing immense frustration while working on a project lately. The project involves creating a unique Webpage that can calculate the total cost for users based on their selections of radio buttons and check boxes. Assuming that all other functi ...

What is the best way to position the publication date of an article at the bottom of the

I am looking to have the article publishing date (1/9/2016) positioned at the bottom inside the container. Right now, the date appears just below the text, but I want it to be aligned at the bottom of the container. The website in question is watchathletic ...

I am seeking to enable a specific div when the crawler condition is met

This specific div houses the character and becomes active when the character is clicked. Upon clicking, a jQuery function is triggered to display other countries. //this particular div should remain active when the crawler condition is met. It displays th ...

Python web scraping with BeautifulSoup to gather information

After searching on zillow, I aim to extract all items from the unordered list and target a specific class: "StyledPropertyCardDataWrapper-c11n-8-84-3__sc-1omp4c3-0 bKpguY property-card-data" This class contains information such as price, address ...

Is there a method to implement a pop-in animated contact form without the use of Javascript or query selectors?

Although I was successful in creating a contact form with this functionality using Javascript, I encountered some difficulties when attempting to achieve the same result without it. My goal is for the form to slide in from the right when the "open" icon is ...

Issue with the reload animation jumping too quickly in Framer Motion on NextJS

While creating an animation for my landing page using NextJS with framer motion, I encountered a strange behavior after deploying the site. The animations started to happen too quickly and it felt a bit off. During development on localhost using Chrome, ev ...

jQuery function failing to produce the intended results

I am trying to apply the active class to the last img element using HTML and jQuery Below is my code in HTML and jQuery: function initialSteps() { $(".row").find("column:last").find("img:first").className += " active"; // this does not work either ...

What is the method for obtaining receipt numbers in sequential order, such as the format AB0810001?

Is AB the receipt code that should remain constant followed by the current date (08) and 10001 as the receipt number? ...