"Setting a maximum height for a div element, along with nested divs and paragraphs

.wrap
                {
                  
                  width:400px;
                  height:200px;
                  border:1px solid #000;
                  }
                
                .content
                {
                  width:300px;
                  max-height:200px;
                  background-color:blue;
                  margin-left:50px;
                  }
                .content p
                {
                  margin:0;
                  }
                .header
                {
                  margin-left:20px;
                  font-weight:bold;
                    height:20px;
                   }
                .header p
                {
                  margin:0;
                  
                  }
<div class="wrap">
              <div class="header">
                <p>What is Lorem Ipsum?</p>
              </div>
              <div class="content">
               
                <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
                
                
              </div>
              
              
             </div>

Hello, I have a CSS-related question. I would like the paragraph to always be contained within the blue div, and the blue div should not exceed the dimensions of the "wrap" div. When I add more text to the header, the height of the blue div should decrease accordingly.

Answer №1

set max-height:180px; and include overflow: hidden for .content

.wrap
{
  
  width:400px;
  height:200px;
  border:1px solid #000;
  }

.content
{
  width:300px;
  max-height:180px;
  background-color:blue;
  margin-left:50px;
  overflow: hidden;
  }
.content p
{
  margin:0;
  }
.header
{
  margin: 0 0 0 20px;
  font-weight:bold;
    height:20px;
   }
.header p
{
  margin:0;
  
  }
<div class="wrap">
  <div class="header">
    <p>What exactly is Lorem Ipsum?</p>
  </div>
  <div class="content">
   
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    
    
  </div>
  
  
 </div>

Answer №2

How about something similar to this?

HTML

.main-content
{
  width:400px;
  max-height:200px;
  background-color:green;
  margin-left:25px;
  overflow:hidden;
  overflow-y:auto;
  }

Answer №3

.wrap {
    width:400px;
    height:200px;
    border:1px solid #000;
    overflow-y: auto;
}

.content {
    width:300px;
    min-height:200px;
    background-color:blue;
    margin-left:50px;
}

To automatically adjust the height of .content if the content exceeds the minimum height of 200px, change max-height to min-height in the respective CSS rule.

Including the overflow-y: auto property in the .wrap class will add a scroll bar to the wrapper only if the content exceeds the defined height of

200px</code, or you can explicitly set <code>scroll
to always display the scrollbar.

.wrap {
    width: 400px;
    height: 200px;
    border: 1px solid #000;
    overflow-y: auto;
}

.content {
    width: 300px;
    min-height: 200px;
    background-color: blue;
    margin-left: 50px;
}

.content p {
    margin: 0;
}

.header {
    margin-left: 20px;
    font-weight: bold;
    height: 20px;
}

.header p {
    margin: 0;
}
<div class="wrap">
   <div class="header">
      <p>What is Lorem Ipsum?</p>
   </div>
   <div class="content">
      <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
      has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
   </div>
</div>

If you prefer to make only the .content scrollable, you can apply the overflow-y: auto to this class specifically instead of the .wrap class.

.wrap {
    width:400px;
    height:200px;
    border:1px solid #000;
    overflow: hidden;
}

.content {
    width:300px;
    max-height:200px;
    background-color:blue;
    margin-left:50px;
    overflow-y: auto;
}

By setting a maximum height for .content and applying the scroll property, the content will become scrollable if it exceeds the defined height, keeping the .wrap container non-scrollable with the overflow: hidden property.

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 next/image as a backgroundImage in a div container

Currently, I am working with nextjs and I am trying to set a background Image for a specific div using next/image. Most of the sources I found only explain how to implement full screen background images with next/image, not for a single div. I stumbled upo ...

Having trouble with ion-item click not functioning in Ionic 3?

Working on my Ionic 3 project, I have encountered an issue with the ion-item click listener. The scenario is that I am adding text over a canvas and then attempting to change the style of the text (such as font-family, font-size, bold, and italic). The UI ...

Refresh the Dom following an Ajax request (issue with .on input not functioning)

I have multiple text inputs that are generated dynamically via a MySQL query. On the bottom of my page, I have some Javascript code that needed to be triggered using window.load instead of document.ready because the latter was not functioning properly. & ...

Create an Ajax request function to execute a PHP function, for those just starting out

