Overflow of content within a rectangular element

One challenge I'm facing is creating HTML/CSS code that automatically adjusts the website layout (blocks and content) when I resize the browser window. For example: I encounter text overflow issues in a block I've created when I minimize the browser window.

I want the block element to dynamically adjust its size based on the length of the content.

The Issue: The block element experiences text overflow, as indicated by a green block with a solid border style.

What I've Tried: I attempted to solve it with overflow: auto;, however, it's not the most elegant solution.

My Desired Outcome: I wish for the block to adapt itself according to the text length and vice versa

.p1 {
  background-color: green;
  border-style: solid;
  float: left;
  width: auto;
  height: 100px;
}

.block {
  display: block;
  width: 180px;
}
<header>
  <h1>Title</h1>
  <nav></nav>
</header>
<main>
  <article class="p1">
    <div class="block">
      <h4>Paragraph I</h4>
      <p>
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,
        sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel...
      </p>
    </div>
  </article>
</main>
<footer></footer>

Answer №1

Not entirely certain if you want the green block to fill the width or height of the browser window. Block level elements such as <article> or <div> will automatically adjust their height to fit the content and occupy the full screen width. To achieve this, simply avoid specifying these properties in your CSS.

See the updated changes below. If it's not quite what you had in mind, feel free to provide feedback in the comments for further adjustments.

Best of luck on your CSS journey!

.p1 {
  background-color: green;
  border-style: solid;
  /* float: left; <--- For matching the width of child elements, consider using width: fit-content; instead */
  width: fit-content;
  /* width: auto; <--- 'auto' is the default value, no need to explicitly set it */
  /* height: 100px; <-- Removing this will adjust the block height based on the content */
}

.block {
  /* display: block; <--- Not necessary as div is inherently a block element */
  width: 180px;
}
<header>
  <h1>Title</h1>
  <nav></nav>
</header>
<main>
  <article class="p1">
    <div class="block">
      <h4>Paragraph I</h4>
      <p>
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,
        sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.
      </p>
    </div>
  </article>
</main>
<footer></footer>

Answer №2

Avoid specifying a fixed height, as this will disregard the actual content height. Instead, using width fit-content will ensure that the element takes on the width of your content:

.box {
  background-color: blue;
  border-style: dashed;
  float: right;
  width: fit-content;
}

Answer №3

Success! I have found the solution, my friends (although most of you probably already knew it). However, there might be a more streamlined approach.

.block {
    background-color: green;
    border-style: solid;
    float:left;
    width: fit-content;
    display: flex;
    margin-left: 100px;
    margin-right: 100px;

  }

  .p1 {
    display: flex;
  }
<html lang="en">
<head>
    <meta charset="UTF-8>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"><
    <meta name="viewport" content="width=device-width, initial-scale=1.0"><
    <meta name="descrption" content="Übung zu Aufgabe 17.6"><
    <meta name="keywords" content="ansprechend, hintergrund, html, css"><
    <link rel="stylesheet" href="style.css"><
    <title>Iran</title><
</head><
<body><
    <header><nav></nav><h1>Title</h1></header><
    <main><article class="block"><div class="p1"><h4>Paragraph I</h4><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit...</p></div></article></main><
    <footer></footer><
</body></html><

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

The button should only be visible when the input is selected, but it should vanish when a different button within the form is

Having an issue with displaying a button when an input field is in focus. The "Cancel" button should appear when a user interacts with a search bar. I initially used addEventListener for input click/focus to achieve this, but ran into a problem: on mobile ...

Troubleshooting: Node.js not receiving data from HTML form

I am currently facing an issue with my HTML form and Node.js server. Despite implementing a form that is supposed to send data to the server, no information is being transferred. The form successfully makes a request, but it fails to send any form data alo ...

Identify the position of a mouse click event when dots overlap

Experience this live demo on CodePen by visiting it here. 1) When you click on the grid, a first red point will be added. 2) Click on the grid again to add a second red point. 3) By clicking back on the first red point, you may notice that the coordinat ...

How can I include inline CSS directly within a string instead of using an object?

Is there a way to write inline CSS in a string format instead of an object format? I attempted the following, but it did not work as expected: <div style={"color:red;font-weight:bold"}> Hello there </div> What is my reason for want ...

What steps can I take to make my search button work with this JavaScript code?

