Two div elements refusing to be positioned side by side

I'm struggling to position two div elements next to each other, with a third one to be added later. I've experimented with floating, display, and positioning options, but nothing seems to work. I've even copied code from other websites, but it still doesn't align as intended.

HTML:

<div class="diensten">
    <div id="dienst1">
        <h2>Ontruimingsoefening</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut varius orci ac velit sodales, nec lacinia eros aliquam. Phasellus consectetur interdum ligula, quis faucibus libero faucibus eu. </p>
        <img src="photos/photo1.jpg" alt="Photo 1">
    </div>

    <div id="dienst2">
        <h2>Beheer brandmeldinstallatie</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut varius orci ac velit sodales, nec lacinia eros aliquam. Phasellus consectetur interdum ligula, quis faucibus libero faucibus eu.</p>
        <img src="photos/photo2.jpg" alt="Photo 2">  
       </p>
    </div>
 </div> 

CSS:

.diensten h2 {
    padding-top: 40px;
}

.diensten p, h2 {
    font-family: Arial, sans-serif;
    padding: 20px;
}

.diensten {
    position: relative;
    display: inline-block;
    height: 500px;
    background-color: #FFFFFF;
    margin: auto;
}

#dienst1 {
    margin-left: 90px;
    height: 500px;
    width: 500px;
    box-shadow: 0px 0px 30px 0px #000;
}

#dienst2 {
    margin-left: 900px;
    height: 500px;
    width: 500px;
    box-shadow: 0px 0px 30px 0px #000;
}

#dienst2 img{
    height: 300px;
    width: 450px;
}

Answer №1

To ensure that #dienst1 and #dienst2 are displayed side by side, you can add display: flex to the parent container. Additionally, consider removing the large margin-left on #dienst2, although it is not necessary for alignment. Any other elements placed adjacent to them will also be displayed in a row.

.diensten h2 {
    padding-top: 40px;
}

.diensten p, h2 {
    font-family: Gotham, "Helvetica Neue", Helvetica, Arial, "sans-serif";
    padding: 20px;
}

.diensten {
    position: relative;
    height: 500px;
    background-color: #FFFFFF;
    margin: auto;
  display: flex;
}

#dienst1 {
    margin-left: 90px;
    height: 500px;
    width: 500px;
    box-shadow: 0px 0px 30px 0px #000;
}

#dienst2 {
    height: 500px;
    width: 500px;
    box-shadow: 0px 0px 30px 0px #000;
}

#dienst2 img{
    height: 300px;
    width: 450px;
}
<div class="diensten">
  <div id="dienst1">
    <h2>Emergency Evacuation Drill</h2>
    <p>We conduct various emergency evacuation drills tailored to healthcare facilities, educational institutions, daycare centers, restaurants, and retail stores. These drills can be conducted both theoretically (Table Top) and practically using professional equipment to make the drill as realistic as possible. </p>
    <img src="Photos/IMG_2670.JPG" data-tilt data-tilt-max="30" data-tilt-speed="400" data-tilt-perspective="900">
  </div>

  <div id="dienst2">
    <h2>Fire Alarm System Management</h2>
    <p>We handle the mandatory monthly and quarterly management tasks of the fire alarm system according to NEN 2654 in a professional manner for healthcare facilities, educational institutions, daycare centers, restaurants, office buildings, and retail stores. The maintenance of the fire alarm system includes testing the reporting to the fire department from automatic smoke detectors and manual call points, testing the reporting of faults in the fire alarm system, a visual inspection of connected components, updating and maintaining the logbook.
      <img src="Photos/IMG_2704.JPG">
    </p>
  </div>
</div>

Answer №2

To optimize the layout, include display:inline; and eliminate any margins

#dienst1 {
    margin-left: 90px;
    height: 500px;
    width: 500px;
    box-shadow: 0px 0px 30px 0px #000;
    display:inline;
}

#dienst2 {

   height: 500px;
   width: 500px;
   box-shadow: 0px 0px 30px 0px #000;
   display:inline;
}

Answer №3

