How to stop a div from shifting downwards with CSS

Within my Web Application, I have implemented HTML code that displays different texts above input fields based on certain conditions.

However, the issue I am facing is that the input fields are being shifted down by the text. I want the input fields to always remain in the same position, regardless of whether the @if($numberBookings > 0) condition is true or not, or even if I decide to replace the text with something completely different.

.bookbox{
  padding-top: 10px !important;
  padding-bottom: 10px !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  background-color: #FFFFFF;
  margin: 16px !important;
  box-shadow:0 4px 10px 0 rgba(0,0,0,0.2),0 4px 20px 0 rgba(0,0,0,0.19);
  flex-wrap: wrap;
}

.bookcard{
  margin-top: 120px !important;
  margin-bottom: 120px !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  background-color: #FFFFFF;
  clear: both;
  width: 100%;
}

.float-left {
  float: left !important;
}

.m-1 {
  margin: 0.25rem !important;
}
<div class="bookbox">
        
            <h3 class="" align="center">
                Hello {{$user->firstName.' '.$user->lastName}},<br>
                @if($numberBookings > 0)
                    <font color="red">You have bookings that have already expired!<br> Please release the vehicles if you no longer need them</font>
                @else
                    Do you need a vehicle?
                @endif
            </h3>
            <div class="bookcard flex-column">
               <form class="w3-container" method="GET" action="{{config('app.PATH_TO_INDEX', '')}}/findCar">
                   
                   <div class="float-left m-1">
                       <input type="text" name="Location" list="Locations" class="w3-input w3-border {{ $errors->has('Location') ? 'border-danger' : '' }}" placeholder="Location">
                       <datalist id="Locations">
                           @foreach($cities as $city)
                               <option value="{!! $city->name !!}"></option>
                           @endforeach
                       </datalist>
                   </div>
                   <div class="float-left m-1">
                       <input type="text" name="StartDate" class="date w3-input w3-border {{ $errors->has('StartDate') ? 'border-danger' : '' }}" id="f0date" placeholder="Start Date">
                   </div>

                   <div class="float-left m-1">
                       <input type="text" name="EndDate" class="date w3-input w3-border {{ $errors->has('EndDate') ? 'border-danger' : '' }}" id="f1date" placeholder="End Date">
                   </div>
                   <div class="float-left m-1">
                       <button type="submit" class="w3-button margin-small signalButton w3-hover-text-blue w3-blue-grey w3-hover-blue-grey w3-hover-opacity" id="submit0">Find Vehicle</button>
                   </div>
               </form>
            </div>
    </div>

Answer №1

To ensure that an element does not disrupt the flow of other elements, it is recommended to apply position: absolute to it:

.container {
  /* Use me as the reference element for absolutely positioned children */
  position: relative;
  
  /* For demonstration purposes only */
  margin: 5px;
  width: 200px;
  display: inline-block;
}

.pos_absolute {
  position: absolute;
  /* Colors for demonstration purposes */
  white-space: nowrap;
  background: antiquewhite;
}

.top-left {
  top: 0;
  left: 0;
  /* Centered nicely (optional) */
  transform: translate(0%, -50%);
}

.top-center {
  top: 0;
  left: 50%;
  /* Centered nicely (optional) */
  transform: translate(-50%, -50%);
}

.content {
  background: coral;
  /* Add padding or margin to avoid overlapping with elements above */
  padding-top: .5rem;
}
<div class="container">
  <span class="pos_absolute top-left">I am positioned at the top left corner</span>
  <div class="content">I am a block element</div>
</div>
<div class="container">
  <span class="pos_absolute top-center">I am positioned at the top center</span>
  <div class="content">I am an inline-block element</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

Retrieve Object Key in Angular 7

I have a specific item: this.item = { name:"Michael", age:18 }; I am looking to display it in an HTML format. name: Michael age: 18 <div >{{ item (name) }}</div> --??? should put name <div>{{ item.name }}</div> < ...

problem arises when I attempt to use the code individually, as well as when I incorporate it into my existing

<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> <title>App Title</title> <!-- Framework's CSS Fil ...

What could be causing my Bootstrap navbar menu item to move to the line beneath it when there is centered text?

I've implemented a basic Bootstrap navbar from the example provided on the Bootstrap website at . However, when I tried to add custom text to the center of the navbar, it caused the "Contact Us" menu item to break onto two lines instead of staying on ...

