I am having trouble placing the button within the image

Essentially, my goal is to place the button within the image and ensure it remains in its position when transitioning to mobile mode or adjusting window size.

This is the desired outcome:

https://example.com/image

https://example.com/button-image

.img-wrapper {
  position: relative;
}

.img-responsive {
  max-width: 100%;
  height: auto;
  margin-top: 180px;
  margin-left: 360px;
}

a.btn {
  display: inline-block;
  padding: 0.2em 1.45em;
  margin: 0.1em;
  border: 0.15em solid #CCCCCC;
  box-sizing: border-box;
  text-decoration: none;
  font-family: 'Segoe UI', 'Roboto', sans-serif;
  font-weight: 400;
  color: #000000;
  background-color: #ffc966;
  text-align: center;
  position: relative;
}

a.btn:hover {
  border-color: #7a7a7a;
}

a.btn:active {
  background-color: #999999;
}

@media all and (max-width:30em) {
  a.btn {
    display: block;
    margin: 0.2em auto;
  }
}
   

<h1>The leading VR/AR hardware supplier in Benelux</h1>

<div class="img-wrapper">
  <img class="img-responsive" src="vrgt.png">
</div>

<a href="#" class="btn">More Info</a>

Answer №1

(Oops! Accidentally clicked on the post button too soon)... Looks like there was no CSS for the image or H1 element in your code. I went ahead and added some CSS, including a max-width property for the image in both the main CSS and responsive (media) CSS. There are still some adjustments that could be made, but I recommend taking a closer look and customizing it to fit your specific requirements. It seems to be closer to what you're aiming for!

UPDATE: I suggest incorporating more media queries to cater to various screen resolutions since absolute positioning can be tricky on devices with different sizes. You can use tools like Screenfly to test how your layout appears on multiple screens. When setting heights, avoid using large pixel-based margins (e.g., 360px), as this might cause elements like buttons to not display correctly. Instead, consider using vertical height (vh) or percentages. The [calc function] and CSS centering techniques like margin : 0 auto or text-align:center; can also come in handy.

Good luck, and I hope this guidance proves helpful!

h1 {
  text-align: center;
  font-weight: bold;
  font-size: 1.8em;
}

img {
  max-width: 70%;
  max-height: 80%;
  margin: 10px 12%;
}

img-wrapper {
  display: inline-block;
  position: relative;
}

nav {
  margin: 0 auto;
}

nav ul {
  list-style-type: none;
  background-color: lightgrey;
  width:100%;

}

nav ul li {
  min-width:18%;
  display: inline-block;
  text-align: center;
  padding: 10px;
  margin:0 auto;
}

ul li a {
  padding: 10px 15px;
  text-decoration: none;
}


/*.img-responsive {
  /* margin-top: 100px;
  margin-left: 360px;
}*/

a.btn {
  display: inline-block;
  padding: 0.2em 1.45em;
  margin: 0.1em;
  border: 0.15em solid #CCCCCC;
  box-sizing: border-box;
  text-decoration: none;
  font-family: 'Segoe UI', 'Roboto', sans-serif;
  font-weight: 400;
  color: #000000;
  background-color: #ffc966;
  text-align: center;
  position: relative;
}

a.btn:hover {
  border-color: #7a7a7a;
}

a.btn:active {
  background-color: #999999;
}

@media all and (max-width:30em) {
  img {
    max-width: 400px;
  }
  a.btn {
    display: block;
    margin: 0.2em auto;
  }
}
<h1>De zakelijke VR/AR hardware leverancier van Benelux</h1>

<nav>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Gallery</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>

<div class="img-wrapper">
  <img class="img-responsive" src="https://gilsig.ca/wp-content/uploads/2015/10/12391435_192385974441516_971157875868647436_n.jpg-North-Shore-Mountains-Dec-2015.jpg">
</div>

<a href="#" class="btn">Meer Info</a>

Answer №2

The button should be positioned absolutely in relation to its first parent element.

a.btn {
    position: absolute;
    top: 50%;
    left: 50%;
}

.img-wrapper {
  position: relative;
}

a.btn {
  position: absolute;
  top: 50%;
  left: 50%;
  padding: 0.2em 1.45em;
  margin: 0.1em;
  border: 0.15em solid #CCCCCC;
  box-sizing: border-box;
  text-decoration: none;
  font-family: 'Segoe UI', 'Roboto', sans-serif;
  font-weight: 400;
  color: #000000;
  background-color: #ffc966;
  text-align: center;
}

a.btn:hover {
  border-color: #7a7a7a;
}

a.btn:active {
  background-color: #999999;
}
<div class="img-wrapper">
  <img src="https://www.w3schools.com/css/img_5terre_wide.jpg" alt="Cinque Terre" width="1000" height="300">
</div>
<a href="#" class="btn">More Info</a>

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 assign a Bootstrap class as a variable in a custom CSS declaration?

