maintaining the alignment of three div elements

I am struggling to keep three divs side by side within a container, each containing an image and text below it. I had this layout working perfectly before, but now the right div falls underneath the other two when the screen size is reduced, followed by the middle one. I know it's possible to keep them all side by side as I've done it in the past, but for some reason, I can't remember how I achieved it.

HTML

<div id='container'>

        <div class='one-third'>
            <a><img src='http://gratisography.com/pictures/264_1.jpg'><h3>Headline</h3></a><p>Medieval texts date the arrival of the Vikings in the British Isles to the 790s A.D., when fierce raiders from Scandinavia suddenly appeared along the coasts, plundering rich monasteries and terrorizing local communities. </p>
        </div>

        <div class='one-third'>
            <a><img src='http://gratisography.com/pictures/264_1.jpg'><h3>Headline</h3><p>Medieval texts date the arrival of the Vikings in the British Isles to the 790s A.D., when fierce raiders from Scandinavia suddenly appeared along the coasts, plundering rich monasteries and terrorizing local communities. </p>
        </div>

        <div class='one-third'>
            <a><img src='http://gratisography.com/pictures/264_1.jpg'><h3>Headline</h3><p>Medieval texts date the arrival of the Vikings in the British Isles to the 790s A.D., when fierce raiders from Scandinavia suddenly appeared along the coasts, plundering rich monasteries and terrorizing local communities. </p> 
        </div>
       
    

CSS

.one-third  {
        width: 30%;
        display: inline-block;
        margin: 5% 0 0 2.5%; }

    .one-third img {
        width: 100%;
        height: 300px; }

    .one-third h3 {
        margin: .5em 0 2em 0; }
    

Answer №1

Your code had some issues with unclosed <a> tags and setting a height on the <img>. To fix this, I recommend not setting a height on the image unless you want it to distort when scaling. Here's how you can address the problem:

Corrected code:

.one-third  {
    max-width: 30%;
    display: block;
    float: left;
    padding: 5% 0 0 2.5%; 
}

.one-third img {
    width: 100%;
}

.one-third h3 {
    padding: 0.5em 0 2em 0;
}

<div id='container'>
    <div class='one-third'>
        <a>
            <img src='http://gratisography.com/pictures/264_1.jpg'>
            <h3>Headline</h3>
        </a>
        <p>Medieval texts date the arrival of the Vikings in the British Isles to the 790s A.D., when fierce raiders from Scandinavia suddenly appeared along the coasts, plundering rich monasteries and terrorizing local communities. </p>
    </div>
    <div class='one-third'>
        <a>
            <img src='http://gratisography.com/pictures/264_1.jpg'>
            <h3>Headline</h3>
        </a>
        <p>Medieval texts date the arrival of the Vikings in the British Isles to the 790s A.D., when fierce raiders from Scandinavia suddenly appeared along the coasts, plundering rich monasteries and terrorizing local communities. </p>
    </div>
    <div class='one-third'>
        <a>
            <img src='http://gratisography.com/pictures/264_1.jpg'>
            <h3>Headline</h3>
        </a>
        <p>Medieval texts date the arrival of the Vikings in the British Isles to the 790s A.D., when fierce raiders from Scandinavia suddenly appeared along the coasts, plundering rich monasteries and terrorizing local communities. </p>
    </div>
</div>

Answer №2

It seems like your code is functioning as expected, take a look below.

As you decrease the size of your page, eventually the word "Headline" in h3 will become wider than 1/3 of the page width, causing the inline-block elements to wrap.

If you were to use CSS tables, you could keep all three items on one line but this may lead to overflow issues.

Consider how you want your layout to handle extremely narrow cases.

.one-third {
  width: 30%;
  display: inline-block;
  margin: 5% 0 0 2.5%;
}
.one-third img {
  width: 100%;
  height: 300px;
}
.one-third h3 {
  margin: .5em 0 2em 0;
}
<div id='container'>

  <div class='one-third'>
    <a>
      <img src='http://gratisography.com/pictures/264_1.jpg'>
      <h3>Headline</h3>
    </a>
    <p>Medieval texts date the arrival of the Vikings in the British Isles to the 790s A.D., when fierce raiders from Scandinavia suddenly appeared along the coasts, plundering rich monasteries and terrorizing local communities.</p>
  </div>

  <div class='one-third'>
    <a>
      <img src='http://gratisography.com/pictures/264_1.jpg'>
      <h3>Headline</h3>
    </a>
    <p>Medieval texts date the arrival of the Vikings in the British Isles to the 790s A.D., when fierce raiders from Scandinavia suddenly appeared along the coasts, plundering rich monasteries and terrorizing local communities.</p>
  </div>

  <div class='one-third'>
    <a>
      <img src='http://gratisography.com/pictures/264_1.jpg'>
      <h3>Headline</h3>
    </a>
    <p>Medieval texts date the arrival of the Vikings in the British Isles to the 790s A.D., when fierce raiders from Scandinavia suddenly appeared along the coasts, plundering rich monasteries and terrorizing local communities.</p>
  </div>

  <div>

