What's the best way to align text in relation to the image?

enter image description here

I need help with positioning my text relative to the resizing image at different breakpoints. Can anyone assist with this?

Is there a way to position text relative to the background in a manner that it always stays attached to the image when resizing?

This is the HTML code snippet that I am currently using:

<section id="background1">
    <!--Section to display content-->
    <section id="content">
        <figure><img class="img-fluid" src="../Others/wolf-wolves-snow-wolf-landscape-89773.jpeg" onclick="openModal()" class="hover-shadow"></figure>
        <!--Display content-->
        <p class="small">Sustainability</p>
        <p class="big">Starts with<br> You</p>
        <a href="../Website/about.html">Learn more!</a>
    </section>
</section>  

Below is the CSS code used to style the text:

@charset "UTF-8";

/*default*/
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

#background1 {
  height: 600px;
  background-color: #d3d3d3;
}

#content figure img {
  position: relative;
  width: 100%;
  color: white;
}

#content .small {
  position: absolute;
  top: 45%;
  left: 49%;
  display: block;
  text-align: center;
  font-family: "EB Garamond", serif;
  font-size: 2vw;
  color: rgb(37, 37, 37);
  transform: translate(-50%, -50%);
}

.big {
  position: absolute;
  top: 65%;
  left: 49%;
  display: block;
  text-align: center;
  font-size: 5vw;
  font-family: "Montserrat", sans-serif;
  font-weight: 600;
  color: #fff;
  transform: translate(-50%, -50%);
}

#content a {
  position: absolute;
  top: 90%;
  left: 49%;
  display: block;
  padding: 5px;
  border: 3px solid #2d1c8a;
  border-radius: 80px;
  text-align: center;
  font-size: 2vw;
  text-decoration: none;
  font-size: 20px;
  color: rgb(255, 255, 255);
  background-color: #1e3094;
  font-family: "Montserrat", sans-serif;
  transform: translate(-50%, -50%);
}

Answer №1

After completing positioning, feel free to apply any styles to the button or text that you require, such as color or font-family.

#content {
  position: relative;
}

.content {
  position: absolute;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  text-align: center;
}

#content figure img {
  width: 100%;
}
<section id="background1">
  <!--Section to display content-->
  <section id="content">
    <figure>
      <img class="img-fluid" src="https://static1.squarespace.com/static/5baeb0a5da50d36d5bfc1f67/t/5bb6862ee79c70988e4f7594/1538688629297/dreamstime_xxl_94596211+-+wolf+profile.jpg?format=1500w" onclick="openModal()" class="hover-shadow"></figure>
    <!--Display content-->
    <div class="content">
      <p class="small">Sustainability</p>
      <p class="big">Begins with<br> You</p>
      <a href="#">Discover more!</a>
    </div>
  </section>
</section>

Answer №2

give this a shot

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8>
<title>Sample</title>
<style type="text/css">
    .container{
        position: relative;
        display: inline-block; /* Ensure box width matches image */
    }
    .container .text{
        position: absolute;
        z-index: 999;
        margin: 0 auto;
        left: 0;
        right: 0;        
        text-align: center;
        top: 40%; /* Adjust this value for vertical positioning */
        background: rgba(0, 0, 0, 0.8);
        font-family: Arial, sans-serif;
        color: #fff;
        width: 60%; /* Set the width of the positioned div */
    }
</style>
</head> 
<body>
    <div class="container">
        <img src="https://images.pexels.com/photos/531321/pexels-photo-531321.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" alt="Flying Kites">
        <div class="text">
            <h1>Sample</h1>
        </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

A vertical line in Javascript extending upward from the base of an element

Is there a way to create a design where an infinite vertical line extends from the bottom of a circle (or in my case, a rectangle) without using css :after or pseudo-elements? I would like the line to be its own element and not limited by the restriction ...

Troubleshooting: Bootstrap 5 Submenu Dropdown Fails to Expand

I am struggling with implementing a dropdown menu in Bootstrap 5. Although the dropdown menu is visible, I am facing an issue where the submenu items do not expand upon clicking. Below is the code snippet that I am using: <body> <nav class=&qu ...

Is it possible to access a comprehensive list of all the elements that are currently available?

Is there a way to retrieve all HTML tag names that are supported by the browser for my web application? I want it to be displayed like this: console.log(getAllElements()) //[a, abbr, acronym, address, applet, area, base, ...] ...

What are the steps for incorporating information into a designated PhpMyAdmin account?

