Is there a way for me to superimpose text onto an image in this instance?

I am attempting to place a "Text overlay goes here" over the profilepic.jpg image below. I believe there may be an issue with my CSS code. My approach was to create a new div class named overlay and embed that line within the imgcontainer class. Can someone pinpoint where I may be going wrong?

HTML

<div id="timeline">
        <div class="block2x3 block">
            <div class="imgcontainer">
                <img src="profilepic.jpg" />
                <p><div class="overlay">Text Overlay Goes Here</div></p>
            </div>

            <div class="commentcontainer">
                <div class="peoples">
                    <a href="#"><strong class="peoplename">Joe Schmo</strong></a> and <a href="#">42 other people bought this</a>
                    <p>Have commented on your <a href="#">wall post</a></p>
                </div>
            </div>
        </div>

CSS

#timeline div[class*="block2x3"] .imgcontainer  {height:66.6%;}
#timeline div[class*="block2x3"] .commentcontainer  {height: 33.4%;;}
#overlay{
    float: top;
    background: rgba(0,0,0,.7);
}

Answer №1

Check out this demo on how to implement this feature: http://jsfiddle.net/codershop/ddg2ymxm/

Make sure to remove the paragraph tags surrounding the overlay div.

<div class="overlay">Insert Overlay Text Here</div>

In your CSS file, update the #overlay selector to a class selector .overlay. It should look like this:

.overlay{
    background: rgba(0, 0, 0, 0.7);
    color: #fff;
    left: 15px;
    position: absolute;
    top: 15px;
}

Answer №2

float: top; is not a valid CSS property :) If you want to position an element at the top, use position: absolute

  • Add position: absolute to the overlay container to place it over the top of the image.

  • Ensure your image container has position: relative to allow the overlay to position itself in relation to it.

  • Avoid setting fixed height properties and let the content dictate the height instead.

  • Use display: inline-block on the image container for automatic resizing with the image's width.

  • Keep the overlay container's width as width: 100% to match the full width of the image.

CSS / HTML / Demo

* {
    margin: 0;
    padding: 0;
}

#timeline {
    padding: 10px;  
}

.image {
    position: relative; 
    display: inline-block; /* allow width to be determined by the images size */    
}

.block .overlay {
    position: absolute;
    bottom: 0;
    left: 0;
    text-align: center;
    width: 100%; /* Full width of .image */
    background: rgba(0,0,0,.7);
    color: #FFF;
}

.comment {
    padding: 10px 0 0;  
}
<div id="timeline">
    <div class="block2x3 block">
        <div class="image">
            <img src="http://www.placehold.it/200" />
            <div class="overlay">Text Overlay Goes Here</div>
        </div>
        <div class="comment">
            <a href="#"><strong class="peoplename">Joe Schmo</strong></a> and <a href="#">42 other people bought this</a>
            <p>Has commented on your <a href="#">wall post</a></p>
        </div>
    </div>
</div>

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

Is there a way to achieve a transparent background while animating the second text?

I am seeking to create a unique typography animation that involves animating the second text, which is colored and consists of multiple text elements to animate. The animation should showcase each text element appearing and disappearing one after the other ...

Blending an HTML jQuery toggle

Is there a way to make the current headline fade out while simultaneously fading in a new one when switching to the next div using a jQuery .html switch? Check out the codepen example: https://codepen.io/balke/pen/JpNNve $(window).scroll(function() { ...

Shifting and implementing various styles across numerous elements at the same time

There is an anchor tag containing two spans... <a class="banner-logo" href="/search"><span id="banner-logo-hello">Hello</span><span id="banner-logo-world">World</span></a> When hovering over the anchor tag, I want to c ...

Turn off automatic resizing of iframes

I'm currently dealing with a webpage that contains an iframe. The iframe contains quite a bit of data, and every time it loads, its height adjusts to match the content within. Unfortunately, this is causing my page layout to be disrupted. Is there a w ...

Having trouble getting CSS3 Keyframes to function properly?

