Proper Alignment of Div Elements

Just starting out with coding and currently practicing display and positioning. I've created a webpage with multiple divs containing the same content, displayed in a vertical scroll order. Now, I'm looking to position these divs side by side in rows of 2 as I go down the page - 2 images next to each other with their respective information below them. I've attempted this several times but haven't quite mastered it yet. Can someone please provide guidance on the best approach? Do I need to utilize a grid system for this layout? Any help would be greatly appreciated, as this is where I'm struggling the most in my learning process. Here's my HTML and CSS code:

<!DOCTYPE html>

<html>

<head>
<link href="./stylesheet.css" type="text/css" rel="stylesheet">
<meta charset ="utf-8"/>
<link href="https://fonts.googleapis.com/css?family=Indie+Flower" rel="stylesheet">
</head>

<body> 

<div id="container">

  <nav class="banner">
    <img src="./banner.jpg" alt="banner image of various Moto GP riders racing on track">
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Contenders</a></li>
      <li><a href="#">Manufacturers</a></li>
      <li><a href="#">Tech Sec</a></li>
      <li><a href="#">Calendar</a></li>
    </ul>
  </nav>

  <div class="opening">
    <h1>MotoGP World Championship Contenders</h1>
    <p>Here are the top Moto GP riders that will be competing for the World Championship in 2018...</p>
  </div>

  <div class="motogp-contenders">

    <div class="marquez">
      <img class ="rider-image" src="./marquez.jpg" alt="photo of Marc Marquez">
      <h2 class ="rider-name">Marc Marquez</h2>
      <p class="rider-desc">Reigning 4-times Moto GP World Champion</p>
    </div>

    <div class="dovi">
      <img class="rider-image" src="./dovi.jpg" alt="photo of Andrea Dovizioso">
      <h2 class="rider-name">Andrea Dovizioso</h2>
      <p class="rider-desc">So close in 2017!</p>
    </div>

    <div class="rossi">
      <img class="rider-image" src="./rossi.jpg" alt="photo of Valentino Rossi">
      <h2 class="rider-name">Valentino Rossi</h2>
      <p class="rider-desc">Can he win another title before he retires?</p>
    </div>

    <div class="vinales">
      <img class="rider-image" src="./vinales.jpg" alt="photo of Maverick Vinales">
      <h2 class="rider-name">Maverick Vinales</h2>
      <p class="rider-desc">Can he be fast at every round and win the championship?</p>
    </div>

    <div class="lorenzo">
      <img class="rider-image" src="./lorenzo.jpg" alt="photo of Jorge Lorenzo">
      <h2 class="rider-name">Jorge Lorenzo</h2>
      <p class="rider-desc">JL got used to the Duke at the end of the 2017 season...will he be a title contender this year?</p>
    </div>

    <div class="pedrosa">
      <img class ="rider-image" src="./pedrosa.jpg" alt="photo of Dani Pedrosa">
      <h2 class="rider-name">Dani Pedrosa</h2>
      <p class="rider-desc">The greatest rider never to win a world title?</p>
    </div>

    <div class="zarco">
      <img class ="rider-image" src="./zarco.jpg" alt="photo of Johann Zarco">
      <h2 class="rider-name">Johann Zarco</h2>
      <p class="rider-desc">The dark horse?</p>
    </div>

  </div>

</div>

</body>

</html>

My CSS styling is as follows:

body {
font-family: "arial";
background-color: black;
margin: 0;
}

#container {
width: 1200px;
margin: auto;
}

.banner img {
width: 100%;
}

.banner ul {
text-align: center;
list-style: none;
}

.banner li {
display: inline-block;
font-size: 18px;
padding: 10px;
background-color: white;
border-radius: 3px;
margin: 0 10px;
}

.banner a{
text-decoration: none;
color: black;
}

.banner li:active {
position: relative;
top: 2px;
}

.opening h1 {
color: white;
text-align: center;
font-family: 'Indie Flower';
font-size: 45px;
}

.opening {
color: white;
text-align: center;
font-size: 18px;
margin-bottom: 40px;
}

.rider-image {
width: 35%;
height: 290px;
margin: auto;
display: block;
border-radius: 50px;
}

.rider-name {
color: white;
text-align: center;
font-family: 'Indie Flower'
}

.rider-desc {
color: white;
text-align: center;
font-size: 16px;
margin-bottom: 40px;
}

