Troubleshooting Media Queries Problems in HTML and CSS

Check out the code snippet below:

//Modifying text content
(function() {

  var texts = $(".altered");
  var textIndex = -1;

  function displayNextText() {
    ++textIndex;
    var t = texts.eq(textIndex)
      .fadeIn(2000)
    if (textIndex < texts.length - 1)
      t.delay(2000)
      .fadeOut(2000, displayNextText);
  }

  displayNextText();

})();
.altered {
  display: none;
  padding: 0;
}

.altered svg {
  width: auto;
  height: 2em;
}

#signature {
  stroke-dasharray: 1920;
  stroke-dashoffset: 1920;
  animation: sign 6.4s ease;
  animation-fill-mode: forwards;
}

@keyframes sign {
  to {
    stroke-dashoffset: 0;
  }
}

#hero h1 {
  margin: 0;
  display: flex;
  font-size: 64px;
  font-weight: 700;
  height: 1em;
  color: transparent;
  background: black repeat;
  background-clip: text;
  -webkit-background-clip: text;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<section id = "hero">

<!-- This HTML should only show on smaller screens -->
<h1 style="margin-bottom: 16px">Text that needs to be displayed and centered on small devices</h1>

<!-- This HTML should only shown on larger screens -->
<h1 style="margin-bottom: 16px">Example
  <div class="altered">
    <h1>&nbsp;Text
    </h1>
  </div>
  <div class="altered">&nbsp;<svg xmlns="http://www.w3.org/2000/svg" width="999" height="622" viewBox="0 0 999 622" fill="none">
⟨path id="signature" d="M9 300.5C53.9664 263.275..." stroke="#0563bb" stroke-width="8" stroke-linecap="round" stroke-linejoin="round"/>
...
</svg></div>
</h1>
</section>

Upon running this code, you will see two distinct elements:

  1. Text that needs to be displayed and centered on small devices

  2. Sample Text which must be visible on larger devices.

The goal is to ensure that the Sample Text with signature displays flawlessly on larger screens, while

Text that needs to be displayed and centered on small devices
appears solely on small screens and is center-aligned. Though media queries could help in achieving this objective, previous attempts have been unsuccessful.

Expected Result

For screen sizes ranging from min-width: 320px to max-width: 1024px (ideal for phones and tablets), the expected appearance is as follows:

https://i.sstatic.net/3DUE7.png


However, on screens with a min-width of 1025px, only the Sample Text showcasing the signature animation should be visible.

Note: The animated signature should not appear on smaller screens; instead, only the static text as depicted in the provided image should be visible. Any recommendations on how to accomplish this task would be greatly appreciated. Thank you!

Answer №1

Here is a solution to the issue:

@media (min-width: 320px) {
  #hero > h1:nth-of-type(1) {
    display: block;
    text-align: center;
    margin: auto;
  }

 #hero > h1:nth-of-type(2) {
   display: none;
  }
}


@media (min-width: 1024px) {
  #hero > h1:nth-of-type(1) {
    display: none;
  }

  #hero > h1:nth-of-type(2) {
    display: block;
    margin: 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

Developing Java-based Ajax scripts

Is there a way to implement AJAX functionality in Java without actually using AJAX itself? I'm searching for a JAVA API that can achieve this. Just like we can submit data from a web page using a JAVA program, I want to handle AJAX operations through ...

What is the best way to dynamically update or display unique CSS styles when a service is invoked or provides a response in AngularJS using JavaScript

Seeking to display a unique CSS style on my HTML FORM prior to invoking a service in my code and then reverting back after receiving the response. I have implemented the use of ng-class to dynamically add the class when the boolean activeload1 is set to tr ...

Syntax Error in Node.js for MongoDB: Unexpected token or invalid symbol

I'm having trouble figuring out what's going on. Node v16.4.2, NPM v7.18.1 const mongoose = require("mongoose"); // const dotenv = require('dotenv') require('dotenv').config({path:'variables.env'}); mongoo ...

Delay the loading of JavaScript libraries and multiple functions that are only executed once the document is

After learning how to defer the loading of JS libraries and a document ready function from this post, I encountered a challenge. The code I currently have handles multiple document ready functions inserted by different modules, not on every page. echo&ap ...

