Utilizing JQuery for adjusting the CSS top property

I needed to overlay text on an image, so I used CSS for this purpose. However, as I incorporated Bootstrap and hesitated to include absolute positioning in the CSS file, I faced issues with centering the text when resizing the image. To tackle this problem, I created a JQuery script to handle width changes, but it's not functioning properly.

WidthChangeScript.js

if($(window).width() > 500){
$( "#FactionsWidthtoPos" ).css( "top","255px" );
}

index.html

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>GrassCraft</title>
    <!----Bootstrap---->
    <link href="CSS/bootstrap.min.css" rel="stylesheet">
    <link href="CSS/textoverimage.css" rel="stylesheet">

</head>

<body>
    <nav class="navbar navbar-default navbar-fixed-top">
        <div class="container">


            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">Brand</a>
            </div>

<!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li><a href="#">Forums</a></li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Shop<span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="#">Factions Grass</a></li>
                            <li><a id="FctQu" href="#" >Factions Quartz</a></li>
                            <li role="separator" class="divider"></li>
                            <li><a href="#">How it works</a></li>
                        </ul>
                    </li>
                </ul>
                <form class="navbar-form navbar-left" role="search">
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="Search">
                    </div>
                    <button type="submit" class="btn btn-default">Submit</button>
                </form>
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#"></a></li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="#">Action</a></li>
                            <li><a href="#">Another action</a></li>
                            <li><a href="#">Something else here</a></li>
                            <li role="separator" class="divider"></li>
                            <li><a href="#">Separated link</a></li>
                        </ul>
                    </li>
                </ul>
            </div><!-- /.navbar-collapse -->

        </div>
    </nav>
<!---Site body--->
    <div id="imgTop" class="imageBehindText">
        <img src="http://placehold.it/1920x800" class="img-responsive"  width="1980" height="800">  
        <h2 id="FactionsWidthtoPos" class="textOverImage" >Factions</h2>
    </div>
    <div class="placeHolderImage">
        <img src="http://placehold.it/1920x100" class="img-responsive"  width="1980" height="800">>
    </div>
    <div class="container">
        <div class="jumbotron">
            <h1 id="abtUs" > GrassCraft</h1> 
            <p id="abtUsTxt" >A new Factions server made by 2 young enthusiasts. Any financial support would mean alot</p> 
        </div>
    </div>






        <!----Bootstrap required JQuery---->
    <script src="jquery.min.js"></script>

    <script src="JavaScript/bootstrap.min.js"></script>
    <script src="JavaScript/WidthChangeScript.js"></script>
</body>

textoverimage.css

 .imageBehindText { 
  position: relative; 
  width: 100%; /* for IE 6 */
  }

 .textOverImage { 
  position: absolute; 
  top: 100px;
  text-align: center;
  width: 100%; 
  color: #f0f8ff;
  text-decoration: underline;
  }
  .placeHolderImage {
  opacity: 0;

  }

This constitutes the majority of the code for this website. If you have any suggestions for optimizing the code, feel free to share!

Answer №1

If you want to adjust your code based on resizing the window using jQuery, you can do so by placing it inside a resize event handler.

$(document).ready(function(){
  function resize(){
    if($(window).width() > 500){
      if(!$("#FactionsWidthtoPos").hasClass("something"))
        $("#FactionsWidthtoPos").addClass("something");
    }else{
      if($("#FactionsWidthtoPos").hasClass("something"))
        $("#FactionsWidthtoPos").removeClass("something");
    }
  }
  resize();
  $(window).resize(function(){
    resize();
  });
});

In your CSS file, you would define a new class like this:

.something {
  top: 255px;
}

You can also utilize media queries to update your CSS styles based on the window size. Here's an example:

@media only screen and (min-width: 500px) {
  .textOverImage {
    top: 255px;
  }
}

This media query will automatically update the .textOverImage class when the window size is at least 500px wide.

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

Semantic UI allows for both left and right action input functionalities to be utilized

Is there a way to add actions on both ends of an input field? Here's what I've tried so far: <form class="ui form"> <div class="ui left action input"> <select class="ui compact selection dropdown" name="seats"> ...

Execute the following onclick event when the button is enabled

I am trying to display a popup when the "add to cart" button is clicked (but only when the button is active!). I thought about using a div that wraps the button and changes the class from "disabled" to "enabled". I have tried using jQuery, but it could als ...

Deactivate Pagination with a Button-Based Approach

After conducting thorough research online, I have experimented with various approaches but unfortunately, none have proven successful. view image description here I am seeking guidance on how to disable pagination when clicking on the replace button. Due ...

