What is the best way to place text on a banner and ensure it adjusts to different screen sizes?

One issue I am facing is that the "h1" text is not appearing as intended, and upon resizing the screen, the "p" text shifts down the page and no longer aligns with the image.

Below is the HTML code snippet:

<div id="home_header">
        <img src="resources/assets/home_header.png" alt="youth futures header">
        <h1>Hi</h1>
        <p class="textstyle2">14 WARM BEDS. YOUTH 12-17.<br/>YOUR TEMPORARY HOME:)</p>

</div>

Here is the associated CSS code:

h1 {
font-family: "quicksand-bold";
font-size:214px;
line-height:244px;
color:#ffffff;
margin:0;
width: auto;
text-align:center;
}

.textstyle2 {
font-family:"quicksand-bold";
font-size:38px;
line-height:43px;
color:#9ec54d !important;
}

Answer №1

Your CSS Styles

.hero {
      width:960px;
      margin:1rem auto;
      position: relative;
    }

    .text {
      background:rgba(255,255,255,0.5);
      position: absolute;
      top:50%;
      left:0;
      padding:1rem;
      transform:translateY(-50%);
    }

Here is your HTML Markup

<div class="hero">
  <img src="http://lorempixel.com/output/city-q-c-960-480-4.jpg" alt="" />
  <div class="text">
   <h1>Hello</h1>
        <p class="textstyle2">14 COZY BEDS. AGES 12-17.<br/>YOUR TEMPORARY ABODE :)</p>
  </div>
</div>

Answer №2

#home_header{position: relative;}
Adjust the position of your container in relation to other elements, and then position the elements within it using

img, h1, p {
     position: absolute;
}

Your div should now have the same height as your image. Next, fine-tune the positioning of your elements based on the container element #home_header. For example, if you want the paragraph 10px above the bottom of the image:

p {bottom: 10px;}

If you'd like the h1 at the top of the image with a 10px margin:

h1 {top: 10px;}

To keep them aligned together, create another container element for h1 and p and adjust its position relative to #home_header

<div class="banner_text">
    <h1>...</h1>
    <p>...</p>
</div>

To center the text

.banner_text {
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

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

I am looking to retrieve the value from a combobox and pass it to my search form

Here is an example of a combobox: <select> <option value="product">Product</option> <option value="company">Supplier</option> </select> I am looking to extract the selected value from the combobox and use it in ...

What is the best way to adjust the size of an icon in material UI?

I am currently stuck on a project involving React and Material UI, trying to figure out how to widen the width of an icon from Material UI without affecting its height. There is an icon wrapped inside a https://i.sstatic.net/6WASt.png tag, originally with ...

What is the process for importing a table from a website into Python using HTML?

I would like to extract data from a table in Python, specifically this one: After clicking on 'share and more' and then 'Embed this table', I obtain the url with the html code. However, I am unsure how to utilize this information to re ...

Adjust SVG size to fit parent container/division

I am trying to make the SVG completely fill the containing TD. I don't care about preserving the proportions of the SVG. Instead, I want the height of the SVG to be the same as its width when the height should be larger than the width. There is paddin ...

Is it possible to remove content from a Content Editable container?

JSFiddle <div contenteditable="true"> <p>Trying out editing capabilities of this paragraph.</p> <figure> <img src="http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/ima ...

Revealing and concealing adjacent elements within a specified class

In an attempt to create a carousel that functions by hiding and showing images when the next and previous buttons are clicked, I have organized my images in a table and assigned the li elements a class of 'li'. There are four images in total, wit ...

What is the reason for the return of undefined with getElementsByClassName() in puppeteer?

Currently, I am utilizing puppeteer to fetch certain elements from a webpage, specifically class items (divs). Although I understand that getElementsByClassName returns a list that needs to be looped through, the function always returns undefined for me, e ...

Creating and implementing a HTML template from scratch, devoid of any frameworks

Is there a way to create a quiz where all questions follow the same format and only one question is displayed at a time, without duplicating code? Perhaps using a template would be the ideal solution in this scenario. I appreciate your help. ...

Stop the external animation on the SVG

One way to stop an SVG animation is by utilizing document.getElementById("mysvg").pauseAnimations(). Nonetheless, this method appears to only be effective for inline SVGs. Is there a technique available to halt an SVG animation that has been imported usin ...

Click once to change the element, but it will remain changed and will not revert back no matter how many times you

After the first click displaying "X" and then changing to "O" in my tic-tac-toe JavaScript game, subsequent clicks only show "O", failing to switch back to "X". I am currently working on creating a tic-tac-toe game using JavaScript. Upon the first click, ...

Is there a way to ensure that spans are inline with a div?

My structure is pretty straightforward: <div></div><span></span><span></span> However, I'm looking to have them all appear on a single line. Currently, they are rendering like this: <div /> <span />&l ...

Discovering the root cause of why a particular CSS style is not being implemented correctly

Currently, I am in the process of developing a secondary theme for one of my websites. It's going to be a dark theme. I'm facing an issue where a specific CSS style is not being applied to a particular element, even though it works perfectly fine ...

How to extract text from an HTML file using Bash programming

I have a script that navigates through my data directory and retrieves text from HTML files using the Lynx tool. However, when I view the text file generated by the script, it displays an error message: 410 GONE This doesn't exist any more. Try html ...

Is it possible to alter the Bootstrap CDN in Django?

Starting out with Django and CSS, I've encountered an issue in my base.html file where I included a bootstrap cdn like this: <meta charset="UTF-8"> <link rel="shortcut icon" href="{% static 'favicon.ico' %}"> <meta http-equiv ...

Design: A stationary text box positioned on the left side alongside a selection of scrollable MDL cards on the right

I'm currently experiencing a challenge with adjusting the text on two specific pages. On one side, there is a box with text, while on the other are two MDL cards displaying dialogues. The trouble arises when I try to set a fixed position for the text ...

Tips for reliably resizing slider images with varying widths

I am trying to scale '.slick-slide img' by height because the images vary in width. Scaling them by width would result in inconsistent proportions. However, when I apply a dimension to the height property for '.slick-slide img', the ima ...

Combine jQuery and CSS to create a scrollable and extended fixed header

I am experiencing an issue where the header is fixed when scrolled down, but when I scroll back up to the top, it detaches and becomes unfixed again. How can I make the header stay fixed for a prolonged period of time? Are there any specific adjustments I ...

Have you tried incorporating Nostramap Javascript into your vue.js projects?

Instructions on Using Nostramap API to Call a Map. Refer to the following example: ...

What is the best search engine for static HTML sites that are free from branding and advertisements?

I am in the process of developing a large HTML website with over 12,000 pages and the count is increasing. I am searching for a solution to incorporate search functionality without any advertisements or branding (restricting Google as an option that I alre ...

The lack of responsiveness in the column flex-direction

Having an issue with the flex-direction: column property in responsive mode. When applying flex-direction: column to the .container, it does not behave responsively like flex-direction: row; instead, it overflows the layout. How can I resolve this? & ...