How can you place text on top of an image using JQuery and CSS?

I've been working on overlaying text over an image using Jquery and css. The overlay text is displaying nicely on the image, but I'm struggling to change the font and size of the text. I attempted to make changes in the CSS for the font family and other properties, but nothing seems to be taking effect. Can someone please take a look at my code and provide some guidance?

Here's the relevant CSS:

    #container {
  width: 100px;
  text-align: center;
  margin: auto;
}

.back
{
  position:absolute;
  top:0;left:0;
}
.wrap
{
  width:100px;
  height:100px;
  position:relative;
  margin:auto;
  overflow:hidden;
}

.comment
{
  position:absolute;
  width:100px;
  top:100px;
  left:0px;
  letter-spacing: -1px;
  color: white; 
  font-family:"Bookman Old Style", "Book Antiqua", Garamond;  
  font-size:100%;
  background: #4A4D4A;
  padding: 10px;
  filter:alpha(opacity=60);
  -moz-opacity:0.6;
  -khtml-opacity: 0.6;
  opacity: 0.6;
  line-height: 90%
}

JQuery:

<script type="text/javascript">

    $(function () {
        $('.wrap').hover(function () {
            $(this).children('.comment').stop().css("top", "0px");
        }
            , function () {
                $(this).children('.comment').stop().animate({ "top": '400px' }, 600);
            });
    });

</script>

HTML:

<div id="container">
                <div class="wrap">
                    <SPSWC:ProfilePropertyImage PropertyName="PictureUrl" ResizeToFit="100" RenderWrapTable="False"
                        ShowPlaceholder="true" ID="PictureUrlImage" runat="server" class="back" />
                    <span class="comment"><a href="http://www.google.com" id="A1"  >Edit Picture </a>
                    </span>
                </div>
            </div>

Currently, the image looks like this:

However, I would like it to resemble Facebook's style.

Any advice from experts in the field would be greatly appreciated!

JSfiddle : jsfiddle.net/ItsMeSri/7mfj7/1

Answer №1

I am uncertain about the resulting HTML since it appears that you are using server-side evaluated tags like SPSWC:ProfilePropertyImage. Interestingly, achieving similar functionality does not require Javascript; just a simple CSS can suffice. You may refer to this Fiddle#d42SU/1/ which accomplishes exactly what you desire. Assuming the following HTML structure:

<div id="container">
    <div class="wrap">
       <img src ="http://www.w3.org/html/logo/downloads/HTML5_Logo_256.png">
       <span class="comment">
          <a href="http://www.google.com" id="A1" >Edit Picture </a>
       </span>
   </div>
</div>

Utilizing CSS:

#container{
  width: 256px;
}
#container .wrap{
  width: 256px;
  height: 256px;
  overflow: hidden;
  position: relative;
}
#container .wrap img{
  position: relative;
}
#container .wrap span.comment{
  background-color: yellow;
  position: absolute;
  top: 0px;
  right: 0px;
  background-color: #000000;
  padding: 2px;
  display: none;
}
#container .wrap:hover span.comment{
  display: block;
}
#container .wrap:hover span.comment a{
  text-decoration: none;
  color: #ffffff;
}

Answer №2

CSS3 has the capability to achieve most of these effects, including hover effects.

Update: Enhanced Functionality

If you are implementing this on a .wrap element, ensure that you set its position:relative so that the :after pseudo-element can be correctly positioned.

Consider the following:

.element:hover:after{
    content: "<a class='edit_picture' href='' title=''>Edit Picture</a>";
    position:absolute;
    width:80px;
    margin-right:-80px;
    left:100%;
    top:0%;
}

Additionally, some JavaScript is required for full functionality:

$(function () {
    $('.edit_picture').click(function () {
        //perform necessary actions...
    });
});

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

Avoid insecurely assigning an any value in TypeScript

Take a look at this code snippet: const defaultState = () => { return { profile: { id: '', displayName: '', givenName: '', }, photo: '', } } const state = reactive(defaultState() ...

What is the best way to incorporate a sliding div into these CSS Message Bubbles?

Just dipping my toes into web development, so please bear with me. I am working on a prototype of a Messaging Application and have most of the basics sorted out. Now, I am adding some finishing touches to enhance user experience. My goal is to make it so t ...

Node.js data querying methods and best practices for code structuring

Recently delved into the world of nodejs and still fairly new to JavaScript. In my quest for best practices, I stumbled upon this helpful resource: Currently, I am working on an application following the structure recommended in the resource. The suggesti ...

The datepicker in Angular Material refuses to open when used within a modal dialog box

I successfully integrated an angular material 2 date-picker into a bootstrap modal form: <div class="modal-dialog modal-lg"> <div class="modal-content"> <div class="modal-header"> <h4 class="modal-title">{{title}}</h ...

There is plenty of negative space within masonry design

I'm having trouble configuring a masonry layout for my cards. $('.grid').masonry({ fitWidth: true, horizontalOrder: true, // set itemSelector so .grid-sizer is not used in layout itemSelector: '.grid-item', // us ...

What is the process for utilizing jQuery and ajax to retrieve data from a MySQL database?

I am currently working on a form that includes a "select" box. The goal is to display the relevant records from the database on the same page once an option is selected. This process involves two files: an HTML page containing the form: <form id="theF ...

Tips for incorporating a personalized label to a selected marker using the react-google-maps library

Just recently, I followed a tutorial on using the React-Google-Maps library. However, I encountered a challenge at the end: I want my app to read coordinates from a file and display them on the map as markers. How can I set a label like "1" for one marker ...

"Troubleshooting the issue of ng-init not functioning properly in conjunction

I need help with displaying a list of numbers inside a div element. I am attempting to achieve this using the following code snippet: <div ng-init="range=[1,10,3,4,5]" ng-repeat="n in range" > {{n}} </div> Unfortunately, the numbers withi ...

Error: The absence of type definition

I am struggling to implement the functionality of an Add Question button, encountering an error message that reads: TypeError: form.questionText is undefined Can anyone point out where I went wrong? <script type="text/javascript"> fun ...

What's the best way to conceal the navbar while attempting to print the page?

Currently, I am working on developing an application for my school project. One of the features is an invoice sheet with a print option. However, when I try to print by clicking the button or using ctrl+p, the navbar appears in a chaotic and disorganized m ...

Express application receiving repetitive post requests

Recently, I have been working on developing a YouTube video conversion app that utilizes youtube-dl for streaming videos from YouTube and saving them. Everything was going smoothly until I encountered an issue when trying to stream a video that exceeded on ...

Ways to adjust the position of a DIV based on its index value

I'm currently working on a unique project that involves creating a triangular grid using HTML and CSS. The challenge I am facing is offsetting each triangle in the grid to the left by increasing amounts so they fit seamlessly next to one another. Righ ...

Navigating to the parent node in a treeview within the wijmo flex grid: a step-by-step guide

Utilizing the wijmo flex grid, I've successfully created a tree view for my data. I can determine if a specific node has children and its level, but I'm struggling to navigate to the parent node from a given one. Additionally, I am able to retrie ...

Transfer properties to the children of this component

I'm a beginner with React and facing an issue when trying to pass custom props to this.props.children. I attempted using React.cloneElement, and while I can see the prop in the console.log within the class where I created it, it seems to get lost duri ...

Has the Blueprint CSS framework become obsolete?

Is the Blueprint CSS framework still being actively maintained? The last version was released over a year ago and there have been no recent commits in the Github repository. ...

My goal is to monitor every action (button) that users take while using the PDF viewer

Each browser displays the view differently, and I am interested in monitoring specific user actions such as button presses on a PDF viewer. I am currently setting up an iframe and configuring its attributes. Is there a way to achieve this? ...

Manage unexpected errors by displaying pop-up notifications

Is there a way to display an error message from my backend code in an alert box using javascript? I'm currently working with ASP.NET MVC 1.0. Thank you, Rod. ...

Instructions for setting a default value for ng-options when dealing with nested JSON data in AngularJS

I need help setting a default value for my ng-options using ng-init and ng-model with dependent dropdowns. Here is an example of my code: <select id="country" ng-model="statessource" ng-disabled="!type2" ng-options="country for (country, states) in c ...

Having trouble pinpointing the issue with this particular if statement?

I am currently working on creating a form that compiles all entered data when the user fills out all fields. The form is connected to a PHP file and functions properly, but I encountered issues when implementing validation logic. The "Validation" section ...

AngularJS: Blocking access to specific state for users

I am currently in the process of developing an application using sails.js for the backend and Angular for the frontend. My goal is to restrict access to the admin control page for unauthorized users. I have come across several solutions, but none of them s ...