Placing images in a webpage (HTML)

While everything works fine with the CSS code for hovering effects on these images, I am facing a challenge in re-arranging their position on the page.

Currently, they are stacked vertically on top of each other. How can I make them appear side by side?

body {
      color:#000000;
      background-color:#000000;
      background-image:url('Background Image');
      background-repeat:no-repeat;
    }
    a  { color:#0000FF; }
    a:visited { color:#800080; }
    a:hover { color:#008000; }
    a:active { color:#FF0000; }
    #cf {
  position:relative;
  height:281px;
  width:450px;
  margin:0 auto;
}

#cf img {
  position:absolute;
  left:0;
  -webkit-transition: opacity 200ms ease-in-out;
  -moz-transition: opacity 200ms ease-in-out;
  -o-transition: opacity 200ms ease-in-out;
  transition: opacity 200ms ease-in-out;
}

#cf img.top:hover {
  opacity:0;
}
<a href="http://example.com/page1">
<div id="cf">
<img class="bottom" src="images/img_a_1.jpg" />
<img class="top" src="images/img_a_2.jpg" />
</div></a>

<a href="http://example.com/page2">
<div id="cf">
<img class="bottom" src="images/img_b_1.jpg" />
<img class="top" src="images/img_b_2.jpg" />
</div></a>

<a href="http://example.com/page3">
<div id="cf">
<img class="bottom" src="images/img_c_1.jpg" />
<img class="top" src="images/img_c_2.jpg" />
</div></a>

Answer №1

As mentioned by arhak earlier, it is important to ensure that IDs are unique on a page. Here are the modifications that should be made:

  • Replace each #cf with the class .cf.

  • Add display:table-cell to each .cf for side-by-side alignment.

  • Adjust the dimensions of each .cf to 50 x 50px.

  • Correct non-functional <img> src values.

  • Reduce the size of each .cf to 50 x 50px.

All changes mentioned are optional except for the first and second ones.

*While your relative URLs may work, using applicable examples will aid in clarity during troubleshooting. Using images of equivalent size can help streamline the resolution process.

SNIPPET

body {
  color: #fff;
  background-color: #000000;
  background-image: url('Background Image');
  background-repeat: no-repeat;
}
a {
  color: #0000FF;
}
a:visited {
  color: #800080;
}
a:hover {
  color: #008000;
}
a:active {
  color: #FF0000;
}
.cf {
  position: relative;
  height: 50px;
  width: 50px;
  margin: 0 auto;
  display:table-cell;
}
.cf img {
  position: absolute;
  left: 0;
  -webkit-transition: opacity 200ms ease-in-out;
  -moz-transition: opacity 200ms ease-in-out;
  -o-transition: opacity 200ms ease-in-out;
  transition: opacity 200ms ease-in-out;
}
.cf img.top:hover {
  opacity: 0;
}
<body>
  <a href="http://example.com/page1">
    <div class="cf">
      <img class="bottom" src='http://placehold.it/50x50/00f/fff?text=1'>
      <img class="top" src='http://placehold.it/50x50/cf0/000?text=2'>
    </div>
  </a>

  <a href="http://example.com/page2">
    <div class="cf">
      <img class="bottom" src='http://placehold.it/50x50/0e0/110?text=3'>
      <img class="top" src='http://placehold.it/50x50/0bb/011?text=4'>
    </div>
  </a>

  <a href="http://example.com/page3">
    <div class="cf">
      <img class="bottom" src='http://placehold.it/50x50/fff/000?text=5'>
      <img class='top' src='http://placehold.it/50x50/f00/fff?text=6'>
    </div>
  </a>
</body>

Answer №2

 body {
      color:#000000;
      background-color:#000000;
      background-image:url('Background Image');
      background-repeat:no-repeat;
    }
    a  { color:#0000FF; }
    a:visited { color:#800080; }
    a:hover { color:#008000; }
    a:active { color:#FF0000; }
    

#cf img {
  text-align: center;
}

#cf img.top:hover {
  opacity:0;
}
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<body>
<a href="http://example.com/page1">
<div class="col-xs-12 form-group" id="cf">
<img class="bottom col-xs-6" src="images/img_a_1.jpg" />
<img class="top col-xs-6" src="images/img_a_2.jpg" />
</div></a>

<a href="http://example.com/page2">
<div class="col-xs-12 form-group" id="cf">
<img class="bottom col-xs-6" src="images/img_b_1.jpg" />
<img class="top col-xs-6" src="images/img_b_2.jpg" />
</div></a>

<a href="http://example.com/page3">
<div class="col-xs-12 form-group" id="cf">
<img class="bottom col-xs-6" src="images/img_c_1.jpg" />
<img class="top col-xs-6" src="images/img_c_2.jpg" />
</div></a>
  </body>

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

Is it possible to have a linear gradient with one color being completely see-through?

I'm looking to create a unique footer design similar to the one featured on . Instead of using a solid color for the left section, I want to incorporate an image as the background. Is there a CSS technique to stack backgrounds, with an image as the ...

Set the height of a div based on the height of another div

My challenge involves a textarea that dynamically expands as content is added to it. The structure of the textarea within a div element is illustrated below: <div id='sendMes' class='container-fluid'> <form action='#&apos ...

Issue with Cross Site Scripting Iframe Permission Denied

I'm encountering a Cross Site Scripting error when using the code below. Javascript function resizeIframe(ifRef) { var ifDoc; //alert(ifRef); try { i ...

`On mobile devices, the Bootstrap button no longer displays focus changes once clicked.`

When clicking a primary bootstrap button, it changes its background color to blue on mobile devices. https://i.sstatic.net/VNPDP.jpg For example, the first button appears normal and the second one turns blue after clicking. However, this background effec ...

What is the method for altering the text color of a concealed <option> element?

Is there a way to change the text color of a hidden option in select dropdowns? I want the hidden option to display as a placeholder text, not selectable, and have its text displayed in red. However, I don't want the placeholder text to appear as an a ...

If a div is hidden, disregard this particular CSS rule

In my current scenario, I am dealing with 3 distinct divs: - Menu - Header (#rt-top-surround) - Showcase (#rt-showcase) - Main Body content (#rt-mainbody-surround) The Menu is set as 'sticky' with position: fixed. As a result, I have to adjust ...

What is the best way to apply conditional formatting to text in a Shiny app using a lookup table of hex codes?

I'm really stuck on this one. Hopefully someone out there has a solution. Let me simplify my issue for you. Imagine I have a sentence in R like this: "The quick brown fox jumped over the lazy dog." Now, let's say I want to highlight the word " ...

Spacing between table cells is only applied to the first row and footer

In an attempt to add spacing between the first row, second row, and footer of a table, I have experimented with cell spacing combined with border-collapse. However, the spacing is being applied all over the table instead of in specific areas. I am utilizin ...

Issues with aligning text in a flexbox using Bootstrap 4's text-center property

I am currently working on a project to create a webpage with two vertical columns that are clickable and lead to different pages. Here is what I have so far. HTML <!-- Choices --> <div class="container-fluid"> <div class="row"> ...

Tips for retaining a chosen selection in a dropdown box using AngularJS

How can I store the selected color value from a dropdown box into the $scope.color variable? Index.html: <label class="item item-select" name="selectName"> <span class="input-label">Choose your favorite color:</span> <select id="colo ...

My elements are overlapping one another

I've encountered an issue with my website layout - the left-menu div goes through the footer instead of pushing it down. I've tried using clear:both; and overflow:auto, but nothing seems to work. Can anyone help me figure out what's wrong he ...

The validation process fails when the button is clicked for the second time

When adding a username and email to the userlist, I am able to validate the email on initial page load. However, if I try to enter an invalid email for the second time and click the add button, it does not work. <form id="myform"> <h2>Ad ...

Endlessly tapping through each tab with no direction

I am attempting to click on all unopened tabs (elements) randomly across this page. While the code below usually does the trick, it sometimes doesn't click on all elements. I suspect that there might be an issue with how it loads indexes or some othe ...

Scroll Bar Menu (User-Controlled)

I'm looking for any libraries out there that can replicate or provide a similar horizontal scrolling menu to that of espn.com's homepage. I'm relatively new to jquery and feeling a bit lost on where to begin. I did some searching on Google, ...

Enhancing HTML Layouts using Visual Basic for Applications

I'm currently experiencing some difficulties when embedding HTML into VBA to send an email. The formatting is not appearing as intended, and I've been experimenting with different options. My first concern is regarding the font size displayed in ...

Extract the URL parameter and eliminate all characters following the final forward slash

Here is the URL of my website: example http://mypage.com/en/?site=main. Following this is a combination of JavaScript and PHP code that parses the data on the site. I am now looking for a piece of code that can dynamically change the URL displayed in the ...

Radio button - arranging the background image and text in alignment

I'm struggling to align my custom radio buttons alongside text. Here is an example image: https://i.sstatic.net/2g5w8.jpg Despite using CSS for styling, I am unable to properly align the radio buttons. Below is a snippet of my HTML: label.item { ...

Designing a slider to display a collection of images

I am currently working on a slider project using HTML, javascript, and CSS. I'm facing an issue where only the first two images (li) are being displayed and it's not transitioning to the other (li) elements. Below is the HTML code snippet: <se ...

What's the best way to customize a partially collapsible Navbar in Bootstrap 4 to display items exactly where I need them to

Is there a way to make a Bootstrap 4 Navbar only partially collapse? I want certain items on the Navbar aligned to the right to always be visible, while the rest of the Navbar items on the left will collapse into a menu when viewed on mobile devices. The ...

The header, main content, and footer sections expand to occupy the entire page

I am in need of HTML structure that looks like the following: <header></header> <div id='main'></div> <footer></footer> I want all elements to be positioned relatively. The header and footer will have fixed h ...