Images that are not aligned to the left

I'm really struggling with this issue that seems so simple. I am trying to create a grid layout of images like the following:

[Image] [Image] | [   Big
[Image] [Image] |    Image   ]

Creating the grid with just images is not difficult for me, but the problem arises when I nest the images inside links. I can't seem to figure out how to make the links float left.

This is the HTML code I am using:

<div class="row">
<div class="col-md-6">
    <a href="img/1-1full.png" title="" data-gallery>
        <img src="img/1-1.png" alt="">
    </a>
    <a href="img/1-2full.png" title="" data-gallery>
        <img src="img/1-2.png" alt="">
    </a>
    <a href="img/1-3full.png" title="" data-gallery>
        <img src="img/1-3.png" alt="">
    </a>
    <a href="img/1-4full.png" title="" data-gallery>
        <img src="img/1-4.png" alt="">
    </a>
</div>
<div class="col-md-6">
    <a href="img/1-largefull.png" title="" data-gallery>
        <img src="img/1-large.png" alt="">
    </a>
</div>
</div>
<div style="clear: both;">

And here is my CSS code:

.gallery .col-md-6 a {
    float: left;
    display: block;
}

.gallery .col-md-6 a:nth-child(odd) {
    width: 307px;
    margin: 0 30px 30px 0;
}

.gallery .col-md-6 a:nth-child(even) {
    width: 278px;
    margin: 0;
}

Each col-md-6 element has a width of 585px which should fit together perfectly. The .gallery class is being targeted correctly as well, as confirmed through the code inspector. There must be some fundamental aspect of floating elements that I am missing here.

Answer №1

Utilizing a grid framework like Bootstrap can offer the solution you're seeking with just a bit of extra markup.

<div class="row">
  <div class="col-md-6">
    <div class="row">
      <div class="col-md-6">
        <a href="img/1-1full.png" title="" data-gallery>
          <img src="img/1-1.png" alt="" class="img-responsive">
        </a>
      </div>
      <div class="col-md-6">
        <a href="img/1-2full.png" title="" data-gallery>
          <img src="img/1-2.png" alt="" class="img-responsive">
        </a>
      </div>
    </div>
    <div class="row">
      <div class="col-md-6">
        <a href="img/1-3full.png" title="" data-gallery>
          <img src="img/1-3.png" alt="" class="img-responsive">
        </a>
      </div>
      <div class="col-md-6">
        <a href="img/1-4full.png" title="" data-gallery>
          <img src="img/1-4.png" alt="" class="img-responsive">
        </a>
      </div>
    </div>
  </div>
  <div class="col-md-6">
    <a href="img/1-largefull.png" title="" data-gallery>
      <img src="img/1-large.png" alt="" class="img-responsive">
    </a>
  </div>
</div>

Answer №2

Could this solution be a good fit for your needs? I've provided two different examples based on the layout you described: one with 4 items in a row and the other with 2 items in a row.

