Trying to use an image and text inside a <button> - html

Im trying to have a button with format like I have in my image 1.
Where I have a div with background red at left, and inside this div I have an image, and I want this image aligned at center of my div.
And then I have the text of my button at right of my red div.
Image 1:
But What Im having is this:
Image 2
I dont have my image at center of my red div, and my button text is stucked in my div.
Do you see what Im doing wrong?
This is my fiddle with issue Im having: http://jsfiddle.net/nXuU2/2/
This is my html
<button class="btn">
<div id="btn_container"><img src="http://placehold.it/30x30" width="30" height="30"/></div>
<span>Button link 1</span>
</button>
And my CSS:
.btn
{
position: relative;
width: 200px;
height: 60px;
margin-bottom:20px;
padding: 0 10px;
text-align: left;
font-size: 16px;
color: #333;
background:gray;
}
#btn_container
{
width:40px;
height:40px;
border-radius:5px;
background:red;
float:left;
}
.btn img
{
margin-right: 10px;
vertical-align:middle;
}
.btn span
{
display:inline-block;
width: 120px;
vertical-align:middle;
}

Just playing with CSS positioning here, make your div with the red background position: relative; and use absolute for your img tag
Demo
.btn img {
left: 50%;
margin-left: -15px; /* 1/2 of total width of your img */
top: 50%;
margin-top: -15px; /* 1/2 of total height of your img */
position: absolute;
}
Demo 2 (Alternate way to do this, you won't need CSS positioning)
In the Demo 2, you won't need CSS positioning, just remove margin-right from .btn img and use text-align: center; on #btn_container, and lastly, apply margin-top to .btn img
I don't think that you will need a wrapping element for background-color, just apply to img tag, so if you don't want to use your wrapping element than refer the solution below ..
CSS
.btn img {
display: inline-block;
vertical-align: middle;
background: #f00;
padding: 5px;
border-radius: 5px;
}
HTML
<button class="btn">
<img src="http://placehold.it/30x30" width="30" height="30"/>
<span>Button link 1</span>
</button>
Demo 3

Try out the below HTML and CSS code:
HTML Code:
<button class="btn">
<div id="btn_container">
<img src="http://placehold.it/30x30" width="30" height="30"/>
<span class="btn-txt">Button link 1</span>
</div>
</button>
CSS Code:
.btn {
background: #fff;
border:3px solid grey;
padding: 5px;
width: 200px;
text-align:left;
}
#btn_container img {
border: 6px solid red;
margin-right: 5px;
vertical-align: middle;
}
.btn-txt {
vertical-align: middle;
}
Check out the DEMO on JSFiddle

You can fix by updating your margins for your img and span
.btn img
{
margin-left: 5px;
margin-top: 5px;
}
.btn span
{
display:inline-block;
width: 120px;
margin-top: 10px;
margin-left: 10px;
}

In addition to the answer above, you can add line-height to match the height of your container to force the button text into a centered position. Also, I used text-indent to push the text away from the image.
.btn span { line-height: 60px; text-indent: 10px;}
.btn img { margin: 5px; }
#btn_container { margin: 10px 0 0 0; }
Fiddle

Related

Thumbnail hover caption centered with padding/margin?