Answer №1

To achieve this layout, you can make use of the Bootstrap framework and their grid system:

However, if you prefer not to rely on an external library, let me know and I can provide you with the raw code.

UPDATE: In response to the requester's preference for original source code over using libraries, the CSS snippet below showcases how the code operates, allowing for a better understanding (Please note that this code was directly extracted from BootStrap 4.0.0).

* {
margin: 5px;
}

.row {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  margin-right: -15px;
  margin-left: -15px;
}

.col {
  position: relative;
  width: 100%;
  min-height: 1px;
  padding-right: 15px;
  padding-left: 15px;
  -ms-flex-preferred-size: 0;
  flex-basis: 0;
  -webkit-box-flex: 1;
  -ms-flex-positive: 1;
  flex-grow: 1;
  max-width: 100%;
}
<div class="row">
    <div class="col">
      <img class="rider-image" src="./marquez.jpg" alt="photo of Marc Marquez">
      <h2 class="rider-name">Marc Marquez</h2>
      <p class="rider-desc">Reigning 4-times Moto GP World Champion</p>
    </div>

    <div class="col">
      <img class="rider-image" src="./dovi.jpg" alt="photo of Andrea Dovizioso">
      <h2 class="rider-name">Andrea Dovizioso</h2>
      <p class="rider-desc">So close in 2017!</p>
    </div>
  </div>

  <div class="row">
    <div class="col">
      <img class="rider-image" src="./marquez.jpg" alt="photo of Marc Marquez">
      <h2 class="rider-name">Marc Marquez</h2>
      <p class="rider-desc">Reigning 4-times Moto GP World Champion</p>
    </div>

    <div class="col">
      <img class="rider-image" src="./dovi.jpg" alt="photo of Andrea Dovizioso">
      <h2 class="rider-name">Andrea Dovizioso</h2>
      <p class="rider-desc">So close in 2017!</p>
    </div>
  </div>
  
   <div class="row">
    <div class="col">
      <img class="rider-image" src="./marquez.jpg" alt="photo of Marc Marquez">
      <h2 class="rider-name">Marc Marquez</h2>
      <p class="rider-desc">Reigning 4-times Moto GP World Champion</p>
    </div>

    <div class="col">
      <img class="rider-image" src="./dovi.jpg" alt="photo of Andrea Dovizioso">
      <h2 class="rider-name">Andrea Dovizioso</h2>
      <p class="rider-desc">So close in 2017!</p>
    </div>
  </div>
  
   <div class="row">
    <div class="col">
      <img class="rider-image" src="./marquez.jpg" alt="photo of Marc Marquez">
      <h2 class="rider-name">Marc Marquez</h2>
      <p class="rider-desc">Reigning 4-times Moto GP World Champion</p>
    </div>

    <div class="col">
      <img class="rider-image" src="./dovi.jpg" alt="photo of Andrea Dovizioso">
      <h2 class="rider-name">Andrea Dovizioso</h2>
      <p class="rider-desc">So close in 2017!</p>
    </div>
  </div>

Answer №2

To create a layout with rows of two columns each, simply wrap pairs of div elements that should appear side by side inside a new container div. By floating the elements inside this container, you can position them next to each other as desired. This technique allows for a clean and organized display of content in your design.

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

How can I ensure that my data remains properly aligned in a row with 9 columns while utilizing bootstrap?

My approach to creating a row with 9 columns involved using custom CSS to set each column's width to 11.11%, resulting in equal distribution across the row. I then applied the "custom-column" class to populate the row data: .custom-column { widt ...

Creating a dynamic web application using Node.js, HTML, and MySQL arrays