Initially, divs are block level elements which means they each appear on their own lines. There are various ways to position two divs side by side based on the desired effect you want to achieve. Here are a few options:

  • Use float property to float them (float: left)
  • Make them inline block using display: inline-block
  • Utilize flexbox by applying display: flex to their parent element
  • Turn them into grid items by using display: grid on their parent (note: this is a newer feature and may require fallbacks)

If you're looking to enhance your CSS layout skills, exploring different methods is key as each serves a unique purpose. A good starting point would be checking out resources like learnlayout.com. For more in-depth knowledge, consider delving into my book.

Answer №4

If you plan to place 3 divs side by side, it's important to reduce the width of each individual div.

Having each one set at 500px won't work unless your target audience is using large desktop screens;

So, I adjusted the width and height to be 300px for all divs, with overflow set to scroll on the y-axis.

If you prefer not to have the scroll feature, simply remove 'overflow-y: scroll;' and any related stylings, while ensuring the height is specified so that all content remains visible)

For a better view, click on Fullscreen mode (Stackoverflow may force a resize)

Answer №5

To achieve the desired layout, you can apply the following CSS:

position: relative;
display: inline-block;

to the child div elements, and

position: absolute;

to the parent div. Here's a functional example with three paragraphs displayed side by side. The width has been reduced for better visibility within a small window. Feel free to adjust margins as needed.

.services h2 {
    padding-top: 40px;
}

.services p, h2 {
    font-family: Gotham, "Helvetica Neue", Helvetica, Arial, "sans-serif";
    padding: 20px;
}

.services {
    position: absolute;
    display: inline-block;
    height: 500px;
    background-color: #FFFFFF;
    margin: auto;
}

#service1 {
    margin-left: 90px;
    position: relative;
    display: inline-block;
    height: 500px;
    width: 100px;
    box-shadow: 0px 0px 30px 0px #000;
    overflow: hidden;
}

#service2 {
    margin-left: 90px;
    position: relative;
    display: inline-block;
    height: 500px;
    width: 100px;
    box-shadow: 0px 0px 30px 0px #000;
    overflow: hidden;    
}

#service3 {
    margin-left: 90px;
    position: relative;
    display: inline-block;
    height: 500px;
    width: 100px;
    box-shadow: 0px 0px 30px 0px #000;
    overflow: hidden;    
}

#service2 img{
    height: 300px;
    width: 450px;
}
<div class="services">
    <div id="service1">
        <h2>Evacuation Drill</h2>
        <p>We conduct various evacuation drills tailored for healthcare facilities, educational institutions, daycare centers, restaurants, and retail stores. These drills can be conducted in both theoretical (Table Top) and practical settings using professional equipment to create a realistic experience.</p>
        <img src="Photos/IMG_2670.JPG" data-tilt data-tilt-max="30" data-tilt-speed="400" data-tilt-perspective="900">
    </div>

    <div id="service2">
        <h2>Fire Alarm System Management</h2>
        <p>We provide monthly and quarterly management tasks for fire alarm systems according to NEN 2654 standards for healthcare facilities, educational institutions, daycare centers, restaurants, office buildings, and retail stores. Maintenance includes testing automatic smoke detectors and manual call points for fire brigade notification, testing fault notifications from the fire alarm system, visual inspection of connected components, and updating the maintenance logbook.<img src="Photos/IMG_2704.JPG"></p>
    </div>

    <div id="service3">
        <h2>Fire Alarm System Management</h2>
        <p>Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text ...
        <img src="Photos/IMG_2704.JPG"></p>
    </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

Tips for accessing information from a FOR loop within DIV tags

Let's explore the following code snippet: Here is the HTML generated: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Website ...

obtaining data from an HTML input field

I'm struggling to retrieve the value from an HTML input element, and despite my own research, it seems like I have the correct syntax. However, I'm still unable to get it to work, leaving me confused. When I log the value to the console, it appe ...

A step-by-step guide on showcasing a Word document in a popup window using MVC 4 Razor

I'm looking to display a Word file in a pop-up window using MVC 4 Razor. The file is located at a specific path and has already been stored in my project. ...

Generating HTML widgets dynamically with jQuery: A step-by-step guide

Looking for a way to display an interactive widget on your page while allowing users to generate multiple instances and interact with them simultaneously? Imagine having a widget like this: <div id="my_widget"> <script type="text/javascript" ...

