Managing the width of siblings in Bootstrap 4: A Guide

With two <p> elements nested under an <h1> element, my goal is to ensure that the width of the displayed paragraphs never exceeds 90% of the content width within the header. The content of both <p> and <h1> elements is dynamically generated on the server side via Django and may wrap on smaller screens.

Experimenting with d-flex, flex-column, and flex-shrink has yielded some success, although I have struggled to maintain responsive resizing of the <p> elements while also centering all three elements horizontally within the parent container div.

<div class="container">
  <div class="text-center">
    <h1 class="display-4">A Brief Yet Bold Title!</h1>
    <p class="lead">This sentence could potentially be longer than the title above. Nevertheless, its width should not exceed 90% of the h1 tag's width.</p>
    <p><i>While this sentence might be shorter, I still want to limit its width accordingly.</i></p>
  </div>
</div>

Answer №1

Approach-1 (blue): In order to ensure that 90% of the paragraphs fit under the heading, we initially examined the width of the heading which turned out to be 100% of the viewport's width. This implied that to achieve a 90% width, a margin of 5% (relative to the viewport's width) needed to be applied on both sides.

Approach-2 (green): Following this, we analyzed the rendered width of the <h1>, which was measured at 413px. Subsequently, we adjusted our paragraph width to match this measurement and set margins to auto for achieving centered text alignment.

Both strategies have been illustrated in the code snippet below:

.divStrategy1 p {
  margin: 0 5vw;
  color: blue;
}

.divStrategy2 p {
  width: 413px;
  margin: auto;
  color: green;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">

<div class="text-center divStrategy1">
  <h1 class="display-4">A Short Big Title!</h1>

  <p class="lead">This is a pretty long sentence, and it could be longer than the title above, however, I would not like its width to exceed 90% of that h1 tag above.</p>

  <p><i>This is a shorter sentence, however, I still have the same hopes for its width.</i></p>
</div>

<div class="text-center divStrategy2">
  <h1 class="display-4">A Short Big Title!</h1>

  <p class="lead">This is a pretty long sentence, and it could be longer than the title above, however, I would not like its width to exceed 90% of that h1 tag above.</p>

  <p><i>This is a shorter sentence, however, I still have the same hopes for its width.</i></p>
</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 it possible to adjust the background size using jQuery?

When attempting to set backgroundSize with jQuery using the code provided, I consistently encounter an error on the fourth line: "unexpected identifier." bigwidth = 200; bigheight = 100; $(e.target).css({backgroundImage: 'url(' + ...

Refresh the webpage content by making multiple Ajax requests that rely on the responses from the previous requests

I am facing a challenge where I need to dynamically update the content of a webpage with data fetched from external PHP scripts in a specific sequence. The webpage contains multiple divs where I intend to display data retrieved through a PHP script called ...

Leverage a single JavaScript file across all Vue components without the need for individual imports

My project includes a JavaScript file called Constant.js that stores all API names. //Constant.js export default { api1: 'api1', api2: 'api2', ... ... ... } Is there a way to utilize this file without having to impo ...

Is it possible to adjust the height of the dropdown menu in a mat-select component in Angular 7?

How can I adjust the height of a mat-select in Angular7 to display all items properly? Here is my component file: import { Component, ViewEncapsulation } from "@angular/core"; import { FormControl } from "@angular/forms"; /** @title Select with multiple ...

Error encountered when trying to update Express Mongoose due to duplicate key

In my MongoDB database, I have a unique field called mail. When attempting to update a user, I encounter an issue where if I do not change the mail field, it triggers a duplicate key error. I need a solution where it is not mandatory to always modify the ...

navigation menu not displaying properly on small screens

Struggling to make my navigation menu fit properly on mobile devices like iPhones and iPads? Check it out. Here are the CSS properties I've set: #wrapper{ max-width: 1600px; min-width: 320px; width: 100%; margin: 0 auto; position: ...

When retrieving data from a PHP $_SESSION using an Ajax $_POST call, outdated information is being returned

I am encountering an issue that I cannot seem to figure out. Currently, my application consists of three pages: Home.php Nearmiss.php Reports.php Each of these pages includes code at the top with a specific "thispage" variable unique to that page. < ...

JQuery continuously firing off AJAX requests

Currently, I am experimenting with using a Jquery Ajax request to incorporate an AutoComplete feature. This involves utilizing ElasticSearch on the backend for data retrieval. This is what my autocomplete.html looks like: <!DOCTYPE html> <html l ...

Execute Two Functions When OnClientClick is Triggered

This is a snippet of my current code in progress <asp:LinkButton ID="lnkDel1" Text="Delete" runat="server" OnClientClick="return confirm('Delete this alert?','Confirm');" onClick="lnkDelete1_Click" CssClass="del_lnk" CommandArgumen ...

Angular JS was unable to show the complete sum of the row

I'm attempting to calculate the total of the Money In and Money Out columns. I have defined two functions to accomplish this, but instead of displaying the total at the bottom of the web page, it shows NaN. I'm confused about where I went wrong. ...

AngularJS - Multi-controller Data Calculation

I am currently in the process of developing an Angularjs application. The project is quite extensive and requires segmentation into multiple Controllers to manage effectively. One challenge I am facing is performing calculations across these controllers. ...

Can you guide me on implementing CSS Houdini in Next.js?

Exploring the world of CSS Houdini in my Next.js project has been quite an adventure. After successfully installing the necessary CSS Houdini package and css-paint-polyfill using yarn, I decided to follow the webpack guidelines provided at . Below is a sn ...

Transmit an attached image through Express

Currently, I have an Express application that utilizes Multer to upload images. However, once the image is uploaded, the file extension is not saved as a security measure. My goal is to serve these images with the correct extensions based on their mime typ ...

Passing values from child components to parent components in React Native for display

Within my application, I have a child component and a parent component. The purpose of the child component is to handle star ratings. I am now looking to pass the rating value from the child component to the parent component and utilize this data within th ...

Why doesn't DataView or Int32Array interpret underlying bytes as individual integers instead of groups?

I am interested in extracting integers from a byte buffer and accessing them by their specific index. For example, retrieving i[0] would yield the integer value represented by the initial four bytes of the buffer, while i[1] would provide the integer value ...

Transitioning NodeJS from local development to a live website

After successfully creating a site using NodeJS with one-page HTML/jQuery, everything is functioning properly on localhost. However, I am facing issues when trying to put the site online at www.xxxx.com. I already have a registered .com domain, but I am un ...

Currently, my goal is to place a div underneath two other divs. Here is the code snippet that I am working

I am attempting to position the div with id 4 so that it appears after the divs with ids 2 and 3, which are placed next to each other. However, when I try to do this, I notice that the div with id 4 seems to start from the top itself. Even though the con ...

Preventing Vue.js SPA from accessing cached version when JWT expires: the solution

I'm encountering an issue with my Vue.js / Express application that I can't seem to resolve. Here's how the process unfolds: An unauthenticated user logs into the app and is presented with the login page. Once successfully authenticated, t ...

Styling with CSS in Django templates

Having trouble with CSS in my Django template, My settings.py looks like this: BASE_DIR = os.path.abspath(os.path.dirname(__file__) + '/') STATIC_URL = BASE_DIR + '/static/' I have a folder called "static/css/home_css.css" in my path ...

Issues with the functionality of jQuery event functions

My webpage retrieves content from a database using jQuery and AJAX. There are various processes on the page such as adding new content, editing, and deletion, all of which use AJAX. However, I am experiencing issues with event functions like click and mous ...