Place two identical DIVs next to each other on the same line with a gap in between

I'm struggling to place two identical divs side by side on the same row, covering the entire width with some space between them. However, using margin impacts both divs due to their shared class, causing the second div to drop below. I've attempted solutions like overflow:hidden and adjusting size, but nothing works. Adding borders with overflow hidden also doesn't resolve the issue.

The only workaround is to create multiple divs with the same class, despite needing them to span the full width of the row.

Edit: Here's a simplified example of my dilemma:

<div class="container">
    <div class="block">content...</div>
    <div class="block">content...</div>
</div>

<style>
.container{width:1000px; margin:0px auto;}
.block{width:480px;height:500px;float:left;}
</style>

I aim to position the first block followed by a 40px gap, then the second block.

Answer №1

To ensure that the two .block divs appear on the same row, it is recommended to use percentages instead of pixels.

Here is an example of how you can achieve this:

Assign an id of 'right' to the div you want on the right, and an id of 'left' to the one you want on the left:

<div class="container">
<div class="block" id="right">content...</div>
<div class="block" id="left">content...</div>
</div>

Then style it with:

<style>
.container{width:1000px; margin:0 auto;}
.block{width:48%;height:500px;display:inline;}
#left{float:left;}
#right{float:right;}
</style>

You can adjust the exact width percentage to fit your design preferences.

Answer №2

If you need to manipulate two elements separately, it's a good idea to assign them distinct class names or ids.

<div class="container">
<div class="block1">content...</div>
<div class="block1">content...</div>
</div>

To display them side by side, you can use inline display property.

.block1, block2 {
display: inline;
}

After that, you have the flexibility to style each element differently by targeting their respective classes.

Answer №3

Are you looking for something similar to this?

http://jsfiddle.net/justin_thomas/9S46N/

The CSS:

.myRow {
   width:48em;
}
.myclass {
   padding: 1em;
   margin-left:1em;
   margin-right:1em;
   float: left;
   display:inline;
   width: 20em;
}

The HTML:

<div class="myRow">
  <div class="myClass">Blah... blah...</div> 
  <div class="myClass">Blah... blah...</div> 
</div>

In this setup, I've used floats to achieve the desired layout. However, one downside is that you need to specify the width of the row's container in physical units or one of its parent elements. Additionally, you must know the number of columns in advance to determine the width amount in the class with the divs.

This solution can be cumbersome as you have to ensure that the total width of each div (including margins) does not exceed the width of the row container.

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

Accessing CSV data stored externally using JavaScript

Hi there, I am struggling to load external CSV data into a script due to what I believe is the browser's same origin policy restriction. I came across some information about using cross-document messaging as a workaround, but I have no idea how to go ...

Conflict between jquery and hoverIntent libraries

I've encountered an issue with a website that was functioning properly until we added a new form to a specific page. The form, created by another person, utilizes javascript/jQuery for processing. Since adding this form, it has caused the majority of ...

Troubleshooting issues with CSS dropdown menu in Internet Explorer and Chrome

I am encountering an issue with my dropdown menu not functioning properly in Internet Explorer 10, Chrome, and compatibility mode. Surprisingly, it works flawlessly in the latest version of Firefox. CSS: #menu_items { float: left; width: 600px; } #me ...

"Resolving the issue with unnecessary line breaks in Optimizepress when input is contained within a paragraph

I am experiencing an issue with the code on my server: <p>This is a test to determine why the <input type="text" style="text-align: center;" value="input"/> element appears on a new line when displayed on a wordpress page.</p> When view ...

Seeking help with a problem regarding the Tooltip Effect not displaying over a button

I'm currently struggling with implementing a tooltip for the "Back-end" button on my webpage. Despite my efforts, the tooltip effect fails to display over the button, and I'm at a loss as to why. Below is the code snippet I am working with: < ...

What are the reasons and methods for storing multiple images within a single image?

Lately, I've observed a trend where websites are consolidating multiple images into one large image, similar to Google's homepage. Although we see many small images on the left side, it is actually just one single image: I am curious about how ...

Are CSS3 animations limited to working only with Webkit?

Trying to create a CSS3 animation featuring a puzzle piece on my website. Below is the code I'm using. Strangely, the animation only seems to be working on Safari and Chrome. Any tips on how to make it compatible with Firefox and Opera? Appreciate you ...

Maintain mouse attention through CSS

Currently, I am facing an issue with my login screen involving the mouseenter and mouseleave events. I have a gif image that disappears when the mouse hovers over it and reappears when the cursor moves to a different part of the page to allow users to ente ...

Changing the Row's Background Color in Angular When the Button is Clicked

My code includes a list where each row has a button to open a sidebar. Due to the large number of elements in the list, I want the background color of the row to turn yellow when the button is clicked, as a way to indicate what has been selected. Currently ...

Enable scrolling exclusively for the content within a tab, without affecting the tab label in Clarity Tabs

I have a tab with lengthy content in my project (check out the StackBlitz reference here). This causes the appearance of a scroll. https://i.sstatic.net/ZcNKM.png The corresponding code snippet is provided below: <div class="content-container&qu ...

tips for stopping links from affecting their descendent styles

During my website redevelopment, I want to include simple links in the menu for users to easily open them in new tabs. Currently, a menu item looks like this: <li class='menu-left-row' id='messages' onclick=\"javascript:showscr ...

Block elements such as divs are displayed first before inline elements on a webpage

I have multiple child divs inside a parent div. I want the child divs to stack vertically (block) within the height of the parent div, but if they exceed the height, I want them to align horizontally (inline). I have provided an image and included my code ...

How to center an item in a Bootstrap navbar without affecting the alignment of other right-aligned items

Currently, I am in the process of designing a website and have implemented a Bootstrap navbar. The navbar consists of three icons aligned to the right, and I am looking to center another element. However, when I use mx-auto, the element does not center per ...

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 ...

Unique rephrased text: "Amongst a backdrop devoid of white,

In a div with a colored background, I noticed that adding an input element results in an unsightly border. <div class="input-group"> <input type="search" style="height:30px;"> </div> Is there a way to remove this border? ...

Immediate family members nearby are not an option. We will have to create the form dynamically

On a previous page, there is a form that allows users to input data and define the number of "Attributes" they want to assign to a device by clicking on a button. Users are prompted to specify a name and type for each attribute. If the user selects "Selec ...

Creating a visual that when clicked, reveals an enlarged version at a different location

Is there a way to make an image appear in a different location on the page when it's hovered over? I've searched online but couldn't find a solution using just HTML and CSS. Does anyone know how to achieve this effect? Image not hovered: ht ...

Unique ways to serialize an HTML element efficiently: JavaScript tricks

Is there a way to store a reference of an HTML tag for future use? For instance, if I click on a div and save a pointer to that div in JavaScript, is it possible to serialize this pointer and then de-serialize it to use in another part of the web applicat ...

Tips for notifying a user about leaving a page without saving their information

I created a small table where users can input product names and numbers. My question is, how can I notify the user if they try to leave the page without saving the entered information? <form action="index.php" method="post"> <input type="text" ...

What is preventing my code from being recognized as a valid XML file?

Encountering an error indicating that this code snippet is not valid within an XSL file. This is the only feedback provided by the document system. While this code is a part of a larger document, I decided to isolate the issue to determine if it lies in ...