Ways to adjust the size of a container to perfectly match the width of its child elements

My challenge involves two squares in a container that overlap using transform: translate. I am seeking to eliminate the padding to the right of the blue square so that the container perfectly accommodates the width of the children. View the image for clarity.

Visual representation of the issue

I attempted setting the container size to 90px, which should match the total width of the children (50px + 50px - 10px). However, this caused the blue box to drop to the next row. Why does this occur? I also experimented with applying padding-right: 0 but observed no changes.

.container {
  width: 110px;
  border: 1px solid black;
}

.box {
  height: 50px;
  width: 50px;
  display: inline-block;
}

.one {
  background: red;
}

.two {
  background: blue;
  transform: translate(-10px, 15%);
}
<div class="container">
  <div class="box one"></div>
  <div class="box two"></div>
</div>

My goal is to have no left or right padding present.

Answer №1

For precise positioning, utilize position: absolute along with top and either left or right. Also, remember to apply position: relative on the container element:

.container{
  width: 90px;
  border: 1px solid black;
  padding-right: 0;
  position: relative;
}

.box{
  height: 50px;
  width: 50px;
  display: inline-block;
}

.one{
  background: red; 
}

.two{
  background: blue;
  position: absolute;
  left: 40px;
  top: 15%;
}
<div class="container">
  <div class="box one"></div>
  <div class="box two"></div>
</div>

Answer №2

To create the desired effect, apply a negative margin-right to the second square

.container {
  width: 94px;
  height: 57px;
  border: 1px solid black;
}

.box {
  height: 50px;
  width: 50px;
  display: inline-block;
}

.one {
  background: red;
}

.two {
  background: blue;
  margin-right: -10px;
  transform: translate(-10px, 15%);
}
<div class="container">
  <div class="box one"></div>
  <div class="box two"></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

Is there a way to use jQuery to animate scrolling on a mobile web browser?

I'm trying to make the page scroll to a specific position when a button is clicked. The code I currently have works fine on browsers like Chrome and IE, but doesn't seem to work on any mobile browser. Below is the code snippet I am using: $("#p ...

Display a list with collapsed columns on an accordion format for a more compact view on smaller screens

My Bootstrap table setup looks like this: <table class="table table-striped"> <thead> <tr> <th>Date</th> <th>Name</th> <th>LastName</th> <th>Color</th> <th>Car</th> ...

Encountered a problem while attempting to create two separate div elements. The template root is designed to have only one element. This issue

<template> <div class="footerpart-container"> <div class="general-container"> dadada </div> <div class="legal-container"> dadad </div> <div class="helpsupport-container"> dada ...

Effortlessly glide through columns in Bootstrap 4

On my page, I have three columns with a class of col-md-6. Initially, only box1 and box2 are visible. When the user clicks on the button labeled BTN, I want to display box2 and box3, smoothly transitioning all boxes to the left. I attempted using jQuery ...

I am having difficulty centering the contents on the page horizontally

Struggling to achieve horizontal alignment, I suspect the floats are causing issues. Removing them disrupts the layout with left_wrap and right_wrap not staying next to each other. Check out this example on JSFiddle .main_wrap {width:100%;} .main_wrap_2 ...

jquery, slideToggle not behaving correctly on mouse hover

Whenever the mouse hovers over my div with the class .frame, I slideToggle a div with the class .content. And when the mouse leaves .frame, I toggle .content back again. Everything seems to be working fine except for two issues: the effect gets messed up i ...

What is the best way to position the publication date of an article at the bottom of the

I am looking to have the article publishing date (1/9/2016) positioned at the bottom inside the container. Right now, the date appears just below the text, but I want it to be aligned at the bottom of the container. The website in question is watchathletic ...

preserve functionality when switching between pages

I have created two simple functions that can change the background color of the body. <script type="text/javascript"> function switchBackground(color){ document.body.bgColor=color; } </script> I am using links with onclick to execute thes ...

What is the best way to achieve concave borders in HTML/CSS, similar to the example provided?

Check out this unique graphic created with normal divs in a flexbox layout, courtesy of mode.com. https://i.sstatic.net/hWwhQ.png Ever wondered how this effect is achieved? Upon inspecting the page source, I discovered that there are four elements arrang ...

Center the image and align the floated texts on each side horizontally

I've recently started learning about HTML and CSS, but I'm having trouble grasping one concept. My goal is to align two floating <p> elements on either side of a centered <img>. Here's an example of what I'm trying to achiev ...

What is the best way to line up the columns of various divs?

Is there a way to align the content of these different divs? I'm trying to match up the headers of each column with the corresponding items in the divs below. Can someone assist me in achieving this alignment? row-header-course should align with cou ...

Auto language-switch on page load

Wondering if using jQuery is the best approach to create a single French page on my predominantly English website. I want it to work across multiple browsers on pageload, currently using HTML replace. jQuery(function($) { $("body").children().each(funct ...

Transform the JavaScript object received from an AJAX request into HTML code

Here is an example of what the result (data) looks like: <tr> <td> Something... </td> </tr> <div id="paging">1, 2, 3... </div> This is using ajax to retrieve data. ... dataType: "html", success: function( ...

Get the ID from a row that was created dynamically

I have a webpage that pulls information from my database and displays it. Users should be able to click on the "details" link to see more details about a particular record, or click on an input box in the "check" column and submit to update the record stat ...

I am looking to reposition the navbar span tag so that it appears beneath the icon, utilizing both HTML and CSS

I'm currently working on a navbar design with 5 items, each containing an icon and a span tag. However, I'm facing difficulties in aligning the span tag below the icon for all 5 items in the navbar. Despite my best efforts, I have been unable to ...

The CSS for the UI Grid is malfunctioning

I have been working on creating a grid using UI Grid (recent version of ngGrid) within my current project. However, I am facing some issues with the display. The icons like angle down and row selected icon are not showing up correctly when I include the CS ...

Prevent vertical scrolling on touch devices when using the Owl Carousel plugin

Is there a way to prevent vertical scrolling on Owl Carousel when dragging it horizontally on touch devices? It currently allows for up and down movement, causing the whole page to jiggle. If anyone has a solution, I can share the JavaScript file. Appreci ...

Guide on Generating Dynamic JSON to Set and Retrieve Values for Forms and Displaying the Bound Values

I'm a beginner in Ionic 3 and I'm having trouble creating a page that redirects to another page without validation. I want to display the data on the new page, but it's not working. Please help! I also want to create a dynamic JSON object a ...

JQuery for Seamless Zoom Functionality

I am working with some divs that have images as backgrounds, and I have added a zooming effect on hover. However, the zoom effect is not smooth. Is there a way to modify the script to make the zoom effect smoother? Here is an example of my HTML structure: ...

Using a variable as an argument for a DOM function in JavaScript

I found this code snippet on a website and made some changes to it. However, the modified code below is not functioning as expected. My goal was to hide the div with the id "demo1", but for some reason, it's not working. What could be causing this is ...