How to use CSS to dynamically adjust the maximum width of an overflow container based on its children's content

I have a container with an overflowing <div> that displays multiple <ul> <li> lists. Each list always contains the same number of bordered <li> items, resembling a table. However, there may be instances where a <ul> has only ...

When the parent element is already set to justify its content with flexbox, you can ensure equal heights for CSS classes by utilizing

Within my design, I have several rows with 3 columns each. Every column contains a main content section labeled SavedSearchBox-content (with various permutations that affect its height) and a footer labeled SavedSearchBox-footer. To structure this layout, ...

Are external JavaScript files cached in the same manner as images?

Is it possible for a single pagehandler script to be called from the browser cache when navigating between multiple pages that refer to it? ...

Issue with CSS on various mobile devices

Using this CSS to display a fixed part at the bottom of the mobile screen, I noticed that it works well on the Samsung Grand Prime but appears narrower on the Samsung J7 Max. Here is the code snippet and images for reference: .footer { position: fixed ...

The HTML5 audio player is experiencing technical difficulties on Internet Explorer

I'm having an issue with html5 audio not working on Internet Explorer. <audio id="beep-voice" controls autoplay style="opacity:0"></audio> Could someone please provide a solution? Thanks! ...

Styling PHP Input Buttons with CSS

I'm facing an issue with styling the button on a working form. Even though I added CSS and declared the button class, it's not applying the styles. The CSS is defined at the top of the HTML file, and the PHP code is embedded within. Both classes ...

When a user chooses an item, the ui-select dropdown appears hidden behind additional fields on the screen

In my AngularJS application, I am utilizing ui-select within a table that repeats rows using ng-repeat. The following code snippet displays how ui-select is implemented: <ui-select name="{{'selProperty' + $index}}" ng-model="thing.property" ...

"Encountered an Ajax Error while appending request parameters to the same link during a

I can't figure out why my ajax request is sending to the same link instead of the specified url. It's only this ajax request on the entire page that is behaving strangely. Can anyone shed some light on this issue? $(document).ready(function(){ ...

Avoiding page breaks in Puppeteer PDFs

I'm facing an issue with my HTML code where my tabular data is splitting across two pages when converting to PDF using puppeteer. I want to prevent the table from splitting across pages. <table> <tbody></tbody> </table> Despit ...

Utilizing Ant Design Icons without an Internet Connection

In my current project, a reactJS application utilizing ant design for the UI was recently deployed to production on locked down computers. These computers lack internet access, causing ant design icons on modals to appear as empty boxes. Upon investigation ...

The jQuery panel slider magically appears when you click the button, but refuses to disappear

One issue I am facing on my webpage involves a button that triggers a right panel to open using the jquery and modernizr frameworks. The button is positioned at the far right of the screen. When clicked, it slides to the left along with the panel it reveal ...

Adjust the size of the cursor

Currently, I am in the process of creating a drawing application akin to Paint or Sketchpad. One issue I have encountered is the inability to resize the cursor based on the line width of the pencil I am using. The workaround I have discovered involves usin ...

Total Output Calculation

In my latest coding project, I have crafted a unique algorithm to calculate exam scores with the inclusion of interactive buttons! function incorrectResponse() { var calc = 0; var calc2 = 1; var divElement = document.createElement("div"); divEle ...

Is it possible to obtain the hostname through CSS?

.trPic{ width:640px; height:480px; position:absolute; top:0px; left:0px; width:640px; height:480px; background: url(http://192.168.2.150:8090/?action=stream) no-repeat; border:0px solid #000000; } Is it possible to include a hostname in CSS? For ...

"Media queries in CSS don't consistently apply all styles when reaching the minimum

The CSS styles I am using are as follows: @media (min-width: 1280px) { .window-padding { padding-top: 20px; padding-bottom: 20px; padding-left: 30px; padding-right: 30px; } } @media (min-width: 769px) { .windo ...

How can I align a single button to the left side while positioning the rest of the elements to the right in Bootstrap 4?

I am attempting to align the B1 button on the left side and have the remaining elements positioned on the right side. Below is my code snippet using Bootstrap 4.1: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/b ...