div banner with responsive content within

Creating a Responsive Div with Image and Content https://i.sstatic.net/vN3Ni.png

I want to design a div that contains a background image with text on it, just like the example shown. The main requirement is for this div to be responsive, so the image stays at 100% width within the screen frame no matter the screen size.

The issue I'm facing is that when zooming in or out (using ctrl + scroll), the image tends to lose its height instead of maintaining the same height. How can I prevent this loss of height when zooming?

I have tried various solutions but haven't found one that works perfectly. Here's what I have so far:

enter code here

       <div id="container">
            <div class="content-inner">
                <h1>HELLO!!</h1>
                <hr>
                <p>I HAVE A QUESTION AND WAS WONDERING IF YOU CAN HELP?</p>
            </div>
        </div>

<style>
html, body{
margin:0;
}
#container {
    position:relative;
    border:1px solid red;
    position: relative;
    width: 100%;
    min-height: auto;
    text-align: center;
    color: #fff;

    background: radial-gradient(circle, rgba(17, 5, 19, 0.94), rgba(20, 7, 35, 0.78), rgb(0, 0, 0)), url(backgrounddark.png) no-repeat;
    background-position: center;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    background-size: cover;
    -o-background-size: cover;
            }

#container .content-inner {
    position: relative;
    width: 50%;
    padding: 100px 15px;
    text-align: center;
    margin:auto;
}

#container .content-inner .content-inner h1 {
    margin-top: 0;
    margin-bottom: 0;
    text-transform: uppercase;
    font-weight: 700;
}

#container .content-inner .content-inner hr {
    margin: 30px auto;
}

#container .content-inner .content-inner p {
    margin-bottom: 50px;
    font-size: 16px;
    font-weight: 300;
    color: rgba(255,255,255,.7);
}


</style>

Thank you

Answer №1

Within your code, the height of the container has not been set and the min-height is set to auto. This means that it will adjust its size according to the content within the div, with the background image not being considered part of this calculation.

As you zoom out, the font size and padding decrease in relation to the screen size, causing the div to shrink as a result (content and div height both reduce).

If you want to prevent the image from scaling down when zooming out (while allowing the text to do so), consider setting a specific height for the div. You can use viewport height units (vh) to set a percentage relative to the viewport height. Alternatively, ensure that all elements above #container have a height or min-height of 100%, allowing you to specify a percentage value for the height/min-height of #container.

Keep in mind that if you opt for a dynamic height, vertical centering of your text will require additional CSS adjustments. Refer to this guide on how to achieve vertical alignment: Vertically align text in a 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

Is there a way to shift a background image pattern?

After searching extensively, I came up empty-handed and am seeking guidance on how to achieve a specific effect. Specifically, I am in need of a JavaScript or jQuery script that can smoothly shift a background image to the right within a designated div con ...

Display a JavaScript variable as an attribute within an HTML tag

let disableFlag = ''; if(value.action.length === 0) { disableFlag = 'disabled="disabled"'; } row += '<td><input type="checkbox" name="create" value="1" class="checkbox" data-action="'+ value.action + '" data-co ...

When selecting an option in the burger menu, the dropdown does not react properly

I am facing an issue with the burger menu where one of the options should trigger a dropdown, but the content is not adjusting properly. The dropdown menu should push the other content downward, but currently, it overlaps. I have included the code for th ...

Exploring the hover effects on Kendo charts and series

