Ensuring even distribution of three divs within a container

Imagine a container that is 1200px wide, with three divs inside. Each div is only 292px wide. The first div should align with the left margin, the third div with the right margin, and the second div should sit right in the middle of them. Adding to the complexity, when viewed on mobile devices, the container will be reduced to 320px and all three divs should line up vertically underneath each other. Here is the current CSS for each div:

.test1 {
    float: left;
    width: 292px;
    background-color: #F2F2F2;
    margin: 0 4px 5px;
    border: 1px solid grey;
    line-height: 0;
}

Answer №1

To ensure consistent alignment, apply text-align:justify to the container. This approach eliminates the need to calculate percentage widths for individual elements within the container.

Check out the FIDDLE

<div class="container">
    <div class="test1"></div>
    <div class="test1"></div>
    <div class="test1"></div>
</div>

CSS

.container {
    width: 1200px;
    text-align: justify;
}
.container:after {
    content: '';
    display: inline-block;
    width: 100%;
}
.container .test1{
    display: inline-block;
    width:292px;
    background-color:#F2F2F2;
    margin:0 4px 5px;
    border:1px solid grey;
}

Answer №2

The desired layout can be achieved in various ways depending on the structure of your HTML code.

Using float:

  <tag left   :floatleft  />
  <tag right  :float:right/>
  <tag center :margin:auto/>

Utilizing display:flex;

<parent 
       style="display:flex;justify-content:space-between;">
  <child left   />
  <child center />
  <child right  />
</parent>

By using @media queries, you can change the layout from rows to columns when the width is insufficient for all three elements: DEMO

section {
  display:flex;
  justify-content:space-between;
}
article {
  width:292px;
  background:green;
}
@media all and (max-width:900px) {
  section {
    flex-direction:column ;
  }
  article {width:100%;
  }
}
<section>
  <article> 292px width</article>
  <article> 292px width</article>
  <article> 292px width</article>
</section>

Alternatively, using display:inline-block:

<parent 
       style="text-align:center">
  <child left    style="display:inline-block"/>
  <child center  style="display:inline-block"/>
  <child right   style="display:inline-block"/>
  <pseudo-tag    style="display:inline-block;width:100%"/><!--this can be either a pseudo element or a neutral tag in HTML to enhance compatibility for IEs<8 -->
</parent>

For easy implementation and resizing of three boxes with 292px width, check out this CodePen: http://codepen.io/gc-nomade/pen/nrbDl

Answer №3

Does this meet your needs?

.wrapper {
    width: 1200px;
}
.box1 {
    float:left;
    width: 24.333333333%;
    height: 200px;
    background-color:#F2F2F2;
    margin-left:13.3%;
    border:1px solid grey;
    line-height:0;
}

.box1:first-child {
    margin-left: 0;
}

jsfiddle

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

Use JavaScript to create a new window and load the HTML content from an external URL

Just starting out with HTML and Javascript I'm trying to use JavaScript to open a window and load content from an external source. I attempted using document.write(), but it only works when I hardcode the HTML as input. Any suggestions on how to get ...

Is the phrase "This site was built with Wix" visible on the website?

Recently, I've begun using Wix to build a website. I'm curious - does the message 'This website is created using Wix' appear at the top and bottom of the page when it's published? 'This website is created using Wix' I ...

Mastering Instagram Automation: Techniques for Navigating the Lightbox with Selenium

I am in the process of developing a Python script that assists users in Instagram engagement groups by efficiently liking everyone's photo during each round. I am facing an issue where Selenium is unable to click on the first photo when I reach a user ...

Ways to declare a function within the events onMouseOver and onMouseOut

<div align="right" style="border:1 #FF0 solid; background-color:#999" onMouseOver="javascript: function(){this.style.backgroundColor = '#DDD';}" onMouseOut="javascript: function(){this.style.backgroundColor = '#999';}"> Register & ...

Changing the border of an iframe element using jQuery or Javascript from within the iframe itself

Is it possible to set the border of an iframe to zero from within the iframe itself using JavaScript or jQuery? Any guidance on how this can be achieved would be greatly appreciated. Thank you! ...

