Tips for creating responsive text within a hero image

I'm exploring the world of creating responsive websites using Bootstrap, a new venture for me. One challenge I've encountered is trying to create a hero image with text positioned perfectly in the center. Despite my efforts, the text does not adjust when the background image resizes for smaller viewports, resulting in an issue like this: https://i.sstatic.net/EqI1p.png

Here's what I have tried so far: HTML:

    <header class="header-container">
        <!-- background image -->
        <div class="bg-image d-flex justify-content-center align-items-center">
            <div class="text-center">
                <h1 class="h1-hero h-font">where great ideas come to life</h1>
                <p class="p-hero text-font">Passionate creative studio helping startups grow their
                    business!
                </p>

            </div>
        </div>
    </header>

CSS:

.header-container {
    padding: 48px 50px 0px;
    max-width: 100%;
}

.bg-image {
    background-image: url('../img/hero.png');
    background-size: 100%;
    height: 100vh;
    background-repeat: no-repeat;
}

.h1-hero {
    font-size: var(--font-size-h1);
    font-weight: 500;
    line-height: 106px;
    letter-spacing: 4.5px;
    text-transform: uppercase;
    color: white;
}

.p-hero {
    font-family: var(--font-family-text);
    font-size: 20px;
    color: white;
    opacity: 80%;
    font-weight: 300;
    line-height: 23px;
    letter-spacing: 1.3px;
    padding-top: 12px;
}

Answer №1

If you want to improve the alignment of your text-center div, consider implementing a flexbox display. This method can help align your h1 and p tags without relying solely on line-height.

To ensure your design is responsive across different viewport sizes, make use of media queries. For example, you can adjust font sizes at specific breakpoints like 600px.

Additionally, don't forget to fine-tune background image settings for better visual presentation. Changing the size from 100% to cover can enhance the overall look of both the background image and the containing text element.

.header-container {
  padding: 48px 50px 0px;
  max-width: 100%;
}

.bg-image {
  background-image: url('https://images.unsplash.com/32/Mc8kW4x9Q3aRR3RkP5Im_IMG_4417.jpg');
  background-size: cover;
  height: 100vh;
  background-repeat: no-repeat;
}

.h1-hero {
  font-size: var(--font-size-h1);
  font-weight: 500;
  letter-spacing: 4.5px;
  text-transform: uppercase;
  color: white;
}

.p-hero {
  font-family: var(--font-family-text);
  font-size: 20px;
  color: white;
  opacity: 80%;
  font-weight: 300;
  letter-spacing: 1.3px;
  padding-top: 12px;
}

.text-center {
display: flex;
justify-content: center;
align-items: center;
flex-flow: column nowrap;
}