"Maximizing Efficiency: Chaining Several Actions Using the JavaScript Ternary Operator

When the condition is true, how can I chain two operations together? a = 96 c = 0 a > 50 ? c += 1 && console.log('passed') : console.log('try more') I attempted chaining with && and it successfully worked in react, b ...

Problem identified with Vue.js: The Log in screen briefly flashes before redirecting the authenticated user (resulting in a full page refresh)

My routing is functioning properly, utilizing navigation guards to prevent users from accessing the login or register routes once they are signed in. However, when I manually type '/auth/signin' in the address bar, the login screen briefly appear ...

Perform a Selenium action to click on each individual HTML 'a' tag on

I am attempting to automate clicking on all the "a" tags within a website using Selenium in Python. However, I found that all the "a" tags I want to click have the same structure as shown in the code snippet below. I tried clicking them by their class si ...

Error: jquery unexpectedly encountered a token 'if'

I've successfully created an autocomplete suggestion box, but I'm facing an issue when using if and else along with console.log(). An error is displayed in my console saying Uncaught SyntaxError: Unexpected token if, and I'm not sure why. Ho ...

The use of the `foreach` loop

How can I change the position index to start at 1 instead of 0? I want the first name to be listed as number 1 and end at number 50. Is there a way to fix this issue? Here is my code: <html> <head> <title>SEATPLAN</title> &l ...

How can I achieve the function of // @codekit-prepend "" with gulp?

I have recently started using gulp and I am familiar with codekit where I used to write // @codekit-prepend "foo.js" // @codekit-prepend "bar.js" to concatenate js files. Can someone guide me on how to achieve the same result with gulp? Below is the ...

Invoking servlet using Ajax

I have created a JSP file with Javascript functions and AJAX calls to invoke a servlet (ReadprojectInfo). <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE ...

Tips for controlling HTML elements using JavaScript

I'm currently working on implementing a mouse-over scale effect for an HTML image. I chose to use JavaScript for this task because I need the ability to manipulate multiple elements in different ways simply by hovering over one element. Below is the J ...

Eliminate every instance using the global regular expression and the replace method from the String prototype

function filterWords(match, before, after) { return before && after ? ' ' : '' } var regex = /(^|\s)(?:y|x)(\s|$)/g var sentence1 = ('x 1 y 2 x 3 y').replace(regex, filterWords) console.log(sentence1) sentence2 ...

Is there a way I can align these items in the center of the span?

Issue at Hand: The SVG and text elements are not aligning to the center of my spans as desired. Attempts Made: I have experimented with different display modes, used margin properties for alignment, and tried adjusting text-align. I even searched for so ...

Addressing component validation conflicts in Vuelidate on VUE 3

I am currently experiencing an issue with VUE 3 Vuelidate. In my project, I have 2 components that each use Vuelidate for validation (specifically a list with CRUD functionality implemented using modals). However, when I navigate from one component to anot ...

What is preventing me from retrieving an ID value within an IF condition?

I'm trying to create a script that only allows devices with matching IP addresses, but I'm having trouble using an if statement. Whenever I include x.id inside the statement, it doesn't seem to work... any suggestions? <html> <sc ...

Enhancing middleware chaining in Express

Below is the code for my Express configuration: var server = express() .use(express.cookieParser()) .use(express.session({secret: buffer.toString('hex')})) .use(express.bodyParser()) .use(express.static('./../')); serv ...

Attempting to download an image through an axios fetch call

There is an issue I am facing while trying to retrieve an image from the website www.thispersondoesnotexit.com. function getImage() { axios({ method: 'get', url: 'https://www.thispersondoesnotexist.com/image' }) ...

Issue with Ajax calendar extender not displaying full calendar due to being cut off

I've implemented the Ajax calendar extender with my own custom CSS classes. The styles are being applied correctly, but I'm encountering an issue. When the calendar at the bottom of the page opens, it gets cut off at the bottom due to the restric ...

Creating a fieldset and form in HTML code involves using

I encountered a strange issue recently. I had set up a form in HTML to send data to my PHP script, and everything was functioning correctly. However, the design looked a bit messy without margins, so I decided to make some CSS adjustments. After applying s ...