Tips for ensuring text on an image dynamically resizes based on the browser's dimensions

Is there a way to keep text centered over an image when resizing the browser window to ensure responsiveness? I've tried positioning the text as an absolute element, but it keeps overflowing the boundaries of the image. How can I prevent this?

.banner {
  display: block;
  width: 100%;
}

.banner-img {
  display: block;
  width: 100%;
}

.banner-text {
  position: absolute;
  top: 30%;
  left: 25%;
  font-size: 70px;
  color: aliceblue;
  font-family: 'Merriweather', serif;
  text-align: center;
  width: auto;
}
<div class="banner">
  <img class="banner-img" alt="banner image" src="images/banner.jpg">
  <div class="banner-text">Hello, Welcome To My Portfolio!
    <p>I'm Nicholas!</p>
  </div>
</div>

Answer №1

Give this a shot. It will center your text both horizontally and vertically. Plus, for responsive web design, opt for relative units like em or rem instead of pixels.

Check out this link for further clarification https://www.w3schools.com/howto/howto_css_image_text.asp

.banner {
  position: relative;
  width: 100%;
}

.banner-img {
  width: 100%;
}

.banner-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 3rem;
  color: aliceblue;
  font-family: 'Merriweather', serif;
  text-align: center;
}
<div class="banner">
  <img class="banner-img" alt="banner image" src="https://images.unsplash.com/photo-1605580556856-db8fae94b658?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80">
  <div class="banner-text">Hello, Welcome To My Portfolio!
    <p>I'm Nicholas!</p>
  </div>
</div>

Answer №2

If you're struggling with a design issue, I've shared some CSS code that might help:

.banner {
  display: block;
  width: 100%;
  position: relative;
    min-height: 700px;
}

.banner-img {
  display: block;
  width: 100%;
}

.banner-text {
  position: absolute;
  top: 50%;
  left: 50%;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
  font-size: 70px;
  color: aliceblue;
  font-family: 'Merriweather', serif;
  text-align: center;
}

@media only screen and (max-width: 850px) {
    .banner-text {
        font-size: 35px;
    }
}

Here are some key points to consider:

When using "position:absolute," it's best to have a parent element with "position:relative"

For setting the width of text elements, adjust the "font-size" rather than the "width" property

Understand how to use @media rules in CSS for responsive design, enabling property changes at different screen sizes

Adjusting the top and left properties can help center text; utilizing "transform:translate" is crucial for precise centering. Feel free to tweak these values for your desired layout.

Answer №3

Utilize font-size:clamp() and the grid property to effortlessly center content without worrying about its position:

The function clamp(MIN, VAL, MAX) is interpreted as max(MIN, min(VAL, MAX))

header {
  display: grid;
  align-items: center;
  font-size: clamp(30px, 70px, 6vw);
  color: aliceblue;
  font-family: 'Merriweather', serif;
  text-shadow: 0 0 1px #333;/* for improved visibility */
}

header h1 {
  font-size: inherit
}

header img,
header div {
  grid-row: 1;
  grid-column: 1;
  width: 100%;
  text-align: center;
}
<header>
  <img src="https://picsum.photos/id/103/1200/800">
  <div>
    <h1>Hello, Welcome To My Portfolio!</h1>
    <p>I'm Nicholas!</p>
  </div>
</header>

Answer №4

To ensure your text remains readable on varying screen sizes, it is important to use relative values for font-size. This allows the font size to decrease proportionally as the screen size decreases. Additionally, adding position: relative to the parent element, in this case .banner, ensures that when you apply position: absolute to .banner-text, it is contained within its parent element. See the code snippet below.

.banner {
  position: relative;
  display: block;
  width: 100%;
  height: 50%;
  display: flex;
  align-items: center;
}

.banner-img {
  display: block;
  width: 100%;
}

.banner-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 2.4em;
  color: aliceblue;
  font-family: 'Merriweather', serif;
  text-align: center;
  width: auto;
}

@media screen and (max-width: 600px) {
  .banner-text {
    font-size: 1.7em;
  }
}
<div class="banner">
  <img class="banner-img" alt="banner image" src="https://static.vecteezy.com/system/resources/previews/000/582/542/non_2x/vector-abstract-template-black-frame-layout-metallic-red-light-on-dark-background-modern-luxury-futuristic-technology-concept.jpg">
  <div class="banner-text">Hello, Welcome To My Portfolio!
    <p>I'm Nicholas!</p>
  </div>
</div>

For smaller screens, a media query has been added to adjust the font size accordingly.

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

Tips for getting the setInterval function to work with a progress bar even when a tab is not in focus

After browsing through similar questions, I couldn't find the answer I was looking for. As a newbie in programming experimenting with JavaScript Progress Bar, I encountered an issue where the progress bar would pause and the counter wouldn't coun ...

Upon calling the method, an undesirable page refresh occurs

