Is there a way to align one <article> next to another <article> to create a horizontal layout?

Can anyone help me figure out how to display two <article>s on the same line or side-by-side, with the FirstArticle positioned to the left and the SecondArticle to the right?

.MainContent {
    border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    overflow: hidden;
    line-height: 30px;
}

.Content {
    float: left;
    border-radius: 5px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    width: 48%;
}

.FirstArticle {
    margin-left: 10px;
    margin-top: 30px;
}

.SecondArticle {
    margin-left: 10px;
    margin-top: 30px;
}
<div class="MainContent">
    <div class="Content">
        <article class="FirstArticle">
            <header class="Header">
                <h2>First Article</h2>
            </header>
            <footer class="Footer">
                <p class="post-info">some text for the footer..
                </p>
            </footer>
            <content>
                <p><q>All good people agree,
And all good people say,
All nice people, like Us, are We
And every one else is They:
But if you cross over the sea,
Instead of over the way,
You may end by (think of it!)
                    looking on We
As only a sort of They!</p></q>
            </content>
        </article>
        <article class="SecondArticle">
            <header class="2Header">
                <h2>Second Article</h2>
            </header>
            <footer class="Footer">
                <p class="post-info">some text for the footer..
                </p>
            </footer>
            <content>
                <p><q>All good people agree,
And all good people say,
All nice people, like Us, are We
And every one else is They:
But if you cross over the sea,
Instead of over the way,
You may end by (think of it!) looking 
                    on We
As only a sort of They!</p></q>
            </content>
        </article>
    </div>
</div>

Answer №1

To start, you must apply the float:left property to both articles.

Next, specify a width for each article, such as width:100px.

The final CSS code would look like this:

.FirstArticle {
    margin-left: 10px;
    margin-top: 30px;
    width: 100px;
    float: left;
}
.SecondArticle {
    margin-left: 10px;
    margin-top: 30px;
    width: 100px;
    float: left;
}

Answer №2

One solution is to apply the float property directly to the articles (not their container) while setting a specific width limit:

The key code snippet looks like this (with additional details included below):

.Content {
  width: 96%;
}
.FirstArticle, .SecondArticle {
  float: left;
  width: 47%;
}

.MainContent {
  border-radius: 5px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  overflow: hidden;
  line-height: 30px;
}
.Content {
  border-radius: 5px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  width: 96%;
}
.FirstArticle {
  margin-left: 10px;
  margin-top: 30px;
  float: left;
  width: 47%;
}
.SecondArticle {
  margin-left: 10px;
  margin-top: 30px;
  float: left;
  width: 47%;
}
<div class="MainContent">
  <div class="Content">
    <article class="FirstArticle">

      <header class="Header">
        <h2>First Article</h2>
      </header>

      <footer class="Footer">
        <p class="post-info">some text for the footer..
        </p>
      </footer>

      <content>
        <p><q>All good people agree,
And all good people say,
All nice people, like Us, are We
And every one else is They:
But if you cross over the sea,
Instead of over the way,
You may end by (think of it!)
                    looking on We
As only a sort of They!</q>
        </p>
      </content>

    </article>

    <article class="SecondArticle">

      <header class="2Header">
        <h2>Second Article</h2>
      </header>

      <footer class="Footer">
        <p class="post-info">some text for the footer..
        </p>
      </footer>

      <content>
        <p><q>All good people agree,
And all good people say,
All nice people, like Us, are We
And every one else is They:
But if you cross over the sea,
Instead of over the way,
You may end by (think of it!) looking 
                    on We
As only a sort of They!</p></q>
      </content>
    </article>
  </div>
</div>

Answer №3

When utilizing the flexbox method:

To create a flex-container, incorporate display: flex; in your .content class.

.content {
  display: flex;
}

This action will designate every direct child as a flex-item, such as your article elements. Apply flex: 1; to these elements to occupy all available space within the flex-container.

.first-article {
  flex: 1;
}
.second-article {
  flex: 1;
}

or

.content article{
  flex: 1;
}

flex: <positive-number> is equivalent to

