Elevate the block when encountering errors in HTML/CSS

Here is a picture of what I want, and below I will provide a more detailed description:

Link to the image (I cannot paste it here)

I am explaining the elements involved. There are three main components:

1) The topmost text "Bla bla bla bla"

2) The button "Click me"

3) And the bottom text

When you click the button, an error message will appear. I want to move the first two elements higher without affecting the bottom text. Therefore, the bottom text should remain in its original position.

My question is - How can I achieve this using HTML/CSS code only (excluding JavaScript for text appearance)? I require HTML/CSS code that will ensure the movement of the top two elements higher.

Answer №1

It would be more effective to utilize javascript in this scenario, allowing for the dynamic calculation of the error message display height. Resorting to other methods may lead to hacky solutions based on fixed heights, as illustrated below.

//Purely for CSS demonstration
$("#btn").click(function(){
  $(".msg").toggleClass("error");
});
.msg {padding: 3em; border:1px solid black;}
.msg.error {padding-top: 1em;
border-color:red;}

.errorMsg { 
display:none;
color:red;
height:2em;}

.error .errorMsg{
  display:block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="msg">
<h1>bla blah bla</h1>
   <input type="button" value="Click Me" id="btn" />
   <div id="error" class="errorMsg">Error message goes here</div>
   <div>Text has to not move</div>
</div>

Another approach is to leverage the visibility attribute to maintain the hidden element's position on the page

.msg {padding: 30px; border:1px solid black;}
.errorMsg { visibility:hidden; /*Important line*/
color:red;}

.errorMsg:target{
  visibility: visible;
}
<div class="msg">
<h1>bla blah bla</h1>
   <a href="#error">Click Me</a>
   <div id="error" class="errorMsg">Error message goes here</div>
   <div>Text has to not move</div>
</div>

I've incorporated the use of target in the above example for styling purposes, but modifications can be made by assigning a class to the error element or its parent and styling accordingly.

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

Challenge with the positioning of HTML output layout

Is there a way to center the output/result of the HTML code below both horizontally and vertically on the web page using CSS? I have tried the following code but it only centers the output horizontally. Vertical alignment is not working properly. Can someo ...

When using iOS, inserting an iFrame with a source URL (SRC) on a webpage will automatically open the URL

Currently facing a peculiar issue within a Cordova app, specifically on iOS. The scenario is: I have an HTML page with existing content. At a later stage, I fetch additional HTML from a remote source and inject it into the original HTML page. However, whe ...

I have a question regarding the CSS universal selector

I've been working on developing a website, but I'm having trouble with the universal selector. No matter how many times I try, it just won't work. If you'd like to see an example of what I'm dealing with, check out this link: https ...

What is the proper way to include 'rowspan' specific CSS within an HTML table?

I have an HTML table with rowspans in it: table tr, td { border: 1px solid black; } tr:nth-child(1) { background-color: red; } tr:nth-child(2) { background-color: blue; } <table> <tr> <td rowspan=2>Section 1</td> ...

I am looking to adjust the height of my MUI Grid component

Recently exploring React, and I'm looking to set a height limit for MUI Grid. I've structured my project into 3 sections using MUI grid components. My goal is to restrict the page height in order to eliminate the browser scrollbar. If the conten ...

What are the ways to incorporate the width property in @media queries within CSS?

I've been struggling to set the width inside @media in my code, but it's not working as expected. Here is what I have tried: mat-toolbar { app-toolbar-left { width: 35%; } app-toolbar-right { width: 22%; } } @me ...

How to save HTML content in a variable for future use in Next.js

When working with Next JS, my code resembles something like this: function Test() { return ( <section> <div>Test</div> </section> ); } Now, imagine that I want to have multiple entries here, gen ...

Failure to receive Ajax XML data in success callback

I am struggling to access the book.xml file that is located in the same folder as other files. Everything seems fine, but the ajax function refuses to enter the success state and instead shows an [object object] error message. The XML file is very simple, ...

Tips for creating a zoomable drawing

I have been working on this javascript and html code but need some assistance in making the rectangle zoomable using mousewheel. Could someone provide guidance? var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var width ...

Is there a way to clear out input values in a form after some errors are caught and the rest of the data is saved upon resubmission?

Currently, I am in the process of validating my HTML contact form using PHP. To test its functionality, I have tried submitting the form with no data and then attempted again with partial data. While submitting partial data, I noticed that the correct fiel ...

Two adjacent divs positioned next to each other, where the right-hand div occupies the remaining space within its

I am facing a common issue with two side-by-side divs, which I usually solve without any problems by floating both divs left and adding a clear:both div after them. However, my requirements have made this problem more complicated to solve... What I would ...

Trigger submission issue encountered with Jquery in a dialog (modal) window

It's somewhat difficult for me to fully explain my problem. Nevertheless, I'll give it a go. In my HTML code, I have implemented several triggers using Jquery. Trigger 1 is responsible for validating specific items within a form. Once Trigger 1 ...

How can I dynamically adjust the size of an image in a directory based on its filename with php?

Is it possible to resize a specific image in a folder by its name using PHP? ..................................................................................................................................................................... I have trie ...

Load Image Timing Discrepancy in Web Development (jQuery/PHP/HTML)

I am currently using jQuery to develop a basic slideshow feature. Within my home.php file, I have created a slideshow. Additionally, there is a menubar that allows users to navigate to different pages. Upon clicking on "Home" in the menu, the home.php con ...

Are there any alternatives to PHP for implementing an auto-complete search feature?

I have decided to focus on using HTML, MySQL, JavaScript, and jQuery for my project. Though I understand that learning PHP would be beneficial in the long run, I don't have enough time to master it all within a week. As for the server-side, I will be ...

Prevent scrolling in a full-screen modal using CSS

I came across a beautiful fullscreen modal that is purely based on CSS. The only issue I encountered was that the modal isn't scrollable. I attempted various solutions but haven't been successful. Here's the script I am using: ...

A demonstration of VueJS Nested Builder functionality

I am currently exploring VueJS and working on a project to create a simple page builder application that allows users to add sections and subsections. I am seeking guidance on how to properly set up the Vue data property for this functionality. Any advice ...

Enter the ISO code for the country in an HTML form to submit

I'm currently working on a form that requires users to select a country from a list. I came across a website that provides a comprehensive list of countries in HTML format. However, the site displays the full country name but sends the ISO code to the ...

Trouble displaying AngularJS $scope.data in the HTML code

I'm experiencing an issue where the data received from a POST request is being logged in the console, but is not displaying on the HTML page. Despite having a controller set up, the {{user}} variable is not appearing on the HTML page. While I can se ...

Issue with React Ref: Invariant Violation when trying to addComponentAsRefTo

I'm encountering an issue while attempting to add a ref to a React component. The error message I'm seeing is as follows: invariant.js:39Uncaught Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs. You might be adding ...