Creating Memes with Style (Assistance with jQuery and CSS)

Currently, I am engaged in creating a simple meme generator using jQuery to enhance my skills. However, I have encountered a small issue along the way.

Here is the basic setup that I have implemented so far:

1.) There are three sample images available for selection, clicking on which will display them inside the "meme" div at the top of the page. 2.) Users can input text in the two forms provided for adding top and bottom text.

While everything seems to be working fine functionality-wise, I suspect that the problem lies more on the CSS side. As a novice in jQuery, I could use some guidance in resolving this issue.

The specific concern I have pertains to the text entered by the user which tends to overflow and extend beyond the boundaries of the col-sm-6 container where the meme image and text are placed.

Although I managed to prevent overflowing by setting 'overflow: hidden' in the CSS for each text section, the text still spans the full width of the col-sm-6 container, which is larger than the actual image size.

My primary objective is to ensure that the text wraps around the borders of the image rather than exceeding the dimensions of the outer col-sm-6 container. While I understand that Bootstrap columns may not work in this scenario, the meme generator in the results window demonstrates this behavior with overflowing text outside the image boundaries.

$(document).ready(function() {
  $('#meme-controls').on('keyup','#top-text-input', function() {
      $('#top-text').text($(this).val());
  });
  $('#meme-controls').on('keyup','#btm-text-input', function() {
      $('#btm-text').text($(this).val());
  });
  $('#meme-samples').on('click','img', function() {
    $('#meme').empty().append($(this).data('src'));
  });
});
#meme {
  position: relative;
}

#meme-samples {
  cursor: pointer;
}

#top-text {
  top: 10px;
  margin: 0 auto;
  right: 0;
  left: 0;
  color: red;
  position: absolute;
  overflow: hidden;
}

#btm-text {
  bottom: 10px;;
  margin: 0 auto;
  right: 0;
  left: 0;
  color: red;
  position: absolute;
  overflow: hidden;
}
<!-- MEME Display -->
<div class="container">
  <div class="row text-center">
    <div class="col-sm-3"></div>
    <div class="col-sm-6">
      <div id="meme"></div>
      <h2 id="top-text"></h2>
      <h2 id="btm-text"></h2>
    </div>
    <div class="col-sm-3"></div>
  </div>
</div>

<!-- MEME Controls -->
<div id="meme-controls" class="container">
  <div class="row text-center">
    <div class="col-sm-3"></div>
    <div class="col-sm-6">
    <input type="text" id="top-text-input" class="form-control" placeholder="Top Text Goes Here."><br>
    <input type="text" id="btm-text-input" class="form-control" placeholder="Bottom Text Goes Here."><br>
    <div class="col-sm-3"></div>
  </div>
</div>

<!-- MEME Sample Photos-->
<div id="meme-samples" class="container">
  <div class="row text-center">
    <div class="col-sm-4">
      <img src="https://makeameme.org/media/templates/250/willy_wonka_sarcasm_meme.jpg" data-src="<img src='https://makeameme.org/media/templates/250/willy_wonka_sarcasm_meme.jpg'>">
    </div>
    <div class="col-sm-4">
      <img src="https://makeameme.org/media/templates/250/game_of_thrones_meme.jpg" data-src="<img src='https://makeameme.org/media/templates/250/game_of_thrones_meme.jpg'>">
    </div>
    <div class="col-sm-4">
      <img src="https://makeameme.org/media/templates/250/the_most_interesting_man_in_the_world.jpg" data-src="<img src='https://makeameme.org/media/templates/250/the_most_interesting_man_in_the_world.jpg'>">
    </div>
  </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Any assistance or advice you can provide would be greatly appreciated!

Answer №1

Include both the image and text inside #meme, then use display: inline-block to collapse it (alternatively, you can utilize float or other techniques depending on the context). The text will not exceed the width of #meme, which will only be as wide as the image.