After querying my database from MySQL Workbench, I need to convert the retrieved data into HTML code. connection.query( 'SELECT dealer_infor.dealer_id, dealer_infor.dealer_branch, dealer_infor.dealer_address, dealer_infor.dealer_tel, dealer_infor. ...

Switching the way hyperlinks behave when clicked, from requiring a double-click to a single

My code is working properly, but I need my links to open with just a single click. The issue is that the delete, approve, and not approve links require a double click for their function to run. I'm hoping someone can help me with this. index.php < ...

What's the best way to display a component once a function has been executed?

My service controls the visibility of components using *ngIf! Currently, when I click a button, the service sets to true and the components appear instantly! I want the components to only show after a specific function has finished executing. This means I ...

Obtain the identifier for a single button within a collection of buttons in an HTML form using Django

I've incorporated Django as the framework for a tracking platform, utilizing it as an FSM. At this juncture, I aim to enable users to modify the machine's state. The state is denoted by a django-fsm field, essentially a CharField integrated with ...

Elevation on Tab button from the upper side

On my website, I have a large form where pressing the tab key switches between input fields. The inputs will also jump to the top if we reach the bottom of the form. The issue is that I have a fixed header at the top, covering the first 60px. Is there a w ...

Designing a navigation system that revolves around a central logo image

I have created a navigation bar that you can see in this image: https://i.stack.imgur.com/eT4TL.jpg Using foundation to construct a website, I have designed the nav bar in the following manner: HTML: <nav class="top-bar"> <ul> ...

The div width set to 100% is not functioning properly upon the initial form loading in IE11, however, it is working

[![enter image description here][1]][1]Can anyone assist me in resolving this issue? When my Div with the id "Coverage" loads initially, its width is set to the content width. However, after a refresh, it expands to 100% width. This problem does not occu ...

Using JavaScript to listen for events on all dynamically created li elements

Recently, I've created a simple script that dynamically adds "li" elements to a "ul" and assigns them a specific class. However, I now want to modify the class of an "li" item when a click event occurs. Here's the HTML structure: <form class ...

What exactly is the mechanism behind how the text-overflow property's fade value operates?

I am struggling to add a one line description to each flex-child using the fade() value, but it seems to not be working as expected. .flex-container { display: flex; flex-wrap: wrap; justify-content: center; flex-direction: row; } .flex-child { ...

Implementing a vertical tabindex change in Material UI Dialogue table

My material UI dialogue contains a table: <Dialog open={open} onClose={handleClose} maxWidth={'xl'} aria-labelledby='scroll-dialog-title' aria-describedby='scroll-dialog-description' disa ...

Achieving Vertical Alignment of Text and Icon in the Same Line Using HTML and CSS

I am struggling to align these icons vertically center with the text. However, there seems to be a slight offset of a few pixels. I have experimented with different solutions, but this is the closest I could get to. Can anyone explain why it's not ali ...

Achieving a persistent footer at the bottom of the page within Material Angular's mat-sidenav-container while using the router-outlet

I am looking to keep my ngx-audio-player fixed at the bottom of the screen, similar to what you see on most music streaming websites. I currently have a structure with divs and various elements for dynamic content and playing music. The issue is that the ...

How can you change a particular inline style using the Firefox browser?

I am looking for a solution to override an inline style on a specific webpage within the Firefox browser without access to the source code. Previously, I would manually modify the page source using Firefox development tools. Specifically, I encounter a we ...

The navbar does not dictate the views

Greetings lovely individuals I've been grappling with this issue for a while now and it seems I can't quite crack it on my own... I'm hopeful that you can assist me. My objective: A functioning navigation bar controlled by PHP in an MVC pat ...

conflicting character encoding systems

Request normally comes in this format: Parameters: {"utf8"=>"✓", "status_id"=>"12686"} However, from one particular client, the request looks like this: Parameters: {"utf8"=>"\xE2??", "status_id"=>"12686"} As a result, I am encounter ...

Is there a way to transform JSON into HTML with PHP?

I am attempting to reverse engineer the function referenced in this discussion: [Post][1] [1]: https://stackoverflow.com/a/23427469/19867306 @scozy demonstrated a function for converting HTML text to a JSON model. Now, I need to convert the same JSON mode ...

Understanding the scope of variables in HTML files, JavaScript files, and PHP files when echoing statements

Is there a way to define a global javascript variable that can be accessed from multiple places within an HTML page? I need the variable to be accessible in three specific locations: Within the code of the HTML page favorites.php. Inside a separate javas ...

Triggering the 'Next' function with a key press event

Currently, I am utilizing the following code to manage the keydown event for the tab and enter keys within an input field. $(document).on('keydown', ".className" , function(e) { var keyCode = e.keyCode || e.which; alert(keyCode) / ...

Using Angular2, you can dynamically assign values to data-* attributes

In my project, I am looking to create a component that can display different icons based on input. The format required by the icon framework is as follows: <span class="icon icon-generic" data-icon="B"></span> The data-icon="B" attribute sp ...