html5 - Placing checkbox over an image without using 'top' and 'position:absolute'? -


is possible put checkbox on image without using 'top' , 'position:absolute' ?

<div class="main"> <img src="http://www.standard.co.uk/incoming/article9760552.ece/binary/original/rooney.jpg" class="image"/> <input class="checkbox" type="checkbox" value="1" /> </div> 

jsfiddle

there's few possible ways. if want avoid top , absolute position using negative margins. or if want checkbox checked when image clicked wrap image in label , tie label checkbox. i've done both here.

html:

<div class="main">   <label for="checkbox">     <img src="http://www.standard.co.uk/incoming/article9760552.ece/binary/original/rooney.jpg" class="image" />   </label>   <input id="checkbox" type="checkbox" value="1" /> </div> 

css:

.image {   height: 200px;   width: 250px; }  input {   display: block;   margin-top: -200px;   position: relative; } 

a quick explanation: position: relative allows checkbox sit on top of image (z-index won't cut here), negative margin-top pulls onto image, , display: block makes top margin can applied (i'm not sure why doesn't work on inline elements.) expected have use negative margins on margin-left well, seems naturally move left on it's own. i'm not sure why either. work , not need position: absolute or top.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -