How can I ensure that Div and its contents are only responsive to changes in width?

Initially, I'm feeling a bit confused but I'll try my best to articulate my issue and what I need.

Within a div element, there's another div that I want to move left and right only, following the same path as an image when resizing (refer to my project).

How can I ensure that the div behind the white circle always moves in sync with the circle?

body {
  margin: 0;
  width:100%;
}

body > div {
  height: 200px;
}



.header {
  background-color: transperent;
  height: 100px;
  color: white;
}

.product {

  margin-top:0px;
  height: 600px;
   background-color: blue;
  color: white;
  float:left;
  width:50%;
  margin:0;
  padding:0;
  display: inline-block;
}

.product2 {
  height: 600px;
  margin-top:0px;
  background-color: red;
  color: white;
  width:50%;
  float:left; 
  margin:0;
  padding:0;
  position: relative;
  display: inline-block;
}

.product2 img{
  position: absolute;
  right: 0;
  bottom: 0;
}

.main{
background-image: url("http://i.imgur.com/Y5hHusa.png");
height:650px;
}

#crew {
  height:50px;
  clear:both;
  background-color: tomato;
  color: darkgrey;
}

.stick {
    position: fixed;
    top: 0;
}

.tour {
  background-color: black;
  color: darkgrey;
}

.pricing {
  background-color: gold;
  color: black;
}

.contact {
  background-color: black;
  color: white;
}

.menu {
    float: right;
    font-size: 18px;
    list-style: outside none none;
    margin-top: -5px;
margin-right: 50px;
}

.menu li a {
    color: blue;
    display: block;
    text-decoration: none;
}

.menu li {
    display: inline;
    float: left;
    padding-right: 23px;
}
 
.menu li a:hover{
background-color:none;
color:red;
}

.div_form {

height:35%;
width:40%;
margin-top:36%;
margin-left:41%;
background-color:blue;
}

.product2 .div_form{

}

.product2 .div_form .form_title{
position:absolute;
z-index:1;
margin-top:270px;
margin-left:1em;
font-size:3em
}

.product2 .div_form .form_circulo{

z-index:1
}

.product2 .div_form .div_email .input_first_email{
margin-top: -70%;
margin-left:50%;
height:3em;
border-radius:5px;
padding:1em;
width:45%;
}

.product2 .div_form .divbtnsubmit{
background-color:red;
margin-left:60%;
width:20em;
height:3em;
border-radius:1em;
text-align:center;
margin-top:1em;
width:45%
}