Answer №3

Consider including box-sizing: border-box; in the .one-third style. By doing this, you can ensure that margins and padding are included in the total width of the element.

Answer №4

Adjust the parent's font-size to 0 and then revert the child elements back to their original size. To illustrate, I have configured the divs to precisely occupy 33% of the parent each:

.one-third {
  width: 33%;
  display: inline-block;
  margin: 0%;
  font-size: 12px;
}
.one-third img {
  width: 100%;
  height: 300px;
}
.one-third h3 {
  margin: .5em 0 2em 0;
}
#container {
  font-size: 0;
}
<div id='container'>

  <div class='one-third'>
    <a>
      <img src='http://gratisography.com/pictures/264_1.jpg'>
      <h3>Headline</h3>
    </a>
    <p>Medieval texts date the arrival of the Vikings in the British Isles to the 790s A.D., when fierce raiders from Scandinavia suddenly appeared along the coasts, plundering rich monasteries and terrorizing local communities.</p></a>
  </div>

  <div class='one-third'>
    <a>
      <img src='http://gratisography.com/pictures/264_1.jpg'>
      <h3>Headline</h3>
      <p>Medieval texts date the arrival of the Vikings in the British Isles to the 790s A.D., when fierce raiders from Scandinavia suddenly appeared along the coasts, plundering rich monasteries and terrorizing local communities.</p></a>
  </div>

  <div class='one-third'>
    <a>
      <img src='http://gratisography.com/pictures/264_1.jpg'>
      <h3>Headline</h3>
      <p>Medieval texts date the arrival of the Vikings in the British Isles to the 790s A.D., when fierce raiders from Scandinavia suddenly appeared along the coasts, plundering rich monasteries and terrorizing local communities.</p>
</a>     
 </div>

  <div>

@Marc Audet deserves credit for pointing out that your <a> tags should be properly closed for correct HTML markup.

Answer №5

Eliminate the gaps between divs:

<div id='container'>
    <div class='one-third'>
        <a><img src='http://gratisography.com/pictures/264_1.jpg'><h3>Headline</h3></a><p>Medieval texts refer to the arrival of the Vikings in the British Isles around 790 AD, when fierce raiders from Scandinavia suddenly appeared along the coasts, looting rich monasteries and frightening local communities. </p>
    </div>
    <div class='one-third'>
        <a><img src='http://gratisography.com/pictures/264_1.jpg'><h3>Headline</h3><p>Medieval texts mention the arrival of the Vikings in the British Isles around 790 AD, when fierce raiders from Scandinavia suddenly emerged along the coasts, pillaging wealthy monasteries and intimidating local communities. </p>
    </div>
    <div class='one-third'>
        <a><img src='http://gratisography.com/pictures/264_1.jpg'><h3>Headline</h3><p>Historical documents record the entry of the Vikings into the British Isles during the 790s AD, as aggressive raiders from Scandinavia abruptly surfaced along the shores, ransacking prosperous monasteries and causing fear among local residents. </p>
    </div>

    <div>

Alternatively, you can utilize the float property.

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

Achieve a seamless integration of a navbar and list on the same line by utilizing the power of Bootstrap

No matter how much I tried, I couldn't solve the problem of my list not appearing in the same line. I attempted using display: inline-block; and padding: 0, but to no avail. /* added by editor for demonstration purpose */ body { background-color: ...

If the content within a <div> element exceeds the available width, a horizontal scrollbar will be displayed

I have a scenario where I am using an absolute div to overlap another div. The div that overlaps is the absolute one (.content), and if the overlapped div (.left) doesn't fit the screen width, a horizontal scroll bar does not appear. How can I automat ...

What steps can I take to prevent the array from constantly updating and instead only show the final task?

<h1>Custom Calendar and Task Scheduler</h1> <div class="navigation-text">Use the arrows to switch between months</div> <div class="navigation-text">Click on a date to create tasks</div> &l ...

Is there a way to create a responsive navbar using flexbox that automatically transitions into two rows at smaller screen sizes?

