Position the Radio Buttons on Top of the Image

I need assistance with positioning radio buttons on top of an image at a specific location. Both the image and radio buttons are currently in a Div container, but I am unsure about the next steps. Below is where I would like to place the radio buttons (red circles):

Here is what my code looks like at the moment:

.center {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
}

body {
  background-color: #979797
}
<div class="general">
  <img src="https://via.placeholder.com/300" class="center">
  <form action="">
    <input type="radio" name="answer 1" value="apple">
    <input type="radio" name="answer 2" value="chicken">
    <input type="radio" name="answer 3" value="carrot">
  </form>
</div>

Your help is greatly appreciated!

Answer №1

To enhance the design of the radio buttons, you can place each button in a separate div and then apply a background to the div. Here is an example:

<html>
<head>
<style>
.general{}
    .center {
      display: block;
      margin-left: auto;
      margin-right: auto;
      width: 50%;
    }
    .radio-size {
      height: 100px;
      width: 100px;
    }
    .bg-apple {
      background-image:url('https://images.unsplash.com/photo-1513677785800-9df79ae4b10b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80');
      background-size: cover;
    }
    .bg-chicken {
      background-image:url('https://images.unsplash.com/photo-1426869981800-95ebf51ce900?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80');
      background-size: cover;
    }
    .bg-carrot {
      background-image:url('https://images.unsplash.com/photo-1445282768818-728615cc910a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80');
      background-size: cover;
    }
    
</style>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>

<body bgcolor="#979797">

<div class="general">
<img src="https://images.unsplash.com/photo-1518796745738-41048802f99a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80" class="center" style="width:20%;" >


  <form action="" style="background-img:url("")">
    <div class="radio-size bg-apple">
      <input type="radio" name="answer 1" value="apple">
    </div>
    <div class="radio-size bg-chicken">
      <input type="radio" name="answer 1" value="chicken"> <br>
    </div>
    <div class="radio-size bg-carrot">
      <input type="radio" name="answer 1" value="carrot"> 
    </div>
  </form>
</div>


</body>
</html>

note: Make sure all radio buttons have the same "name" attribute to allow selecting only one option. For multiple selections, consider using checkboxes instead.

Answer №2

It seems like there may be some confusion about the positioning of the radio buttons in relation to the image. In the picture provided, the radio buttons appear to be on the right side of the image. If you need assistance with adjusting their position, feel free to let me know.

Below is a simple example that I have created. You can customize the positioning as needed or ask for further guidance.

P.S. Remember, using the bgcolor attribute in the <body> tag is not supported in HTML5. It's recommended to utilize CSS instead.

.center {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 50%;
}

body {
  background-color: #979797
}

form {
  position:absolute;
  right: 25%;
  top: 50%;
  transform:translateY(-50%) translateX(100%);
  display: flex;
  flex-direction: column;
}

.general {
  position: relative;
}
<div class="general">
  <img src="http://via.placeholder.com/640x360" class="center">
  <form action="">
    <input type="radio" name="answer 1" value="apple">
    <input type="radio" name="answer 2" value="chicken">
    <input type="radio" name="answer 3" value="carrot">
  </form>
</div>

Answer №3

Here is a solution using flexbox:

body {
  background:#979797;
}
.question {
  display:flex;
  flex-direction:row;
  flex-wrap:nowrap;
}
.answers {
  display:flex;
  flex-direction:column;
}
label.answer {
  position:relative;
  height:100px;
}
label.answer input[type="radio"] {
  top:0;
  left:0;
  position:absolute;
}
<form action="">
  <h2>Question 1:</h2>
  <div class="question">
    <img src="https://picsum.photos/300">
    <div class="answers">
      <label class="answer">
        <img src="https://picsum.photos/100">
        <input type="radio" name="answer1" value="apple">
      </label>
      <label class="answer">
        <img src="https://picsum.photos/100">
        <input type="radio" name="answer1" value="chicken">
      </label>
      <label class="answer">
        <img src="https://picsum.photos/100">
        <input type="radio" name="answer1" value="carrot"> 
      </label>
    </div>
  </div>
  <h2>Question 2:</h2>
  <div class="question">
    <img src="https://picsum.photos/300">
    <div class="answers">
      <label class="answer">
        <img src="https://picsum.photos/100">
        <input type="radio" name="answer2" value="apple">
      </label>
      <label class="answer">
        <img src="https://picsum.photos/100">
        <input type="radio" name="answer2" value="chicken">
      </label>
      <label class="answer">
        <img src="https://picsum.photos/100">
        <input type="radio" name="answer2" value="carrot"> 
      </label>
    </div>
  </div>
</form>

Answer №4

If you want to improve the query, consider implementing the code below. I made adjustments to the HTML structure and completely revamped the CSS:

.general{
  width: fit-content;
}
.img{
  display: flex;
  align-items: end;
}
img{
  border: 1px solid black;
}
body {
  background-color: #979797
}
.options{
  display: flex;
  flex-direction: column;
}
.radio{
  position: relative;
  height: 50px;
}
input[type="radio"]{
  position: absolute;
  top: calc(50% - 4px);
  right: 0;
  margin: 0;
}
<div class="general">
  <form action="">
    <div class="img">
      <img src="https://via.placeholder.com/150" class="center">
      <div class="options" class="center">
        <div class="radio">
          <img src="https://via.placeholder.com/50">
          <input type="radio" name="answer1" value="apple">
        </div>
        <div class="radio">
          <img src="https://via.placeholder.com/50">
          <input type="radio" name="answer1" value="chicken">
        </div>
        <div class="radio">
          <img src="https://via.placeholder.com/50">
          <input type="radio" name="answer1" value="carrot">
        </div>
      </div>
    </div>
  </form>
