Trouble encountered in aligning two DIVs in a horizontal position

I have been through numerous discussions on the same problem, but none of the solutions seem to work in my case. I am currently working on a section of my website that has 3 small boxes, each containing an image on top and text below. The issue arises when I float one to the right to align them horizontally; it moves to the right but doesn't move up alongside the others.

Below is the relevant part of my code for the divs.

CSS:

#features {
background:yellow;
position: relative;
width: 100%;
padding-bottom: 50px;
padding-top: 50px;
height: 400px;
}

#features .section2 {

margin: 0 auto;
background: blue;
width: 960px;
height: auto;

}

#column_1 {
background: orange;
width: 290px;
}

#column_1 .index-icon {

 width: 290px;
}

#column_1 .index-icon img.news_image {

margin-left: 103px;
width: 90px;
height: 90px;
}

#column_1 .text {

padding-top: 30px;
}

#column_1 .text h4 {

text-align: center;
}

#column_1 .text p {

font-size: 14px;
text-align: center;
padding-top: 20px;
color: #659EC7;
} 

#column_2 {
width: 290px;
background: pink;
}


#column_2 .index-icon img.news_image {

margin-left: 103px;
width: 90px;
height: 90px;
}

#column_2 .text {

padding-top: 30px;
}

#column_2 .text h4 {

text-align: center;
}

#column_2 .text p {

font-size: 14px;
text-align: center;
padding-top: 20px;
color: #659EC7;
} 

HTML:

<section id="features">

    <div class="section2">

            <div id="column_1">
                <div class="index-icon"><img class="news_image" src="images/mail.png"></div>
                <div class="text">

                    <h4> NEWS </h4>

                    <p> We're not just great Photoshoppers also have experience developing products as entrepreneurs. This allows us to contribute to your project beyond just the design. </p>

                </div>
            </div>


            <div id="column_2">
                <div class="index-icon"><img class="news_image" src="images/mail.png"></div>
                <div class="text">

                    <h4> NEWS </h4>

                    <p> We're not just great Photoshoppers also have experience developing products as entrepreneurs. This allows us to contribute to your project beyond just the design. </p>

                </div>
            </div>

    </div>

</section>

Your help with this issue is greatly appreciated. Thank you!

Answer №1

Consider implementing the following CSS:

#features {
    background:yellow;
    position: relative;
    width: 100%;
    padding-bottom: 50px;
    padding-top: 50px;
    height: 400px;
}
#features .section2 {
    margin: 0 auto;
    background: blue;
    width: 960px;
    height: auto;
}
#column_1 {
    background: orange;
    width: 290px;
    float: left;
}
#column_1 .index-icon {
    width: 290px;
}
#column_1 .index-icon img.news_image {
    margin-left: 103px;
    width: 90px;
    height: 90px;
}
#column_1 .text {
    padding-top: 30px;
}
#column_1 .text h4 {
    text-align: center;
}
#column_1 .text p {
    font-size: 14px;
    text-align: center;
    padding-top: 20px;
    color: #659EC7;
}
#column_2 {
    width: 290px;
    background: pink;
    float: right;
}
#column_2 .index-icon img.news_image {
    margin-left: 103px;
    width: 90px;
    height: 90px;
}
#column_2 .text {
    padding-top: 30px;
}
#column_2 .text h4 {
    text-align: center;
}
#column_2 .text p {
    font-size: 14px;
    text-align: center;
    padding-top: 20px;
    color: #659EC7;
}

Include float: left; for #column1 and float: right; for #column2.

Demo.

Answer №2

It's a common issue, but there's an easy fix for the problem you're dealing with.

I made some changes in this demo, adding a class to your columns so that I could apply CSS to all of them at once.

The key is to manipulate the CSS attribute display to control how specific elements are shown on the page.

For example, a span tag is normally displayed inline, which means they line up one after the other horizontally. On the contrary, div tags are displayed as block by default, forcing them onto a new line each time.

If you want multiple divs to be shown on the same line, just add display:inline; to its CSS.

In the provided fiddle, I used display:inline-block;, which is very similar to display:inline;

For more information about the display attribute, visit this link

I hope this explanation clears things up for you!

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

Sending a request to a PHP file using Ajax in order to display information fetched from

I am attempting to display the database row that corresponds with the student's forename and surname selected from the dropdown list. I have managed to store the first name and surname in a variable called clickeditem. However, I suspect there may be ...

Prevent event propagation in jQuery by using .stopPropagation() when hovering over a

