Styling text over images using HTML and CSS

Is there a better way to put text over an image? Should I use a different HTML tag for the text? Any suggestions?

Check out this example on jsFiddle


body{
    background-color:#F0F0F0 ;
    color:#000305;
    font-size: 87.5%;
    font-family: Arial,'Lucida Sans Unicode';
    line-height: 1.5;
    text-align: left;
    width:80%;
    margin:2% auto;
}

.main {
    width:45%; 
    height:300px;
    background-color: #20b2aa;
    border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
}

.main img{
    width:80%; 
    height:auto;
    margin: 6% 10%; 
    float: left;
}

.main h2 { 
    color:white;
    position: absolute; 
    margin:50px;
    margin-left: 50px;
    width: 100%; 
}

Answer №1

Consider using the image as a background-image for your div and adjusting the text content within it. This approach allows you to work with just one div element that requires editing.

http://jsfiddle.net/Y67AB/1/

Answer №2

View the code snippet here

In my opinion, absolute positioning is a better choice than using float in this scenario.

.main {
    width:45%; 
    height:300px;
    background-color: #20b2aa;
    border-radius: 5px;
    position: relative; //keep children absolutely positioned within this element
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
}

.main h2 { 
    color:white;
    position: absolute; 
    margin: 6% 10%;
    top: 0;
    left: 0;
}

Answer №3

An alternative method is to utilize the z-index property in conjunction with separate div elements. Ultimately, you must include either position:absolute; with varying positions or margin-top: -whatever paired with position:relative;. There isn't a definitive right or wrong way to accomplish this task. The most important thing is that it functions correctly on your website.

Answer №4

Utilizing the <div> tag allows you to create a single div element and position it above an image, set its location, or connect it with another div serving as your image. The possibilities are vast when working with the <div> tag. For example.


<div style="abcd">
      top: 99;
      left: 99;
      position: absolute;
      z-index: 1;
      visibility: show;">
<!-- content will go here -->
</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

Utilizing the vertical grid layout with columns in Bootstrap for HTML websites

I have been attempting to showcase the attached image below in a vertical column-wise grid view using Bootstrap in HTML. Can someone provide me with ideas on how to achieve this? Currently, I am displaying a row-wise column grid with reference to Grid sys ...

NiceScroll is failing to show up after an Ajax reload

I've been searching everywhere for a solution to my problem. The issue I'm facing with the AJAX load and nicescroll plugin is a bit unique compared to what I've found so far. The problem is that Nicescroll isn't functioning on a separat ...

The removeEventListener method in JavaScript fails to function properly

On my website, I have a unique feature where clicking an image will display it in a lightbox. Upon the second click, the mouse movement is tracked to move the image accordingly. This functionality is working as intended, but now I'm faced with the cha ...

Navigating from Page 1 to Page 2 and then returning to Page 1 using the browser's back button causes Page 1 to malfunction in NextJS version 13

I am currently using next version 13.4.5 and implementing routing with typescript through /app. On my first page, I have a <Link> (next/link) element that takes me to Page 2. However, when I use the browser back button to return to page 1, the layou ...

Eliminating the gray background using CSS

How can I remove the gray background that automatically appears in my CSS header? .header { padding: 60px; margin: 20px auto auto auto; width: 1400px; border-radius: 10px; text-align: center; background: # ...

The class "pagination pagination-lg" from the link https://angular-ui.github.io/bootstrap/#/pagination seems to be malfunctioning in Bootstrap 4

Just starting out with angularjs and I'm trying to implement pagination using https://angular-ui.github.io/bootstrap/#/pagination. However, I'm facing an issue where the style class "pagination pagination-lg" is not applying properly. I'm cu ...

Overlapping Dropdown Javascript Menus

To achieve the desired effect in the header, I need to create two expandable dropdown menus for the first two titles. My coding experience is limited, especially when it comes to javascript and jquery. Currently, I have managed to create the effect for one ...

Combining PHP and MySQL with a join statement

I am trying to calculate the average rating for each store in a directory list using MySQL's AVG function. Whenever someone reviews a store, they assign a rating ranging from 1 to 5. This rating is then inserted into the rating column of the reviews ...

What is the best way to adjust padding within a flexbox layout?

I am attempting to replicate the design of this webpage using HTML and CSS: https://i.sstatic.net/bFVmg.jpg But, I am struggling to adjust the padding for the box on the right side. Here is how my version looks currently: https://i.sstatic.net/WMwii.jpg ...

Is there a way for me to create a button in Javascript that will mute the background music when clicked?

Currently, I am working on my website () to implement a functional mute button. The goal is for the mute button to silence the background music that is currently playing. How can I achieve this functionality when the image is clicked? Here is the HTML co ...

Vertical stacking of Bootstrap cards is a common design practice

Good day everyone! I'm in the midst of a personal project that involves utilizing the bootstrap framework and PHP. My goal is to fetch data from my MySQL database and have it displayed in horizontal rows of 4 individual cards within a bootstrap card e ...

Highlighting HTML syntax for attributes without quotation marks

Regardless of whether it's a good approach or not: Is there a way to enable Sublime Text to highlight HTML syntax for unquoted attributes? For example, <div class=foo data-label=bar> I was able to resolve the issue of unquoted id attributes ...

Ways to align my image to the left and text to the right using Bootstrap

body{ height: 100%; font-size: 1rem ; } .navbar-brand{ font-family: 'Syne Mono', monospace; color: black; size: 0.5rem; } header { width: 100%; height: 100vh; background: rgb(237,104,104); background: linear-gradient(90d ...

Can using display: none impact the SEO of a website's navigation menu?

Is there a difference in SEO impact between using display:none with CSS and using display:none with jQuery for menus? Appreciate the insights! ...

Unable to get `tr:nth-child` to function properly within an MVC view

I have come across many questions regarding my issue, but none seem to offer a solution that works for me. The problem lies with the styling in the upper section of my view (which I plan to transfer to bootstrap later on). My main objective is to implement ...

Prevent Form Submission in Microsoft Edge

I need help figuring out how to prevent Microsoft Edge from keeping form values, such as inputs, when a page refreshes. You can see the issue by looking at the first name last name example on this page https://www.tutorialspoint.com/html/html_forms.htm. ...

How can I limit user access to web content in Django with a daily usage allowance?

I am the administrator of an online pricing website built with Django. My goal now is to limit access to price information for non-VIP users. I want to implement a system where non-VIP users can only view prices on my website up to 3 times per day after l ...

Is there a way to replicate the data from one canvas onto another canvas using getContext('webgl')?

I am currently working on a project that involves displaying medical images on one canvas and annotating those images by drawing shapes or lines on another canvas. My issue arises when I try to copy the drawn annotations from canvas#2 to canvas#1. Here is ...

Unable to link JavaScript to HTML file through script tag

Upon testing out a responsive nav bar, I encountered an issue with the JavaScript not connecting. Despite placing the script tag at the bottom of the body as recommended, it still fails to function. index.html <html lang="en"> <head> ...

Separate compound words without hyphens at the boundaries of each word

I am currently incorporating the use of Bootstrap for my project. Let's assume that I have text displayed in this way: "Stackoverflow" Is there a method to automatically ensure that it breaks like below if the text exceeds the container: "Stack ...