Create a nested div with its own header inside another div

Here's a challenge I'm facing: I have nested divs where the inner one acts as a header for the outer div. The issue arises when I try to add padding to the outer container, causing the inner one to adjust accordingly. How can I maintain the padding of the container while ensuring the inner div aligns perfectly with zero margins on top, left, and right?

Below is the code snippet:

<div class="notification">
    <div class="notification_head">
        <p>Testing a notification.</p>
    </div>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit...</p>
</div>

And here's the corresponding CSS:

.notification {
border: 1px solid #fc2929;
border-radius: 7px;
padding: 0 2% 2% 2%;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}

.notification .notification_head {
width: 100%;
background: #fc2929;
color: #ffffff;
display: inline-block;
padding: 0.5%;
}

This is how it appears:

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

Answer №1

To achieve a cleaner design, avoid using paddings on container elements and instead apply paddings to inner contextual block-level elements such as p, h1, h2, ul, ol...:

*{margin:0;padding:0;} /* Start with a reset */

p, h1, h2, h3/*etc*/{
  padding:15px;
}
.notification {
  border: 1px solid #fc2929;
  border-radius: 7px;
  overflow:auto;
}
.notification .notification_head {
  background: #fc2929;
  color: #ffffff;
}
<div class="notification">
  <div class="notification_head">
    <p>Testing a notification.</p>
  </div>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean congue nibh vel velit posuere, eu rhoncus purus interdum. Praesent urna metus, mollis sed blandit ut, bibendum at neque. Aenean nulla metus, faucibus eget metus pharetra, ullamcorper placerat quam. Aenean euismod sagittis hendrerit.</p>
</div>

This approach will result in a simpler and more organized layout.

Answer №2

To enhance the appearance of your content, consider adding an additional div element and adjust the padding and margins to suit your preferences.

.custom-box {
border: 1px solid #3366ff;
border-radius: 10px;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}

.custom-box .header{
width: 100%;
background: #3366ff;
color: #ffffff;
display: inline-block;
    
}

.custom-box-content {
   margin: 20px;
}
<div class="custom-box">
    <div class="header">
        <p>Example of a custom box.</p>
    </div>
    <div class="custom-box-content">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam tempus arcu justo, nec egestas urna pulvinar non. Morbi vehicula nisi id gravida ultricies. Vestibulum et dui ac tortor porttitor mattis ut eu lacus. Aliquam erat volutpat. Nam suscipit, est in tincidunt scelerisque, lorem ante pharetra tellus, vel varius eros mauris sed velit. Nunc semper augue at orci viverra, vitae faucibus quam dignissim. Proin su odio metus. Vestibulum pulvinar congue orci, et dictum ex bibendum at. Fusce malesuada vulputate mi sit amet mollis. Pellentesque placerat maximus justo, quis pretium turpis tempor nec.</p>
    </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

Slider with FadeIn effect remains unresponsive to the FadeOut command (JQuery / Javascript)

I'm currently working on a slider that is supposed to fade in and out. However, I am facing an issue where the slide fades in correctly but instantly disappears instead of fading out. Do you have any insights into why this might be happening and any s ...

Issue with querying and ordering products in Django using Python when passing values through the GET request

I have been working on a project where I need to filter products and sort them by price in ascending and descending order. Here is the code snippet from my view: def is_valid_queryparam(param): return param != '' and param is not None def f ...

Begin an unnumbered hierarchical list commencing at 1.2.1

I am trying to create a nested unordered list with specific numbering. My goal is for the list to start at "1.2.1, 1.2.2, etc." Please refer to the attached fiddle for reference. The desired outcome is shown in the following image: https://i.stack.imgur ...

Having trouble with the page background image link not functioning properly because of the wrapper

I am facing an issue with my HTML layout. I have a full background image set within the body tag, followed by a wrapper element: <body> <a href="#" id="bkimage">Background Image</a> <wrapper> ................. Here are the styles ...

Is it possible to change the CSS styling of a specific DIV class that contains nested anchor tags using jQuery?

