Tips on collapsing margins of floating elements

Is there a way to eliminate the vertical margin between these floating divs?

.container {
  display: block;
  padding: 1px;
  max-width: 500px;
}

.float-left {
  display: block;
  float: left;
  margin: 20px;
}

.float-right {
  display: block;
  float: right;
  margin: 20px;
}

.center {
  display: block;
  text-align: center;
  margin: 20px;
}

.container * {
  outline: 2px solid red;
  box-shadow: 0px 0px 0px 20px rgba(0,0,0,0.5);
}
<div class="container">
  <div class="float-left">::::::::::::::::::::::FloatLeft::::::::::::::::::::::</div>
  <div class="float-right">::::::::::::::::::::::FloatRight::::::::::::::::::::::</div>
  <div class="center">::::::::::::::::::::::::::::Center::::::::::::::::::::::::::::</div>
</div>

Final output:

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

Note that it is important for .float-left, .float-right, and .center to behave consistently when adjusting the width of .container, even if there is an alternative solution other than using floats.

Appreciate any help provided.

Answer №1

After some experimentation, I finally cracked it:

.wrapper {
  display: block;
  padding: 15px;
  max-width: 600px;
}

.float-left {
  display: inline-block;
  float: left;
  margin: 15px;
}

.float-right {
  display: inline-block;
  float: right;
  margin: 15px;
}

.center {
  display: block;
  text-align: center;
}

.center_container {
  display: inline-block;
  margin: 15px;
}

body *:not(.center) {
  outline: 3px solid blue;
  box-shadow: 0px 0px 0px 13px rgba(0,0,0,0.7);
}

body .wrapper {
  box-shadow: inset 0px 0px 0px 13px rgba(0,0,0,0.7);
}
<div class="wrapper">
  <div class="float-left">::::::::::::::::::::::FloatLeft::::::::::::::::::::::</div>
  <div class="float-right">::::::::::::::::::::::FloatRight::::::::::::::::::::::</div>
  <div class="center">
    <div class= "center_container">::::::::::::::::::::::::::::Center::::::::::::::::::::::::::::</div>
  </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 position the button on the right side rather than the center using a negative right Y value?

I'm looking for something that: Has a button on the right side, not just beside the input. The current setup involves using an unattractive width: 138%, which doesn't consistently work across different browsers and devices. The button ends up a ...

What is the purpose of using a visually striking and prominent HTML tag?

After 15 years of working in html development and managing multiple websites, I have come to realize that bold and italic tags essentially serve the same purpose. I can apply CSS styles to both to achieve similar effects. I have even experimented with maki ...

Having trouble with the full-screen feature not functioning properly?

I am currently in the process of creating a custom video player and I need to include a full-screen button. However, when I click on it, the video does not expand to fill up the entire screen. I am using javascript, css3, and html5 for this project. Any as ...

Using the ::before selector to loop through td with Sass

I've recently started using sass and I'm encountering some difficulties with the ::before syntax within a @for loop. My table row consists of six td elements. <tr> <td></td> <td></td> <td da ...

a text input field with an invariable placeholder inside

I'm attempting to design a textarea with a consistent title in the top left corner. Here's what I have so far: <div style="position: relative"> <div style="position:absolute; top:3px; left:3px; font-size:9px; line-height:10px">My ...

Is there a Joomla extension available that can display or conceal content upon clicking?

Looking to enhance my Joomla site by installing a plugin that allows me to toggle the visibility of content with a click, similar to how FAQ sections work on many websites. Question 1 (click here for the answer) { Details for question 1 go here } Questi ...

What is the best way to add a hyperlink to my directory pictures?

Recently, I came across this piece of code: <? include "top.php"; ?> <center> <a href='upload.php'><button>Upload Image</button></a> </center> <br> <?php $www_root = 'http://annexsecurit ...

How to reference color variables from code behind in ASP and use them in CSS files

I have a total of 3 files dedicated to my Login-Screen: Login.aspx, Login.aspx.cs, and LoginStyle.css Upon the execution of the Page_Load method, I retrieve various data from the current system, including the theme-color represented as a string (e.g., #00 ...

CSS only accordion divs that are all opened by default with no need for jquery

My current setup involves: jsfiddle.net/wromLbq5 I would like to enable the functionality of having multiple accordion sections open simultaneously upon page load. This means that when one section is opened, the others should not close. Is there a way to ...

Footer stays put as you scroll, peeking out from behind the main content

I'm trying to replicate the sticky-footer effect that can be seen on the website From what I understand, the footer needs to be fixed in place. The content should have a higher z-index. It seems like the body may need a margin-bottom equal to the ...

Unable to modify jwplayer css styles to customize the chromecast icon

Is there a way to customize the chromecast icon within the JWPlayer skin to have a specific color? I've identified that the styles I need to change are --connected-color and disconnected-color when inspecting the element. Despite my attempts through ...

Vertically align the text

Is there a way to center this text vertically within the divs? I've attempted adjusting the position using relative/absolute, and modifying the top and left properties of the paragraph, but none of these methods have been successful. div.roxo{ ...

Tips for navigating through a div with an unknown height

I am currently working on developing a sleek messaging system and I have encountered an issue with the interface design. My project is built using Materialize, however, I am open to tweaking it with custom CSS if necessary. The layout consists of a list of ...

What is the process for applying CSS styles to a particular component?

What is the recommended way to apply CSS styles to a specific component? For instance, in my App.js file, I have added Login and Register components for routing purposes. In Login.css, I have defined some CSS styles and then imported it into Login.js. T ...

Adjusting column sizes smaller than 1/12 within the Primeng flex grid in Angular 8

In using primeng grid, a common desire is to have 2 columns behave a certain way: the first column should take up 1/13 of the total space the second column should take up the remaining space. Many attempt different solutions like: <div class="p-g ...

Floating elements can be vertically aligned with spans

I am facing an issue with aligning three spans vertically inside a div. It seems simple to achieve, but vertical alignment is not working properly when I use float. My goal is to have the light blue bar vertically centered. Here is the code snippet: .co ...

Employing Modernizr along with jQuery for Animation in Cases when CSS3 Transitions are Absent

Can Modernizr and jQuery be combined to create a transition effect similar to CSS3 if it's not supported? My current approach looks like this... <div class="hoverable"> <p>This div changes both width and height on hover</p> </di ...

What is the best way to incorporate component-specific CSS styles in React?

This is the layout that I am attempting to replicate (originally from react-boilerplate): component |Footer |style.css |Footer.js In Footer.js, the styles are imported in a very elegant manner like this: import React from 'react'; im ...

Tips for effectively utilizing the :valid pseudo-class in Material-UI components to dynamically alter the border styling

Currently, I am attempting to adjust the border color of Mui TextFields using createTheme from Mui v5 when the input is valid. The border itself is defined within the ::before and ::after pseudoclasses. For the TextFields, I am utilizing variant="stan ...

What methods can I use to prevent multiple calls to isValid in this particular jQuery validation scenario?

I am currently working on validating a field with the following requirements: No validation when the user first lands on the page Validation triggers when the user clicks on the Name Query field, and it validates on both key up and focus out events The f ...