Currently, I am experimenting with HTML and Php. The website is quite simple at the moment and lacks adequate security measures. My objective is to update a specific user's competition field in the users table located within the authentication folder ...

Jump to Anchor when Page Reloads

Hey there! I'm currently developing a gallery script and I'm trying to figure out how to automatically position the page to the top of a specific anchor when it is loaded. I attempted to use the following code: <script>location.href = "#tr ...

Tips on maximizing the file size limit of the fileupload control in asp.net

I am trying to determine the file size limit for my file upload control. Below is the HTML code snippet: <input id="uplTheFile" style="WIDTH: 318px; HEIGHT: 22px" type="file" size="80" name="uplTheFile" runat="server"> I have checked the code but ...

Troubleshooting an HTML Design Flaw: How to create a 3x3 table with

I have been attempting to design a HTML page with a 3x3 layout. Despite trying various methods, including divs and tables, I am having difficulty achieving equal spacing around the centered image and the other table rows. Below is an example of the layout ...

Challenge with PHP mail function in sending emails

I run a website and have a "Contact" section where users can fill out a form to reach me. The form is a basic one with its action being a php page. Here is the php code: $to = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4 ...

Showing a div above another div without relying on z-index

I have designed custom drop down menus that consist of one <div> containing the current value and functioning as a <select> field, with another <div> below it that appears when the top one is clicked, displaying all the <option> val ...

Ensuring that the bootstrap5 column extends all the way to the footer

Looking to display a specific email address mailbox for a project. The goal is to have the 'Mail Card List' scrollable instead of extending beyond the page. Tried using 'max-height:;' CSS, but it sets a fixed height. Seeking a variable ...

Eclipse now features automatic CSS class suggestions to help streamline your coding

After using NetBeans for a while, I am now considering making the switch to Eclipse. Everything seems fine, but one thing that is bothering me is that Eclipse does not provide CSS class proposals like NetBeans does. In NetBeans, whenever I use or create a ...

Removing HTML tags from Angular template bindings

My data list sometimes includes HTML elements <li *ngFor="let result of results"> <span [innerHTML]="result.question.title"> </span> </li> The issue with using innerHTML is that the HTML is parsed and rendered, affecting ...

Image uploading in Angular is not compatible with Internet Explorer, however, it functions correctly in Google Chrome

As of now, my implementation has been successful in all browsers except for Internet Explorer 11. Despite being able to successfully upload .jpeg, .jpg, and .png images in Google Chrome, I am facing issues when trying to upload them in IE 11. The code wo ...

Why isn't Latex rendering when called from Javascript?

Below is the code I'm working with: function test() { document.getElementById('demo').innerHTML="$$\left[ x=0 \right] $$";//same code from demo1.but not rendered } test(); <script type="text/javascript" src="http://latex.co ...

What is the best way to extract this content from the HTML using Selenium Webdriver?

My goal is to extract the text "1532.6788418669355" from this HTML code, but I've been facing challenges in doing so. Here's what I've attempted: IList options = Driver.FindElements(By.XPath(".//span//*")); Options[0].Text = "" There are n ...

The div height adjustment peculiarities in IE7 and IE8 are causing quite a stir

I recently encountered a problem with my HTML/JS code that I thought was simple. The code is designed to expand the size of a div on mouseover and then collapse it back on mouseout. Here's how the code looks: CSS: .sign-in-up { position: absolut ...

angular bootstrap dropdown opening on the left side

I am facing an issue with the angular bootstrap dropdown. I have successfully implemented it, as shown in this link. However, the problem is that the dropdown opens on the right side by default. Is there a way to make it open on the left side instead? Bel ...

Steps to store user input into an array and subsequently combine the stored input:

I am currently working on a form that consists of two text boxes: Task and Description. My goal is to be able to log the input from both boxes and save it via a submit button. For example: Task: do laundry Description: do a buttload of laundry (idk lol) I ...

Issue with CSS styling on a content slider causing the last picture to display incorrectly

I recently updated a Content Slider and tweaked the styles to my preference. Check out the changes I made on this link. In addition, I included an extra Thumbnail Picture on the right (7th one) along with the Main Picture. Oddly enough, the Slider seems ...

Is the H1 tag styled differently when located within an article element? Additionally, what is the best way to style multiple paragraphs within a

What is the best and most effective way to style multiple paragraphs within a div or semantic tag, each with different styles? Also, I have noticed that when I include an h1 tag inside the article, it does not display at the expected size. Does anyone know ...