I've designed a sleek navbar located at the bottom of my webpage featuring a central logo leading to the home page, with two links on each side directing users to specific pages. Using @media CSS, I made it responsive by reducing its size as the scree ...

What is the process for incorporating a full-page blog into a blog preview?

I customized a template, but there is a blog section within the div that I am struggling to replicate. Here is the test website for reference: Below is the HTML code for the blog section: <!-- Blog Start --> <section class="section bg ...

Generating DOM elements at specific intervals using Jquery

I am looking to dynamically create 1 div container every x seconds, and repeat this process n times. To achieve this, I have started with the following code: $(document).ready(function() { for (var i = 0; i < 5; i++) { createEle(i); } }); f ...

Tips for extracting innerHTML or sub-string from all elements that have a specific class name using JavaScript

When it comes to shortening the innerHTML of an element by its Id, I have found success using either slice or substring. While I'm not entirely clear on the differences between the two methods, both accomplish what I need them to do. The code snippet ...

Guide on how to align h1 in the navigation bar

I've been attempting to center text on a nav bar but haven't been successful. What could I be overlooking? // include external modules import React, { Component } from "react"; import { Navbar } from "reactstrap"; import { Menu } from " ...

The JQUERY Uncaught Error: Syntax error message popped up when trying to access an unrecognized expression on the javascript: void(0)

I've encountered an issue after upgrading to jQuery 3.4.1 while using bootstrap 3. The console keeps showing this error: Uncaught Error: Syntax error, unrecognized expression: # Here is the section of code causing the error: <div class="btn-grou ...

Save to clipboard HTML with forward slash

I need help creating a button that will copy a specific text to the clipboard when clicked. <button onclick="copyToClipboard('A\B\C\D')">Click here</button> However, I've encountered an issue where the text copie ...

Refrain from using inline JavaScript code within your

Here is the basic structure of my code: <a href="javascript:void(0);" onClick="viewServices(1)">Services</a> <a href="javascript:void(0);" onClick="viewServices(43)">Services</a> <a href="javascript:void(0);" onClick="viewServic ...

Troubleshooting my HTML5 local storage issues for optimal functionality

I've been working on using HTML5's localstorage to save two variables and load them upon page refresh, but I seem to be encountering some issues when trying to load the saved items: Variables in question: var cookies = 0; var cursors = 0; Savi ...

Looking to switch up the background color while scrolling, any tips?

I'm trying to change the background color of my navbar section on scroll, but I've had no luck so far. Below is the code I have been using: const [colorChange, setColorChange] = useState(false); const changeColor = () => { if (window.scro ...

The font in my Next.js / Tailwind CSS project starts off bold, but unexpectedly switches back to its original style

I recently integrated the Raleway font into my minimalist Next.js application with Tailwind CSS. I downloaded the font family in .ttf format and converted it to .woff2, but I'm having trouble changing the font weight using custom classes like font-bol ...

Is it possible to create a translucent glass floating box over HTML elements, similar to the frosted glass effect seen in iOS7?

Creating an overlapping div with a frosted glass effect typically requires using an image background (like ). I am interested in having a floating div (position:fixed) that can apply a frosted glass effect over any content below it, whether it be an image ...

A step-by-step guide on showcasing database data on an HTML page using jQuery

I am currently using jQuery to make an HTTP GET request and fetch JSON data from a database. After executing the request, I received the JSON array successfully. Now, I am attempting to display this JSON data on an HTML page by using the jQuery $(newData ...

Delete the following td when the mouse hovers over it

How can I modify my code to remove the next td on mouseenter of a particular td? Currently, my code is removing the first occurrence of mouseenter on any td and the current mouseover td. Here is the snippet of my code. Can anyone help me identify what&apos ...

Tips for handling the accent mark (diacritic mark)

My primary language is Spanish, which means I use accent marks quite frequently (á, é...). When I need to type them out, I resort to using &aacute;, &eacute;, and so on. However, I'm facing an issue when trying to compare two sentences in m ...

In Redux, it is possible to add characters to an array but for some reason the remove action does not successfully reach the reducer

As a user types or erases characters, I am utilizing redux to update an array of characters. This allows me to set a success flag when the user correctly types the entire phrase. Currently, when typing in characters, the SET_INPUT action in redux fires of ...

The click event triggered by the onclick clone/function may not always activate the click handler

As a newcomer in the JavaScript domain, I am encountering an issue where the first clone created after clicking 'add more' does not trigger my click me function. However, every subsequent clone works perfectly fine with it. What could be causing ...