What is the best way to specify the maximum margin-left value?

I have a website with a responsive design, where the body has been set to max-width: 500px, and there is a div named 'arrow' with a margin-left of 45%. However, when the screen size exceeds 500px, the body remains fixed but the arrow div starts moving in the wrong direction.

Is there a way to limit the margin-left value so that the arrow div does not move past a certain point even if the screen size increases further? Below is the CSS code:

.arrow {
background-color:#ECEFF5;
width: 30px;
margin-top: -12px;
position: absolute;
margin-left: 45%;
}

html, body {
margin: 0 auto;
}

body {
background-color: #ECEFF5;
font-weight: lighter;
max-width: 500px;
}

Here is the HTML code:

<body>  
    <div class="userdata">
        <h2 style="font-weight:bold;"> First Name</h2>
        <input class="input" type="text" name="firstname">

    </div>
    <div class="arrow">
        <img src="arrow.png" style="position:absolute; width:   30px;">
    </div>
    <div class="instructions" id="test">
        <h5>Step 1/7</h5>
        <hr width="100%">
        <h6 >Write your name here</h6>
        <a href="xyz.html" class="button-nav" > Back </a>
    </div>

Answer №1

To ensure the margin is based on the body rather than the window, include position:relative; in the body CSS styles. This will set the margin to be 45% of the body.

For example:

.arrow {
  background-color: red;
  width: 30px;
  margin-top: -12px;
  position: absolute;
  /* margin-left: 45%; */
  left: 45%; /* <- why not just position left rather than using a margin? */
}
html, body {
  margin: 0 auto;
  height: 100%; /* <-just for demo */
}
body {
  background-color: #ECEFF5;
  font-weight: lighter;
  max-width: 500px;
  border: 1px solid red; /* <-just for demo */
  position: relative;  /* <- make relative */
}
<div class="arrow">arrow</div>

Answer №2

experiment by using %50 margin-left and subtracting half the width for center alignment

  .pointer {
    background-color:#ECEFF5;
    width: 30px;
    margin-top: -12px;
    position: absolute;
    margin-left: 50%;
    left:-15px;
    }

Answer №3

Update the CSS code by removing position: absolute and setting margin-left to auto; Here is the revised code:

.arrow {
  background-color:#ECEFF5;
  width: 30px;
  margin-top: -12px;
  margin-left: auto;
}

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

The Alert Component fails to display when the same Error is triggered for the second time

In the midst of developing a Website using Nuxt.js (Vue.js), I've encountered an issue with my custom Alert Component. I designed a contact form on the site to trigger a specialized notification when users input incorrect data or omit required fields ...

Adding margin to an HTML element causes the entire page to shift downward

Despite successfully applying margin to other elements, I encountered an issue with #searchbarcontainer where it causes the entire page to shift downward. My goal is to center it slightly below the middle of the page, but I'm struggling to find a solu ...

Combining two HTML tables using jQuery/JavaScript

My table is displayed below: <table id="first" class="merge"> <tr> <td>Mick Jagger</td> <td>30</td> <td>50</td> <td>10</td> </t ...

Guide for setting the value of an input field using an unordered list generated from ajax in Selenium Webdriver with C#

The HTML structure of the form input is as follows: <span> <input type="hidden" name="color"> <!-- start async created --> <ul> <li value="green">Green</li> <li value="blue">Blue</li ...

responding to a forum comment in jquery by either replying or quoting

I'm attempting to develop a forum "quote" feature, similar to reply options on many forums. However, I am struggling with selecting the appropriate items from my comment and dealing with dynamically generated IDs. Here is the HTML of my comment: & ...

Require presenting only four lists - slideDown in jQuery

I am trying to display only 4 out of 8 lists initially, and upon clicking a "more" button, the rest of the list should be revealed. Can someone please assist me with this? Below is the jQuery code: $(document).ready(function() { $("#accordion").hide(); ...

Can a specific element be chosen based on its background color?

Is it possible to change the background color of a child element within a div that has a specific background color using CSS? See my explanation below. For example: .container[background-color=some color] .content { background-color: some other color ...

Issue with webkit-box-reflect causing errors on Android devices

I have been attempting to incorporate a reflection effect on a mobile webview application (specifically for Android and iOS using webkit). I followed examples I found on various websites, including Stack Overflow. "-webkit-box-reflect: below -14px -webkit ...

What could be the reason behind the issue of my HTML draggable feature not functioning properly on touch

I've set up a draggable div with headers and p tags, but I'm encountering an issue when trying to run it on a touch screen. The div tag isn't draggable in this scenario. Can anyone suggest the best solution for making this page touch screen ...

Utilizing V-model with a div element component

Encountering an issue with using v-model on div elements. It seems that div tags do not support v-model, prompting me to create a separate comment section component. I opted to use this div text area for better UI/UX design. Traditional form input tags lik ...

Executing PHP function from an HTML form

Despite trying multiple suggestions provided by this site, I have been unsuccessful in finding a solution to my problem. Here's the issue at hand: I have a page where all links are loaded through AJAX, including a profile page that fetches data from a ...

Easily navigable pages equipped with the R Kable feature for a

Can kable help in achieving this specific behavior? https://i.sstatic.net/AuCcJ.png I am interested in creating an R Markdown document with HTML output that allows me to navigate between pages of a table. ...

How to center two divs side by side horizontally using Bootstrap

How can I make the layout work on screens 575px and below with the class "col-xs-6 col-sm-6 col-md-8"? <div class="container"> <div class="row align-items-center"> <div class=&q ...

Assigning event to the body element

Is there a way to bind an event to the entire page, specifically the html or body tag? I am trying to achieve the following: document.addEventListener('mousemove', function() { alert('a'); }); I want an alert to be triggered whenever ...

What is the method for showcasing AM and PM in various languages within the input type of time?

I am using the input type="time" like this: <input type="time" id="openTime" name="openTime" value=""> My inquiry is regarding how to localize the display of AM - PM in another language? ...

HTML5 offers the ability to display multiple lists side by side with different

Looking to align three different types of lists next to each other: an unordered list, an ordered list, and a description list. I've experimented with various methods but haven't had success getting them to display side by side. If anyone has ins ...

Modifying CSS to ensure compatibility with various browsers

I am curious if it is possible to modify some CSS when viewed in a different browser. For instance, I have this CSS for a flexbox div: .wdFlex { display: -webkit-box; display: -moz-box; display: -ms-flexbox; display: -webkit-flex; display: flex; -webkit- ...

What is the best way to re-render a component immediately following an update in React?

As I attempt to change the color of a bar to green and then back to black, it seems like the latter color update is taking precedence in my code. const [color, setColor] = useState("black") const bubbleSort = async () => { const sleep = ms => ...

Customizing the appearance of charts in AngularJS using the Chart.js

I just started experimenting with AngularJS and recently created a horizontal bar chart using Chart.js and HTML. My next step is to make the chart dynamically appear on the page with the help of AngularJS. Can someone please provide some guidance on how I ...

Troubleshooting Double Scrollbar Issue in Angular Material Design Page Footer

I've implemented a page footer in the following way. HTML <footer class="app-footer-main"> <section class="app-footer-items"> ... </section> </footer> Styles .app-footer-main { background-color: ...