I came across a JS-Jquery file that almost meets my needs. It currently calls a PHP function when a checkbox is clicked, and now I want to add another checkbox that will call a different PHP function. My initial attempt was to copy the existing function a ...

Utilizing Object Arrays in Chart.js for Optimal Performance

I'm struggling with using chart.js to create charts for my admin panel. I'm having trouble figuring out how to properly utilize my JSON array of objects in order to generate the correct dataset. What exactly should I be putting in the data field ...

Having an issue with transmitting information to the database using ajax and Laravel

I am currently working on creating a drag and drop list that can be sorted. Each time an element is dropped into a new area, the order of the list should change accordingly. I am implementing this using AJAX and Laravel, but encountering an error when drop ...

Is there a simple method to add animation to a group of images displayed on click using JQuery?

Here is my JavaScript code that I'm currently using: $(document).ready(function() { $(".button-list .next").click(function() { project = $(this).parents().filter(".projektweb").eq(0); currentimg = project.find(".im ...

Defeat a JavaScript function or turn it into a Singleton Function

Is there a method to stop a running JavaScript function? Or is there a way to make sure that only one instance of the function runs at a time and any previous instances are removed upon restart? For example, if I call: _.defer(heavyDutyPaint); //How ca ...

Ensure that two div elements expand to occupy the available vertical space

I am looking to create a layout similar to the following: |---| |------------| | | | b | | a | |____________| | | |------------| |___| |______c_____| Here is the code I have so far: <table> <tr> <td class="leftSid ...

Can someone explain the reason behind the sizing discrepancies of HTML and body elements when emulating a mobile device in Chrome Developer Tools?

Recently, I attempted to load a tool on my phone that I have been developing for internal use at my workplace. Upon loading the tool, I noticed some discrepancies in the display, which I had anticipated. However, one specific element seemed particularly u ...

JavaScript Numbers Having Strange Behavior

Insight The highest integer that can safely be stored in JavaScript is 9007199254740991 link Dilemma 1000000000000 === 999999999999.999999 // Returns true 1000000000000 === 999999999999.99999 // Returns true 1000000000000 === 999999999999.9999 // Re ...

Exploring an Array Based on User's Input with JavaScript

Looking to implement a search functionality for an array using AJAX. The array is pre-populated with values, and the user will input a value in a HTML text box. If the entered value is found in the array, it should display "Value found", otherwise "not f ...

Tips for aligning three <p> elements side by side using flexbox

I need assistance with aligning three boxes next to each other, each containing an image, header, and text. The issue arises when resizing the browser, causing the content in box 2 and 3 to be positioned higher than box 1. Below is the code I have been us ...

What is the best way to alter the text of a button when the mouse hovers over it?

I'm looking to create a button that can dynamically change the text inside it when the cursor hovers over it, and then revert back to the original text when the cursor moves away from it. I attempted this in VScode using "document.getElementById().inn ...

The Register Page is failing to load

I'm currently tackling a validation form project with NodeJS. However, I'm encountering an issue where register.ejs is refusing to render. Despite my attempts to tweak the paths and navigate through different folders, the problem persists. As a n ...

Vue Quasar Drawer Layout with Bottom Bar

Struggling to implement a footer in my q-drawer. Below is the template and component code I am currently using: <template> <q-layout view="hHh lpR fFf"> <q-header elevated> <q-toolbar> <q-btn flat de ...

What is the process for tallying checked checkboxes in a functional component using React Native?

The Whole Code is Right Here Within this code, you will find two flat lists: one displaying category names and the other showing their subcategories with checkboxes. I am looking to implement a feature where if a user checks multiple or just one checkbox ...

Creating dynamic values in data-tables using Vuetify

As I work with JSON data, my current task involves formatting it using Vuetify's Data Tables. The official documentation provides guidance on defining table headers as shown below: import data from './data.json' export default { data ...

Why does the browser indicate that a GET request has been sent but the promise does not return anything?

Having trouble with a puzzling issue and unable to find any solutions online. I am attempting to make a basic HTTP get request to receive a JSON object from an API I created (using express+CORS). I have experimented with both Axios and VueResource, yet en ...

Guide to placing an element behind text using Bootstrap 3

On my webpage, I have a straightforward Bootstrap Jumbotron displayed prominently: <div class="jumbotron"> <div class="container"> <h1>Welcome to My Wonderful Site</h1> <p>There's some information her ...