$(document).ready(function() {
  $('#meme-controls').on('keyup','#top-text-input', function() {
      $('#top-text').text($(this).val());
  });
  $('#meme-controls').on('keyup','#btm-text-input', function() {
      $('#btm-text').text($(this).val());
  });
  $('#meme-samples').on('click','img', function() {
    $('#meme-image').attr('src', $(this).attr('src')); /* use selected image's src */
    $('#meme-image').show();
  });
});
#meme {
  position: relative;
  display: inline-block; /* collapsible container */
}

#meme-samples {
  cursor: pointer;
}

#top-text {
  top: 10px;
  margin: 0 auto;
  right: 0;
  left: 0;
  color: red;
  position: absolute;
  overflow: hidden;
}

#btm-text {
  bottom: 10px;;
  margin: 0 auto;
  right: 0;
  left: 0;
  color: red;
  position: absolute;
  overflow: hidden;
}
<!-- MEME Display -->
<div class="container">
  <div class="row text-center">
    <div class="col-sm-3"></div>
    <div class="col-sm-6">
      <div id="meme"> <!-- use #meme to contain image and text -->
        <h2 id="top-text"></h2>
        <h2 id="btm-text"></h2>
        <img id="meme-image" style="display: none;" />
      </div>
    </div>
    <div class="col-sm-3"></div>
  </div>
</div>

<!-- MEME Controls -->
<div id="meme-controls" class="container">
  <div class="row text-center">
    <div class="col-sm-3"></div>
    <div class="col-sm-6">
    <input type="text" id="top-text-input" class="form-control" placeholder="Top Text Goes Here."><br>
    <input type="text" id="btm-text-input" class="form-control" placeholder="Bottom Text Goes Here."><br>
    <div class="col-sm-3"></div>
  </div>
</div>

<!-- MEME Sample Photos-->
<div id="meme-samples" class="container">
  <div class="row text-center">
    <div class="col-sm-4">
      <img src="https://makeameme.org/media/templates/250/willy_wonka_sarcasm_meme.jpg" data-src="<img src='https://makeameme.org/media/templates/250/willy_wonka_sarcasm_meme.jpg'>">
    </div>
    <div class="col-sm-4">
      <img src="https://makeameme.org/media/templates/250/game_of_thrones_meme.jpg" data-src="<img src='https://makeameme.org/media/templates/250/game_of_thrones_meme.jpg'>">
    </div>
    <div class="col-sm-4">
      <img src="https://makeameme.org/media/templates/250/the_most_interesting_man_in_the_world.jpg" data-src="<img src='https://makeameme.org/media/templates/250/the_most_interesting_man_in_the_world.jpg'>">
    </div>
  </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Answer №2

Experience the power of <canvas>. Witness its capabilities here: http://codepen.io/sarahl/pen/fpJKct Transforming a <canvas> element into a base64 image string and saving it as an image is easily achievable without the need for jQuery. Just rely on pure vanilla JavaScript.

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

Struggling to get a basic HTML form to function with JavaScript commands

In my form, there are two input fields and a button. Upon clicking the button, a JavaScript function is triggered which multiplies the values entered in the inputs. The result is then displayed in a <p> element and evaluated through an if else statem ...

Receive information from a form and store it in an array

Struggling to figure out how to convert this into an array. I'm having trouble grasping the concept of taking input from a form and storing it in an array. In my project instructions, it clearly states: Do NOT save the input in variables and then tra ...

What is the process for importing a file that contains special characters in its name?

