How can you increase the width of an element by x pixels while still maintaining its liquid width

Is it possible to have a container with liquid width and 20px of padding in CSS, where most contained elements respect the padding but a few ignore it and are the same width as the container itself?

I am aware that I could use margins on the contained elements instead, but due to the complexity of my HTML structure, this is not ideal.

<div class="cont">
    <div class="item-normal">Normal Item</div>
    <div class="item-wide">wide Item</div>
    <div class="item-normal">Normal Item</div>
    <div class="item-wide">wide Item</div>
    <div class="item-normal">Normal Item</div>
</div>
body {
    padding: 0;
    margin: 0;
}
.cont {
    background: grey;
    padding: 20px;  
}
.item-normal {
    background: blue;
    margin-bottom: 10px;
}
.item-wide {
    margin-left: -20px;
    background: gold;
    margin-bottom: 10px;
}

Answer №1

To make the .item-wide element have a margin-right of -20px:

.item-wide {
    margin-left: -20px;
    background: gold;
    margin-bottom: 10px;
    margin-right:-20px;
}

Here is an example on jsFiddle.

You can simplify the rule by combining the three separate margins into one with margin: 0 -20px 10px;

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

Bootstrap: one row inside a column with a large width in another row

Hello, I am trying to create a layout with a blue row inside a red col-lg-4 div within a yellow row. Here is the code snippet I have attempted: <!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> < ...

What is the best way to center a paragraph vertically around a particular word?

Within this div, I have a paragraph. I am trying to position a specific word within the paragraph to be centered vertically, but my attempts using a span have been unsuccessful. Is there an easy way to achieve this? div { width: 300px; height: 250px ...

Struggling to properly position a search input and submit button in a navbar on smaller screens

While creating a search view for a website, I noticed that the search box and submit button placed next to it (aligned to the right at most screen widths) would become misaligned in very small max-widths. Using Bootstrap 4, I utilized ".ml-auto" to align ...

Is the jSON data featured at the top with DIV / Table Headers positioned at the bottom?

I'm really struggling with this issue. I've tried a few different things but nothing seems to work. Any suggestions on how to tackle this problem? Here's a link to a screenshot of the error I'm encountering... <div class="contain ...

"Learn the steps to toggle a sub menu using an onclick event and how to hide it using another

I created a sidebar navigation that displays submenus on mouseover, but I want them to open on click and close when clicking on the same tab. Please take a look at my code on this CodePen link. Thank you. <nav class="navigation"> <ul class="mai ...

Verify if a personalized angular directive is able to display or conceal an element

I have a custom directive that toggles the visibility of an element based on the value of another service. I attempted to write a test to verify if the directive functions as intended. Initially, I used the ":visible" selector in my test, but it consistent ...

Expanding content does not increase CSS height to 100%

I recently had a question similar to this one, but I struggled to explain it properly without a JSFiddle. After spending about 20 minutes creating this fiddle, I hope it helps clarify my issue! Check out the JSFiddle here In my project, I have expandable ...

Executing JavaScript Code Through Links Created Dynamically

I am currently developing a website with a blog-style layout that retrieves information from a database to generate each post dynamically. After all posts are created, the header of each post will trigger an overlaid div displaying the full article for tha ...

Designing a final splash screen for my game with the help of JavaScript, Cascading Style Sheets, and

I'm facing a challenge with setting up an end screen for my candy crush game. I have implemented a timer, but when the time runs out and certain conditions are met, the end screen fails to appear. Do you know what might be causing this issue? functio ...

Make the element switch from hidden to visible when hovering over it using CSS

I had a challenge to only show some rating text on my website when I hovered over it. Being new to coding with CSS and JavaScript, I was experimenting with overriding styles using the Stylebot extension in Chrome. I encountered an issue while trying to mo ...

Creating a minimalist metro-inspired menu using HTML and CSS

After researching online for a few days, I've just started learning HTML/CSS and my skills are currently at about a 6 out of 100. I have this code that functions as a blogger HTML widget, but I want to customize it further. Here is the code snippet: ...

I'm not sure why the CSS attribute that restricts character numbers exists

https://i.sstatic.net/dkRt3.png The Divtag appears to be malfunctioning due to insufficient properties. I am currently unsure of how to resolve this issue and would appreciate any insights or opinions. <div class="col boardsBox" style="margin-right:0. ...

How can I center text both vertically and horizontally using CSS?

https://i.sstatic.net/ffLj8.jpg My goal is to achieve a specific look, but I seem to be struggling with it. Here's what I currently have: https://i.sstatic.net/MHcuD.jpg Below is the code snippet I am working with: .blog-lockup max-width: 1000px ...

What is the simplest method for finding the position of a child element in relation to its parent?

My HTML markup is structured similarly to this: <div id="parentElement"> <div class="child"></div> <div class="child"></div> <div class="child"></div> <div class="child"></div> </div ...

Play around with my design

I'm having trouble with my styling and need some assistance. Specifically, I want to make sure that my page_content_div appears under my header instead of below the right and left carousel controls. I tried using float:left on the page_content_div, bu ...

Switch back and forth between `display: none` and `display: flex` using JavaScript

There is a JavaScript function in place for a responsive navigation system that includes a burger button to toggle the visibility of the navigation menu when the screen size is too small. The issue I am facing is that despite setting the CSS style as disp ...

Creating dynamic CSS backgrounds that scroll horizontally

Styling with CSS #container { min-width: 800px; max-width: 1000px; height: 100%; margin: 0 auto; text-align: left; } #left-section { height: 100%; position: relative; float: left; ...

Moving divs to a separate line in mobile display

I currently have a basic image-thumbnails landing page set up like this: <div style="display: inline-block;"><img style="margin: 3px 3px;" src="..." alt="" width="200" height="200" /></div> <div style="display: inline-block;"><i ...

Why does the HTML and CSS slider fail to load upon page refresh, yet works perfectly when the next or previous button is clicked?

There seems to be an issue with the slider images not loading on page refresh. Oddly enough, they appear only after clicking the next button. However, upon refreshing the page again, the images disappear. <div class="slide-banner"> < ...

Transfer the div above to the other div

My dynamic set of divs have varying heights generated dynamically even though their widths are fixed. Despite setting them to float:left, they are not aligning perfectly under each other. The divs are forming rows with the height of the row being determine ...