Troubleshooting issues with hero banner image display in CSS and HTML

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Mountains</title>
        <link rel="stylesheet" href="/css/style.css">
        <style>
            /*css stylization*/
            *{
                margin: 0px;
                padding: 0px;
                box-sizing: border-box;
            }
            header{
                justify-content: space-between;
                align-items: center;
                display: flex;
                padding: 10px 50px;
            }
            .logoname{
                margin-bottom: 10px;
            }
            .logo img{
                height: 50px;
                width: 50px;
                cursor: pointer;
            }
            .navlinks{
                list-style-type: none;
            }
            .navlinks li{
                display: inline-block;
                padding: 20px;
            }
            .navlinks li a{
                transition: all 0.3s ease 0s;
                text-decoration: none;
                font-family: roboto;
            }
            .navlinks li a:hover{
                color: aquamarine;
            }
            /*hero image code*/
            .herobanner {
                background-image:url(/image/herobanner.jpg);
                width: 100%;
            }
            .herotext {
                text-align: center;
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
                color: #000;
            }
            </style>
        </head>
        <body>
            <header>
                <figure class="logo">
                    <img src="\image/logo.png" alt="logo">
                </figure>
                <nav>
                    <ul class="navlinks">
                        <li><a href="#">HOME</a></li>
                        <li><a href="#">ABOUT US</a></li>
                        <li><a href="#">GALERY</a></li>
                    </ul>
                </nav>
            </header>
            <div class="herobanner">
                <span class="herotext">
                    <h1>LIVE HIGH</h1>
                </span>
            </div>
        </body>
</html>

Having created a navigation bar and attempted to add a hero banner underneath it, I encountered an issue where the image URL did not work properly, resulting in the image not being displayed. However, the text within the herotext class was successfully centered as intended.

If anyone has suggestions on how to resolve this issue with the code, your assistance would be greatly appreciated. Thank you for taking the time to help me troubleshoot this problem.

Answer №1