I want to create a white hover caption that is centered vertical and horizontal. Around that i would like to add the same padding/margin and within some padding/margin with the title/caption centered, horizontal and vertical.
Because everything is responsive i think is best to use % for the most of the elements. Maybe the structure of the thumbnail/caption (html) needs to be written differently, i'm a bit stuck now.
In the example image i added some red marks what i mean.
Example:
---> FIDDLE
<div class="col-4">
<a class="thumb" href="#">
<img src="http://fakeimg.pl/500x330/ccc/">
<div class="caption"><span>Project title centered vertical and horizontal</span></div>
</a>
</div>
One option can be the use of inline-block on the span element to vertical algin, and for the space use padding and box-sizing on caption. Check this:
.caption {
position: absolute;
width: 80%;
height: 80%;
top: 10%;
left: 10%;
background-color: white;
text-align: center;
opacity: 0;
-webkit-transition: all 0.3s;
transition: all 0.3s;
box-sizing:border-box;
padding:5px;
}
.caption span {
display: inline-block;
opacity: 0;
vertical-align:middle;
color: #111;
text-transform: uppercase;
letter-spacing: 1px;
background-color: white;
}
.caption:before {
content:" ";
height:100%;
width:0;
display:inline-block;
vertical-align:middle;
}
DemoFiddle
Check this fiddle.
Display the parent as a table-cell and use vertical align to center everything
.block {
background-color: #eee;
min-height: 200px;
display: table-cell;
vertical-align: center;
text-align: center;
}
.block-contents {
background-color: #fff;
padding: 20px;
margin: 20px;
}
You could always position the span absolutely too.
.caption span {
display: block;
position: absolute;
top:50%;
left:50%;
width:100%;
transform: translate(-50%, -50%);
color: #111;
text-transform: uppercase;
letter-spacing: 1px;
background-color: white;
}
JSfiddle Demo

how to do a vertical align in my case

I am trying to do a vertical align for my texts. I also want to make sure the green background div need to cover from top to bottom inside the red color div. Currently the green color div only covers 90% of the red oolor div. I am not sure what happened in my case. Can anyone explain and help me out?
html
<div id='wrapper'>
<div class='head'></div>
<h2 class='title'>Warm-Up</h2>
</div>
css
.title{
display: inline;
padding-left: 15px;
vertical-align: middle;
margin: 0;
}
.head{
width: 30px;
height: 50px;
display: inline-block;
background-color: #A9D075;
}
#wrapper{
width:200px;
background-color: red;
}
http://jsfiddle.net/rmS2f/3/
Thanks.
Demo http://jsfiddle.net/rmS2f/6/
Your html structure will work but you need to change the styles:
.title {
display: inline-block;
padding-left: 45px;
vertical-align: middle;
margin: 0;
line-height:50px;
}
.head {
position:absolute;
left:0;
width: 30px;
height: 100%;
display: inline-block;
background-color: #A9D075;
}
#wrapper {
position:relative;
width:200px;
height:50px;
background-color: red;
}

Vertically align a text with an image inside a div

I saw a similar post to this which is here but I noticed that no one answered and that when I do press the jsfiddle, it doesn't center it.
I have this current code and what I'm trying to accomplish is to have the image directly in the middle vertically inside the div. I've tried vertical-align and putting it in span and top % but nothing seems to work. I'm also using Chrome so I don't know if that matters.
http://jsfiddle.net/y84mx/
<div class="hours">
<div id="sun">
<img src="http://jsfiddle.net/img/logo.png" alt="Opening" height="auto" width="20%" align="left" vertical-align="middle" />
<span>Hours</span>
</div>
</div>
Thanks!
You can use:
#sun {
position:relative;
}
img {
background: #3A6F9A;
position:absolute;
top:0;
bottom:0;
left:0;
margin:auto;
}
Updated Fiddle
For both items set display: inline-block;
Then make sure to match the line-height of the <span> the height of the image.
Demo
HTML
<div id="hours">
<span id="sun">
<img src="http://jsfiddle.net/img/logo.png" alt="twibuffer" />
</span>
<span> Hours</span>
</div>
CSS
#hours {
margin-bottom:2%;
text-align: center;
}
#hours span {
display: inline-block;
vertical-align: middle;
}
#hours #sun {
border: 1px solid red;
/* to show the centering */
}
I have updated the fiddle, Please check
#sun{
line-height : auto;
border: 1px solid;
text-align : center;
}
.hours{
padding-left: 2%;
padding-right: 2%;
text-align: right;
color: #000;
font-size: 40px;
}
img {
background: #3A6F9A;
}
Check out this
http://jsfiddle.net/chetangawai/y84mx/9/
#sun{
text-align:center;
display:inline-block;
border:solid
}
.hours{
color: #000;
font-size: 40px;
padding-left: 2%;
padding-right: 2%;
}
img {
background: #3A6F9A;
}

Span and img tags as vertical center for div

