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

The alternating colors in the sorting table are not visible due to the divs being hidden with the display set

I've come across a problem that has me stumped. So, there are two sorting filters on a table and one of them hides rows that don't apply, messing up the alternating colors. Take a look at the function that sorts and the CSS below. The issue is pr ...

Tips for designing a flexible structure with 2 columns, each containing an additional 2 columns within

Is it possible to create a 4-column responsive layout? | column left | column right | column left| column right| I would like to achieve a single row layout on larger devices as shown below | column left | column right | column left| column right| an ...

Ensure that the Div element is able to wrap lines in the same way that the Span element

Is there a method to ensure that a div element wraps the lines similar to how a span does? JSFiddle for testing In this example, I utilized the span tag to create an appearance as if the text was highlighted by a yellow highlighter pen. Everything functi ...

Unable to add texture graphics to the box element in an A-Frame simple environment

Currently, I am experimenting with the demonstration of a Basic Scene in A-Frame. However, when I reach the section about Applying Image Texture and Using Asset Management System, I encounter an issue where the suggested texture does not display as expect ...

What is the best way to connect information from an HTML input field to a JavaScript object with the help of AngularJS?

As a beginner in AngularJS, I'm struggling to find the best approach to achieve my goal. I aim to create a grid of input tags with type=number in my HTML and have it set up so that whenever the value is increased, a new object is added to a list. Simi ...

Conceal the div element if the value exceeds 0

I'm looking for a way to display a div when the number of remaining days is less than 0. I got it working on jsfiddle, but for some reason, it doesn't work anywhere else. if ($('.daysrem&a ...

Learn how to dynamically alter the background color of a webpage by utilizing a JSON API for random color selection

My goal is to trigger a javascript function onload that will dynamically change the background color of the webpage using random colors fetched from a JSON file. ...

Adjusting the dimensions of two pictures side by side using Bootstrap 3

I've been experimenting with Bootstrap to align two images side by side using different columns. I made sure they were aligned properly and even added some background colors for better visibility. However, when I tested the responsive design by chang ...

When anchor links (href) are incorporated within a flex layout and flex-direction is set to row, they may not function as

I've designed a website with three horizontal pages, each filling the entire screen. There's a fixed menu with links that scroll to specific pages when clicked. However, users can also scroll horizontally to navigate between screens. If two page ...

Creating horizontal cards with Angular MaterialWould you like to learn how to design

I need help converting these vertical cards into horizontal cards. Currently, I am utilizing the Angular Material Cards component. While I can successfully create cards in a vertical layout, my goal is to arrange them horizontally. Below is the code sni ...

Having troubles with angular due to doodle throwing errors?

https://codepen.io/tuckermassad/pen/rPYNLq I took the CSS doodle code from the above link and incorporated it into my angular component: <section class="main"> <css-doodle grid="5"> :doodle { @grid: 10 / 100%; } ...

"The sliding function of the React Bootstrap carousel is malfunctioning as it goes blank just before transitioning

Here is the code snippet I am working with: Whenever the carousel transitions to the next image, the current image disappears before displaying the next one. I am using react-bootstrap version 5.1.0, but it seems like there may be an issue with the transi ...

Exploring discrepancies between two tables with the power of Javascript

const firstTable = document.getElementById('table_1') const secondTable = document.getElementById('table_2') const rows1 = firstTable.rows const rows2 = secondTable.rows for (let i = 0; i < rows1.length; i++) { for (let x in rows ...

Comparison of utilizing PHP within CSS versus Implementation through JavaScript [General]

Curious about the incorporation of PHP in CSS, especially in relation to my current Wordpress Theme project. I am aiming for high levels of customization and have been using a JS file to change CSS Properties through PHP. However, I'm not entirely con ...

Fixed-top navbar in Bootstrap obscuring webpage contents

I have tried various solutions to the issue at hand, but nothing seems to be working. I applied padding-top to the container of my main content, but when I add the following CSS: @media (max-width: 768px) { body{ padding: 0; } } it overrides the padd ...

What is the best way to conceal an element when another element is given a specific class?

Imagine you have 2 buttons The first button <button class="arguments variation-one">Some text</button> - this button has dynamic classes like: variation-one, variation-two, variation-three, but the .arguments class remains static. a ...

Ways to Retrieve JavaScript Variable inside HTML Tags in a JSP

I am currently facing a requirement where I must assign js variables to html input tag ids. For example: <input type='text' id='(here I need js variable)'/> I am aware that one way to achieve this is by creating the entire elem ...

Strip inline styles from HTML content in django-tinymce

I am experiencing an issue with django-tinymce where it automatically adds inline styles when I save content. I would prefer to remove this behavior and only save the content in plain HTML. Can someone assist me with resolving this problem? ...

I am looking to enhance a button's appearance by applying CSS styling with a combination of two distinct flat colors

Is there a way to apply CSS styling to a button by incorporating two distinct flat colors? Specifically, I would like the left half of the button to be blue and the right half to be red. ...

What could the possible reason behind the malfunctioning of my prepared statements for the insert operation?

I recently discovered that using prepared statements is a more effective method than using deprecated ones. Despite my best efforts and online tutorials, I haven't had any luck getting this to work or finding the problem. That's why I'm her ...