I want center vertical bootstrap button on an image , I do it like this :
http://jsfiddle.net/fhd031hf/
I use margin-top:-200px
, is that true or there are another better way ?
I want center vertical bootstrap button on an image , I do it like this :
http://jsfiddle.net/fhd031hf/
I use margin-top:-200px
, is that true or there are another better way ?
Your approach as you see will work but instead you could/should use position relative and absolute stylings. They are more intuitive when somebody else or yourself has to look at the code some time later again.
To achieve your desired results you have to add position: relative to the containing div and position: absolute; top: 200px; to the button.
<div style="position: relative">
<img style="width:100% ; height:400px;" class="center-block img-responsive" src="http://images.all-free-download.com/images/graphiclarge/daisy_pollen_flower_220533.jpg" />
<button style="position: absolute; top: 200px" type="button" class="btn btn-default ">TitleText</button>
</div>
http://jsfiddle.net/fhd031hf/2/
Edit: Seeing that you are using a responsive class for the image please take in consideration that both appproaches will break the design when the screen solution is smaller than the original image.
a couple of things I noticed about your fiddle:
1.) you were not resourcing bootstrap correctly. you had the .css file resourced but you were missing the .js file.
2.) your div structure in no way indicated that you were using bootstrap. Bootstrap has classes that you must attribute to your divs in order for bootstrap to work properly. Here is a getting started link for using bootstrap. Bootstrap Getting Started.
Check out this fiddle I have made for you. Instead of using an html img tag I am using a div with a background-image. Setting that div to a relative position and the button inside that div to an absolute position, it allows the button to stay exactly in the center of the image no matter what the screen resolution.
The only downside to this is you need to specify a height for the button. Other than that I uses this method all the time and it hasn't given me any issues.
.center-block.img-responsive {
background: url('http://images.all-free-download.com/images/graphiclarge/daisy_pollen_flower_220533.jpg') no-repeat center center scroll;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
position: relative;
height: 500px;
}
.btn.btn-default {
position: absolute;
width: 100px;
top: 0;
left: 0;
right: 0;
bottom: 0;
height: 40px;
margin: auto;
}