Check out the following code snippet: .startanimation { height: 100px; width: 100px; background: yellow; -webkit-animation: animate 1s infinite; } @-webkit-keyframes animate { 100% { width: 300px; height: 300px; } ...

Why doesn't the background color display properly when the div height is set to auto?

When I set the height of #container to auto, the background-color does not display. However, when I set it to a fixed height like 1000px, it shows up. I've attempted using "overflow:auto", but that did not solve the issue. How can I fix this? I want # ...

Looking for a plugin that can color the text "Google" in the exact shade as the Google logo? Check out this jQuery

Is there a jQuery plugin or similar tool available that can change the color of each letter in the word "Google" to match the colors of the Google logo? For instance, if I have the following HTML markup: <span>Google AdWords</span> I would l ...

Ways to format the direct descendant of a multi-level list?

Check out my nested ul list: <ul> <li> <a href="/"></a> </li> <li> <a href="/"> </a> &l ...

Dealing with ng-repeat and button alignment across Chrome, Edge, and Firefox

Can someone help me understand this strange behavior I am experiencing across all browsers? <div style="margin-top:5px"> <button translate="clear" ng-click="xyz.clear()" class="btn btn-default"></button> <butt ...

Having trouble with animating the background color of an input button using jQuery?

I'm completely confused as to why the background color isn't changing despite investing a significant amount of time into it: <input type="button" class="front-consultation-btn" value="Get A Free Consultation"> <script> $(' ...

ways to set a background color for a textarea element using bootstrap

How can I add a background color to my text area field in Bootstrap? <div class="form-group"> <div class="col-xs-12"> <textarea class="form-control" id="exampleTextarea" rows="6" placeholder="Message" style="background-color: #f ...

What is the best way to showcase my products in a horizontal line? I am seeking a solution using PHP and Bootstrap to exhibit all the items retrieved

I am experiencing an issue with displaying the most recent events from my database using PHP. The Bootstrap framework and CSS are causing them to appear as a single column on the page, but I would like them to be displayed in rows of three. I have attempt ...

What is the best way to position a logo and navbar in alignment?

I'm still learning the ropes here. I'm having trouble getting the navbar to align with the logo because margin-top/bottom doesn't seem to be working for some reason. <HTML> <HEAD> <link href="style.css" rel="stylesheet"&g ...

The size of the cursor varies depending on the line it's placed on

I have a content editable div where three lines are separated by BR tags. When I click on the second line, the cursor becomes bigger than that in the first line. .content { line-height: 35px; } <div class="content" contenteditable="true"> ...

Adjusting Spacing Between Characters

I've been looking for a way to convert regular characters (ABC) to full-width characters (ABC), but I haven't had any luck so far. That's why I'm turning to you guys for help. Currently, I don't have any JavaScript code writt ...

In my opinion, CSS3 transform translateZ can be likened to scaling in some way

There are instances where I believe that the translateZ and scale properties produce similar effects, akin to zooming in or out. I suspect there is a mathematical relationship between them. If I know the value of one, such as translateZ(-1000px), along wi ...

What is the process for implementing hover transitions on link elements?

I am trying to incorporate a transition effect for my links. Essentially, I want the text-decoration (which is currently set to underlink) to gradually appear. Unfortunately, my previous attempt at achieving this has been unsuccessful: #slink{ transit ...

CSS: What causes the gap below my border to appear?

I've noticed an issue in the HTML code snippet below: there seems to be some extra spacing under the border of the 'bucket' div. Despite my attempts, I haven't been able to figure out why this is happening. Can someone provide some ins ...

Tips for creating a stylish, blurred, and centered box for a login form on a full-size background that is also responsive

I attempted to create a login form on an HTML page using Angular, featuring a full-size background image that is centered. The form itself is contained within a div with a blurred background, also centered both horizontally and vertically within the browse ...

Creating two columns using React and Material-UI Grid while utilizing just one map function for iteration

I am facing a challenge with Material-UI Grid. I want to display the information from my map function in two columns instead of one. I tried adding another Grid item sm={6} and using the same map function, which resulted in two columns but with identical i ...