I have been experimenting with dynamically generating additional input fields using vuejs when a user clicks a button. Initially, my code functions properly by creating more fields, but unexpectedly the page refreshes automatically, resetting the user&apos ...

Expanding the link directory

I am struggling with accessing the "file.php" in the "folder1" directory from the "site" directory. The href path I'm using is: href = "folder1/file.php" However, it redirects me to: href = "folder1/folder1/file.php" Additionally, when try ...

Navigating an indefinite amount of state variables in React.js: A comprehensive guide

Receiving data from the server with an unknown number of items in the API leads me to utilize the map method in HTML for rendering. Below is the return section: if (loading) { return ( <div> <div> <Header /> ...

Incorporating multiple CSS style sheets within a single HTML document

As part of my assignment, I have successfully created two different CSS style sheets. The next step is to link both style sheets to a single HTML page and provide users with the option to switch between the two styles. I am wondering how to achieve this. ...

Interacting with API using Ajax and jQuery

I am struggling to get my simple jQuery request to work correctly. I'm not sure what I'm doing wrong. Can you help me identify the error? http://jsfiddle.net/k6uJn/ Here is the code snippet: $(document).ready(function() { $.ajax({ type: "G ...

The feature "text-underline-position: under" is not functioning properly in Android Chrome

Take a look at this example page: html, body { margin: 0; padding: 0; } header { padding: 16px 0; text-align: center; background: black; } header img { width: 234px; height: 222px; ...

Steps to reverting CSS attributes back to their default values for a specified element (or blocking inheritance)

How can you reset a specific CSS attribute of an HTML element, which is automatically inheriting styles from its parent, to the default value? For example: CSS: .navigation input { padding: 0; margin: 0 30em; } HTML <div class="navigation"& ...

Trouble embedding iframes in local files?

I have recently created a file which includes the following code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8> <style> iframe { height: 500px; width: 600px; } ...

Why does Javascript Tic-Tac-Toe delay notifying the user until the entire board is full?

There have been a lot of questions about Tic-Tac-Toe javascript, but my issue is unique. My code works fine, but it doesn't declare a winner or a tie game until the entire board is filled. I suspect that my if statements are causing problems and can&a ...

Retrieving HTML array values using jQuery

Presented below is an array of input boxes. <form> 9 <input type="checkbox" name="date[]" value="9"> 10 <input type="checkbox" name="date[]" value="10"> 11 <input type="checkbox" name="date[]" value="11"> </form> The o ...

Can the styling and variables of Bootstrap4 be altered dynamically?

I have recently started working on a project that utilizes Bootstrap4 for design and layout. However, there is now a need for the ability to change certain core styles (such as font-face, primary color, and background color) dynamically at runtime. Curren ...

Convert the first child of the element with the id "images" into a string using document.getElementById("images").children

When using the toString() method, it will display [object HTMLImageElement]. My goal is to get a string representation of the image element '<img src="..." />'. However, in Firefox, outerHTML returns undefined. Is there another way I can a ...

Is it possible to remove tsconfig.spec.json from an Angular project?

I recently started learning Angular and was introduced to the files tsconfig.app.json and tsconfig.spec.json when setting up an Angular app using Angular-CLI. I found a helpful point about these files on this website. Both tsconfig.*.json files serve as ...

Shrink Font-Awesome icon sizes using a Node.js and Express.js web application

Currently, I am in the process of developing a website using node.js + express.js and implementing font-awesome for icons. The local hosting of Font-awesome has resulted in a 1.2 MB JS file [font-awesome.js], even though we are only utilizing 10-15 icons. ...

Every div must have at least one checkbox checked

Coding in HTML <div class="response"> <input type="checkbox" /> <input type="checkbox" /> <input type="checkbox" /> <input type="checkbox" /> </div> <div class="response"> <input type="check ...

Utilizing BeautifulSoup to Extract Links

Currently, I am extracting cricket schedules from a specific website using Python's Beatiful Soup library. The webpage I am scraping corresponds to the following URL: www.ecb.c0.uk/stats/fixtures-results?m=1&y=2016 This particular link displays ...

Javascript and CSS combo for creating a stylish fade effect

Hey everyone, I have a design challenge where I need to change the color of a picture inside a box when the user hovers over it. I have four boxes like this and my goal is to make everything else fade out when a user hovers over a specific box. Anyone kn ...

Is there a way to ensure that the column widths in the Huxtable RTF output align perfectly with the widths of the HTML

It appears that the HTML output in huxtable has a column width setting that automatically adjusts to fit the content, creating an aesthetically pleasing output. On the other hand, the RTF output makes all columns equal width, which is not as visually appea ...

Is it possible to create a pure CSS responsive square that is positioned to the top right of a div element of any size?

My current goal is to achieve the following task... I aim to position a square perfectly in one or both corners of the top right section of a div, ensuring it never exceeds the boundaries regardless of the div's size or dimensions that necessitate th ...