Tips for maintaining consistent formatting of a div element across various sizes

Looking to create a Bootstrap Jumbotron that features a background image, text (h1 and p), and a call-to-action button that scales appropriately for different viewports without sacrificing formatting. Essentially, the goal is for the div to behave like an image when it comes to scaling.

A project has been started to experiment with this concept at: https://codepen.io/Codewalker/pen/xxKRLBd. Various attempts using transitions and transforms have been made, but the desired display behavior hasn't been achieved yet as the div continues to act responsively.

<div class="container">
  <div class="row">
    <div class="col-sm-12">
      <img src="https://picsum.photos/id/1056/1424/562" alt="test" class="img-fluid">
      <div class="heroContent">
        <h1>Jumbotron H1 goes here</h1>
        <p class="pHero">
          This is the first line in the jumbotron.<br>
          The second line appears underneath the first line.
        </p>
        <button type="button" class="btn btn-primary">Chat With Us</button>
      </div>
    </div>
  </div>
</div>

.container {
  max-width: 1424px;
}
.heroContent {
  position: absolute;
  top: 30%;
  left: 30%;
  transform: translate(-50%, -50%);
}

The main objective remains for the entire div to scale seamlessly while maintaining its original layout and format, akin to how images adjust in size.

Answer №1

Discover a clever CSS trick for creating aspect ratio boxes. By utilizing the calculation of padding-top by the browser, you can achieve a unique effect. When using a percentage-based value, it is actually relevant to the width of the element rather than its height. Learn more about this technique: https://css-tricks.com/aspect-ratio-boxes/

In your layout, simplify the markup with an outer div (.postcard) and an inner div that holds all the content (.heroContent).

The outer div should have position: relative, as the inner div will be positioned absolutely. Apply the aspect ratio trick as follows:

.postcard {
  width: 1424px;
  padding-top: calc(562 / 1424 * 100%);
}

By calculating the aspect ratio through padding-top, you ensure that the container maintains a fixed height/width aspect ratio. Utilize background images on this div, and position the inner content to cover the area:

