Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fit in an image inside span tag when onclick?

Tags:

html

jquery

css

I have an image inside a span tag. But the problem is the image doesn't fit inside the span tag. Instead a part of the image goes out of the span tag.

I want to have when onclick on the word span it change the image color while when i onclick on that image it will change the color of the text. I guess something wrong on my code.

How can I achieve it?

MY HTML :

<div class="games-platform-item pt-item">
<ul class="games-sub-menu clearfix">
<li class="tab1 current">
    <!-- <img src="../images/abc11.jpg"/ class="topimgG1" "> -->
    <input type="radio" name="imgSwap" id="rdoImg1">
    <label class="topimgG1" for="rdoImg1"></label>
    <span>编辑精选</span>
</li>
<li class="tab2">
    <div>
        <!-- <img src="../images/abc2.jpg"/ class="topimgG2" "> -->
        <input type="radio" name="imgSwap" id="rdoImg2">
        <label class="topimgG2" for="rdoImg2"></label>
        <span>老虎机</span>
    </div>
</li>
<li class="tab3">
    <!-- <img src="../images/abc3.jpg"/ class="topimgG3" "> -->
    <input type="radio" name="imgSwap" id="rdoImg3">
    <label class="topimgG3" for="rdoImg3"></label>
    <span>桌面游戏</span>
</li>
<li class="tab4">
    <!-- <img src="../images/abc4.jpg"/ class="topimgG4" "> -->
    <input type="radio" name="imgSwap" id="rdoImg4">
    <label class="topimgG4" for="rdoImg4"></label>
    <span>累计大奖</span>
</li>
<li class="tab5">
    <!-- <img src="../images/abc5.jpg"/ class="topimgG5" "> -->
    <input type="radio" name="imgSwap" id="rdoImg5">
    <label class="topimgG5" for="rdoImg5"></label>
    <span>小游戏</span>
</li>
<li class="tab6">
    <!-- <img src="../images/abc6.jpg"/ class="topimgG6" ">   -->
    <input type="radio" name="imgSwap" id="rdoImg6">
    <label class="topimgG6" for="rdoImg6"></label>
    <span>视频扑克</span>
</li>
<li class="tab7">
    <!-- <img src="../images/abc7.jpg"/ class="topimgG7" "> -->
    <input type="radio" name="imgSwap" id="rdoImg7">
    <label class="topimgG7" for="rdoImg7"></label>
    <span>所有游戏</span>
</li>

MY CSS :

/*Hide the Radio Button*/

.games-sub-menu input[type=radio] {
    display: none
}
/*Set a box for the label, this is what is clicked on*/

.games-sub-menu label {
    display: block;
    width: 150px;
    height: 100px;
}
/*Set Images...this would work better with sprites*/

.games-sub-menu label.topimgG1 {
    background-image: url("../images/abc1.jpg");
    background-repeat: no-repeat;
    width: 80px;
    height: 40px;
    right: 820px;
    top: 0px;
    position: absolute;
    z-index: 999;
}
.games-sub-menu input[type=radio]:checked + label.topimgG1 {
    background-image: url("../images/abc1_onclick.jpg");
}
.games-sub-menu label.topimgG2 {
    background-image: url("../images/abc2.jpg");
    background-repeat: no-repeat;
    width: 80px;
    height: 40px;
    right: 690px;
    top: 0px;
    position: absolute;
    z-index: 999;
}
.games-sub-menu input[type=radio]:checked + label.topimgG2 {
    background-image: url("../images/abc2_onclick.jpg");
}
.games-sub-menu label.topimgG3 {
    background-image: url("../images/abc3.jpg");
    background-repeat: no-repeat;
    width: 80px;
    height: 40px;
    right: 560px;
    top: 0px;
    position: absolute;
    z-index: 999;
    clear: both;
}
.games-sub-menu input[type=radio]:checked + label.topimgG3 {
    background-image: url("../images/abc3_onclick.jpg");
}
.games-sub-menu label.topimgG4 {
    background-image: url("../images/abc4.jpg");
    background-repeat: no-repeat;
    width: 80px;
    height: 40px;
    right: 430px;
    top: 0px;
    position: absolute;
    z-index: 999;
}
.games-sub-menu input[type=radio]:checked + label.topimgG4 {
    background-image: url("../images/abc4_onclick.jpg");
}
.games-sub-menu label.topimgG5 {
    background-image: url("../images/abc5.jpg");
    background-repeat: no-repeat;
    width: 80px;
    height: 40px;
    right: 305px;
    top: 0px;
    position: absolute;
    z-index: 999;
}
.games-sub-menu input[type=radio]:checked + label.topimgG5 {
    background-image: url("../images/abc5_onclick.jpg");
}
.games-sub-menu label.topimgG6 {
    background-image: url("../images/abc6.jpg");
    background-repeat: no-repeat;
    width: 80px;
    height: 40px;
    right: 18 5px;
    top: 0px;
    position: absolute;
    z-index: 999;
}
.games-sub-menu input[type=radio]:checked + label.topimgG6 {
    background-image: url("../images/abc6_onclick.jpg");
}
.games-sub-menu label.topimgG7 {
    background-image: url("../images/abc7.jpg");
    background-repeat: no-repeat;
    width: 80px;
    height: 40px;
    right: 43px;
    top: 5px;
    position: absolute;
    z-index: 999;
}
.games-sub-menu input[type=radio]:checked + label.topimgG7 {
    background-image: url("../images/abc7_onclick.jpg");
}
like image 373
Morgan Ng Avatar asked Sep 19 '16 06:09

Morgan Ng


People also ask

Can I put an image in a span tag?

Usually span element is used to group inline elements together to implement style (using id or class attributes) or language information or JavaScript DOM(using id or class attributes), when no other element is found suitable to form that group. 3. The element can contain a piece of text or an image directly.

Does span support onclick?

span element has onclick event to handle mouse clicks.

How do I center an image in a span?

To center an inline element like a link or a span or an img, all you need is text-align: center . For multiple inline elements, the process is similar. It's possible by using text-align: center .

How do I put an image size in HTML?

Step 1: Firstly, we have to type the Html code in any text editor or open the existing Html file in the text editor in which we want to change the size of an image. Step 2: Now, place the cursor inside the img tag. And then, we have to use the height and width attribute of the img tag for changing the size of an image.


1 Answers

I want to have when onclick on the word span it change the image color while when i onclick on that image it will change the color of the text.

If interpret Question correctly, you can use <input type="checkbox"> with two <label> elements as adjacent siblings, both with for attribute referencing previous <input type="checkbox"> element

input {
  display: none;
}
label {
  width: 50px;
  height: 30px;
  background: blue;
  display: block;
}
:checked + label + label {
  background: red;
  color: white;
}
:checked + label {
  background: green;
}
<input type="checkbox" id="1">
<label for="1"></label>
<label for="1">text</label>
like image 170
guest271314 Avatar answered Nov 01 '22 09:11

guest271314