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

Connecting a href link to a TAB

Check out this useful CODE example I am using. I have a webpage with links that have href attributes. Now, I want to link these pages to another page and have them automatically open a specific tab when clicked. Is this possible? Here are the links on th ...

Conceal or style the filter panel in a DT datatable

Here is an example to consider: library(DT) L <- 10 datatable( data.frame( var1 = sapply(1:L, function(x) paste("<X>",paste0(x, letters, LETTERS, "\n", ...

Assistance needed with CSS - making an element occupy the entire remaining vertical space

Although I'm quite confident in my CSS/XHTML abilities, this particular issue has me stumped. You can view the problem here: - (please note that the website is still under development, most features are not functional, and it's subject to frequ ...

The call function in Tween.js fails to execute after adding an EventListener

I encountered an issue while using tween.0.6.2. The following code snippet (borrowed from the tween.js Getting Started page and slightly simplified) works perfectly: createjs.Tween.get(circle) .to({x: 400}, 1000, createjs.Ease.getPowInOut ...

What is the best way to split an array into four columns and allocate 10 <li> items from the array to each column?

If I have a dynamic array with 40 elements that changes on every render, how can I loop through the array and return 4 groups of elements, each containing 10 items without any repetition? I want to style these objects in the array using a parent flex con ...

Only one admin account can be accessed at a time by multiple logins

I am looking to add a new feature to my app where only one admin can log in at a time. If someone else tries to log in with the same admin ID on another device, a warning should be shown, indicating that the user is already logged in and cannot access the ...

"Implementing jQuery to dynamically set the href attribute for a link when

My website features a tabbed menu that displays content from various WordPress categories including Cars, Trucks, and Buses. The active tab is randomly selected each time the page is reloaded. However, there seems to be an issue with the "View More" button ...

Centering an image that is absolutely positioned within a container that is relatively positioned, horizontally

Looking to center an image that is positioned absolutely horizontally within a container that is relatively positioned. After attempting with CSS and not finding success, I ended up using Jquery. http://jsfiddle.net/CY6TP/ [This is my attempt using Jquery ...

Is there a way to stop Bootstrap from automatically bolding the font?

When testing this simple example without the bootstrap link, everything seems to be working correctly: Hovering over any word changes its color to red, and when hovering away it returns to black. But as soon as the bootstrap link is included, the text bec ...

Bootstrap 3's Clear:Left Media Query Feature

Bootstrap is being used, and there is a div with col-xs-12, col-sm-6, col-md-4 classes. Task : To add a clear on every 1 item for col-xs-12, add a clear on every 2 items for col-sm-6, and clear on every 3rd item for col-md-4. The current code only s ...

Automatic Full-Screen Switching Upon Video Playback in HTML5

Currently, I have a video clip set to play when the timer reaches a certain point. My goal is for the video to automatically enter full-screen mode when it starts playing and return to its original position when it stops, as the timer will continue ticking ...

I'm throwing in the towel on CSS, I just can't seem to get a simple button to sit at the bottom of

Struggling to position a button at the bottom of a div? I've tried everything I could find online but nothing seems to work! Here's my code: <ion-col > <div id="secondCol"> <ion-text> {{card.cardPo ...

Generating alternate list items

I'm attempting to design a list style that resembles the one depicted in the image linked below: https://i.stack.imgur.com/U7vMw.jpg My issue is that when I try to add borders, they end up applying to the entire structure. .styled-list { list-st ...

adjustable backdrops

Hi there, I'm trying to create a background image that resizes with the window while maintaining its proportions. I want to achieve this using CSS only. I've been searching for a solution but haven't found anything that works for me. I even ...

Changing the position of an image can vary across different devices when using HTML5 Canvas

I am facing an issue with positioning a bomb image on a background city image in my project. The canvas width and height are set based on specific variables, which is causing the bomb image position to change on larger mobile screens or when zooming in. I ...

Is there any method to ensure that IE8 properly displays opacity for a `:before` pseudo element?

Here is a simple CSS snippet that I am working with... div:before { content: "Hello there"; filter: alpha(opacity=40); -moz-opacity: .4; opacity: .4; } Check it out on jsFiddle. In Firefox 6, the :before pseudo element displays with t ...

Adjust the div container to wrap underneath the image if the webpage width decreases

Hello there, I am currently in the process of creating my own portfolio. I have a brief introduction that I would like to display next to an image of myself, with the image positioned on the right side. I have a div called "container" which contains anoth ...

Obtaining the width of a scrollbar using JavaScript in the most recent version of Chrome

Looking for a way to determine the width of the scrollbar using JavaScript for CSS purposes. I attempted the following: document.documentElement.style.setProperty('--scrollbar-width', (document.offsetWidth - document.clientWidth) + "px" ...

Is there a way to trigger the opening of a new file or page when a CSS animation comes to an end?

Is there a way to delay the loading of a function or page until after an animation has finished running in JavaScript, HTML, and CSS only? For instance, I'd like to run an animation first and then have a different website or content load afterwards fo ...

The CSS "ul" selector targets unordered lists in HTML

How can I target the ul elements highlighted below in my HTML markup without using a class or id for reference? Here is the relevant portion of my HTML code: <div id="mainmenu"> <ul class="menu"> <li class="item-472"> < ...