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:


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

Unable to retrieve res.user.username while using passport within express-session

I'm currently diving into the realm of creating sessions with Passport.js and Express.js. My goal is to retrieve the username from a user stored in a session using res.user.username, but I seem to be facing some challenges. Below is the snippet of m ...

Update the Div when Fancybox is Closed

Is there a way to refresh a parent page's div after closing Fancybox? I am aware of using onClosed: function(), but I'm unsure which specific function to include in it. ...

Execute function upon changing the title of a list item using jQuery

Hey there, I've got a question for you. Is it possible to trigger an event when the title of a list element is changed? For example: <ul> <li id="li_1" title="40">40</li> </ul> So when the title attribute changes (coming ...

Exploring the issue of nested subscriptions causing bugs in Angular

My current challenge involves nesting subscriptions within "subscribe" due to the dependency of some data on the response of the previous subscription. This data flows down the subscription chain until it is stored in an array. Starting with an array of I ...

Show information retrieved from fetch api

Hi there! I've been trying to fetch data from the Avascan API and display it on my HTML page, but so far, I haven't had any luck. I've experimented with using the Fetch API, JSON, and AJAX methods, but none of them seem to work for me. Do yo ...

Retrieve the Checkbox id of a material-ui checkbox by accessing the object

I'm currently working on extracting the id of a Checkbox object in my JSX code. Here's how I've set it up: <div style={{display: 'inline-block', }}><Checkbox id='q1' onclick={toggleField(this)}/></div> ...

What is causing the delay in retrieving elements using getX() method in Selenium?

Recently, I've been experimenting with web scraping using Selenium. However, I've noticed that there is a delay when calling getText(), getAttribute(), or getTagName() on the WebElements stored in an ArrayList from the website. The website I&apo ...

Tips for Uploading the Contents from 2 Select Boxes

In my ASP.NET MVC application, I have two list boxes named #AvailableItems and #AssignedItems. I am able to transfer items from one box to the other. Additionally, there are related values stored as attributes on the Save link that also need to be submitt ...

Is it possible to retrieve and return multiple lists from a WCF Service?

Recently, I created a WCF service using C# and I've been able to successfully call the service from jQuery to return a list of data. Now, my goal is to return multiple lists. Is this achievable? The current implementation involves passing back the li ...

Tips for changing the TextField variant when it receives input focus and keeping the focus using Material-UI

When a user focuses on the input, I'd like to change the variant of the TextField. The code snippet below accomplishes this, but the input loses focus. This means the user has to click again on the input to focus and start typing. import React, { useS ...

What is the process for displaying HTML page code received from an AJAX response?

My current project involves implementing JavaScript authentication, and I have a specific requirement where I need to open an HTML file once the user successfully logs in. The process involves sending an AJAX request with the user's username and passw ...

AngularJS and adding to an array in the routing process

I'm currently working on creating a contact list with two different views. One view displays all the contacts and includes an option to add a new contact, which is represented by a button rather than a space to input information directly. The other vi ...

Establishing a global variable in Cypress through a function

My current workflow involves the following steps: 1) Extracting a field value from one page: var myID; cy.get('#MYID'). then(($txt) => { myID= $txt.text(); }) .should('not.equal', null); 2) Mo ...

Determine if an object has been submitted in a MEAN / AngularJS application

I am working with an AngularJS frontend and using Express, Node, and MongoDB on the backend. My current setup is as follows: // my data to push to the server $scope.things = [{title:"title", other properties}, {title:"title", other properties}, {titl ...

How can one effectively overcome the constraint in HTML5 canvas that prevents altering the positions of previously sketched elements?

I am currently working on creating a wind map that displays data from seven different weather stations. My goal is to develop a fluid and interactive map similar to the ones found in this animation or this animation. In my research, I came across an artic ...

What might be causing Flex not to function properly on my personalized landing page?

I attempted to create a basic landing page featuring an image background with a logo, button, and a row of 4 images displayed side by side using Flex. However, for some reason, the Flex is not functioning properly for the row of images. Here is the code s ...

When attempting to change an image using JavaScript, the image fails to load

I am having trouble understanding what is wrong with my code. <html> <body> <script type="text/javascript"> function changeImage() { var image = document.getElementById('myImage'); if (image.src.match("bulbon")) { ...

What is the best way to tally the number of occurrences a value is true within an hour or day using React?

I am developing an alarm application and I want to track the number of times the alarm has been triggered within an hour and a day. Utilizing redux toolkit, I manage the state of the alarm (on/off) and have a reducer called countAlarm to increase the count ...

How can I assign a class to my Typography component in Material UI?

When using my material-ui component, I wanted to add an ellipsis to my Typography element when it overflows. To achieve this, I defined the following styles: const customStyles = (theme) => ({ root: { textAlign: "left", margin: theme.spacing( ...

Type of flow: customizable prop types for React components that can be extended

In the scenario where a SelectOption component is present, with props defined as: type OptionType = { +id: string, +label: string, } type Props = {| +options: OptionType[], +onItemPress: OptionType => void |} I intentionally left the OptionType ...