.heroContent {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

To adjust the positioning of the content, use padding-top to your advantage:

.heroContent {
  padding-top: 8%;
  padding-left: 8%;
}

Additionally, scale the content within the div by setting font-size values relative to the viewport width:

.heroContent h1 {
  font-size: 3vw;
  margin-top: 0;
}

.pHero {
  font-size: 1.8vw;
}

.heroContent .btn {
  font-size: 1.2vw;
}

View the snippet in full screen mode and adjust the viewport width to see the effect in action. I hope this explanation aids you in implementing this technique successfully!

.postcard {
  position: relative;
  width: 1424px;
  max-width: 100%;
  padding-top: calc(562 / 1424 * 100%);
  background-image: url('https://picsum.photos/id/1056/1424/562');
  background-repeat: no-repeat;
  background-position: left top;
  background-size: cover;
}

.heroContent {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  padding-top: 8%;
  padding-left: 8%;
}

.heroContent h1 {
  font-size: 3vw;
  margin-top: 0;
}

.pHero {
  font-size: 1.8vw;
}

.heroContent .btn {
  font-size: 1.2vw;
}
<div class="postcard">
  <div class="heroContent">
    <h1>Jumbotron H1 goes here</h1>
    <p class="pHero">
      This is the first line in the jumbotron.<br> The second line appears underneath the first line.
    </p>
    <button type="button" class="btn btn-primary">Chat With Us</button>
  </div>
</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

Utilizing useState for React Data Picker

I recently attempted to implement the React Data Picker in my React project using npmjs. However, I encountered an issue when trying to import useState from import React, { useState } from "react"; The error message displayed: 'useState&a ...

Resetting the state of toggle/click states in AJAX and jQuery

Currently, I am encountering a small dilemma with a .on function and AJAX in conjunction with a mobile menu. The mobile menu is located in the header of a site that relies heavily on AJAX for its content loading. This poses an issue because when an AJAX ca ...

Design a dynamic card with an eye-catching Font Awesome icon that adapts well to

As I delve into the realm of CSS, my current mission is to replicate the captivating cards showcased on this particular website. Utilizing Font Awesome icons has become a pivotal aspect of this endeavor. For instance, let's take a look at this card:ht ...

Combining PHP and HTML: Utilizing nested foreach loops with interspersed HTML code

Attempting to create a layout of items using an Accordion arrangement (from Bootstrap) has led me to retrieve data from a pgsql db. Fortunately, the data retrieval process is successful. The challenge lies in displaying this data, as I am encountering an ...

Automating testing for numbered lists with CSS list-style-type decimal

I need help with a JavaScript and CSS project. I have a situation where I have a numbered list like this: Coffee Tea Cola Here is the code structure I am using: <!DOCTYPE html> <html> <head> <style> ul.a {list-style-type ...

What is the best way to set the width of a nextjs Image using CSS styles

Do you think it's more beneficial to use 'next/image' rather than the traditional img tag? If so, why? I am trying to display an image by specifying width and height in styles using a class called 'img', like this... <img class ...

Saving Pictures in Database Without Using jQuery

Hey there fellow developers, I'm currently immersed in a web project that involves reconstructing a social media platform similar to snapchat. To capture images, I am utilizing my webcam with JavaScript and saving the image data to a variable named i ...

Enhance your search experience with AJAX-powered search boxes

I need to implement multiple textboxes on my webpage, each with its own search query to retrieve data from the database. While I have successfully implemented this for one textbox, I am facing difficulties getting it to work for two or more textboxes. He ...

Troubleshooting an Issue with MediaStreamRecorder in TypeScript: Dealing with

I've been working on an audio recorder that utilizes the user's PC microphone, and everything seems to be functioning correctly. However, I've encountered an error when attempting to record the audio: audioHandler.ts:45 Uncaught TypeError ...

Using CSS in Rails based on a certain condition

My project involves creating an app with a specific view. Within this view, I must check a condition and if it is met, I need to color a table row accordingly. While the simplest solution would be to modify the header within the view, I prefer to keep al ...

What is the best way to design a shape (a quadrilateral with rounded edges) using CSS?

Struggling with creating a widget in React Js that has an image as the background? The image is designed to be dynamic, changing color and size based on the device. I've attempted using inline SVG and SVG within an img tag but can't seem to contr ...

Developing an Easy-to-Use PDF Popup Interface

I need a simple PDF modal where I can input the text: 'The sky is blue' and have it link to a PDF that opens up when clicked. I found a similar sample online, but it includes an image plus an image modal that pops up. I want to replace the imag ...

AngularJS inputs using ng-model should be blank when in the pristine state

Check out this input element within an HTML form: <input ng-model="amount" /> Currently, the input displays as $scope.amount = 0 in the controller. I want to start with a blank input field instead. This way, users can easily input data without havi ...

How do I directly display a variable in index.html using node.js?

Is there a way to retrieve user inputs from the index.html file, process them in Node.js, and then display the result back on the index.html page instead of redirecting to a new page? Form Handling with Express var express = require('express'); ...

Display an error message in the input type file Form Control if the file format is not .doc or .docx

I need a way to display an alert if the user tries to submit a file that is not of type doc or docx. I've implemented a validator for this purpose and would like the alert message (Unacceptable file type) to be shown when the validation fails. Here i ...

HTML5 will consistently display the video control panel

I am looking to keep the control panel of the html5 <video> element visible at all times while hiding the play/pause button. I attempted the following: <style> video::-webkit-media-controls-panel { display: flex !important; ...

Enhance your website's user experience with jQuery by implementing smooth scrolling alongside

I have a fixed header at the top of my page. When it scrolls, I want to ensure that it does not cover or hide any of my content, especially the top portion of the section. // This code enables smooth scrolling (source: css-tricks) $('a[href*="#"]:no ...

How to Style an Element Based on Fragment Identifier in Href Attribute Using CSS

I've implemented the tabview feature from the YUI3 library. This is how the markup code appears: <div id="demo" class="yui3-tabview-content"> <ul class="yui3-tabview-list"> <li class="yui3-tab yui3-widget yui3-tab-selected"> ...

Krajee Bootstrap File Input, receiving AJAX success notification

I am currently utilizing the Krajee Bootstrap File Input plugin to facilitate an upload through an AJAX call. For more information on the AJAX section of the Krajee plugin, please visit: Krajee plugin AJAX The JavaScript and PHP (CodeIgniter) code snippe ...

What steps do I need to take to make this JavaScript code function properly? How can I create a carbon copy email setup in

Recently, I've been struggling to implement this particular code on my website as I am not very proficient in JavaScript. Despite searching for various online resources, none of them seem to address my issue. This code is meant to be a part of a form ...