@media all (min-width: 600px) {
  .h1-hero {
    font-size: 3rem;
  }
  .p-hero {
    font-size: 10px;
  }
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9bba6a6bdbabdbbbbaba883a93bf88965956986b">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">

<header class="header-container">
  <!-- background img -->
  <div class="bg-image d-flex justify-content-center align-items-center">
    <div class="text-center">
      <h1 class="h1-hero h-font">where great ideas come to life</h1>
      <p class="p-hero text-font">Passionate creative studio helping startups grow their business!</p>
    </div>
  </div>
</header>

Answer №2

Some adjustments have been implemented

  1. The bg-image class had a background-size of 100% that prevented the image from filling the container properly. This caused an issue where the background image didn't resize, making it appear as though the text was no longer centered.

2. To ensure the text is responsive, consider utilizing container sizing units, viewport units, or clamp for better adaptability. Find out more about Viewport units and clamp.

Congratulations on mastering bootstrap!

.bg-image {
     --height: 500px;
     background-image: url("--img");
     background-repeat: no-repeat;
     background-size: cover;
     block-size: var(--height);
}

.h1-hero {
      font-size: 6cqi;
      font-weight: 500;
      text-transform: uppercase;
      color: white;
}

.p-hero {
      font-size: 2cqi;
      color: white;
      opacity: 80%;
      letter-spacing: 1.3px;
      margin-top: 12px;
}

Answer №3

If you want to center the text, you can achieve it by utilizing a mix of flexbox with relative and absolute positioning:

<div class="bg-black position-relative text-white">
<div class="position-absolute top-0 end-0 bottom-0 start-0">
    <!-- Insert background image in this element -->
</div>
<div class="align-items-center d-flex flex-column justify-content-center position-absolute top-0 end-0 bottom-0 start-0">
    <h1 class="h1-hero h-font">where great ideas come to life</h1>
    <p class="p-hero text-font">Passionate creative studio helping startups grow their business!</p>
<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

The background widths do not stretch to 100%, but rather stop right before reaching the

Experience it live: Adjust your screen width slightly to activate the mobile layout, but ensure that there is still enough room for a horizontal scroll bar. Astonishingly, all my background images stop short of this point. Innovative Fix To prevent the ...

Having trouble changing font color using AngularJS - what am I missing?

I am currently working on a straightforward program where the user inputs text into a textbox, and that text is displayed below. However, I also want the font color to change based on what the user types in the "color" field. Why isn't this functional ...

The minimum height increases as the inner divs expand

Can you help me with the code below? html <div class="content"> <div class="col"> </div> <div class="col"> </div> <div class="col"> </div> <div class="col"> </div> ...

Struggling with the alignment of divs in a vertical manner

Looking for a way to align multiple divs inside another div vertically with even spacing between them? They should all start at the top. Currently, my solution has the child-divs positioned at the top-left corner of the parent div (see the first picture): ...

Looking for assistance with parsing football league standings from a website using PHP

Is there a way to use PHP to extract an HTML table from a website? References: The table I want to parse can be found here: This is the code I attempted to use: <div class="row"> <div class="col-md-8"> <h1>Standings</h1 ...

Using PHP to perform live calculations with arrays in real time

Currently, I am working on developing a new system for a client that allows them to create bookings and invoices to send to their clients. One specific requirement my client has is the need for a table column to be added below the container columns in whi ...

Tips for keeping certain webpages from showing up in Google search results

I recently launched a website that allows users to create their own profiles. The problem is, when someone links to their profile from their website or blog, it ends up showing in Google searches for my site. Unfortunately, the only person who has done thi ...

Displaying AJAX Confirmation in a Popup Window

My form is set up to insert data into a database via an AJAX request. Upon successful insertion, the "rentinsert.php" will display a success message on the page that reads "Your order has been placed". However, I would like this success message to appear i ...

Issues are arising with Bootstrap's functionality within the Vite React app

I am new to working with react and vite. I am currently developing a vite react application that requires the use of bootstrap. I followed the instructions provided on the official Bootstrap website here. However, not all Bootstrap functionalities are work ...

Generating SVG images that show up as black in light mode and switch to light colors in dark mode

I am currently working on a website that has the option to switch between light and dark mode, and I need my logo to adjust accordingly. The black elements of my logo should appear black in dark mode and light in light mode. I attempted using the CSS cod ...

Modify the text tone within a specific cell

Welcome to my unique webpage where I have implemented a special feature. The text highlighted in red represents the unique identifier for each individual cell. Interestingly, below there is an input field containing the code "0099CC", which corresponds to ...

Having difficulties creating an array due to identical id tags causing HTML to break in JavaScript

I've been grappling with this issue all day. Before delving into dynamic solutions, I'd like to create multiple divs with unique id tags or classnames rather than repeating ids. Whichever option proves simpler would be greatly appreciated. Curren ...

Tips for determining the full width of a webpage, not solely the width of the window

While we can easily determine the width of the window using $(window).width(); This only gives us the width of the window itself, not accounting for any overflowing content. When I refer to overflowing, I mean elements that extend beyond the visible view ...

Arrange the select default option using AngularJS as the first option

I've been working on a project using Angular where I fetch data from a JSON file and push it to an array object. This data is then displayed in the options of a select element. My issue is: The default option with selection appears first, but I only ...

Unable to achieve full width for a div within a scrollable container

Currently, I am in the process of constructing a new website using Gatsby.js. However, I have encountered a minor issue with some CSS related to code blocks. Since CSS is not my strong suit, I have been unable to resolve it thus far. Below is a runnable sn ...

Position 2 identical classes side by side on the horizontal axis

Currently enrolled in the MIMO HTML course and facing a challenge where I am unable to align both elements with the class="column" attribute horizontally using display: inline-block; I have attempted using float:right and other CSS properties to achieve a ...

The background size of each list item is determined by the size of the item

I'm trying to create a menu using an unordered list, where each item has a background color. However, I want the width of each item to be smaller than the list itself and aligned to the right. Any ideas on how to achieve this? Here is the HTML code: ...

When clicking on an HTML link pointing to a cloud, it does not open in the same frame but instead opens in a new window

I am currently working on an HTML5 application that features a screen divided into two parts. The left part of the screen contains a few links, and upon clicking one of these links, the resulting content should open in the right side frame within the same ...

Is there a way to alter the footer across all my pages using just one document?

I'm having trouble coming up with a title for my question, so please bear with me. I am working on a Bootstrap website and I want to create a consistent navbar and footer across all pages without duplicating the code in each document. How can I achiev ...

Trouble with Bootstrap CSS buttons: color consistency problem persists

I am facing an issue with my Bootstrap button, which currently looks like this: https://i.sstatic.net/MWHYP.png The HTML code for the button in question is as follows: <button type="button" class="btn-primary.custom-btn" data-bs-to ...