Can anyone recommend a user-friendly JavaScript dialog box compatible with jQuery that is mobile-responsive?

Mobile browsers. Using Jquery dialog alert box. Avoiding the use of alert() due to its unattractive appearance in web browsers. In the process of creating a website for both mobile and web platforms, seeking a dialog box that is compatible with both. ...

The AJAX success function is operational, yet the file does not execute

As a newcomer to StackOverflow and web development, I am pressed for time with this assignment, so please bear with me if I struggle with understanding web development terminologies. My current challenge involves calling another php file through ajax in my ...

When making an AJAX request, ensure the response is in JSON format with individual properties rather than as a single

Here's an example of an AJAX request with JSON dataType: JavaScript: function checkForNewPosts() { var lastCustomerNewsID = <?php echo $customers_news[0]['id']; ?>; $.ajax({ url: "https://www.example.com/check_for_n ...

Fetching data from dynamic inputs set up through Ajax

I have dynamically created a form using Jquery $.get. Now, I am looking to submit the form using another $.get request, but the values of my form fields are showing up as undefined. I have been researching how to achieve this using the .on() handle, but I ...

display or conceal a div when a radio button is selected

I need a way to display a specific div containing unique text once a radio button is selected, while hiding all other div sections. <?php foreach ($items as $item){ $i=1; echo ' <div class="form-check"> <input class="fo ...

Modifying Checkbox BorderWidth in Material UI v5

Seeking assistance with adjusting the checkbox border width in Material UI v5. I currently have a custom checkbox and would like to remove the border width. Despite trying numerous solutions, I have been unsuccessful so far. checkbox <Checkbox de ...

Enhance your iPad's design with a stylish div box-shadow

I'm in the process of developing a touch-optimized web application. As part of the design concept, I've implemented some visually appealing div elements that respond to clicks or touches for easy navigation. While this functionality works perfec ...

IE8 experiencing issues with displaying Recaptcha AJAX API widget

Struggling with getting the Recaptcha widget to show up in IE 8, even though it works fine in Firefox, Safari, and Chrome. I've set up a feedback form that loads via AJAX when a user clicks a link, and I'm using the Ajax API directly to place th ...

What is the process for editing or importing CSS within a React project?

I'm a beginner in React and I am encountering an issue with CSS not loading properly. I followed the tutorial for importing it, but it seems like there might be a better way to do it now as the tutorial is outdated. Below is my code, I would appreciat ...

Issue with the scope of Bootstrap Accordion

Observing that the event triggers on a single Bootstrap accordion can affect all other accordions on the same page, I am wondering if there is a way to isolate this behavior without altering the markup or using $(this). Any suggestions? Check out this exam ...

Using JQuery to generate dynamic URLs for Flask application

Presently, my form consists of filters. I am returning a serialized version of this form using the following code: $.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() { if (o[this ...

Adjusting div to not be absolutely positioned

Trying to create a div with animated borders, but it only works when positioned absolutely in the center. For illustration, here is the HTML and CSS code: .bb, .bb::before, .bb::after { position: absolute; top: 0; bottom: 0; left: 0; right: 0; ...

Problems Arising from Bootstrap Label and Input Field Alignment

I'm encountering an issue with my Angular app that uses Bootstrap 4. I'm trying to have my form label and input text box appear on the same line, but instead, they are displaying on separate lines. I've tried searching for a solution, but ha ...

Adjust the size of an image to fit within a set height row using CSS grid

I have a container with fixed width and height where I want to display an image and some text. My goal is to have the text appear first, followed by the image filling up the remaining space in the container. Check out the image below for reference: Exampl ...

When the PHP response is received by AJAX, an error occurs due to a failed JSON parsing request

Every time I try to run my small JavaScript code with an AJAX call to PHP, it keeps coming back with a JSON parser error. In the PHP code, I can see that the JSON is populated with an array like this: json encode: {"Year":"2012","Make":"Ford","Model":"Tau ...

Content displayed above a slideshow of images in a stunning gallery on Squarespace platform

Currently working on a one-page landing page using Squarespace and running into some issues with creating a slideshow. You can check out my website here (the slideshow is located at the bottom of the page): Squarespace provides default options for text p ...

Is there a way to customize the background color of Material-UI MenuItem when hovering over it through AutoComplete props?

AutoComplete utilizes the Menu component to display MenuItems as demonstrated in the documentation on those respective pages. I am looking to modify the backgroundColor of a MenuItem when it is hovered over. While I have been successful in changing the co ...