The text outside of the button is overridden by my CSS styling

I've been working on a simple card matching game and I decided to style the buttons to enhance their appearance. However, I'm facing an issue where the styling code has caused the text to appear outside of the button. Whenever I try to adjust the text, the button also moves. Could someone please help me identify what I'm doing wrong?

Here is the complete HTML code:

<!DOCTYPE html>
<html lang="en">
<head>
    <link href="style.css" rel="stylesheet" type="text/css">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>cardgame</title>
</head>
<body>

<h1 id="result">2</h1>

<form class="cards">
    <div id="cards1"></div>
    <input id="cards1[1]" value="1" name="card1" type="radio">
    <label for="cards1[1]">1</label>
    <input id="cards1[2]" value="2" name="card1" type="radio">
    <label for="cards1[2]">2</label>
    <input id="cards1[3]" value="3" name="card1" type="radio">
    <label for="cards1[3]">3</label>
    </div>
    <div id="cards2">
        <input id="cards2[1]" value="1" name="card2" type="radio"><label for="cards2[1]"> </label>
        <input id="cards2[2]" value="2" name="card2" type="radio"><label for="cards2[2]"> </label>
        <input id="cards2[3]" value="3" name="card2" type="radio"><label for="cards2[3]"> </label>
    </div>
    <input type="submit">
</form>

<script src="script.js" charset="utf-8"></script>
</body>

</html>

Here is the complete CSS code:

.credit {
    background: lightyellow;
}

body {background-color: floralwhite}

label {
    position: relative;
    left: -15px;
    top: -3px;
    font-size: 8pt;
    opacity: .5;
}

input[type="radio"] {
    display: none;
}

input[type="radio"] + label:before {
    background-color: white;
    border: 2px solid #ccc;
    border-radius: 6px;
    content: "";
    display: inline-block;
    font-size: 12px;
    height: 80px;
    width: 50px;
    line-height: 16px;
    margin: 6px 6px 0 0;
    vertical-align: middle;
}

/*
input[type="radio"]:checked + label:before {
    border: 2px solid red;
}
*/

Answer №1

Enclose your content within a span element and experiment with its positioning. Here is an example:

<div id="cards1">
 <input id="cards1[1]" value="1" name="card1" type="radio">
 <label for="cards1[1]"><span class="text">1</span></label>
 <input id="cards1[2]" value="2" name="card1" type="radio">
 <label for="cards1[2]"><span class="text">2</span></label>
 <input id="cards1[3]" value="3" name="card1" type="radio">
 <label for="cards1[3]"><span class="text">3</span></label>
</div>

CSS:

.text {
  display: inline;
  font-size: 8pt;
  opacity: .5;
  position:relative;
  left:-35px;
}

Answer №2

If you want to fix this issue, consider converting your input elements to buttons, updating your CSS accordingly to style the buttons, and encapsulating the buttons within a div element for the card. Below is an example solution:

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <link href="style.css" rel="stylesheet" type="text/css">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>cardgame</title>
</head>
<body>

<h1 id="result">2</h1>

<form class="cards">
    <div id="cards1">
        <div class="card">
            <input id="cards1[1]" value="1" name="card1" type="button">
        </div>
        <div class="card">
            <input id="cards1[2]" value="2" name="card1" type="button">
        </div>
        <div class="card">
            <input id="cards1[3]" value="3" name="card1" type="button">           
        </div>
    </div>
    <div id="cards2">
        <input id="cards2[1]" value="1" name="card2" type="button"><label for="cards2[1]"> </label>
        <input id="cards2[2]" value="2" name="card2" type="button"><label for="cards2[2]"> </label>
        <input id="cards2[3]" value="3" name="card2" type="button"><label for="cards2[3]"> </label>
    </div>
    <input type="submit">
</form>

<script src="script.js" charset="utf-8"></script>
</body>

</html>

Here is the CSS:

.credit {
    background: lightyellow;
}

body {
    background-color: floralwhite;
}

label {
    position: relative;
    left: -15px;
    top: -3px;
    font-size: 8pt;
    opacity: .5;
    text-align: center;
}

input[type="radio"] {
    display: none;
}

input[type="button"] {
    background-color: white;
    border: 2px solid #ccc;
    border-radius: 6px;
    content: "";
    display: inline-block;
    font-size: 12px;
    height: 80px;
    width: 50px;
    line-height: 16px;
    margin: 6px 6px 0 0;
    vertical-align: middle;
}

.card {
    position: relative;
    display: inline;
}

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

Top tip: Utilize jQuery for the most effective method of adding content to the HTML of a paragraph situated

One interesting challenge I'm facing involves a textarea that stores dynamic HTML content within paragraph (p) tags. Initial HTML: <textarea rows='7' class='form-control' id='comments'><p>My variable HTM ...