</div>

It's important to note that if the questions only allow a single answer, the radio box should have the same name with different values. Otherwise, all radio buttons will be selected together and cannot be deselected.

This updated code should serve your needs. Thanks.

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

No data retrieved from database using PDO, Empty Table

I am struggling to integrate MySQL database information into HTML. Despite researching extensively on this forum for solutions, I have not been successful. The code below aims to retrieve data such as courseNumber, section, instructor, startTime, endTime ...

Determine whether a click event originated from within a child window

Currently, I am utilizing window.open to initiate a new window in javascript and my goal is to identify clicks made within the child window. Essentially, if a click event occurs in the child window, I aim to modify the parent window accordingly. I have a ...

Using JavaFX to showcase FontAwesome icons

I've been working on a project to create a custom icon class that can load FontAwesome icons, but I'm facing issues with reading the fontawesome-webfont.ttf file. Here is the code for creating an icon: import javafx.scene.control.Label; import ...

`Enhancing the appearance of code in PHP`

I'm struggling with customizing the data pulled from the database. Can anyone provide assistance? I attempted to define $style within the while loop and assign it to $questions, but nothing is displaying on the webpage. While I have some familiarity w ...

The significance of the "$=" or "?=" symbols in lit-element illustrations

I'm struggling to comprehend the purpose of ?= or $= in these two instances: First Example: Lit-Element README <div id="box" class$="${this.uppercase ? 'uppercase' : ''}"> <slot>Hello World</slot> </div> ...

Responsive background image not displaying properly

The developer site can be found at http://www.clhdesigns.com/cliwork/wintermeresod/about.php I am encountering an issue where the background image on the home page scales perfectly, but on the about us page it does not scale at all. I am seeking a solutio ...

Changing the Title of UI Tabs

I am facing an issue with setting the title in tabs, as the title is appearing behind the background. If I set background: transparent; in the #tabss .ui-tabs-nav class, then my title shows up properly. Otherwise, it gets displayed behind the background. ...

Custom property fallback values do not function properly with CSS Variables

I defined a custom property in the :root selector as shown below, but for some reason my color is not rendering correctly. Can someone please help me identify what I am doing wrong? :root { --color-primary: var(--color-primary, #ffc107); } body { ...

Combine multiple jQuery click functions into a single function

One issue I am facing on my website is that there are multiple modules, each of which can be disabled by the user. The problem lies in the fact that I have a separate script for each module. Hence, my query: How can I combine these scripts into one (perhap ...

Custom sized box causing Bootstrap Navbar to be unresponsive

I am having trouble loading the Bootstrap 3 navbar inside a .box class with specific CSS rules: .box { height: 600px; width: 320px; position: relative; border: 2px solid #eee; } Unfortunately, it is not displaying re ...

Refreshing a webpage to accurately display changes made in a CRUD application without the need for a hard reset

My to-do app is almost fully functional - I can create items, mark them as completed, and delete them using a delete button. However, there's one issue: when I delete an item, the page doesn't update in real-time. I have to manually refresh the p ...

The alignment of flexbox within a list item is not positioned at the top as expected

When I place a flexbox inside a list item, the content is being pushed down by what seems to be a full line height. I have tried various CSS properties like setting margin-top: 0 for the flexbox, margin-top: 0 for its children, adding flex-wrap to the fle ...

Achieve a full page layout with content and attach the footer to the bottom using flexbox in Tailwindcss and Nextjs

How can I use flex-grow to make the body section fill the screen and keep the nav fixed at the bottom? I'm having trouble figuring out which elements to apply these properties to. Can anyone provide guidance on which element should have these styles? ...

Ensure that any modifications made to an Angular service are reflected across all components that rely on that service

I am currently in the process of replicating a platform known as Kualitee.com, which serves as a test-management tool utilized by QA Engineers. Within Kualitee, users can access multiple projects, each containing various test cases and team members. The ab ...

Stopping CSS animations at a specific point along the path without using timing functions can be achieved by implementing a targeted

Is there a way to pause an animation at the 50% mark for 2 seconds and then resume? P.S. The setInterval method is not recommended! function pauseAnimation() { document.getElementById("0").style.animationPlayState = "paused"; }; var pauseInterval = set ...

Maintain the link's functionality even after it's been clicked by utilizing the same

I have a fixed navbar with same-page anchors and I want to keep the link active after it has been clicked and taken to that section of the page. While this is simple for links to another page, I'm struggling to figure out how to do it here. Here&apos ...

Using JavaScript and HTML, showcase the capital city of a country along with its corresponding continent

As a newcomer to this platform, I am excited to share a code snippet that I have created using HTML and JavaScript. The code generates a textbox in an HTML page where users can type the name of a country. Upon inputting the country's name, the code dy ...

I encountered an issue with my foreach loop where the checkbox failed to properly send the value

Having an issue with the checkbox not sending the desired value. This is my HTML code for the checkbox. I am using a foreach loop. <form id="signupform" autocomplete="off" method="post" action="inputchecklist.php" class="form_container left_label"> ...

What is the best way to create square editor tabs in Eclipse without using swt-border-radius?

The design of Eclipse's rounded and/or swooshing tabs is starting to feel outdated in today's modern era. I thought that by adding the following line in my default.css file, I could get rid of the rounded corners: swt-corner-radius: 0px Howeve ...

Is there a solution available for tidying and organizing a CSS stylesheet efficiently?

In the project I am currently working on, the previous developers took an interesting approach by constructing all the selectors based on individual properties. For instance: .panel .selected a, a.selected-thumb, .date-picker input[type="submit"], .headi ...