What is the best way to ensure a div properly envelops content with adequate spacing?

I've been struggling to configure a div to automatically adjust its width based on surrounding content. Despite trying various methods, the div stubbornly remains at 100% width. My goal is to add some space on the left and right sides for a neater border, but it keeps getting placed below other divs.

https://i.sstatic.net/lzthJ.png

The desired outcome

https://i.sstatic.net/Ov7Lk.png

The current result (subheader is full width and appears below)

I've experimented with different approaches like overflow:hidden and float:left, but nothing seems to work.

Here is the code snippet:

<div class="box">
<div class="side"></div>
<div class="psubheader">Test</div>
</div>

<style>

.box {
width: 50%;
height: 1000px;
border:1px solid;
}

.psubheader {
    padding-bottom:7px;
    font-family: 'sans-serif';
    font-size: 1.35em;
    border-bottom: 1px solid #a2a9b1;
    margin-left:10px;
    margin-right:-10px;
}

.side {
width: 50%;
height: 1000px;
background-color:red;
float:right;
}

Any assistance would be greatly appreciated. Thank you.

Answer №1

Eliminate all instances of float. Instead, apply flex to the parent element and specify the widths for each flex item. I noticed in your design, the psubheader did not have a defined width and was getting nested within the floated div. To address this, consider using a text element with a border setting that restricts its width.

.box {
  width: 100%;
  height: 1000px;
  border: 1px solid;
  display: flex;
  flex-flow: row-reverse;
}

.psubheader {
  padding-bottom: 7px;
  font-family: 'sans-serif';
  font-size: 1.35em;
  width: 50%;
  border-bottom: 1px solid #a2a9b1;
  margin-left: 10px;
  margin-right: -10px;
}

.side {
  width: 50%;
  height: 1000px;
  background-color: red;
}

h5 {
  margin: 0;
  border-bottom: solid 1px #a2a9b1;
  width: 95%;
}
<div class="box">
  <div class="side"></div>
  <div class="psubheader">
    <h5 class="tst">Test</h5>
  </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

How to arrange table data in Angular based on th values?

I need to organize data in a table using <th> tags for alignment purposes. Currently, I am utilizing the ng-zorro table, but standard HTML tags can also be used. The data obtained from the server (via C# web API) is structured like this: [ { ...

Convert a div into a clickable link using JavaScript without using too many classes or any ids

After using shortcodes generated by functions.php in a WordPress parent theme, I have come across the following HTML code: <div class="pricing-table-one left full-width "> <div class="pricing-table-one-border left"> <div class=" ...

Chrome tab freezes when scrolling with the mouse

Working on a particularly interesting bug, I've managed to replicate it using a fiddle. The issue arises when the middle mouse button is clicked over the div element containing text, causing the pointer to become stuck. Is this possibly a browser bug ...

Guide on resizing a background image to fit a div

Looking to enhance the appearance of my website by incorporating a cover image. Positioned at the top of the page is a div tag with dimensions set to 80% width and auto height. I aim to insert an image as the background of this div, stretching in width b ...

Switching between menu options and buttons

My game has a menu for selecting the class. Each class has its own button - Knight, Mage, Archer. I want to use jquery so that when any of these buttons are clicked, a new set of buttons appears to choose the race. I tried using .replaceWith() but it does ...

Adjusting the width of images using jQuery Mobile or jqTouch

I am looking to incorporate a static footer image with 5 navigation buttons into my mobile website. You can view the image here. The default color for the icons should be blue, but I want the yellow icon to appear only when hovered over or in an active sta ...

bootstrap 4 appears to encounter some responsiveness issues when it comes to rendering on iPhone

https://i.sstatic.net/rdOtX.png While inspecting my website on mobile using developer tools, everything looks perfect. However, when I view it on my actual iPhone 6, the layout seems off. Could this be a bug specific to the iPhone 6? Here are my meta tag ...

Form appears on each page, but it should only be processed on specific pages. If the form is not currently being displayed, then redirect. When on the correct page,

video.php == 'video' esse.php == 'esse' I have two main pages - 'esse' which serves as a welcome page, and 'video' where users can view videos. The 'esse' page includes a top nav container and sidebar, mak ...

Current version of document object model code

While working on a webpage that involves javascript and manipulating elements in the dom, I've encountered an issue. I cannot seem to view the updated source code of the dom in Chrome or Firefox (even with firebug), although the browser rendering look ...

Utilizing Angular Material for Styling the Web

https://i.sstatic.net/GVcgu.png I am new to working with Angular and currently, I have a sidenav container with the content being a mat toolbar. My issue is that when viewed on a full-sized desktop, the background color of the toolbar stretches and fills t ...

Creating a persistent div with fixed positioning, vertical overflow, and spanning the entire width using Bootstrap 4

I'm looking for a way to create two columns with two tables using Bootstrap 4.1. I want the right table to be fixed with auto overflow, but when I try to do this, the table does not span the full width of the column. Are there any bootstrap solutions ...

Ways to display or conceal various divs by employing an if/else statement?

I am aiming to conditionally display various divs based on certain criteria: Show the "x" div if it's not already visible Display the "y" div only if the "x" div is already being displayed Show the "z" div only if both the "x" and "y" divs are alrea ...

Retrieve the value of a dynamically added or removed input field in JQuery using Javascript

Check out this informative article here I'm looking for a way to gather the values from all the text boxes and store them in an array within my JavaScript form. I attempted to enclose it in a form, but I'm struggling to retrieve the HTML ID beca ...

Display the HTML content once the work with AJAX and jQuery has been successfully finished

Below are the codes that I have created to illustrate my question. It's not a complete set of code, just enough to explain my query. The process goes like this: HTML loads -> calls ajax -> gets JSON response -> appends table row with the JSO ...

Internet Explorer: JQuery deselects radio button upon its selection

One issue I have encountered is with a listener for a group of radio buttons in IE. When a radio button is selected, it triggers a database call to populate a select element. However, in IE, after the code runs successfully, the selected radio button becom ...

When a base html tag is dynamically added, the browser mistakenly loads assets twice

When managing relative paths on a website, I utilize the <base> tag within the <head> section of each page. Although all resources loaded via relative-like paths in the documents are displayed correctly, my observations show that browsers such ...

Navigational Bar with Bootstrap

I am currently working on a Bootstrap Navbar design where I want it to appear as a hamburger for the entire window, except when it reaches around 700px in width. At that point, I need it to display all the navigation drop-downs without stacking. The issue ...

Guide on accessing a div by using the class name of another div

If I have five divs where the first four share the same class, while the fifth has a different class. How can I target any of the first four divs using the class name present in the fifth div which is labeled as "otherclass" in this scenario? No IDs are ...

Inline checkbox label with Bootstrap 5 styling

How can I align the checkbox with its label? Example <div class="row g-1 border-bottom p-3"> <div class="col border-end justify-content-center d-flex align-items-center"> <div class="h6 text-sm-center h-25&quo ...

Center-align the text vertically in relation to the icon

A query has arisen, regarding a div that contains text and an icon. The issue at hand is that on larger screens the text is not vertically aligned in the center. For further reference, please visit this bootply link: http://www.bootply.com/CHKeRxKOX6 http ...