Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Button over image using CSS?

Tags:

html

css

i am new in CSS so sorry if this question is just stupid or too simple but i just dont know how to do it.

I need to place a button over the image, how it should looks: enter image description here

You see there a blue button "Kopit" Thats IT! i style this thing already to my website but as one single image so my code looks like:

CSS:

#shop{
    background-image: url("images/shop_bg.png");
    background-repeat: repeat-x;    
    height:121px;
    width: 984px;
    margin-left: 20px;
    margin-top: 13px;
}

#shop .content{    
    width: 182px; /*328 co je 1/3 - 20margin left*/
    height: 121px;
    line-height: 20px;
    margin-top: 0px;
    margin-left: 9px;
    margin-right:0px;
    display:inline-block;

}

My HTML:

 <div id="shop">
      <div class="content"> Counter-Strike 1.6 Steam <a href="#"><img src="images/CSsteam.png"></a></div>
      <div class="content"> Counter-Strike 1.6 Steam <a href="#"><img src="images/CSsteam.png"></a></div>
      <div class="content"> Counter-Strike 1.6 Steam <a href="#"><img src="images/CSsteam.png"></a></div>
      <div class="content"> Counter-Strike 1.6 Steam <a href="#"><img src="images/CSsteam.png"></a></div>
      <div class="content"> Counter-Strike 1.6 Steam <a href="#"><img src="images/CSsteam.png"></a></div>         
  </div>

Is here anybody who can help me to style that button as a different element?

Thanks a lot for reading this post.

like image 843
user258459 Avatar asked Apr 23 '14 07:04

user258459


People also ask

How do I display a button over an image in CSS?

To add a button to an image, first, take a <div> element and use position: relative property to it. Set the width of the container and add an image with width: 100% so that the image covers the whole container. Now add a <button> with position: absolute to place it over the image.

How do I display a button over an image in HTML?

Show activity on this post. You need to give relative or absolute or fixed positioning to your container ( #shop ) and set its zIndex to say 100. You also need to give say relative positioning to your elements with the class content and lower zIndex say 97.

Can we add a button on an image?

The image buttons in the HTML document can be created by using the type attribute of an <input> element. Image buttons also perform the same function as submit buttons, but the only difference between them is that you can keep the image of your choice as a button.

How do I overlap an image button?

Set the position property value for the parent to relative (to control overlap) and that of the button DIV to absolute . Now place your button inside the button DIV , then use negative values for left , right , top or bottom to position the button DIV where you want.


3 Answers

Adapt this example to your code

HTML

<div class="img-holder">
    <img src="images/img-1.png" alt="image description"/>
    <a class="link" href=""></a>
</div>

CSS

.img-holder {position: relative;}
.img-holder .link {
    position: absolute;
    bottom: 10px; /*your button position*/
    right: 10px; /*your button position*/
}
like image 34
AlexPrinceton Avatar answered Oct 16 '22 14:10

AlexPrinceton


If I understood correctly, I would change the HTML to something like this:

<div id="shop">
    <div class="content">
        <img src="http://placehold.it/182x121"/> 
        <a href="#">Counter-Strike 1.6 Steam</a>
    </div>
</div>

Then I would be able to use position:absolute and position:relative to force the blue button down.

I have created a jsfiddle: http://jsfiddle.net/y9w99/

like image 56
TryingToImprove Avatar answered Oct 16 '22 14:10

TryingToImprove


You need to give relative or absolute or fixed positioning to your container (#shop) and set its zIndex to say 100.

You also need to give say relative positioning to your elements with the class content and lower zIndex say 97.

Do the above-mentioned with your images too and set their zIndex to 91.

And then position your button higher by setting its position to absolute and zIndex to 95

See the DEMO

HTML

<div id="shop">

 <div class="content"> Counter-Strike 1.6 Steam 

     <img src="http://www.openvms.org/images/samples/130x130.gif">

         <a href="#"><span class='span'><span></a>

     </div>

 <div class="content"> Counter-Strike 1.6 Steam 

     <img src="http://www.openvms.org/images/samples/130x130.gif">

         <a href="#"><span class='span'><span></a>

     </div>

  </div>

CSS

#shop{
    background-image: url("images/shop_bg.png");
    background-repeat: repeat-x;    
    height:121px;
    width: 984px;
    margin-left: 20px;
    margin-top: 13px;
    position:relative;
    z-index:100
}

#shop .content{    
    width: 182px; /*328 co je 1/3 - 20margin left*/
    height: 121px;
    line-height: 20px;
    margin-top: 0px;
    margin-left: 9px;
    margin-right:0px;
    display:inline-block;
    position:relative;
    z-index:97

}

img{

    position:relative;
    z-index:91

}

.span{

    width:70px;
    height:40px;
    border:1px solid red;
    position:absolute;
    z-index:95;
    right:60px;
    bottom:-20px;

}
like image 2
W.D. Avatar answered Oct 16 '22 12:10

W.D.