What are the steps to resolve issues with URL Encoding?

Here is my website link: forumdisplay.php?462-%EE%F9%E7%F7%E9%ED-%E1%F8%F9%FA I suspect that the url is URL Encoded. I am using vbulletin 4.1.12 How can I resolve this issue? Replace 462-%EE%F9%E7%F7%E9%ED-%E1%F8%F9%FA with f=462 ...

One particular page is having trouble loading CSS, whereas it loads perfectly fine on a different page

I have two pages, 403.html and 404.html, both of which need to load the same style.css file. Strangely, when I visit the 403.html page, the CSS loads perfectly fine. However, when I navigate to the 404.html page, even though it has identical code with only ...

Angular JS basic API: Displaying only information that starts with the term 'request'

I've been given the task of developing a straightforward AngularJS API. I have managed to set up the basics for displaying data, but I'm facing an issue where the table only retrieves data from the JSON file if it starts with "request". As a resu ...

Is it permissible to have multiple class tags in HTML?

Is the HTML code below correctly formatted? <div class="navbar" class="menu">foo</div> Can two classes be applied to the same element? ...

What is the procedure for altering a particular element using ajax technology?

I have an AJAX request that updates the user information. I need to retrieve a specific value from the response and update the content of a specific element. For example, here is the element that needs to be changed: <div id="changeMe"><!-- New ...

Calculating dimensions for parents and their children

Could someone please explain to me why the size of #child is different from that of #parent? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.or ...

The translation feature in React does not seem to be functioning properly with SASS

After trying various display settings like flex and block without success, I realized that the transform property was not working as expected. Despite centering elements successfully using other methods, the transform doesn't seem to have any effect o ...

Is it possible to absolutely position an element using its own bottom edge as a reference point?

Looking to achieve precise positioning of an element just off the top of the screen, specifically a sliding menu. Utilizing position: absolute and top: 0px is straightforward, but this aligns the element based on its top edge. Is there a way to achieve th ...

Designing numerous vertical lists in HTML

I need help with displaying multiple lists vertically using HTML For example: +--------+---------------------------------------------+--+ | Global | Region Country Currency Account Type Entity | | +--------+---------------------------------------------+ ...

Choose a single SVG file based on its unique ID

Looking for some guidance - I have a vector file with 40 svgs all combined into one image. Each vector is identified by an id inside the <g> tag. How can I select a specific svg from this single file without choosing the entire image in html? PS: E ...

Wait until the link is clicked before showing the list element

Is there a way to prevent the display of list element id="two" in the code below until link "#two" has been clicked, removing element id="one"? I am looking for a CSS or JS solution that completely hides the list element rather than just hiding it from vie ...

Could multiple values be stored in the option tag in Laravel after selection?

I have encountered an issue where I am able to display all the attribute drop-down options and obtain the attribute id in the value="{{$play->id}}" after selection, which is then stored in the select with name="attribute_id". This allows me to easily fe ...

PHP: Using conditional statements to adjust datetime formatting for CSS styling

My title may not make sense, as I am new to PHP. I'm trying to create a custom member tag for my forum that shows "X Years of Experience" with different colors based on the number of years. For example, red for under 6 months, blue for year one, yell ...

What is the best way to eliminate the curved border and inset box shadow from the Material UI TextField component in React?

Check out this input field: https://i.sstatic.net/Z6URV.png Notice the inner shadow and curved borders. Take a look at the given code snippet: import React, {Component} from 'react'; import TextField from 'material-ui/TextField'; im ...

Exploring the usage of arrays within Angular 4 components. Identifying and addressing overlooked input

I'm struggling with array declaration and string interpolation in Angular 4 using TypeScript. When I define the following classes: export class MyArrayProperty { property1: string; property2: string; } export class MyComponent { @Input() object: ...

Novice problem with the <div> tag

I am currently in the process of developing a website, and I have encountered an issue with the title not staying within the designated div box at the top of the page. <div style="width:1000px;height:40px;background:grey;border:3px solid;border-radiu ...

Words fail to display

.sport { content: url("../img/sport.png"); background-color: rgba(0,0,0,1.0); top: 61px; height: 310px; width: 300px; position: absolute; margin: 0; left: 0; border-radius: 4px; overflow: hidden; } .sportsandactivis { background-colo ...

What causes RangeError: Maximum call stack size exceeded when Element UI event handlers are triggered?

I'm currently working on setting up a form and validating it with Element UI. Despite closely following the documentation, I am encountering an issue where clicking or typing into the input boxes triggers a RangeError: Maximum call stack size exceeded ...

To horizontally center a fixed element in HTML and set its width to match the parent's,

Is there a way to center a fixed div inside its parent element? I'm trying to make the fixed div have the same width as its parent, but it's not working. What could be causing this issue? Check out this example on jsFiddle Here is the HTML code ...