Tips for avoiding infinite re-render loop when updating state in React/Next.js

I currently have a unique Next.js application that showcases randomly selected YouTube videos. The state within the app is structured as follows (managed by Redux): const state = { entities: { videos: { 'vidId1': { ...

The knockout.js subscribe function isn't activating during the initial set

In my model class, I have defined observables and I am looking to subscribe to their sets. Below is the code snippet that showcases what I currently have: var dto = function (data) { var self = this; self.Value1 = ko.observable(data.Value1); s ...

Bullet points colliding with one another

Is there a way to display an unordered list with list items in two rows and multiple columns without the bullet points overlapping? I have tried adjusting margins, but is there a more elegant solution to this issue? The requirement is to maintain the bul ...

Can you explain the expected behavior of the ::before and ::after pseudo-elements when used with the <details> element?

I observed that the <details> element is rendered differently in Chrome and Firefox. Take a look at the code sample below. According to the HTML Standard: The first summary element child of the details element represents the summary or legend of the ...

Animations in Angular fail to operate within mat-tabs after switching back when custom components are being utilized

I am facing a similar issue as Olafvv with my angular 16 project. The problem occurs in a mat-tab-group where the :enter animations do not work when navigating away from a tab and then returning to it. However, in my case, the issue lies within a custom su ...

Display the image in a pop-up window before executing a jQuery script

Lately, I've been focusing on implementing zoom in and zoom out functionality for images. The plugin I've chosen to use is guillotine. It works flawlessly except for one small hiccup. My goal is to integrate it into a website built with angularJ ...

The unusual interactions between JavaScript and QML

Encountering strange behavior with JavaScript. I am currently working on a basic example application using QT-QML and JavaScript. Within this application, I have implemented HTTP Requests triggered by a button that sends the request through JavaScript. ...

What is the best way to retrieve the duration of an object tag using JavaScript or jQuery?

My goal is to determine the duration and length of only mp4 type videos. Although I used the video tag to retrieve these values, it does not support mp4 files. Despite several attempts, I was unable to get the video tag to play only mp4 files, as it stric ...

Is it possible to perform advanced SQL-like queries on JSON data using JavaScript?

Lately, I've been faced with a challenge where I need to manipulate a JSON result using SQL commands like left joins, sum, and group by. Has anyone else come across this issue recently? I'm currently experimenting with the jsonsql JavaScript libr ...

Launching a React build using Docker on Apache or AWS

I am a beginner to the world of docker and react. My goal is to successfully deploy a production build on AWS. Here are the steps I have taken so far: Created a dockerfile. Built and ran it on the server. npm run and npm run build commands are functionin ...

Ways to connect to an element's data- attribute

I am working with a data attribute that looks like this: <a href="stuff" data-open-dialog="location-XXXX">click</a> <a href="stuff" data-open-dialog="location-YYYY">click</a> How can I bind to the data-open-dialog and extract the ...

Optimizing HTML and Script loading sequences for efficient execution

I have a query regarding the loading order of scripts in web browsers. I am interested in knowing the most efficient way to redirect users to a mobile website on the client side. For example, if I place a mobile detection script within the <head> se ...

Displaying a message text upon successful AJAX request

I have a web page with an AJAX request that updates data in the database. After the update, I want to display a message to the user on the webpage confirming that the data has been successfully updated. However, currently, no message is being displayed and ...

Refreshing page using jQuery following an AJAX request

My script is functioning properly, however the use of location.reload(); does not seem to be effective. I am aiming to refresh the page after deselecting the last checkbox. $(".catcf, .coursetype, .yearcf, .manthcf, .venucf").on('click' ...

Anchor tag buttons do not affect the font color and variables remain unchanged

I've been struggling to change the font color and background color of the <a> element, but nothing seems to work. I've attempted using hexadecimal notation and styling the .btn class without pseudo-classes, with no luck. Check out my CodeP ...

Providing secure access to Apostrophe-CMS S3 assets and attachments via HTTPS

Currently, I am utilizing Amazon S3 to deliver both my static assets and user uploads within the context of apostrophe-cms. While my site is loading via https, all of my assets appear to be loading using http. I have set up a cloudfront distribution at th ...

discord.js: Bot keeps sending duplicate embeds

I recently set up my discord bot to respond with a message when I enter a specific command, but I've run into an issue where it's sending the same embed twice. I've tried troubleshooting, but so far I haven't been able to pinpoint the c ...