I am struggling with integrating my custom search bar with a JavaScript code to make it functional. Here is the code snippet for the Search Button: document.getElementById('searchform').onsubmit = function() { window.location = 'http:/ ...

Retrieving Data from all Rows in jQuery DataTables

Is there a way to copy all rows in jQuery DataTables into a JavaScript array by clicking the header checkbox? https://i.sstatic.net/57dTB.png I need to locate where jQuery DataTables store the HTML for the remaining page of rows so that I can manipulate ...

Exploring Angular2's interaction with HTML5 local storage

Currently, I am following a tutorial on authentication in Angular2 which can be found at the following link: https://medium.com/@blacksonic86/authentication-in-angular-2-958052c64492 I have encountered an issue with the code snippet below: import localSt ...

Utilize ASP.Net to Retrieve and Showcase RSS Feeds

Looking to read and display a specific feed on my website. Developed in C# using .NET 2. Attempted to follow this tutorial: Encountered the following error: A column named 'link' already belongs to this DataTable: cannot set a nested table n ...

Creating a radial gradient texture using CSS

I'm encountering an issue with my radial gradient and texture combination. Here is the code snippet I am using: background-image: url('../img/texture.png'); /* fallback */ background: -moz-radial-gradient(rgba(253,253,253,0.1) 0%, rgba(2 ...

I'm baffled by this unsemantic grid - why is the final div getting pushed below the rest?

I am working with the unsemantic fluid grid system and aiming to create a row of divs that are all in line and together cover 100% of the page. <div class="grid-container"> <div class="grid-parent yy name-strip zz"> <div class="yello ...

Identifying the accurate folder for file uploads

In my project directory, I have a folder named uploads, along with two files called upload.html and upload.php. The relevant part of my upload.html file looks like this: $(document).ready(function(){ $("#pictureUploadSubmit").submit(function(event) { ...

The CSS styling is not taking effect

I'm having trouble with my CSS changes not appearing on the webpage, and I can't seem to figure out why. Specifically, I'm attempting to modify the font, which has previously worked but is now not taking effect. Both my CSS and HTML files ar ...

Alignment issue: Center alignment failing to work correctly

I'm currently working on a navigation bar with a central title and links to other pages aligned to the right. Despite using text-align: center, the title remains off-center. I've implemented a flexbox to ensure that the title and links are on the ...

What is the best way to create an element that is clickable but transparent in appearance?

Is there a way to achieve an inset box shadow on elements like an iframe without blocking clicks on the element itself? The common strategy of overlaying a div over the iframe achieves the desired visual effect but unfortunately hinders interaction with th ...

Strategies for concealing and revealing content within dynamically loaded AJAX data

I am attempting to show and hide data that is loaded using Ajax. $.ajax({ type: "POST", url: "/swip.php", data: {pid:sldnxtpst,sldnu:sldnu}, success: function(result) { $('.swip').prepend(result); } }); This data gets ...

Error message: "AngularJS encountered a $injector:modulerr error in Internet Explorer 11."

I've successfully created an AngularJS App that functions properly on most browsers like Firefox, Opera, Safari, Edge, and Chrome. However, there seems to be a compatibility issue with IE 11. When attempting to open the app in IE 11, the following er ...

What could be causing an unidentified term to be included in the Vue SCSS compilation process

In my Vue application with TypeScript, I encountered an error during compilation that reads as follows: Failed to compile. ./src/style.scss (C:/../node_modules/css-loader/dist/cjs.js??ref--8-oneOf-1-1!C:/.../node_modules/vue-loader/lib/loaders/stylePostL ...

What is the process for creating a pop-up bubble that appears when the cursor hovers over an image displayed within a table using R Shiny?

I am currently working on code that is almost achieving my desired functionality. I want to make it so that hovering over each question mark in the table will trigger a pop-up bubble displaying the help text, rather than having the text appear at the botto ...

Enable Element Blocks Content Below When Toggled

Is there a way to make a toggle element completely block out all content below it? I attempted to set the width and height to 100%, but it ended up cutting off the content inside the toggle element. ...

Creating resizable rows of DIVs using jQuery

I'm currently developing a scheduling widget concept. The main idea is to create a row of DIVs for each day of the week. Every row consists of a set number of time periods represented by DIVs. My goal is to enable the resizing of each DIV by dragging ...