The HTML: <div id="main_menu"> <a id="menu_item_articles" href="articles">articles</a> </div> The CSS: #main_menu { float: left; padding-top: 10px; vertical-align: middle; text-align: center; width: 500px; ...

Vanishing essence

http://jsfiddle.net/ddGHz/1004/ //html <div id = "anglereturn"/> <div class="box"></div> //js, jquery var box=$(".box"); var boxCenter=[box.offset().left+box.width()/2, box.offset().top+box.height()/2]; $(document).mousemove(function ...

Create a visually engaging book reader with the power of jQuery and images

For hours, I've been attempting to develop an online Comic Book reader to load my images. Everything is functioning correctly, except for a counter using an increment method that just won't work - reducing the increments breaks the function. Is ...

How can I access an InputStream from a local XML file in a PhoneGap application?

Looking for advice on how to fetch an inputstream from a local XML file using JavaScript in my PhoneGap application. I'm new to JavaScript, so any guidance would be appreciated! ...

Switching the input to LTR changes the overall direction to LTR while maintaining the RTL placeholder

The text input is currently set to be RTL, including both the placeholder and the actual input. The settings for this were done elsewhere. My question is how can I modify it so that only the input changes to LTR, while the placeholder remains RTL. For exa ...

Is there a way to produce a unique color using Stylus CSS?

Currently, I'm utilizing Express for Node.js and Stylus as the CSS engine. While Stylus is great, I'm facing some challenges in passing color variables or generating random colors. Despite attempting to use the javascript API for stylus, I find m ...

Issue with displaying and hiding list elements using jQuery

I am attempting to create an accordion feature using <li> elements with classes .level1, .level2, .level3, and so on. The problem I am encountering is that when I click on a .level2 element, the items hide correctly until the next .level2 element wit ...

Dynamic progress bar based on time remaining

Is it possible to create a progress bar that reflects the countdown timer displayed in a div? The timer counts down from 7 minutes and is shown as follows: <div id = "quiz-time-left"> 0:06:24 </ div> I want to ensure that if the page is reload ...

Guide on creating a logarithmic-based bar rating system

I am working on creating a bar rating system for my website, but the current design doesn't look great. I want the height of the bars to increase like an exponential curve gradually, starting off not too steep and gradually increasing afterward. Here ...

Looking to switch up the hide/show toggle animation?

Currently, I have a functioning element with the following code. It involves an object named #obj1 that remains hidden upon page load but becomes visible when #obj2 is clicked. #obj1{ position:fixed; width:100px; bottom:180px; right:10 ...

Creating a nested JSON structure by fetching data from a remote source

i'm looking to populate the json file located at through a get request. This json contains music from various churches in different cities, with data organized into multiple levels. I want to parse this data using jquery and utilize hidden div elemen ...

Issues encountered when using Vue Transition in conjunction with v-autocomplete

Attempting to conditionally display or hide a selection text field using Vue's Transition feature, but experiencing an issue where the text field appears and disappears without any transition effect. <template> <Transition v-if="is ...

Utilizing CSS and JavaScript for Image Masking

Is it possible to create a div that takes on the shape shown in this image and then fill it with an image? Alternatively, is there a way to transform an image into that particular shape using CSS or Javascript? Additionally, is it possible to arrange m ...

The text input is malfunctioning in the IE web browser

For my project, I am incorporating AngularJS but encountering an issue with the input type text in IE browsers specifically IE 11 and IE 10. The problem arises when clicking on the input box - while the focus is triggered, the cursor does not appear insid ...

"Creating a CSS3 animation for changing the background image source - a step-by-step

Unfortunately, the background-image cannot be animated with keyframes in the following way: @keyframes kick{ 0%{background-image: url(images/img-man-1.png);} 25%{background-image: url(images/img-man-2.png);} 50%{background-image: url(images/im ...

What does Event.target.id return - the HTML id value or the HTML name value?

HTML Form Example: <form id="form-product-add" style="display: inline;"> <input name="csrf" type="hidden" value="<?= $this->csrf; ?>"> <input type="hidden" name="a ...