Tips for rearranging button placements by utilizing multiple owl carousels across various webpages

I have successfully implemented two owl carousels on my Shopify website, and they are both functioning properly. However, I am facing an issue with the navigation buttons on the second page. The navigation buttons seem to be picking up a style that I had i ...

Is there a way to adjust the margin for a child DIV element using CSS?

Currently, I am working on my website located at www.buildinghunter.com. On the homepage, there are four div elements positioned in the bottom left corner of the page. I am attempting to align two specific divs (id=text-9 and id=text-10) with the other tw ...

What is the best way to customize the interval time for my specific situation?

I'm working on setting an interval in my app and I have the following code: HTML <div class="text"> {{currentItem.name}} </div> <ul> <li ng-repeat="item in items" ng-click="pickItem($index)">{{item.type}}</li> ...

What is the best way to eliminate the "onclick" attribute from an HTML element using JavaScript?

Is there a way to make my code only execute the onlick function after a button is pressed? I want to prevent any action from happening before that. <!-- deactivate onclick function --> <div class="boardsection2" id="2_8_7" oncl ...

Keeping the color of CSS buttons consistent can be achieved by setting specific

Here is my CSS code: .horizontalcssmenu ul{ margin: 0; padding: 0; list-style-type: none; list-style:none; } .horizontalcssmenu ul a:active{ color: #00FFFF; background: #FF0033; text-decoration: none; } /* Styles for top leve ...

Using jQuery Mobile to Recycle a Header and Navigation

I'm a beginner when it comes to jQuery Mobile and I'm struggling to grasp the concept of reusing headers and general navigation. Currently, I have a header with a menu button on the right. When the menu button is clicked, a popup shows up with l ...

Adhering button for sliding side panel

Check out my JSFiddle HERE to see what I have done. I would really appreciate it if someone could help me figure out how to make the show button float with the sidr panel :) <style type="text/css"> #panel { position: fixed; top: 50%; r ...

Redirecting script upon successful connection detection

I have created a script that checks for internet connectivity using an image, and redirects if internet is available. However, the issue is that it caches the images, leading to attempts to load them even when offline. Is there a different approach I can ...

Utilizing em units within CSS media queries is producing erratic outcomes on various browsers

My CSS media queries are set up in a progressive manner like the following: * { font-size: 14pt; } @media screen and (min-width:85em) { : : } @media screen and (min-width:110em) { : : } When I have two browsers open on the same screen, with identical siz ...

The container that utilizes the grid layout is compact and has a near-invisible height

grid-template: display; font-style: "Josefin Sans", sans-serif; size-of-text: 10px; element-height: 0px; spacing-bottom: 0px; spacing-left: 0px; spacing-right: 0px; spacing-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-up: 0 ...

Firebase has flagged the Google Authentication process with a message stating: Entry denied: The request made by this application

I have connected my domain to Firebase authentication and granted authorization for authentication access. If you want to test it out, feel free to visit this link signInWithPopup(auth, provider) .then((result) => { // This provides a Google Acc ...

Use jQuery ajax to send form data and display the received information in a separate div

I'm working on a PHP test page where I need to submit a form with radios and checkboxes to process.php. The result should be displayed in #content_result on the same page without refreshing it. I attempted to use jQuery Ajax for this purpose, but noth ...

Getting the value of "Page=?" from the href attribute in an HTML tag can be done using Selenium Webdriver and Java

I am looking to extract the value "page = ?" from a specific "href" tag in the HTML code below. I need this value for my Selenium WebDriver script so that my loop can iterate up to page 53. Can someone guide me on how to retrieve the "page =" value mentio ...

Troubleshooting problem with custom Angular directive integrating KineticJS

Hey there, I'm currently working on putting together a KineticJS application in Angular and need to resolve a minor issue. While following an example for setting up a draggable shape in AngularJS using KineticJS from this link: , I encountered this er ...

Having trouble getting the img file to show up in the img tag - the src URL is not updating

<div class="col-lg-3 col-md-4 pl-md-1"> <label class="ORGAd16-preview"> input type='file' name="modelvariant_file" id="imageUpload" accept=".png, .jpg, .jpeg" > <img id="ima ...