What is the best way to vertically center the text in my blue box?
markup:
<div class="btn-blue">
<span class="icon"></span>
Some awesome text here
</div>
and the styles:
.btn-blue {
background-color: #446598;
color: #fff;
display: inline-block;
padding: 0 20px 0 0;
line-height: 0;
}
.btn-blue .icon {
width: 50px;
height: 50px;
display: inline-block;
background-color: #00356b;
margin-right: 10px;
}
Here is the fiddle
http://jsfiddle.net/fSa6r/
Thanks.
Add line-height: 50px; to the .btn-blue class and
vertical-align: middle; to the icon class
FIDDLE
When only one line of text is needed, I find that setting line-height to the height of the box is the easiest way to vertically center text.
There are lots of ways to vertically align text, which you can check out here: http://www.vanseodesign.com/css/vertical-centering/
My personal favourite is using CSS tables (not html tables):
html:
<div id="parent">
<div id="child">Content here</div>
</div>
css:
#parent {display: table;}
#child {
display: table-cell;
vertical-align: middle;
}
This is another method to show a link as a button with icon. You could to improve this code using an sprite instead two separated images:
a.mybutton {
padding: 3px 0px 3px 30px;
color: #c00;
}
a.mybutton:link,
a.mybutton:visited,
a.mybutton:active {
background: #fff url("https://dl.dropboxusercontent.com/u/59816767/assets/map-pin_off.png") left center no-repeat;
}
a.mybutton:hover { background: #fff url("https://dl.dropboxusercontent.com/u/59816767/assets/map-pin_on.png") left center no-repeat; }
Here is the: fiddle
Related
I'm trying to vertically center a text which is inside a div (.site-title), itself inside a div (.site-title-wrapper), and all of this inside another div (.site-header), which is the menu of the website.
Here are some pictures:
In green: .site-title-wrapper
In red: .site-title
and in white: .site-header
And I have the following CSS for these divs:
.site-title-wrapper {
display: table;
height: 100%;
position: absolute;
padding: 23px;
background-color: green;
}
and
.site-title {
font-family: "Roboto", sans-serif;
display: table-cell;
vertical-align: middle;
}
I've seen that using table and table-cell to vertically center a div inside another one was a good solution. It works fine, but the only thing I need to do is to force .site-title-wrapper to take all available height, so the green box goes down to the end of the white one (the menu).
The idea is to simply center the title with the menu elements.
I can't really change the html part, so I'm trying to fix it only with CSS.
Do you know how I can fix it?
.site-header{
width: 100%;
height: 100px;
border: 2px solid black;
}
.site-title-wrapper {
display: table;
height: 100%;
padding: 23px;
background-color: green;
}
.site-title {
font-family: "Roboto", sans-serif;
display: table-cell;
vertical-align: middle;
background-color: red;
}
<div class='site-header'>
<div class='site-title-wrapper'>
<div class='site-title'>
Some Text
</div>
</div>
</div>
EDIT: Here is a fiddle, in which what I tried works (I just removed the absolute) : https://jsfiddle.net/0xhL76gk/2/
Is it possible to put image (css content property) before text inside div in my case?
JavaScript inserts different messages into the element with msgBox. And unfortunatelly I cannot touch JS.
Also text must be in the center (in the middle of the picture).
Requirements:
Message text is in the center
Between image and text strongly 10-20px
Div must not contain other elements
Text and icon should be centered.
document.getElementById("msgBox")
.innerHTML = "Here can be any text any long";
#msgBox {
border: 1px solid black;
padding: 10px;
text-align: center;
}
<div id="msgBox">(icon) text</div>
You could try it with css :before:
#msgBox:before{
content: url('path/to/your.png');
}
http://jsfiddle.net/a4utqvh1/1/
You can achieve this by setting height and verticle align:middle css of image and put image before text.
Here is the updated fiddle
http://jsfiddle.net/a4utqvh1/2/
You can use :before in css to add your icon as a background image, so even if you change the content of your div in JS, the icon will still be there.
#msgBox:before {
content: "";
display: inline-block;
width: 10px; // width of your image
height: 10px; // height of your image
background: url('img.jpg'); // path to your image
}
Here is the fiddle using image as a background image
msgBox {
border: 1px solid black;
padding: 10px;
text-align: center;
background: url(http://png.findicons.com/files/icons/2015/24x24_free_application/24/error.png) no-repeat 5px center;
}
http://jsfiddle.net/a4utqvh1/4/ will this work
You can use the background-image-Attribute form a div-Element.
#msgBox {
...
background-image: url('http://png.findicons.com/files/icons/2015/24x24_free_application/24/error.png');
background-repeat: no-repeat;
background-position-y: center;
...
}
A Sample with nice padding:
http://jsfiddle.net/bronni/a4utqvh1/7/
may this will work, put image tag in innerHTML text
document.getElementById("msgBox").innerHTML = "<img src='http://png.findicons.com/files/icons/2015/24x24_free_application/24/error.png'/> <span>Here can be any text any long</span>";
#msgBox {
border: 1px solid black;
padding: 10px;
text-align: center;
}
<div id="msgBox">(icon) text</div>
http://jsfiddle.net/a4utqvh1/3/
.wrapper {
border: 1px solid black;
padding: 10px;
text-align: center;
float: left;
}
.wrapper img,
#msgbox {
float: left;
}
<div class="wrapper">
<img src="http://png.findicons.com/files/icons/2015/24x24_free_application/24/error.png" />
<div id="msgBox">(icon) text</div>
</div>
I am trying to make responsive design with CSS. Everything works great, with one problem: I can not put few divs with text in the needed location. It is hard to understand what I mean, so here is a fiddle.
As you see a text Filename Some descr is shown in the bottom and I want it to appear inside of the ligth-grey box.
<div class="OD-grid">
<span class="OD-template">
<span class="OD-template-folder">Folder</span>
</span>
<span class="OD-template">
<span class="OD-template-file">
<img src="public/img/image.png"/>
<span class="OD-template-info">
<div> Filename</div>
<div> Some descr</div>
</span>
</span>
</span>
</div>
.OD-grid > .OD-template{
display: block;
float: left;
height: 90px;
width: 33.333333%; // this should be responsive. 50%, 25%, 33.33333%
}
.OD-template-folder{
display: block;
margin: 5px;
cursor: pointer;
height: 70px;
background-color: #9FB9CA;
padding: 10px 0 0 10px;
background: #0072c6;
color: white;
font-size: 17px;
}
.OD-template-file {
display: block;
margin: 5px;
cursor: pointer;
height: 80px;
background-color: #eaecee;
}
.OD-template-file > img{
padding: 16px;
width: 48px;
height: 48px;
background: #b1b1b1;
}
in such a way that if I change the line before // this should be responsive. 50%, 25%, 33.33333% to any of these numbers, it should work properly.
Give the .OD-template-info class the style display:inline-block This shall make the text go inside the light grey area
Demo: jsFiddle
You can add float: left style to .OD-template-file > img and .OD-template-file > .OD-template-info.
I dont now how did you decide to use that labels in that way, but you could add float:left; to .OD-template-file > img
I have updated your fiddle.
I used inline-table and tabledisplays so that their widths are relative to its content.
Having vertical-align:top makes the text align to top, similar to the other ones.
.OD-template-info {
display: inline-table;
vertical-align: top;
}
.OD-template-info > * {
display: table;
}
Alright, this one should be pretty easy for you front-end guys out there. I have the styled purple link all set to go. I'm just having trouble getting the vertical line to look OK. Assume the line is 1px #000 solid
I kind-of got it working making a div w/ a bottom-border and floating the styled link to the right. If I do that, I can't seem to get there to be space between the divider line and the link.
The following involves some extra markup and uses table-cells.
HTML:
<div class="wrapper">
<span class="leader">
<b></b>
</span>
<span class="cell">
<button>Sample Button</button>
</span>
</div>
CSS:
.wrapper {
border: 1px dotted gray;
display: table;
width: 100%;
}
.wrapper .leader, .wrapper .cell {
display: table-cell;
vertical-align: middle;
}
.wrapper .leader {
width: 100%;
padding: 0 10px;
}
.wrapper .leader b {
display: block;
border-bottom: 1px solid red;
}
.wrapper button {
white-space: nowrap;
}
Demo at: http://jsfiddle.net/audetwebdesign/8aSBA/
There are a few advantages to this approach:
You can control the spacing to the left and right of the horizontal line
Vertical alignment is independent of font-size, line-height
You don't need to specify the width of the button
You can use a :before selector in css, though im not sure is compatable in < ie7
.button:before {
background: none repeat scroll 0 0 #000000;
content: "";
float: left;
height: 1px;
margin-top: 12px;
width: 59%;
}
I have the following simplified HTML code:
<div>
<h2>XXX</h2>
<img XXX />
<div class="button" id="btback">back</div>
</div>
And also the CSS code:
.button {
text-align: center;
vertical-align: middle;
background-image: url(button.png);
width: 120px;
height: 60px;
display: table-cell;}
#btback {
margin: 10px auto 10px auto;}
Basically, in a div, there is a headline and an image, below is a button. button.png is the button background image without any text. So I horizontally and vertically align the text above the button background.
The first problem is, the button cannot be horizontally aligned in center. If I don't use display: table-cell, it can aligned in center, but the text is not vertically aligned.
The second problem is, the entire horizontal area (margin area) of the button div can be clicked. But I want only the 120px width area to be clickable.
An example is here: http://jsfiddle.net/cLSUT/
You can remove that inner div and just use the a tag as the button:
http://jsfiddle.net/cLSUT/2/
<div>
<a class="button" id="btback" href="#">back</a>
</div>
.button {
text-align: center;
line-height:60px;
background-color: red;
width: 120px;
height: 60px;
display:block;
}
#btback { margin: 10px auto 10px auto;}
let me see if I understood.
try with this
div>a {
text-align:center;
overflow:hidden;
}
.button {
text-align: center;
background-image: url(button.png);
width: 120px;
height: 60px;
display: table-cell;
line-height: 20px; /*(for example)*/
}
I hope this can help you