flex: <positive-number> 1 0
. This makes the flex item flexible and sets the flex basis to zero, resulting in an item that receives the specified proportion of the free space in the flex container. When all items in the flex container follow this pattern, their sizes will be proportional to the specified flex factor.


Code Snippet:

.main-content {
  border-radius: 5px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  overflow: hidden;
  line-height: 30px;
}
.content {
  border-radius: 5px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  width: 48%;
  display: flex;
}
.first-article {
  margin-left: 10px;
  margin-top: 30px;
  flex: 1;
}
.second-article {
  margin-left: 10px;
  margin-top: 30px;
  flex: 1;
}
<div class="main-content">
  <div class="content">
    <article class="first-article">

      <header class="header">
        <h2>First Article</h2>
      </header>

      <footer class="footer">
        <p class="post-info">some text for the footer..
        </p>
      </footer>
      <section>
        <p><q>All good people agree,
And all good people say,
All nice people, like Us, are We
And every one else is They:
But if you cross over the sea,
Instead of over the way,
You may end by (think of it!)
                    looking on We
As only a sort of They!</q>
        </p>
      </section>

    </article>

    <article class="second-article">

      <header class="second-header">
        <h2>Second Article</h2>
      </header>

      <footer class="footer">
        <p class="post-info">some text for the footer..
        </p>
      </footer>

      <section>
        <p><q>All good people agree,
And all good people say,
All nice people, like Us, are We
And every one else is They:
But if you cross over the sea,
Instead of over the way,
You may end by (think of it!) looking 
                    on We
As only a sort of They!</q>
        </p>
      </section>
    </article>
  </div>
</div>


Notes:

  • Avoid using the <content> element as it is outdated, refer to this resource.
  • Exercise caution with your CSS selectors. Although CSS syntax is case-insensitive, using certain formats may cause compatibility issues in some browsers. Additionally, classes cannot begin with a number unless properly escaped.

Answer №4

To achieve the desired layout, utilize display flex.

Feel free to refer to this code snippet for assistance:

.MainContent {
  border-radius: 5px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  overflow: hidden;
}
.Content {
  width: 100%;
  display: flex;
  flex-direction: row;
}
<div class="MainContent">
  <div class="Content">
    <article class="FirstArticle">

      <header class="Header">
        <h2>First Article</h2>
      </header>

      <footer class="Footer">
        <p class="post-info">some text for the footer..
        </p>
      </footer>
      <content>
        <p>
          <q>All good people agree,
And all good people say,
All nice people, like Us, are We
And every one else is They:
But if you cross over the sea,
Instead of over the way,
You may end by (think of it!)
                    looking on We
As only a sort of They!</q>
        </p>
      </content>

    </article>

    <article class="SecondArticle">

      <header class="2Header">
        <h2>Second Article</h2>
      </header>

      <footer class="Footer">
        <p class="post-info">some text for the footer..
        </p>
      </footer>

      <content>
        <p><q>All good people agree,
And all good people say,
All nice people, like Us, are We
And every one else is They:
But if you cross over the sea,
Instead of over the way,
You may end by (think of it!) looking 
                    on We
As only a sort of They!</q>
        </p>
      </content>
    </article>
  </div>
</div>

Hopefully, this information proves helpful.

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

Having trouble adjusting the height of <a> elements in the navbar

My navbar has an issue where the listed items do not fill the entire height of the navbar. I have tried adjusting padding and margin, but it doesn't seem to be affecting the layout. Any suggestions would be greatly appreciated. Thank you. #navbar { ...

Using jQuery, you can easily insert a <span> tag around selected text and then save this modification permanently in a local HTML file

I have compiled notes in an HTML file stored on my local computer, with the intention of keeping it solely for personal use. For instance, I have a snippet like this: <p> this is an example text</p> My goal is to highlight the word "example" ...

The input values are displayed in a continuous line without any breaks between them

I'm currently learning ReactJS and I have created a simple example to practice. The goal is to display each input value (separated by whitespace) in an HTML element (<h1>). However, instead of showing each output value individually, they disappe ...

Clicking on an iframe activates the loading of the displayed page

I'm attempting to create a functionality where clicking on an iframe will load the page it is displaying. I experimented with placing it within an tag, but that didn't produce the desired result. The effect I'm aiming for is similar to zoom ...

