Step-by-step guide to designing a background-color highlighting box that extends beyond the text column [see accompanying image]

I want to create a column of text with blocks where one block has a colored background that extends beyond the confines of the column. I'm looking to achieve this in two ways: first, maintaining the original text layout, and secondly, adding extra bounding space.

It seems like it should be simpler than I'm finding it.

My attempts involve enclosing each block in a div element with a 10px margin. For the highlighted block, I set a margin of 0px and a padding of 10px.

This method works fine for regular paragraphs. However, when a block starts with a heading (such as an h3 heading), excessive top padding is added – which cannot be manually adjusted easily due to limited values between `padding-top:1px` and `padding-top:0px`.

<html>
<body>
    <div class="block">
        <p>
        Test Test Test Test Test Test
        </p>
    </div>
    <div class="block test2-block">
        <h3>Test Title</h3>
        Test2 Test Test Test Test Test
    </div>
    <div class="block">
        <p>
        Test3 Test Test Test Test Test
        </p>
    </div>
</body>
<style>
    .block {
        margin:10px;
    }
    .test2-block {
        background-color:green;
        margin:0px;
        padding-right:10px;
        padding-left:10px;
        padding-bottom:10px;
        /*This value can't be tweaked manually, try 0, 1, and 10 pixel values*/
        padding-top:10px;
    }
</style>
</html>

I'm unsure how to maintain an undisturbed layout. Any suggestions?

Edit: Thanks to aaronegan and sfyn below, we've solved the issue of extra bounding space (right-hand portion in the picture). Now, if anyone knows how to keep the layout undisturbed (left-hand side in the picture) or achieve a dynamic background change when scrolling to an anchor link, that would be greatly appreciated. Perhaps tweaking margins dynamically above and below the highlighted block could work, but it seems cumbersome.

Answer №1

For optimal column layout, I recommend setting a specific width in pixels or as a percentage of the containing div.

The issue with the h3 element is its default margin-top attribute, causing unwanted spacing at the top.

Here are the steps I took:

  • Define a width for the column in your CSS
  • Remove unnecessary padding from the .test2-block class and keep it to 10px
  • To remove the h3 margin-top, add this code to your CSS:

    .block {
        margin:10px;
        width:200px;
    }
    
    .test2-block {
        background-color:green;
        margin:0px;
        padding:10px;
    }
    
    .test2-block h3 {
        margin-top:0px;
    }
    

View working example on JS Fiddle

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

Achieving the Display of the Desired Description on Search Engines

I recently ran a Google search on my website and noticed that only one page was correctly displaying the <meta name="description" content=...> tag. The meta tags are generated using PHP through the php include function. <head> <?php ...

Looking to learn how to replicate data effortlessly with either javascript or jquery?

I am currently trying to figure out how close I am to successfully cloning this div. Honestly, I am a bit lost at this point and could really use some assistance. Should I consider using jQuery for this task? <div id="question20">20. Details of Chi ...

Creating a Rails application that dynamically fills a table with data from a

Encountering an issue with Ruby on Rails. I have a "Host Model" that contains a method which has a longer runtime. class Host < ActiveRecord::Base def take-a-while # implement logic here end Attempting to access a page where this method ru ...

What is the best way to ensure that my cards maintain consistent proportions?

My cards are not maintaining their proportions, causing buttons to pop out of the card and the card dimensions changing. I realize this issue is due to using fixed width and height in combination with flex. I'm struggling to find the best solution to ...

Differences between JavaScript's window.onload and body.onload functionsWhen

My inquiry is similar yet slightly distinct from the one queried here: window.onload vs <body onload=""/> The comparison in that prior query was between utilizing window.onload and inline js. My question pertains to the disparity between ...

Leveraging HTML Page Variables in Google Tag Manager for Enhanced Merchant Code Integration

I'm currently in the process of incorporating Google Merchant Code via GTM on a product checkout/thank you page. The Google Merchant code is : <script> window.renderOptIn = function() { window.gapi.load('surveyoptin', function() { ...

What is the best way to remove input focus when clicked away?

I am in the process of developing an application using next js, and I need assistance with designing a search field. The primary functionality I am looking to implement is displaying search suggestions when the user starts typing, but hiding them when the ...

What is the best way to include multiple ng-repeats within a single ng-repeat?

Hey, I'm facing quite a tricky situation with this grid and could use some assistance. I'm trying to figure out how to populate the data using ng-repeat. I've attached an image showcasing the desired layout of the grid and the order in which ...

Is it possible to display the triangle external to the container element?

I'm currently working on creating a CSS menu. My goal is to have a triangle of a specific size appear next to the container when the li element has a class of active. To see an example, check out the following http://jsfiddle.net/hcabnettek/MeczJ/ Wh ...

When compiled on a device, Cordova is not utilizing CSS and JS files

I am currently in the process of developing an app using Cordova with an HTML file. However, after building the app, I encountered issues where it is not incorporating CSS and does not seem to be utilizing JavaScript. <!doctype html> <html la ...

Creating a CSS template for organizing sidebar and footer divisions on a webpage

I am facing an issue with the layout of my website. I have a container that is positioned relatively and houses various divs for header, sidebar, main-content, and footer. The problem lies with the sidebar and footer components. The sidebar has been posit ...

Adjust the size of an image within a canvas while maintaining its resolution

My current project involves using a canvas to resize images client-side before uploading to the server. maxWidth = 500; maxHeight = 500; //handle resizing if (image.width >= image.height) { var ratio = 1 / (image.width / maxWidth); } else { var ...

Retrieve the site's title content using PHP

I created a code to scrape website content from platforms like Facebook and Google+. $html = file_get_contents_curl($url); if ($html) { //parsing begins here: $doc = new DOMDocument(); @$doc->loadHTML($html); $nodes = $doc ...

What is the best way to eliminate the left padding on a TimelineItem component?

When using the Timeline component to display information, there are three columns: TimelinItem, TimelineSeparator, and TimelineContent. I tried setting the padding of TimelineItem to 0 but it didn't work. The <Trace/> component is a sub-compone ...

Is there a way to reveal the full contents of a cell when hovering the mouse, even if it has overflow hidden?

I am facing an issue with tables that contain long strings as values. Is there a way to display the entire content of a table cell if the overflowing content is hidden? I thought about expanding the cell while overlapping adjacent cells without displacing ...

Postback does not reload all controls in the NameValueCollection

Recently, I designed a custom User Control in asp.net. public class InputMask : CompositeControl, IPostBackDataHandler, IPostBackEventHandler Imagine the custom control is enclosed within a div element: <div runat="server" id="inputArea"> < ...

The dropdown menu from React Bootstrap is only partially visible when used within the datagrid

They say a picture is worth a thousand words, and what's bothering me now is this predicament: https://i.sstatic.net/UGxyi.jpg I apologize for the blurred text, but I can't disclose much due to company policy... As you can see in the screenshot ...

PHP script to enable functionality for the 'Backspace' button

I recently started learning PHP and have built a basic calculator to perform simple calculations. The calculated result (or if a button is clicked, the relevant number) is shown in the input field <input type="text" id="textbox" value="<?php echo $re ...

Aligning two flex items vertically

I am working with a flex container called .container and two flex items: item-one and item-two. My goal is to center the first item vertically and anchor the second item to the bottom. Struggling to figure out how to vertically align the first item within ...

Replace HTML elements with AJAX and JavaScript

Working with MySQL data in pyramid presents a challenge as I need to dynamically change an HTML if statement based on the results from JS ajax calls. The main page receives data from views.py and passes it to the .mak script. The key views here are the ma ...