.product2 .div_form .divbtnsubmit .btnsumnitform
{
font-size:2em;
color:white;
position:absolute;
padding:.3em;
margin-left:-3.5em
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <link href="styles.css" rel="stylesheet" type="text/css" >
  <title> Layout </title>
</head>
<body>

<div class="main">
<div class="header">
  <img src="http://i.imgur.com/48nYArD.png">
  
  <ul class="menu">
<li><a href="#">Home &nbsp;&nbsp;&nbsp;</a> </li>
    <li><a href="#">Product Tour&nbsp;&nbsp;&nbsp;</a> </li>
    <li><a href="#">Pricing&nbsp;&nbsp;&nbsp;</a> </li>
    <li><a href="#">Try&nbsp;&nbsp;&nbsp;&nbsp;</a></li>
    <li><a href="#">Vision</a></li>            
   </ul>
</div>

  <div class="product">
  </div>
  <div class="product2">
       <img src="http://i.imgur.com/3UTs03w.png">
<div class="div_form">

</div>       
</div>                   
</div>
  </div>
    
  <div id="crew">
  </div>
  <div class="tour">
  </div>
  <div class="pricing">
  </div>
  <div class="contact">
</body>
</html>

PS: Apologies for any confusion, English isn't my strong suit. Thank you for understanding. Regards, Duarte Andrade.

Answer №1

The issue at hand is attempting to vertically position the div_form div by applying a margin-top of 36%. However, it's important to note that when using a percentage value for margin, it is relative to the width of the container, not its height. For more information, refer to the W3C source.

A straightforward solution is to calculate the exact margin based on the known height of the container (product2) which is 600px. Therefore, setting the margin to 36% of 600px equals 216px.

body {
  margin: 0;
}
.product {
  height: 600px;
  background-color: blue;
  color: white;
  float: left;
  width: 50%;
  margin: 0;
  padding: 0;
  display: inline-block;
}
.product2 {
  height: 600px;
  background-color: red;
  color: white;
  width: 50%;
  float: left;
  margin: 0;
  padding: 0;
  position: relative;
  display: inline-block;
}
.product2 img {
  position: absolute;
  right: 0;
  bottom: 0;
}
.div_form {
  height: 35%;
  width: 40%;
  margin-top: 216px; /* Adjusted margin value */
  margin-left: 41%;
  background-color: blue;
}
<div class="main">
  <div class="header">

    <div class="product">
    </div>
    <div class="product2">
      <img src="http://i.imgur.com/3UTs03w.png">
      <div class="div_form">

      </div>
    </div>
  </div>
</div>

Alternatively, if the goal is to use a percentage of the parent element's height, the margins can be removed and the div given a position:absolute similar to the img. This approach allows for specifying the positioning with properties like left:41%; top:36%, although adjustments to z-indexes may be necessary to ensure proper stacking order.

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

Upgrading from Vuetify 2 to 3 involves replacing the deprecated styles like .v-application, rounded, and flat with

I need help with upgrading from Vuetify/Vue 2 to Vue 3. I don't have much experience in front-end development, but I am responsible for updating some old code to keep things running smoothly. The migration guide is not very clear and assumes a certain ...

Issue with vertical alignment within nested divs

I have been attempting to center a search bar inside a top banner vertically. My understanding was that the following code could achieve this: #banner { height: 35px; width: 100%; } #searchbar { height: 15px; position: relative; top: 50%; ...

What is the functionality of 'min-height: 100vh' when child elements stack due to a small width?

In a parent element, I have two child elements: <div id="registration"> <div id="left-panel"></div> <div id="right-panel"></div> </div> The styling is as follows: #registration{ @include l { flex-dire ...

What is causing the chat-widget to display a null value for the style read property?

Could someone assist me with hiding the Widget-chat? I keep getting an error that the property of style is null. Any help would be greatly appreciated. Thank you in advance. document.getElementById("chat-widget").style.display='none'; ...

What is the best way to use appendChild within an AJAX get function to manipulate the DOM

I've been working on a code function where I try to append a list item (li) into the HTML received in 'msg', but it's not functioning properly. Any ideas why? function handleFileSelect(evt) { var files = evt.target.files; $(&ap ...

Can you explain the steps to implement this feature in a modal window using jQuery?

My HTML list contains a bunch of li elements. ul li img .det li I am looking to create a modal popup that appears when I click on something. I want this modal popup to have previous and next buttons, preferably implemented using jQuery. I have alr ...

Troubleshooting: Issues with Integrating Javascript and CSS into

I'm still new to this platform, so please correct me if I make any mistakes. My goal is to create a "task table" that allows users to edit existing tasks and add new ones. Thanks to the base template provided by Ash Blue, I've managed to program ...

Is there a way for me to manage the appearance of the printed document's layout?

I have successfully added 3 print buttons to my website, each one designed to print a specific portion (div) of the webpage. Here is the code snippet for this functionality: function printPage(id) { var html="<html>"; //html+="<link rel="st ...

Tips for designing a three-line label: positioning text in the middle, above, and below

I have a label that looks like this: Is there a way to position 'Lorem ipsum' perfectly in the center between 'dolor' and 'amet'? - reference image This is what I've attempted: .gp-label { vertical-align: m ...

What is the best way to set the value of the days in a month to a div

I've been experimenting with creating a calendar using javascript, HTML, and CSS on my own. I managed to achieve some functionality like obtaining the current month and year as well as the previous month and year by clicking a button in HTML. However, ...

Adjusting the background color of the custom input range thumb when the input is disabled

I've customized the input thumb on my range slider, and I'm looking to change its color when it's disabled. I attempted adding a class to the thumb like this: input[type=range]::-webkit-slider-thumb.disabled and also tried adding the disa ...

Ways to monitor and measure clicks effectively

Within my website, I have a table element with one column and numerous rows. Each row serves as a hyperlink to an external shared drive. <tr><td ><a href="file://xxx">Staff1</a></td></tr> <tr ><td ><a h ...

Connecting text boxes with JavaScript and JSON for gaming experience

I am currently developing a game and have encountered a slight issue. Currently, there is a text box in the game that prompts the player to run into it to progress to the next level. When the player does so, the next level loads seamlessly, which works per ...

Creating a CSS animation to slide a div outside of its container is

I currently have a flexbox set up with two adjacent divs labeled as DIV 1 and DIV 2. By default, both DIV 1 and DIV 2 are visible. DIV 2 has a fixed width, occupying around 40% of the container's width. DIV 1 dynamically adjusts its width to ac ...

Several pictures are not displaying in the viewpage

I have a controller method set up to fetch files from a specific folder. public JsonResult filesinfolder(ProductEdit model) { string productid = "01"; string salesFTPPath = "C:/Users/user/Documents/Visual Studio 2015/Projects/root ...

ISO 8 takes a different approach by disregarding the meta viewport tag and autonomously adjusts the website layout to

<meta name="viewport" content="width=device-width,initial-scale=0"> I am facing an issue with my code not running on iPhone 6 and causing a problem with the responsiveness of my site. Any suggestions on what steps I should take? The site is constru ...

Tips for implementing automatic line wrapping in a PDF document using React-PDF

I am experiencing an issue with a PDF rendering where very long words are not wrapping onto a new line, instead overflowing the container and disappearing off the page. Below is the setup causing this problem. The styles are listed followed by the actual ...

Remove the excess white space surrounding the header background image on the website above the fold

How do I eliminate the white spaces on the left, right, and top of my background image that covers the header area above the fold on my webpage? I am currently testing in Google Chrome and using VS Code as my editor. html, body { width: 100%; height: ...

Is it a scope issue if ng-click is not firing and the function is not working properly?

I'm facing an issue with an animation that is not working as intended. The goal is to have the search button trigger an animation to pull a search bar from the right side, allowing users to input their search query. However, the ng-click function does ...

Incorporate text directly into Bootstrap select input on a single line

I am attempting to include an asterisk in a select input field. I can achieve this easily by applying my own CSS, but the challenge is to accomplish this using Bootstrap 3.3.7 or an older version while displaying the asterisk on the same line as shown in t ...