I have encountered an issue with my kendo chart. The series hover works fine when the mouse hovers over the serie, but I am facing a problem where the value in the line does not appear as expected. I am unsure of why this is happening. $("#chart1").data(" ...

Identifying geometric coordinates within Canvas

Currently, I am adding drag and drop functionality to my HTML5 Canvas application. I have encountered a challenge in determining how to detect if the shape being clicked on is not a rectangle or square. For instance, within my 'mousedown' event h ...

Customizing the Mui Theme in a Unique Way

After generating an MUI component from Mui DatePicker, I encountered the following code: <span class="my-theme-MuiTypography-root my-theme-MuiTypography-caption my-theme-MuiDayCalendar-weekdayLabel css-1mln09i-MuiTypography-root-MuiDayCalendar-week ...

Ensure that the control button is pressed during the JavaScript onclick event function

I am looking to create a JavaScript function that checks if the CTRL button is pressed. Here is my PHP code: <tr class="clickable" onclick="gotolink('<?= base_url() . invoices/createinvoice/' . $customer->Id; ?>')"> And here ...

What is the best way to target a specific class to exclude in CSS styling?

Here is the css code snippet: .single-post .container { width: 60%; } not(.single-post) .container{ min-width:92%; } The goal I am aiming for is to have the container width set to 60% on single post pages and 92% on all other pages. T ...

Make sure to have the list available while choosing an item

I want to design a unique CSS element with the following characteristics: Include a button. When the button is clicked, a list of items (like a dropdown list) should appear. We should be able to select items by clicking on them, and the parent component s ...

Creating a connection between a secondary jQuery anything slider and a distinct CSS style on a webpage

Currently, I am attempting to incorporate two anything sliders within the same webpage. My goal is to apply unique styling to each slider. Despite my efforts, I am encountering difficulties in calling upon a second style sheet without it completely overr ...

Tally up the values of selected checkboxes

  I'm in search of a method to tally up data-values associated with checkboxes. In the example provided below, selecting locations from the checkboxes results in the calculation of primary values, which are displayed in the green box. I also need to ...

After clearing the option, the onChange function stops functioning

I'm facing an issue with the following code: success: function (data) { $('#' + idDivRefresh).endLoading(); if (data.message != '@Geral.Sucesso') { $('#' + idDropDown + ...

Using Angular: How to reference the same ng-template with a component in a single HTML file without making a copy

Currently, I am facing a challenge while developing a web application using Angular. My task involves creating a search form with multiple fields as a component. Below is the code snippet for this component: <ng-template #filter> <app-articles-f ...

Utilizing checkboxes to dictate PHP and MySQL queries

Seeking assistance with MySQL, jquery and PHP. Here is the code provided: HTML <label class="checkbox"><input type="checkbox" name="code_site1" class="code_site1" checked="checked">Code 1</label> <label class="checkbox"><input ...

What is the best way to ensure the width of the div is adjusted to fit the table it contains?

What is the best way to adjust the width of a div containing a potentially wider-than-screen table? Despite setting padding: 10px for the div, the right padding disappears when the table exceeds the screen width. Below is an example code snippet: <div ...

Activate the radio button upon page load in angularjs

<div class="form-group" style="margin-top: 20px;" ng-repeat="row in skuDetails"> <label class="checkbox-inline"> <input type="radio" name="amount_{{$index}}" value="amount" ng-model="row.amount" ng-checked="row.reductionAmount != &apos ...

What causes the discrepancy in checkbox size between Chrome and Edge browsers?

After creating a table with striped rows using bootstrap 4, I noticed that one of the columns contained HTML checkboxes. Check out the jsfiddle for more details. Upon viewing the table in Google Chrome, I noticed that the default checkboxes appear smalle ...

Having trouble showing images in Angular 9?

Currently, I am working on Angular and facing an issue with displaying the avatar of an article that was posted during registration. The image is not appearing correctly. In the HTML code: <ng-container *ngIf="blogpost$ | async as bp; else loading"> ...

How to properly size a child div inside a parent container

I'm having trouble with sizing a child div inside a parent div. The problem is that the child div's size changes according to the number of elements it contains, but I want all the child divs to be the same size regardless. This issue arises with ...

Adjusting the font size results in a shift in the margin and padding

I am currently designing the homepage for my website. I want all the text to be aligned on the same line, but whenever I increase the font size of one element, it creates space around it, shifting it slightly to the right and pushing the text below down. ...