* {box-sizing: border-box;}
.half {width: 50%;}
.quat {width: 25%;}
.sz34 {width: 75%;}
.cols {float: left;}
img {width: 100%; height: 65px; display: block; border: 1px solid #ccc;}
.parent {overflow: hidden;}
.parent div {float: left;}
img.double-height {height: 130px;}
<div class="parent">
  <div class="half">
    <div class="quat"><img src="" alt="Image"></div>
    <div class="quat"><img src="" alt="Image"></div>
    <div class="quat"><img src="" alt="Image"></div>
    <div class="quat"><img src="" alt="Image"></div>
    <div class="quat"><img src="" alt="Image"></div>
    <div class="quat"><img src="" alt="Image"></div>
    <div class="quat"><img src="" alt="Image"></div>
    <div class="quat"><img src="" alt="Image"></div>
  </div>
  <div class="half">
    <img src="" alt="Image" class="double-height">
  </div>
</div>

<hr />

<div class="parent">
  <div class="half">
    <div class="half"><img src="" alt="Image"></div>
    <div class="half"><img src="" alt="Image"></div>
    <div class="half"><img src="" alt="Image"></div>
    <div class="half"><img src="" alt="Image"></div>
  </div>
  <div class="half">
    <img src="" alt="Image" class="double-height">
  </div>
</div>

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

Create various designs for a section of a webpage

My goal is to create a coding playground using flex-box to position different panels. Here is an example in JSBin, with the following html code: <div class="flex-box"> <div class="col" id="html-panel"> <p>html</p> </div& ...

Using PHP to manipulate MySQL databases with HTML tags

I am encountering an issue with strings retrieved from my database. These strings contain various HTML tags within them. For instance: "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys ...

PHP submitting form saves zeros

I have encountered an issue with submitting a form entry provided by the user. Instead of storing the actual values entered by the user, the database is recording 0 as the input. I require assistance to rectify this problem. Below is my code snippet: & ...

Performing arithmetic calculations using HTML

Can math operations be done directly within an HTML element, such as <div width="50/2">, or is it necessary to use Javascript or CSS for this functionality? ...

Creating a CSS animation to repeat at regular intervals of time

Currently, I am animating an SVG element like this: .r1 { transform-box: fill-box; transform-origin: 50% 50%; animation-name: simpleRotation,xRotation; animation-delay: 0s, 2s; animation-duration: 2s; animation-iterat ...

Concealing the submit button until a file has been selected

How can I make the submit button invisible until a file is selected? <form action="upload.php" method="POST" enctype="multipart/form-data"> <input type="file" name="imageURL[]" id="imageURL" /> <input type="submit" value="submi ...

The function document.getElementById().value is malfunctioning

I've been working on a project focused on employee data stored in a MySQL database. To display this data in an HTML table, I utilized PHP. $sql = "SELECT * FROM Stock_Management.`staff_details`"; $result = mysqli_query($conn, $sql); if(mysq ...

Is it possible to create a webpage layout with CSS using the flex or block properties?

This code is automatically generated HTML from a CMS and the items will be dynamic inside a ul tag. However, I require a layout similar to the image below: https://i.sstatic.net/1xnow.png * { box-sizing: border-box; } ul { list-style: none; padd ...

Dynamic Design with Pure CSS

I am in the process of creating a flexible layout design. Everything appears to be functioning as intended on FF, Chrome, Safari and IE8 However, I have encountered an issue with IE7. It seems to be related to the floated containers. I have attempted a fe ...

Creating opaque elements within a see-through container

I'm working with a bootstrap carousel and I want to set the background image of each carousel-item to be .4 opacity, while keeping the caption at full 1 opacity. Is there a way to achieve this? <div id="carouselExampleIndicators" class="carous ...

The embedded CartoDB image header is set to have a style of "display:none"

I'm currently working on a CartoDB map showcasing my readers' summer fishing photos. Each row in the table has an image url, which I've placed at the top of the infowindow display list. I've enabled it and set the image window type to ...

What is the process for adding and removing classes using CSS's "ClassName" property in React?

I've been diving into React recently and one thing I'm struggling with is how to seamlessly add and remove CSS classes. Take for example the component below: import React from 'react'; import "../../styles/signInAndSignUp.css"; import l ...

Transform a list element into three separate rows using Bootstrap

I need assistance wrapping a list of elements into 3 columns using a single <ul> Here is a visual representation of what I am aiming for: https://i.sstatic.net/dptO2.png <ul class="list-group list-group-flush row-cols-3"> <li ...

Tips for incorporating a Python script into a online project

I've been working on a Python code that detects faces and eyes using face recognition. When the code runs in PyCharm, it displays a camera window. Now I'm trying to figure out how to integrate this window into a webpage project written in HTML, C ...

What is the best way to ensure that a link fragment scrolls to the top of the page in Angular?

Having trouble with link fragments in my Angular single-page-app: <a href="/#/search">Search</a> When clicking this link, it takes me to the right page but keeps my scroll position. I want it to scroll to the top of the page so I'm curre ...

Sending data to a React component from regular HTML

I have a question about implementing a method to pass custom attributes from HTML elements as props to React components. Here's an example: function someFunction(props) { return <h1>props.something</h1> } HTML: <div id="someEl ...

The height of the Ionic Checkbox is unevenly proportioned

I'm currently using the code below to display checkboxes in a row: <label class="row item item-input "> <span class="item-text-wrap col col-50 input-label">Inspection Type</span> <d ...

Blurry Gaussian on a d3 Context Chart

I am currently developing an interactive d3 application that includes a segment of the interface inspired by Bostock's focus/context brushing example: http://bl.ocks.org/mbostock/1667367 While everything is functioning correctly so far, I have a spe ...

Two sets of scrolling bars

Having an issue with my JSFIDDLE parallax effect causing two scrollbars in the result. How can I ensure there is only one scrollbar for all my content? Here is some of the HTML code: <div id="intro"> <p>Sed uelit, sed quia...</p> </ ...

Using JavaScript to dynamically alter the background image of an HTML document from a selection of filenames

Just starting out with JavaScript and working on a simple project. My goal is to have the background image of an HTML document change to a random picture from a directory named 'Background' every time the page is opened. function main() { // ...