I have div tag, in him is span and img tags, I want that span and img will be as vertical center for div, I am truing this but for span tag margin-top not works and I can not centering span tag. What is solution?
<div>
<span>
some text
</span>
<img src="..." />
</div>
css:
div {
width: 200px;
height: 40px;
background: #ccc
}
span {
margin-top: -10px;
}
img {
width:20px;
height: 20px;
margin-top: 10px;
}
this is demo http://jsfiddle.net/Q9utY/4/
Try this with padding
div {
width: 200px;
padding: 20px;
background: #ccc
}
img {
width:20px;
height: 20px;
vertical-align: middle;
}
http://jsbin.com/isijop/1/
and here we can use line-height with height
div {
width: 200px;
height: 40px;
line-height: 40px;
background: #ccc
}
img {
width:20px;
height: 20px;
vertical-align: middle;
}
http://jsbin.com/apawog/1/
Span is by default inline element, so margin doesn't affect it.
to fix this, add display:block or display:inline-block to span.
But personally I don't think this is the best way to do it. Here is some article with 6 ways of doing it.
6 Methods For Vertical Centering With CSS
Add vertical-align:middle for img class.
vertical-align:middle;
Try here
http://jsfiddle.net/Q9utY/4/

Horizontal positioning with unknown width (and small parent element)

I'd like to create simple tooltip in CSS3.
Example: http://jsfiddle.net/Cg2SX/
Example HTML (can be changed if necessary):
<div class="icons">
t <span class="tooltip">Twitter</span>
f <span class="tooltip">Facebook</span>
g <span class="tooltip">Google+</span>
</div>​
And CSS:
.icons { position: absolute; left: 40px; top: 30px; }
.icons a { text-decoration:none; font-size: 16px; color: #000000; position: relative; margin-right: 70px; border:1px solid red; }
.icons a:hover { text-decoration:none; }
.tooltip { background-color: green; color: #FFFFFF; font-family: Arial,sans-serif; font-size: 10px; padding: 2px 8px; bottom: -20px; position: absolute; }​
The problem is - I have no idea how to center tooltips below sharing icons (they will be font icons). It wouldn't be complicated if I knew their width but I don't.
Any ideas appreciated. Is it possible anyway?
You could try doing it like this:
updated fiddle
Using a fixed (big enough) width on the span, setting text-align: center on it & putting the text in a pseudo-element to which you give display: inline-block
HTML:
<div class="icons">
<a href="#">t
<span class="tooltip" data-text='Twitter'></span>
</a>
<a href="#">f
<span class="tooltip" data-text="Facebook"></span>
</a>
<a href="#">g
<span class="tooltip" data-text='Google+'></span>
</a>
</div>​
Relevant CSS:
.icons a {
display: inline-block;
position: relative;
margin-right: 70px;
width: 16px;
color: #000;
font-size: 16px;
text-align: center;
text-decoration: none;
}
.tooltip {
position: absolute;
bottom: -20px; left: 8px; /* half the width of link */
margin-left: -35px;
width: 70px;
color: #fff;
font: 10px Arial, sans-serif;
}
.tooltip:after {
display: inline-block;
padding: 2px 8px;
background-color: green;
content: attr(data-text);
}
You could always do something like this demo:
http://jsfiddle.net/Cg2SX/1/
I updated a tags then inside .icons div. Here's what I changed:
.icons a {
display:block;
margin-right:10px /* spacing between a tags - changed from your original code */
float:left;
text-align:center;
width:100px; /* you can change this as needed, but it keeps them all uniform*/
height: 50px; /* Change this to the height of your tallest icon image */
}
Then I update the span.tooltip accordingly, I added this to what you had:
.tooltip {
width:100%;
padding: 2px 0; /* changed the side padding to 0 since width is 100% & it takes the text-align center from the CSS above on the a tag */
display:block; /* Made it a block so the width 100% & centered text would work */
}​
This doesn't matter on the exact size of your icon images - they could all be different or the same, but as long as the over a tags are wider and taller than the tallest and widest image, you shouldn't have any problems.
why don't you work with => width:auto;