The hero div is currently empty, resulting in a height of ZERO (the span is positioned absolutely, so its height is not factored in). To fix this, simply add a 'min-height' property:

        /*css styling*/
        
        * {
            margin: 0px;
            padding: 0px;
            box-sizing: border-box;
        }
        
        header {
            justify-content: space-between;
            align-items: center;
            display: flex;
            padding: 10px 50px;
        }
        
        .logoname {
            margin-bottom: 10px;
        }
        
        .logo img {
            height: 50px;
            width: 50px;
            cursor: pointer;
        }
        
        .navlinks {
            list-style-type: none;
        }
        
        .navlinks li {
            display: inline-block;
            padding: 20px;
        }
        
        .navlinks li a {
            transition: all 0.3s ease 0s;
            text-decoration: none;
            font-family: roboto;
        }
        
        .navlinks li a:hover {
            color: aquamarine;
        }
        /*hero image code*/
        
        .herobanner {
            background-image: url(https://www.artonicweb.com/learn/wp-content/uploads/2018/05/12-HERO-IMAGES-1024x536.jpg);
            width: 100%;
            min-height: 500px; /* Add this line */
        }
        
        .herotext {
            text-align: center;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            color: #000;
        }
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8>"<br/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mountains</title>
    <link rel="stylesheet" href="/css/style.css">

</head>

<body>
    <header>
        <figure class="logo">
            <img src="\image/logo.png" alt="logo">
        </figure>
        <nav>
            <ul class="navlinks">
                <li><a href="#">HOME</a></li>
                <li><a href="#">ABOUT US</a></li>
                <li><a href="#">GALLERY</a></li>
            </ul>
        </nav>
    </header>
    <div class="herobanner">
        <span class="herotext">
                    <h1>LIVE HIGH</h1>
                </span>
    </div>
</body>

</html>

Answer №2

**here is an example of usage **

.coolbanner {
    background-image: url(https://cdn.pixabay.com/photo/2020/06/17/18/03/lights-5310589_960_720.jpg);
    width: 100%;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
 }
 .cooltext {
   text-align: center;
   position: relative;
   color: #000;
 }

Answer №3

After reviewing your code, it appears that the .herobanner element does not have a set height due to the absolute positioning of the span element inside it. There are a couple of solutions to address this issue - you can either remove the absolute position so the height is based on the content, or define a fixed height for the .herobanner element.

Option 1:

.herotext {
    text-align: center;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: #000;
}

Option 2:

.herobanner {
    background-image:url(/image/herobanner.jpg);
    background-size:cover;
    background-repeat:no-repeat;
    width: 100%;
    height:20vh;
}
and adjust the positioning of the span element accordingly

I personally recommend the first solution as it allows the element to take the height of its content.

View example on CodePen here

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 applying CSS3 box-shadow on two sides of a div?

Could you please review this code snippet: http://jsfiddle.net/kerp3/ The inner box currently has a shadow on all four sides. I am looking to modify it so that the shadow appears only on the left and bottom sides. Any suggestions on how to make this adj ...

What is the process for using jQuery to systematically alter the src attribute of all avatar images on Twitter.com?

Summary: I am learning JavaScript and jQuery, and wanted to write a script to replace "_normal.png" with ".png" for all "img.avatar" images on Twitter.com. I encountered an error which I will explain below. Background: Practice makes perfect, so I like to ...

The Bootstrap 5 class "col-md-4" seems to be malfunctioning

My goal is to create a grid layout with 3 columns and 2 rows using Bootstrap cards, but I'm encountering an issue where all the cards are centered and stacked on top of each other. Can someone please assist me in resolving this problem? Service.js co ...

Is it possible to remove the "getContext" property from the HTML5 Canvas element using a script?

Visit the HTML5 conformance test suite to find a test for the prototype of HTMLCanvasElement. While this test fails for Safari and Firefox, it passes for Opera on Windows 7. The script within the test attempts to delete the getContext property of HTMLCan ...

Discover hyperlinks automatically within the textbox area

Is there a way to style hyperlinks in a textarea? I've added a <textarea> tag like this: <textarea class="textarea" name="input_note" id="" cols="30" rows=&q ...

Switch between different table rows

I have here a table that is used for displaying menu and submenu items. It's a mix of PHP (to fetch the menu items and their respective submenus) and HTML. What I am trying to figure out is how to toggle the visibility of only the submenu items under ...

The link tag is failing to load the external CSS file

Starting a fresh project using Angular and encountering an issue. When attempting to load an external CSS file with <link>, it fails to work. However, using an internal style with @import does work! Not working with : <link rel="stylesheet" type= ...

What causes the slower performance in removing elements compared to adding elements in data structures?

Having a table with an extensive amount of rows (approximately 10,000) may seem impractical, but I am interested in understanding the behavior outlined below. Whenever new data is received, my goal is to clear the existing rows first. Interestingly, the pr ...

Attempting to arrange all the images in a horizontal display rather than a vertical one

This is my first ever question on the forum sorry if it is unprofessional hopefully i can learn from my mistakes on this post!! I put the question in the title but if you have any questions feel free to ask below THANK YOU! for the help! My Code ...

Assistance needed with troubleshooting an HTML and CSS table error

When checking the error console in Opera, I noticed an error related to this line of code: filter:alpha(opacity=99); opacity=0.99; MozOpacity=0.99; I am currently using it in this manner. Any idea what might be causing the issue? ...

How can I utilize jQuery to iterate through every anchor tag on an HTML page?

I am looking to reference all anchor tags on the page that have a parent h2 tag. To achieve this, I need to iterate through each anchor tag that has a parent h2 and add an attribute using jQuery. <body> <h1>not me</h1> <a href ...

Unable to conceal element if it is devoid of content

<div id="web-link" class="infoline"> <i class="fa fa-link fa-2x fa-fw coloriconfa"></i> <a href="<?=$data['post']['url']?>" target="_blank"><?=$data[ ...

Is there a way to create a footer similar to this one?

I need to create a footer, but I'm struggling with placing a circle at the top half of the footer like this. Even though I can create a footer, I still encounter some issues. This is the code I have right now: .Footer { position: fixed; left: ...

Using Google Maps within a Bootstrap modal window that is only partially loaded

The maps display perfectly fine in a regular div, but when placed inside a Bootstrap modal, only half or part of the map is visible. However, when I open the console, the issue resolves itself and the entire map is shown. Photo without console opened Pho ...

Combining Bootstrap, flexbox, and a mobile-friendly hamburger menu for optimal

Just a quick note to clarify that I am primarily a backend developer, not proficient in frontend. The layout I currently have looks like this: I am unsure whether I should stick with the current setup or switch to using a grid. My main goal is to have a ...

Setting Image Widths for Various Screen Sizes in HTML for Android Tablets and Phones

My android app functions on both tablets and phones, loading content from HTML files in the assets folder. Currently, I am facing an issue where I need different image sizes for tablet and phone devices. Below is the code snippet for displaying the image ...

What is the best way to populate the "value" field in a form with checkbox input type from a database?

I've been struggling to retrieve values from the database and insert them into a checkbox array, but so far I've had no luck. Can anyone provide some assistance? *The 'img[]' section is always empty; it seems like no value is being pas ...

Do jQuery and AJAX both support application/json content type in all web browsers?

I have been managing my ajax requests like this: @header("Content-Type: text/html; charset=".get_option('blog_charset')); and in the JavaScript: $.ajax(.... ... success: function(response){ var obj = eval('('+response+') ...

Can the looping feature be applied to CSS?

I've been reusing the same CSS code multiple times, just changing the names each time. It feels repetitive to call the same CSS repeatedly with different names. I'm considering looping through it instead. Any suggestions? #block1{display:block; ...

Create a shiny application that makes text bold in the user interface

Can someone please guide me on how to make specific words bold in a sentence within the UI using HTML tags under the h() function? Your assistance is greatly appreciated. Thank you. library(shiny) ui <- navbarPage( title = 'Benvenuto&apos ...