I spy a space, just waiting for my image to take its place

I attempted to position my image within a specific space but encountered difficulties, you can view the jsfiddle here. As seen, the right image and overlay are not aligning correctly as they should. My goal is for them to fit perfectly within the designated gap.

Here is the HTML Code:

<div class = "leftpart fadeInBlock">    
  <a href="http://www.google.com">
    <img src="images/test.jpg"/>
    <div class="flex-caption hvr-fade">Adventurer Lemon</div>
  </a>
  <div class = "sleft1">
    <a href="http://www.google.com">
      <img src="images/test.jpg">
      <div class="flex-caption hvr-fade">Adventurer Lemon</div>
    </a>
    <div class = "sleft2">
      <a href="http://www.google.com">
        <img src="images/test.jpg">
        <div class="flex-caption hvr-fade">Adventurer Lemon</div>
      </a>
    </div>
  </div>
</div>

Answer №1

see snippet

.leftpart {
  width: 43%;
  margin-top: 0.7%;
  float: right;
}

.sleft1,.sleft2 {
  float:left;
  width:50%;
}

.leftpart a img {
  width: 100%;
  height: 130px;
  display:block;
}

.leftpart .sleft1 a img {

  display:block;
  background-color:blue;
  margin-top:4.5%;
}

.sleft2 a img {
  display:block;

  margin-top:4.5%;
  background-color:red
}


.flex-caption {
  width: 96%;
  padding: 2%;
  top: 0px;
  left: 0;
  bottom: 0;
  background: rgba(0, 0, 0, .5);
  color: #fff;
  text-shadow: 0 -1px 0 rgba(0, 0, 0, .3);
  position: relative;
  line-height: 18px;
  box-sizing: border-box;
}
<div class = "leftpart fadeInBlock">
<a href="http://www.google.com">
<img src="images/test.jpg"/>
<div class="flex-caption hvr-fade">Adventurer Lemon</div>
</a>
<div class = "sleft1">
<a href="http://www.google.com">
<img src="images/test.jpg">
<div class="flex-caption hvr-fade">Adventurer Lemon</div>
</a>
            </div>
<div class = "sleft2">
<a href="http://www.google.com">
<img src="images/test.jpg">
<div class="flex-caption hvr-fade">Adventurer Lemon</div>
</a>
</div>
</div>
</div>

there were numerous issues with both your html and your css

HTML

when attempting to have two divs floated next to each other, they need to be siblings in the html. Your .sleft2 was actually a child of .sleft1, and you forgot to close the </div> for .sleft1

CSS

there is no need to write separate styles for identical .flex-caption divs. You can combine them into one style if they should look the same.

IMPORTANT: Remember to float the containers .sleft1 and .sleft2, not the elements inside them! Remove any unnecessary floats from other elements and add

.sleft1,.sleft2 {
   float:left;
   width:50%;
 }

add box-sizing:border-box to .flex-caption to prevent the width from exceeding 100% when using padding:2%

make some minor CSS adjustments

let me know if this resolves your issue

Answer №2

.leftpart {
  width: 43%;
  margin-top: 0.7%;
  float: right;
}

.leftpart a img {
  width: 100%;
  height: 130px;
  display:block;
}
.sleft1{
  float: left;
  width: 45%;
}
.sleft2{
  float: right;
  width: 45%;
}
.leftpart .sleft1 a img, .leftpart .sleft2 a img {
  display:block;
  background-color:blue;
  margin-top:4.5%;
}



.flex-caption {
  box-sizing: border-box;
  padding: 2%;
  top: 0px;
  left: 0;
  bottom: 0;
  background: rgba(0, 0, 0, .5);
  color: #fff;
  text-shadow: 0 -1px 0 rgba(0, 0, 0, .3);
  position: relative;
  line-height: 18px;
}
<div class = "leftpart fadeInBlock">
<a href="http://www.google.com">
<img src="images/test.jpg"/>
<div class="flex-caption hvr-fade">Brave Explorer Grapefruit</div>
</a>
<div class = "sleft2">
<a href="http://www.google.com">
<img src="images/test.jpg">
<div class="flex-caption hvr-fade">Brave Explorer Grapefruit</div>
</a>
</div>
<div class = "sleft1">
<a href="http://www.google.com">
<img src="images/test.jpg">
<div class="flex-caption hvr-fade">Brave Explorer Grapefruit</div>
</a>
</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

Having trouble toggling the navbar on and off in mobile. Error message: Unable to read parentnode of undefined

When I try to make my mobile navbar full screen on a mobile device, the hamburger button works as expected when clicked. Initially, all the links are displayed in full page view, but once I click on a link, it disappears and takes me to the respective sect ...