While trying to make my website responsive, I planned on using media queries to adjust text size and other elements. The issue arises because I rely on bootstrap for setting text sizes. Is there a way to apply bootstrap classes in my CSS file? For instanc ...

Having issues with CSS list hovering functionality in Google Chrome

One of the challenges I'm facing with my HTML list is that when hovering over each list item, the text along with the bullet point should change color. Here's the structure: <ul> <li>A bullet point</li> <li>Another ...

Click on the link or tab to update the marker location on the Google Map

Can you click on Tab 2 to change the marker/location, and then go back to Tab 1 to switch it back to the original location? [Links to Fiddle] http://jsfiddle.net/ye3x8/ function initialize() { var styles = [{ stylers: [{ saturati ...

What is the best way to implement padding for two DIVS on a screen utilizing VH and VW units to ensure they fill up the

I am currently working on a layout with two columns spanning across the page. I need to add some padding around the text in each column so that it sits nicely in the middle. I have been trying to adjust the div width and use wrappers, but something seems t ...

Steps for eliminating the overlay on top of the padding region

My current code includes an overlay over images, but the overlay extends beyond the image itself and covers the padding area as well. If I switch the padding to margin in order to eliminate this unnecessary overlap, it causes the last image to be pushed on ...

Proper Method for Constructing Forms

While developing a signup form, I've noticed that I often turn to using tables for proper alignment, similar to the approach used on the MySpace signup page where colspan is utilized to evenly space out elements. Is this method going against conventio ...

Mounted class not initiating transition in Vue.js

After attempting to apply a class to an element in the mounted lifecycle, I noticed that the transition effect was not taking place. The element would immediately display in its final state: However, when I used setTimeout to delay the class change, the t ...

What criteria do browsers follow to determine the specific colors to use for border styles like inset and outset?

When I set border: 1px outset blue; as the style for an element, the browser displays two distinct border colors: one for the top and left borders, and another for the bottom and right borders. li { border: 10px outset #0000FF; color: #FFF; ...

Struggle with incorporating a file

As part of the login process, I have two options available: easy login and standard login. The easy login requires an employee ID, birthdate, and captcha answer, while the standard login asks for first name, last name, birthdate, and captcha. To facilitate ...

Generating a hyperlink within an HTML file for other HTML files that reside in the same directory as the angular framework

Currently, I am developing a website for my final HTML project at university. This project uses Angular as the main framework. The task is to create 4 articles as separate HTML pages and have the article.component.html file contain links to these pages. I ...

The image link leading to the homepage lacks the functionality to be clicked on

My website has a logo on the top left that links to the home page, but only some areas of the image are clickable. It seems like the clickable and non-clickable areas are scattered randomly. How can I ensure that the link works across the entire area of ...

Can a CSS class be designed to have a value that changes dynamically?

Let's say we have some div elements that look like this: <div class="mystyle-10">Padding 10px</div> <div class="mystyle-20">Padding 20px</div> <div class="mystyle-30">Padding 30px</div> Would it be possible to c ...

Issue with Tailwind CSS table header alignment not properly aligning to the right

Currently, I am facing an issue with tailwindcss where the headers in my table are not following the text-right / text-left / text-center directives. If you want to see the problem yourself, here's a link to the fiddle: https://jsfiddle.net/3u2jgqoc/ ...

Combining several btn-group and btn-group-vertical elements within each other

I am looking to create a button grid resembling a numpad for my project. I am utilizing angular and bootstrap 5 and thought of using btn-group and btn-group-vertical for this purpose. While this setup would work for a regular button grid, I need to have t ...

Dynamically include new events

Here is the code snippet I have in a JS file: $(".dropmenu").on("click", function(event){ event.preventDefault(); $(this).parent().find("ul").slideToggle(); }); On my webpage, I'm using the following code: var append="<ul> ...

A versatile HTML5 element that can be placed anywhere on the webpage

I am currently developing a library that assists in dynamically loading design. For example: A.EJS This is the a.ejs file B.EJS <b><%= phoxy.DeferRender('a', {}) %></b> Will be presented as <b><div id="phoxy_defe ...

Developing a form using jQuery/ajax

Is there a way to update the Contact Us form in Div without redirecting to another thank you page after submission? I want to replace the contact form in that Div with a "Thank You" message upon successful validation. Any suggestions or demo/download code ...

How can I create a component that dynamically adjusts to different screen sizes

I have developed three child components for the main content area (excluding the header) However, one of the components is not responsive. Below is the code snippet: <div className="row" style={{marginTop:'30px',}}> <div class ...

The label element in a CSS form is not displaying correctly

After clicking submit on the form located at this link: I am experiencing an issue where the validation messages are not displaying as intended. I would like them to appear next to the input elements, inline with the form. I suspect that there may be a p ...

Having trouble getting the toggle menu to work using Jquery?

I am trying to create a toggle menu using jQuery on this particular page: . The menu is located in the top right corner and I want it to appear when someone clicks on the "Menu ☰" button, and then disappear when clicked again (similar to this website: ). ...