When trying to implement event.stopPropagation() in a specific scenario, I encountered an issue with a blinking background image on my submenu. To address this, I added a pseudo-element (background:green) to the parent element by toggling a new class using ...

I'm puzzled as to why a scroll bar materializes

I recently encountered an interesting issue while working on my responsive web design project. Upon resizing the browser to a width of 615px or less, a horizontal scroll bar unexpectedly appeared. It's puzzling as to which element is causing this prob ...

Icons from Material Design Lite are not appearing as expected when integrated into an Angular project

Recently, I integrated MDL into my Angular project. While most of it is functioning properly, the MDL icons seem to be giving me trouble... I've implemented them using <i class="material-icons">share</i>, but instead of displaying as an i ...

The Bootstrap modal causes the scrollbar to vanish upon closure

Despite trying numerous solutions and hacks on this issue from StackOverflow, none of them seem to work for me. Currently, I am utilizing a modal for the login process in Meteor (e.g. using Facebook login service) which triggers a callback upon successful ...

Guide on achieving horizontal scrolling in Ionic 3

Check out this image I have a list of 10 names in an ion-scroll, but they are appearing on separate lines like paragraphs. Below is my HTML code: <ion-scroll scrollX="true" style="width:100vw; height:50px" > <ion-row class="headerChip"& ...

JavaScript and CSS failing to implement lazy loading with fade-in effect

Is there a way to add the fade-in animation to an image when it is loaded with JavaScript or CSS? Currently, the code I have only fades in the image once it is 25% visible in the viewport. How can I modify it to include the fade effect upon image load? ...

The `forEach` method cannot be called on an undefined node.js

I have been developing a small study website but encountered an issue with the title. Despite extensive internet research, I have not been able to find a solution. The system I am using is Ubuntu with node.js, express, and mysql. app.js var fs = requir ...

What is the best way to load my CSS file using express.static?

How do I properly load my CSS file using express.static in Node.js? I have attempted various methods to link my stylesheet to my HTML file through Express, and I'm also interested in learning how to include images, JavaScript, and other assets to crea ...

The ideal scenarios for employing functional setState

Lately, I've been delving into the world of React, immersing myself in tutorials and explanations on how to manipulate elements in different ways. One aspect that has caught my interest is the setState function, which allows you to update or override ...

What is the best way to determine if a child component's property has changed within a method?

The child component is app-google-maps-panel and has 2 properties in the parent component: <div class="col-6"> <app-google-maps-panel [longitude]="longitude" [latitude]="latitude"></app-google-maps-panel> & ...

Converting intricate HTML table data into JSON format with the help of jQuery

Looking to extract JSON data from selected rows in the HTML table below using jQuery. The JSON output should include any user-entered comments from the textboxes. Any guidance or suggestions would be greatly appreciated. <table id="potable_grid" class ...

Verify if a <select> element exists inside the main div

Is there a way for me to check if a <select> element is present within the parent div and display certain content based on its existence? Appreciate any assistance! ...

Emphasize the content within the table cell

Currently, I am working with an HTML table that is being generated by Java. My goal is to highlight the text within a table cell without changing the background color of the entire cell. I should mention that I am aware of the option to achieve this by ma ...

Designing numerous vertical lists in HTML

I need help with displaying multiple lists vertically using HTML For example: +--------+---------------------------------------------+--+ | Global | Region Country Currency Account Type Entity | | +--------+---------------------------------------------+ ...

Alert: A notification when navigating away from your site

Is there a way to notify users when they click on an external link that they are leaving your site? <div class="row"> <div class="col-lg-12"> <div class="form-group"> *If you need information on other applicable forms, you ...

Using JQuery functions from other JavaScript files is Simple

When it comes to my CSS, I have taken the approach of creating smaller files for specific functions and then using minification to merge and compress them into one file for easier download. I want to apply the same strategy to my JS Files by logically sep ...

Is there a way to delete highlighted text without using execCommand and changing the font color

With my current use of JavaScript, I am able to highlight or bold a selected text range successfully. However, I am unsure how to undo the bold and unhighlight the text if the button is clicked again and the selected range is already bolded or highlighted. ...

Delete the option to alter button in the Jasny file input tool

I recently incorporated the Jasny Fileinput Widget into my existing form. However, I noticed that when I select a new file, it displays two buttons: Change and Remove. In this case, I believe the Change button is unnecessary and would like to remove it com ...

The HTML was generated without any styling properties assigned

After running my script on a galaxy tab, I encountered a strange issue. I created an HTML element (div) and attempted to access the style attribute, only to receive an error message indicating that style is null. Below is the code snippet: var newDiv = d ...