Does every controller page need to verify the Login Function?

In my PHP pages, I have implemented the MVC Pattern by creating a controller page for each view page to interact with the Model page. However, I have only checked user login at the top of every view page and not in the controller page. This leaves a potent ...

Navigating with hashtags in Angular2 and anchors does not change the page position

I recently came across a helpful post that showed me how to append a fragment to the URL. Angular2 Routing with Hashtag to page anchor Although the fragment is successfully added, I'm encountering an issue where the page does not scroll to the speci ...

Converting SVG circle animation into textual form - a step-by-step guide!

<svg width="1000" height="100"> <circle id="orange-circle" r="30" cx="50" cy="50" fill="orange" /> <animate xlink:href="#orange-circle" attributeName="cx" from="50" to="900" dur="2s" begin="0s" ...

The concept of CSS inheritance within div elements

I've been researching extensively about the concept of CSS inheritance, but I have come across a puzzling question that remains unanswered. Consider the code snippet below: <!DOCTYPE HTML> <html> <head> <style type="text/css"> ...

OceanWP Theme with a Fixed Navigation Bar

Currently utilizing the oceanwp theme, I'm aiming to create a topbar that vanishes while scrolling and have the menu smoothly transition to the topbar location with some opacity. I attempted the following code snippet: #site-header { position: fixe ...

What is the best way to transfer data from a div tag to an li tag using JavaScript?

https://i.stack.imgur.com/se2qk.pngI am attempting to change the div tag content to li tag. Here is a snippet of the code from inspect for reference. I need to remove the div tag with the "domTitle" class. <li style="display: inline;" id="list_name"> ...

Symfony: The Database Query Button with a Pop-up Feature

I am looking to create a button that will automatically search my database for all users with a "secouriste" attribute set and display their first name, last name, and phone number in a popup. After conducting research, here is what I have gathered: In m ...

Connect an existing function to JQuery animation

There is a function in place that changes the background image of a vertical website when a navigation menu item is clicked. <script type="text/javascript"> $(function() { $('a.panel-home').click(function(){ $("#bgimg").attr(' ...

Unable to display background image and color on Internet Explorer

The background beneath the slider and footer appears normal in most browsers, but there seems to be an issue with Internet Explorer. Check it out at this link: Do you have any suggestions on how to fix this? ...

Steps for implementing a conditional statement to handle an empty string in HTML

I need to figure out how to display a different message if my string is empty. Can anyone help me with this? <div class="padding" id="dealBorder"> <pre id="informationDealText"><pan class="inner-pre" style="font-size: 24px; color: whi ...

Employing bootstrap to include oversized images within a compact, fixed div

I am facing an issue with image responsiveness in the middle row of my layout. Despite using Bootstrap 4 and applying classes like img-fluid, the images in my #fullColumn area do not resize properly. The images are dragged into this area by users and are ...

Need to specify a parameter type for the input tag in HTML

Currently, I am developing a customized focus method for my webpage using the following script: <script type="text/javascript"> function myFunction() { document.getElementById("name").focus(); } </script> I ...

Effective ways to enable users to upload files in a React Native app

Being in the process of developing a react native app, I am faced with the challenge of allowing users to easily upload files from their mobile devices (pdf, doc, etc). Unfortunately, my search for a suitable native component has proven fruitless. Can anyo ...

JQuery .click Event doesn't center elements even with transform-origin adjustment

In the JSfiddle provided below, you can see that after a click event occurs, two span (block) elements rotate 45deg to form an "X". However, both elements are slightly shifted left, creating an off-center "X" relative to the parent's true center-origi ...

Accessing getUserMedia within Internet Explorer 11 or utilizing MediaStream on IE 11

I am attempting to utilize the getUserMedia function in IE 11 with the help of the temasys plugin. However, IE does not support the MediaStream instance returned by getUserMedia. Below is a snippet of my code: import React from 'react' import { ...

Tips for Importing HTML Table Data Line by Line into SQL Table

I have the following code here. I am looking to insert HTML table data row by row into an SQL database. However, with this current code, only the last row is being stored in the database table. I believe I need to implement a loop to read the data from e ...