Moving Text Over a Static Background Image in HTML and CSS

Currently working on developing a website that involves coding in HTML and CSS. However, I've encountered an issue while programming. Whenever I attempt to adjust the positioning of my text, the background image shifts along with it, making it impossible for me to overlay the text on the background image.

This is how my website appears:

I want the 'RESPITORY SYSTEM' section over the image

Here's my code:

<!doctype html>
<html lang=''>
<head>
...
</style>

<div id="backimage">
<br>
<body background=backimage>
</div>
<div class="bgimg-1">
  <div class="caption">

...
</div>
    </div>

<blockquote>
  <p>And here is my styles.css:</p>
</blockquote>

<pre><code>@import url(http://fonts.googleapis.com/css?family=Raleway); #cssmenu, #cssmenu ul, #cssmenu ul li, ...

(Apologies for the layout issue)

If you require more information, feel free to ask.

Edit: Updated title from 'CSS' to 'HTML and CSS'.

Answer №1

When faced with this situation, you have two choices:

1- Implement absolute positioning with responsive units (vh - vw, vmin - vmax). For example:

h1.title{
  position: absolute;
  left: 20vw;
  top:25vh;
}
<div class="content">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQTPGf2xetXnwKbT-8mWJWQHGpJyRHUL0Icn9HbfwB_4DNcLPzX">
<h1 class="title">Hello</h1>
</div>

2- Opt for using a background image instead of an image as a block and then center the text:

.content{
  width: 100%;
  height: 200px;
  margin-left: auto;
  margin-right: auto;
  background: url('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQTPGf2xetXnwKbT-8mWJWQHGpJyRHUL0Icn9HbfwB_4DNcLPzX');
}
h1.title{
  text-align: center;
  padding-top: 30vh;
}
<div class="content">
<h1 class="title">Hello</h1>
</div>

Answer №2

Upon review, I made some enhancements to your existing work and noticed a few areas that require further attention.

I observed an abundance of <body> and <html> tags in your code, indicating you may have incorporated various examples without fully understanding the structure. To ensure proper HTML formatting, I recommend referencing this informative website.

Additonally, there were instances of repetitive code and random <link> and <script> tags present. Familiarize yourself with their functions by visiting the same site. These elements are typically used for importing CSS and JavaScript from external sources.

Below is the refined version of your code snippet, which could be optimized further. It's essential to grasp the code snippets you integrate into your project. Best of luck on your coding journey! 🤘

body, html {
  height: 100%;
  margin: 0;
  font: 400 15px/1.8 "Lato", sans-serif;
  color: #777;
}

.caption {
  position: absolute;
  left: 0%;
  top: 50%;
  width: 100%;
  text-align: center;
  color: #000;
}

.caption span.border {
  background-color: #111;
  color: #fff;
  padding: 20px;
  font-size: 25px;
  letter-spacing: 20px;
}

h3 {
  letter-spacing: 5px;
  text-transform: uppercase;
  font: 20px "Lato", sans-serif;
  color: #111;
}


.bg {
    background: url('https://i.pinimg.com/originals/68/5e/18/685e18ff8147427079a14ce27d984688.jpg');
    height:450px;
    background-position: center;
    background-repeat: repeat-x;  
    background-size: cover;
    position:relative;
}
<!doctype html>
<html lang=''>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
   <meta charset='utf-8'>
   <title>Respiratory System</title>
</head>

<body>
  <ul id='cssmenu'>
     <li class='active'><a href='#'>Home</a></li>
     <li><a href='#'>Products</a></li>
     <li><a href='#'>Company</a></li>
     <li><a href='#'>Contact</a></li>
  </ul>

  <div class="bg">
    <div class="caption">
      <span class="border">RESPIRATORY SYSTEM</span>
    </div>
   </div>
  </body>
</html>

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

Preserving the position of inline text

I've been working on a button design that I want to make look more realistic by moving the text down 1px inside the button when it's pressed. To achieve this effect, I added an extra pixel to the top and removed one from the bottom. However, the ...

Unable to retrieve the following element in a JavaScript array

I am a beginner in JavaScript and I am attempting to access the next element of an array using an onclick function but so far I have not been successful. var i, len; function quiz() { var quiz_questions = [ "who is the founder of Fa ...

Place the div directly beside the input field on the right side

I am attempting to align a div directly beside the text being entered in a text input field. It seems logical to me that this could be achieved by measuring the length of the input value and positioning the div accordingly. However, the issue I am facing i ...

jQuery - easily adjust wrapping and unwrapping elements for responsive design. Perfect for quickly undo

Within the WordPress PHP permalinks and Fancybox plugin, there are elements enclosed in an "a" tag like so: <a href="<?php the_permalink(); ?>" class="example" id="exampleid" data-fancybox-type="iframe"> <div class="exampledivclass"> ...

Sending a request to a PHP file using Ajax in order to display information fetched from

I am attempting to display the database row that corresponds with the student's forename and surname selected from the dropdown list. I have managed to store the first name and surname in a variable called clickeditem. However, I suspect there may be ...

Using *ngIf together with fxFlex.lg and fxFlex.xl is causing compatibility issues

One of the challenges I faced involved a div element with a conditional *ngIf statement and responsive layout values set using fxFlex.lg="30" fxFlex.xl="50": <div class="project-test__element" *ngIf="myCondition()" ...

There is a slight misalignment when trying to horizontally center a character within a div

I've experimented with various methods like using a px width or em widths. I've attempted nesting a span and trying different styles such as text-align:center or margin:0 auto. I can manage to center either the R or the S, but struggling to get ...

What is the method for creating text outlines using html5/css3?

I have been experimenting with using shadows to outline text, which is a solution I came across in various Stack Overflow threads. However, support for text-outline and text-stroke features is currently limited. I am encountering an unusual issue with on ...

I have implemented the appropriate code to showcase a background color, yet it doesn't seem to be appearing on the screen. Additionally, I am utilizing Sass in this project

Could someone help me understand why the background color is not showing on my website? This is the CSS I am using html { font-size: 100%; -webkit-box-sizing: border-box; box-sizing: border-box; } *, *::before, *::after { -webkit-box-sizi ...

Could you please provide me with the option to send a list of the

Is there a way to send output via email instead of displaying it in the following div: <div id="fullCalendar" ></div> After spending a whole night searching online, I couldn't find a solution. As I'm not very familiar with jQuery pr ...

Sorting functionality malfunctioning after dynamically adding new content using AJAX

Greetings to all, I have a question about my views. 1- Index 2- Edit In the index view, I have a button and AJAX functionality. When I click the button, it passes an ID to the controller which returns a view that I then append to a div. The Edit view con ...

I prefer children to have their own unique style, instead of inheriting their parent's CSS

I currently have a project structured in the following way: There is an index page with a full layout Separate PHP files that are included in the index page Bootstrap is used in the index page, however, in some of the separate PHP files I also use jqgri ...

`Scrolling through a horizontal menu with no scrollbar present`

Is there a way to create a horizontal menu that can scroll left or right without the scrollbar? I'm looking for a solution like adding on-screen arrow buttons or implementing mouseover functionality. What would be the best approach? div.scrollmenu ...

There is a lag in the loading of css stylesheets

We created a single-page company website that utilizes three different stylesheets connected to the index.html. By clicking a button, users can switch between these stylesheets resulting in changes to colors, images, and text colors. However, Issue 1: The ...

Include a CSS file from an external JavaScript file

Is there a way to include an external CSS file in an external JS file? I am trying to reference and load Default.css inside Common.js like the image below shows. Any suggestions are greatly appreciated! BB ...

D3 not distinguishing between bars with identical data even when a key function is implemented

When attempting to create a Bar chart with mouseover and mouseout events on the bars using scaleBand(), I encountered an issue. After reviewing the solution here, which explains how ordinal scale treats repeated values as the same, I added a key to the dat ...

Retrieve data from HTML in order to apply styling to an element

I am attempting to set the width of my .bar-1 outer span element based on the value of my inner span element. <span class="bar bar-1"> <span name="PERCENTAGE" id="PERCENTAGE" disabled="" title="Total Percentage" maxlength="255" value="66" tabinde ...

Having trouble getting my JavaScript code to function properly on Firefox browser

I have created a script where the cursor automatically moves to the next form field once it reaches its maximum length, in this case 1. Here is the JavaScript code: window.onload=function(){ var container = document.getElementsByClassName("container")[0] ...

What is the most effective way to implement a single modal throughout my web application without having to duplicate HTML code on each page?

After realizing I was repetitively adding the same div to every page for a modal dialog, I decided to place a single div in the site.master page and call it when needed. This method worked fine until I began implementing ajax with partial page updates. H ...

Tips on avoiding the accumulation of event handlers when dealing with click events triggered by the document selector in jQuery

I am currently working on a project that involves using AJAX to load various pieces of HTML code. This is done in order to properly position dynamic buttons within each portion of the HTML content. In my case, I need to trigger click events on the document ...