How can I vertically align icon inserted using :before method?
Here is what I have:
<div class="button submit">
<a class="submit check_car_search" href="#" >Some Text</a>
</div>
CSS:
.button a{
background: none;
background-color: #32BBE7;
text-indent:1px;
color:#FFF;
text-decoration: none;
display: table-cell;
vertical-align:middle;
text-align:center;
height: auto;
padding: 10px;
border-radius: 3px;
font-weight: 600;
font-size: 50px;
font-style: italic;
}
.button a:hover{
background-color: #2597F0;
}
.button a:before {
content:url(http://i.stack.imgur.com/tKsDb.png);
position: relative
}
JSFiddle: https://jsfiddle.net/1z83tc1o/
How Can I align vertically icon relative to the text?
You can use background and set is size (width, height), Then you can use vertical-align.
CSS
.button a:before {
position: relative;
display: inline-block;
width: 18px;
height: 16px;
vertical-align: middle;
content:"";
background: transparent url(http://i.stack.imgur.com/tKsDb.png) no-repeat center center;
margin-right: 20px; // Optional
}
DEMO HERE
You use background and absolute position the icon to perfectly vertical align in the middle.
.button a:before {
content: " ";
background-image: url(http://i.stack.imgur.com/tKsDb.png);
position: absolute;
width: 18px;
height: 16px;
top: 50%;
margin: -8px 0 0 -25px;
}
Here is the example : https://jsfiddle.net/1z83tc1o/3/
Related
I have 1 div with two separate sections of text. However, one of these sections of text is smaller than the other, leaving it slightly lower than the other section.
I would like it to be centered both vertically and horizontally within the div - inline with the larger text... margin-bottom is not working, however margin-left and margin-right are... how do I do this? Thankyou.
.portfolioHeader {
position: relative;
font-family: coolfont;
top: 20px;
font-size: 55px;
color: black;
margin-left: 70px;
text-align: left;
}
a.miniheader {
text-decoration: none;
margin-left: 20px;
}
span.smallerish {
font-size: 35px;
color: #3F3F3F;
text-decoration: none;
}
<div class="portfolioHeader"><a class="header" href="http://www.coopertimewell.com/portfolio">Portfolio ><a class = "miniheader" href="http://www.coopertimewell.com/portfolio/website-design"><span class = "smallerish">di Matteos</span></a></a>
</div>
This is how I would do it:
Fix invalid html
Put the font sizes on the anchors
Vertical align the anchors
If you want the text horizontally centered, then use text-align center on the div (instead of left with margin)
.portfolioHeader {
position: relative;
font-family: coolfont;
top: 20px;
color: black;
text-align: center;
}
a.header {
font-size: 55px;
vertical-align:middle;
}
a.miniheader {
font-size: 35px;
text-decoration: none;
margin-left: 20px;
vertical-align:middle;
}
span.smallerish {
color: #3F3F3F;
text-decoration: none;
}
<div class="portfolioHeader">
<a class="header" href="http://www.coopertimewell.com/portfolio">Portfolio ></a>
<a class="miniheader" href="http://www.coopertimewell.com/portfolio/website-design"><span class = "smallerish">di Matteos</span></a>
</div>
Set vertical property of anchor tag
.portfolioHeader a
{
vertical-align: middle;
}
You need to vertical-align the 'a' tags.
This css should help.
.portfolioHeader {
color: black;
display: table;
font-family: coolfont;
font-size: 55px;
margin-left: 70px;
position: relative;
text-align: left;
top: 20px;
vertical-align: middle;
}
a {
display: table-cell;
text-align: center;
vertical-align: middle;
}
a.miniheader {
left: 30px;
position: relative;
text-decoration: none;
top: -5px;
}
span.smallerish {
color: #3f3f3f;
font-size: 35px;
text-decoration: none;
}
I read many articles, many posts here, but unfortunately I can't center 2 lines of text and an image vertically.
<a class="button" href="">
<img src="http://dummyimage.com/30x30/cf97cf/fff">
Button
<span>Button alt</span>
</a>
I would like the 2 lines next to the image (centered), and center the whole content vertically in the .button.
body {
padding: 20px;
}
.button {
background: #000;
color: #fff;
text-decoration: none;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
height: 120px;
padding: 30px 50px;
display: inline-block;
}
span {
font-size: 11px;
color: #ccc;
display: block;
}
img {
vertical-align: middle;
}
At CSS-Tricks I found an article, but I don't want to use position:absolute to center. Is there any clean way to center the texts/image in the <a>?
JsFiddle: http://jsfiddle.net/7fx3eozd/
you can also wrap the text and span inside a div then wrap that div and img inside another div then add these classes:
also add display: inline-block; on your img
.center {
position: relative;
top: 50%;
transform: translateY(-50%);
}
.btnText {
vertical-align: middle;
display: inline-block;
}
JSFIDDLE DEMO
body {
padding: 20px;
}
.button {
background: #000;
color: #fff;
text-decoration: none;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
height: 120px;
padding: 30px 50px;
display: inline-block;
}
span {
font-size: 11px;
color: #ccc;;
display: block;
}
img {
vertical-align: middle;
display: inline-block;
}
.center {
position: relative;
top: 50%;
transform: translateY(-50%);
}
.btnText {
vertical-align: middle;
display: inline-block;
}
<a class="button" href="">
<div class="center">
<img src="http://dummyimage.com/30x30/cf97cf/fff">
<div class="btnText">
Button
<span>Button alt</span>
</div>
</div>
</a>
You can add display: table-cell to element with class .button and vertical-align: middle:
body {
padding: 20px;
}
.button {
background: #000;
color: #fff;
text-decoration: none;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
height: 120px;
padding: 30px 50px;
display: table-cell;/*change display to table-cell*/
vertical-align: middle;/*add vertical align middle*/
}
span {
font-size: 11px;
color: #ccc;
display: block;
}
img {
vertical-align: middle;
}
<a class="button" href="">
<img src="http://dummyimage.com/30x30/cf97cf/fff">Button
<span>Button alt</span>
</a>
You can wrap img and span inside a div (with class center)
<a class="button" href="">
<div class="center">
<img src="http://dummyimage.com/30x30/cf97cf/fff">
Button
<span>Button alt</span>
</div>
</a>
then add this css:
.center {
position: relative;
top: 50%;
transform: translateY(-50%);
}
Explain: Set position of div with top 50%, then move it to top with 50% its height (translateY by -50%)
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
I have website http://11klassniki.ru and I try to put text in in the middle using text-align:center but it doesn't work.
#konkurs_ege {
position:absolute;
top:10px;
left:380px;
width:140px;
height:80px;
background-image:url('http://11klassniki.ru/banners/konkurs_ege.jpg');
}
#konkurs_ege a {
text-decoration: none;
text-shadow: -1px -1px 0 #000000;
text-transform: uppercase;
font: 16px Arial, sans-serif;
font-weight:700;
width:100%;
text-align:center;
vertical-align: middle;
}
Here is code
<div id="konkurs_ege">
<a href='http://11klassniki.ru/view_post.php?id=144'>Konkurs!<br>how I made<br>IT</a>
</div>
I would like to have text: "Konkurs! how I made IT" in the middle of box (width:140px;
height:80px).
You need to have display: block.
.secondArticle a {
display: block;
text-align: center;
}
If you do not want to center the paragraph below the image, you can use span tag.
.secondArticle a span {
display: block;
text-align: center;
}
I believe the issue is that you're applying the text-center to the a tag, instead of the container. If you add it to the container ( konjurs_ege ), it should work. Worked for me on Chrome and FF.
#konkurs_ege {
position: absolute;
top: 10px;
left: 380px;
width: 140px;
height: 80px;
text-align: center;
background-image: url('http://11klassniki.ru/banners/konkurs_ege.jpg');
}
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;