Is there a way to correctly import a file with special characters in its name using ES6? I am able to: import { tomorrow} from 'react-syntax-highlighter/dist/esm/styles/hljs'; However, I encounter difficulties when attempting to: import { tom ...

The JSON information is not appearing as expected

Utilizing a jQuery function, I am attempting to access a JSON file: $.ajax({ type: 'GET', url: '/data/coffee.json', dataType:'json', success: function(data) { let widget = displayData(data); $(" ...

Unable to change color of SVG icon when active

I seem to be having trouble getting my SVG element to fill with a specific color in the active state. Despite searching online for examples, I couldn't find much relevant information on this topic. Most of the resources available online focus on hover ...

Angular factory transforming service

I am looking to transform the 'i18n' function into a factory in order to return a value instead of just setting it. Any suggestions or tips would be greatly appreciated! services.factory('i18nFactory', function() { var language = ...

"Error: Custom password validator in Express Validator encountering 'undefined' property. Seek solution to

I have created a basic validator to compare the "password" and "passwordconf" exports.RegisterUser = [ check('username').isLength({ min: 1 , max: 10}).trim().withMessage("Must be between 1 and 10 characters"), check('password').isLeng ...

Use Regular Expressions to escape a quotation mark inside a pattern

Looking for a way to replace double quotes within specific characters in a String. Here's an example string: "XML": "<MyTag>"Value1"</MyTag><MyTag2>Value2</MyTag2>" The desired output is: "XML": "<MyTag>\"Value1&b ...

What is the best way to store an image file using html/angularjs?

I'm facing a challenge in my app where I need to save an image on one page and then retrieve it on another. So far, I have explored three different methods but none of them seem to be working for me: First, I attempted to use 'Parse.File' w ...

I am experiencing issues with my Vue.js application when trying to send an HTTP POST request

I am encountering an issue in my Vuejs application while trying to send an HTTP POST request to my server. The error message that keeps popping up in the console is: TypeError: _api__WEBPACK_IMPORTED_MODULE_0__.default.post is not a function at Object ...

Firefox - Eliminating the x-scrollbar

In Firefox, an unwanted x-scrollbar is appearing. Even when attempting to disable it using overflow-x: hidden;, the width remains incorrect (strings are still hidden). .wrapper { position: absolute; } .left { height: 150px; display: inline-block; ...

Positioning bar chart columns and text using CSS

My component generates HTML for a simple bar chart, with the height and background color computed within the component. Everything functions correctly, but I am facing an issue where long text causes displacement of the bar above it. Another problem is th ...

Concealing components in Bootstrap 4.1

Working with bootstrap 4.1, I encountered an issue where I needed to hide certain elements only on mobile devices. The official documentation suggested using the d-sm-none d-md-block classes, but unfortunately, it did not work as expected. To hide eleme ...

Guide on adding a new item to a JSON file in a ReactJS client side and NodeJS Express server side setup

Still learning the ropes with NodeJS and Express, but I'm curious to know if it's possible to add new objects after loading the API via the client-side. Essentially, I'd like to first load the API on the server-side, which contains the exist ...

Tips for incorporating JavaScript into .ascx pages

What are some ways to incorporate javascript in .ascx files? ...

Error message stating: "Node.js 'readline' - Mark-compacts near heap limit are not effective. Allocation failed."

Here's the process I'm following in the code: Primarily, I am engaging in web scraping tasks. I start by reading a text file containing approximately 3500 links. Next, I iterate through each link, filter out the ones I need, and make a request ...

Concealing a Checkbox with JavaScript once it has been selected

I have been exploring a PDF software that enables me to incorporate JavaScript into a checkbox feature. My goal is to have the box checked, which would automatically insert a timestamp in a separate textbox and then conceal the checkbox itself. While I h ...

Encountering an Error when Using Sass 3 to Customize Bootstrap 5 Color Values

Having some trouble changing the default theme colors in Bootstrap using SASS. Whenever I try to change a color and compile, I keep getting an error saying 'invalid CSS value'. I've gone through the documentation and watched a few tutorials ...

Is it possible to display ui-router nested views side by side on a webpage?

In my Angular app, I am using ui-router for navigation and implementing nested states/views in a hierarchical manner. Here is how the structure looks: <!-- index.html --> <h1>Page Title</h1> <ui-view></ui-view> <!-- Page1 ...

The functionality of datepicker is not available in Bootstrap 4.1

Currently, I am developing a website at this domain. In this project, I aim to display a calendar when the user clicks on a search input box. This is the HTML code snippet I have implemented for the input boxes: <div class="dates"> <div class ...