sticky effects on touchscreens when hovered or focused upon

Is there a solution to the issue described in the following quote? Bootstrap: link :hover functionality is not supported on most touch devices, but iOS tries to replicate it by creating persistent hover styles that remain after tapping an element. Thes ...

Adjust the hue as you scroll

I've been searching for a solution to this issue, but I haven't been able to make it work. My goal is to have the page header change from a transparent background to a red background as the user starts scrolling down the page. $(window).on("s ...

Using jQuery to select a specific checkbox from a group of checkboxes with identical IDs

There is an issue with multiple checkboxes having the same ID in an asp.net repeater control. Users can select either email or phone against each record in the repeater rows. In the example below, there are two rows. If you select the email icon in the fi ...

Is there a way in JavaScript to determine if a textarea contains text that is only one line long?

My aim is to modify the behavior of a textarea based on its content, specifically if it contains more than one line of text. The default setting for a textarea is to have enough height for 2 rows even when empty, which I want to change. I would like to f ...

Why can I only click on the text of my button?

Looking for a quick fix here! I've been trying to make the entire button clickable, but so far, only the text is hyperlinked. Any suggestions on how to enclose the whole button with the link? As you can see, I'm working on making it mobile-friend ...

browsing through elements in express-handlebars

this is the data structure I pass to my handlebars template module.exports = function(showHeader, userId){ // defining the model return { userId: userId, seminars: userSeminars; }; }; var userSeminars = [{ // sample data seminarId: 1, ...

Methods for Deactivating an Individual HTML Button

I have a situation in my HTML page where there are 6 buttons with values ranging from 1 to 5, and the sixth button has the value "disable". What I am trying to achieve is the ability to disable a specific button by clicking the "disable" button using jQuer ...

Using CSS for layout positioning

This problem has been confounding me for quite some time now, even though the solution is likely very simple. I can't seem to figure out how to align two divs of different sizes side by side without one being pushed down a few lines when text is added ...

Modify the state of each individual button

I recently came across a cool implementation using clicks and jQuery fade effects to show a div when a button is clicked and then change the state of the button. Check it out here: http://jsfiddle.net/ttj9J/5/ HTML <a class="link" href="#" data-rel="c ...

Leveraging SASS for injecting a variable into a mixin where the declaration is in string format

I have been developing a SASS mixin that allows for the input of various variables to construct a font-awesome declaration in the following format: @mixin after-font-awesome($unicode, $size, $margin, $color, $weight) { &:after { content:$unicode ...

Using JQuery to cycle a class with a timer

My list has the following structure <div id="slider"> <ul> <li class='active'> a </li> <li> b </li> <li> c </li> <li> d </li> <li> e </li> </u ...

Undesirable elements present in the beautified HTML code

My original web scraped HTML text looks like this: > {"overview":"\\u003cp\\u003e\\u003cspan style=\\"font-size: > 10.5pt;\\"\\u003e\\u003cspan class=&bsol ...

JavaScript can be used to manipulate buttons and interact with them on a webpage

In my search for examples for my webpage, I stumbled upon a strange JQuery selector. Could someone please explain how this works and if I can use it to target unique indexed IDs in HTML? //The mysterious selector that puzzles me //What does "^" actually ...

Using Javascript to upload an image and show it in a small display

Hey there, I have a functioning JavaScript code that loads an image uploaded by the user onto the HTML page. However, the issue is that the image doesn't have a set maximum height or width, causing buttons on the page to move out of view and become in ...

Clicking on the button will advance to the next tab rather than selecting the tab

I have this code snippet in HTML: $(document).ready(function() { $("div.bhoechie-tab-menu>div.list-group>a").click(function(e) { e.preventDefault(); $(this).siblings('a.active').removeClass("active"); $(th ...

PHP form button refuses to coordinate

My form page was functioning well, submitting form data to an ajax request without issues. However, it suddenly stopped working and no error messages are being logged. I tried identifying the problem by reviewing recent changes made to the file, but the on ...

Click to deactivate an element in an array using JavaScript

I am working on displaying an array from JavaScript in my HTML code. Here is the HTML code snippet: <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head> <div id ="containe ...

Transform the styles from a React component into Cascading Style Sheets

I have been tasked with transitioning all the styles from the JavaScript file to the CSS file while ensuring the design remains intact. I am facing an issue where using the CSS styles breaks the search field design. The original design can be seen here: S ...

What is the proper way to apply styles to HTML by using an external CSS file?

Is it possible to fetch an HTML document online, present it in a JEditorPane, and format it using Java by incorporating external CSS files or <style>...</style> tags? Currently, I am utilizing jEditorPane.setPage(URL);, but the styling is not ...