Exploring the correlation in size between the body background image and the absolute and relative containers using CSS3

After some careful consideration, I finally settled on a suitable title for this dilemma. The issue at hand involves a background image attached to the body element using CSS:

body,html {
  height: 100%;
}
body {
background: url("../images/header_lg.jpg") no-repeat;
-moz-background-size: 100% auto;
-webkit-background-size: 100% auto;
-o-background-size: 100% auto;
background-size: 100% auto;
}
.fullwidth-head {
  position: relative;
  width: 100%;
  height: 100%;
}
.fullwidth-head-inner {
  position: absolute;
  top: 17%;
  left: 11%;
  width: 78%;
  height: 30%;
  background: #c5c5c5; /* just for visibility */
}

In this setup, two divs are utilized. ".fullwidth-head" acts as the container and is positioned relatively at the top of the page by default, while inside it lies another div, ".full-width-head-inner," which is absolutely positioned. While this arrangement works well on desktop screens, transitioning to portrait view causes issues due to the lack of correlation with the resized background image. One workaround considered involved adding an empty .png file of the same size as the body background image to maintain the aspect ratio, but concerns about additional load prevented its implementation.

Below is the minimal HTML code relevant to the discussion:

<body>

<div class="fullwidth-head">
    <div class="fullwidth-head-inner">
    </div><!-- /.fullwidth-head-inner -->
</div><!-- /.fullwidth-head -->



<!-- js scripts in footer -->

Seeking a solution that does not involve using a blank.png file to adjust the size of the relative container div, any suggestions on how to achieve the desired aspect ratio alignment via CSS?

Thank you.

Answer №1

One easy solution you might be looking for is called the padding-bottom hack.

If you already know the proportions or aspect ratio of an image, this hack allows you to resize an element in the same way a real image would.

Here are some additional resources on this topic:


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

Assigning a modal value to an HTML field

I am in need of a solution for multiple HTML fields with a common modal attached to each one. The code I have currently looks like this: <!-- First Element --> <div class="box box-element ui-draggable"> <span class="config ...

Stacked divs needed to be aligned vertically

Is there a way to make the head and main tabs fixed vertically one below the other? Keep in mind that these need to be separate angular components and cannot be combined under another div. The rows also need to be scrollable. .pages { float: left; ...

Exploring nodes with the help of Cheerio JS

In my HTML code, I am using the Snekfetch library to fetch data. Here is a snippet of the code: <div class="entry-content"> <h4>today's date etc etc</h4> <h3>category name 1</h3> <p> <img class=" ...

``Is there a way to refresh the CSS, button, or a specific part of a webpage after a GET request has been

Upon receiving a GET request, the values appear and the button disappears. View image here. The button functions properly on localhost. See it in action here. I'm attempting to incorporate a Facebook share button on a page with dynamic content like ...

Utilizing a Bootstrap Frontend to Connect with a Rails JSON-API

Forgive me if this sounds naive, but I'm unsure how to access the 'endpoints' from my current Rails JSON-API Server in my Bootstrap page. Do I need to place the Bootstrap page within the view folder of the API server? If so, how do I invoke ...

Incorporating HTML snippets into files using PHP

When attempting to insert an HTML line into the "userlist.php" file using PHP code, I encountered an issue: <?php $username = "pajlok"; $type = ".jpg"; $html = <<<EOD <div onclick="window.location.href = 'user/pajlok/'" styl ...

Is it possible to alter the color of the cursor without affecting the color of the text

My preference is to have the cursor color set to #FFF while keeping my font color at #000. Can this be achieved? ...

Manage the sequence in which the various paths of an SVG element are displayed

Searching for a way to display an image in HTML with a rounded dashed border? Take a look at the example below: https://i.sstatic.net/YrCZS.png The number of dashes and their colors can be controlled, similar to what you see in WhatsApp's status tab ...

Executing JavaScript code from an external link

Currently, I am in the process of developing a horizontal parallax website. The functionality is working seamlessly; when I click on the menu, the slides smoothly transition horizontally and display the corresponding content. However, I have encountered a ...

The value calculated by Auto does not display as a valid number in Angular 8 until it has been manually edited

Having an issue with a form submission for invoicing. The total value field, which is auto-calculated based on quantity and unit price, does not show up as numeric data in the backend onSubmit event unless I manually interact with it by adding something ...

Advanced Image Upload using HTML5 and Ajax in the 'background'

As someone who is just learning HTML5 and Ajax, I'm curious to know if it's feasible to use these technologies to create a service that enables users to upload an image in the background while they navigate through different pages on the same sit ...

Enter a value into the box by clicking a button that retrieves information from a text file

Below is a box where information needs to be filled: https://i.sstatic.net/wkFEv.png The code used to create the box above is as follows: <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> < ...

I am experiencing an issue with setting the border to none using .navbar-toggler

I'm currently utilizing bootstrap 5, and I've encountered an issue with setting the border and outline to none in my CSS file. Specifically, when I apply these properties to the nav-toggler class, it seems to have no effect. .menu-bar .navbar- ...

Best practices for creating a versatile modal component

I have come across a few examples, but they all appear to be contradictory or intended for older versions of ng-bootstrap. What is the correct method for creating a basic modal that can be stored in a shared directory and then used for multiple modals on ...

After 30 seconds of inactivity, apply the CSS once, then remove it before repeating the process

I've got a unique little CodePen with an animation that I want to execute only if the user hasn't clicked on the <div class="someDiv"> for 30 seconds. My question is, can someone guide me in the right direction so that when someone doesn&a ...

Utilizing Wordpress for Effective Internal Linking

Looking to set up a Table of Content on a custom Wordpress template, I decided to hardcode the headings for internal sections in advance. However, I'm facing an issue where it doesn't scroll to the intended section in Wordpress. Take a look at th ...

Cross-Platform: Varied functionalities in disabled input fields (avoiding any direct replication)

My input HTML field is disabled, but I've noticed that in some browsers (such as Chrome, Edge, Internet Explorer, and Opera), it's still possible to select and copy the text. However, in Firefox, this functionality does not work. To test this yo ...

Using a personalized font in CSS isn't functioning correctly and is displaying Times New Roman instead

I've been struggling to include a custom font in my project, but it's not working as expected. Here's how I added it to the .css file: @font-face { font-family: 'Open Sans'; src: url('/open-sans.eot'); src: u ...

Issues with loading CSS, JavaScript, and links when deploying a Spring Boot application as a WAR file in Tomcat

My Spring boot web application runs smoothly as an executable jar, but when I build it as a war and deploy it to Tomcat, the CSS and JS files fail to load. Upon further investigation, I discovered that all links are pointing to the root context path "/" I ...

What methods can be used to troubleshoot an issue of an AngularJS function not being called

I am facing an issue with a dynamic table I have created. The rows and action buttons are generated dynamically when the user clicks on the Devices accordion. However, there seems to be a problem with the function expandCollapseCurrent(). Whenever the use ...