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

Servlet-enhanced Turn-Based Gaming Experience

I am working on developing a turn-based game, similar to Checkers, using Servlets and JSP pages. I have set up a specific page with a newGame button that redirects players to the gamePage. This redirect sends one player to Black.jsp and the other to Red.js ...

Identify input elements that specifically contain an onclick event

Several of the input elements on my page have a function called setSomeFunction() that either shows or hides certain divs when clicked. I know I can locate all the input elements using document.getElementsByTagName("input") and store them in an array. How ...

I am encountering a problem with the $invalid property in AngularJS

Hey there, I've got a coding dilemma involving three number inputs, which I will refer to as the first input, second input, and third input for clarity. Here's the setup: <div> <form name="formOpenMax"> <div clas ...

CSS designs that adapt with the screen: Triangles in

I'm currently working on creating a responsive triangle using CSS. I want both the height and width of the triangle to adjust as the window size changes. Below is my static code: #triangle { z-index: 2; width: 0; height: 0; position:absolute ...

Showing the values of two distinct select boxes in a URL

Here are both select boxes, the goal is to display selected values from both in the URL. This can be achieved by displaying them like this: e.g www.example.com#135+#140 OR www.example.com#135&140 (The order of values doesn't matter as long as bot ...

Intersecting Hyperlink Elements Creating a Hover Effect

I've encountered a perplexing CSS display issue and I'm at a loss for alternative solutions besides simply widening the width of the parent div. Allow me to explain the layout: <div> <ul> <li> <a href="javascrip ...

Tips for getting links to wrap or scroll within a column element in Angular

In my row element with two columns, I am facing an issue where the first column sometimes overlaps the second column due to a very long link in the problem.problem_description section. I want to find a way to have long links wrap or be scrollable. However, ...

Is your text appearing vertically in Internet Explorer?

I'm having an issue with my small single page website where the text in a form is displaying vertically instead of horizontally in Internet Explorer. The problematic part is related to "vehicle condition." Any suggestions on what I might be doing wron ...

Retrieve the content of the nearest 'td' element using the '.closest()' method, then locate the desired

I am struggling to assign the value from a <td> to a variable. My approach involves utilizing the closest() and find() methods in jQuery to locate the desired <td>. Interestingly, when I use alert on the <td>, it displays the correct val ...

What is the significance of $() in jQuery?

I understand that $() is a selector, but I find it puzzling as to what it actually selects since there is nothing inside the parentheses. Is this a common practice or frowned upon in coding conventions? An example of where I have seen it used is when maki ...

Looking for assistance with my website's navigation bar and logo

Looking for assistance in moving my navigation buttons to each side of the logo. As a beginner in web design and CSS, I've been struggling with this task. Essentially, I want the "home" and "about" buttons on the left side of the logo, 16px apart from ...

Create a division that will remain visible on the screen and allow scrolling when a certain class is

Having some trouble with a fixed class div that is not continuing to scroll after the class is removed on scroll. I've attempted to fix this issue but the div keeps getting hidden instead of continuing to scroll. If anyone has any advice or can poin ...

Tips for verifying the HTML5 date format

I am looking to implement the HTML5 date input field <input type='date' name='customDate'> This will allow other users to utilize the built-in datepicker available in their browser. One challenge I have encountered is ensuring ...

"Creating a navigational bar using Bootstrap in a one-page application

I am facing an issue with Bootstrap's navbar in my single-page application. The dropdown navigation menu for mobile is not collapsing properly after clicking. Although I tried to fix it by adding data-toggle="collapse" data-target="#navbar" attributes ...

Are certain browsers unable to play dynamically generated HTML5 audio files?

While working on my project, I encountered an issue with creating and controlling an audio element in JavaScript. The element works perfectly fine in Firefox, Chrome, and Opera; however, it fails to function properly in IE and Safari. In these browsers, ...

When text is wrapped within the <li> tag

In this div, the structure is as follows: <div class="box1" id="infobox"> <h2> Point characteristics: </h2> <div style="padding-left:30px" align="left"> <ul> <li class="infobox_list"><b>X value: </b>< ...

When a Grid Item is enclosed in a link, it does not extend over rows

After avoiding HTML/CSS for a long time, I finally decided to dive in and create a simple CSS grid website with 3 items in the grid. Everything was working well and looking exactly as I wanted it to: However, I had the idea of using the entire Aside-Items ...

Can one intercept the window.location.replace event?

Suppose I am currently browsing the URL "example.com/somepage#somehash" and then decide to execute window.location.hash = "anotherhash", the URL will be updated to "example.com/somepage#anotherhash". This action triggers the window.hashashchange event. On ...

What content appears post-iframe?

After researching for a while, I've only come across information about text displayed after a broken iframe on this topic. However, what I actually want to do is display text below an iframe set at 90% height, like so: I would like to add some text i ...

Navigating the Bootstrap layout resulted in encountering two scrollbars

My HTML code for a todo application is displaying two scrollbars, can anyone suggest the correct Bootstrap layout for my application? <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/> <div ...