I have a list with multiple links in it. I want them to display next to each other with a coloured background and some white space between them.
First I had "display:inline-block;" but then I couldn't give the link a vertical align. So I found out that I had to make the display a table-cell but now I don't have white space because I have a coloured background.
I have used padding but this didn't gave me white space. This is my code with fiddle:
HTML:
<div id="frontpage-menu">
<ul class="button">
<li>Info</li>
<li>Diensten</li>
<li>Projecten</li>
<li>Contact</li>
</ul>
</div>
CSS:
#frontpage-menu {
width: 1000px;
display: block;
height: 50px;
}
.button li {
display: table-cell;
background-color: #4A4A2F;
width: 200px;
height: 65px;
text-align: center;
vertical-align: middle;
}
.button li>a {
text-decoration: none;
font-size: 40pt;
color: #FFFFFF;
letter-spacing: -4px;
}
This is my fiddle: http://jsfiddle.net/0L7275dq/
You can add border: 3px solid; border-color: white in your css for li.
You can check this fiddle link for the demo: Fiddle
add the style
border-collapse:collapse to your cells and table, that SHOULD fix it
Use:
and for larger spaces:
Add the tag at the end of your text and you will get space after your text.
you can find a full list of these codes here HTML Codes
#frontpage-menu {
width: auto;
display: block;
height: auto;
}
.button ul {
height: 65px;
background-color: red;
width: 200px;
display: inline;
}
.button li {
/*display: table-cell;*/
background-color: #4A4A2F;
width: 200px;
height: 65px;
text-align: center;
vertical-align: middle;
padding-left: 10px; /*this is where you should mess around with your white space.*/
}
.button li>a {
text-decoration: none;
font-size: 40pt;
color: #FFFFFF;
letter-spacing: -4px;
}
Related
I have a small problem. I am trying to change the width and height of a button but for some reason, it will not let me. The button automatically stays the same width and height as the contained text.
CSS
.flexcontainer {
display: flex;
align-items: center;
overflow: hidden;
}
img[width="500"] {
border: 3px solid #5F5F5F;
border-radius:3px;
float: left;
}
#leftRetail {
display: block;
height:354px;
width: 1308px;
float:right;
text-align: center;
vertical-align: middle;
line-height: 354px;
}
.button {
width: 250px;
height: 200px;
background: #ed2626;
border-radius: 2px;
color: white;
font-weight: bold;
text-decoration:none;
}
HTML
<div class="flexcontainer">
<div>
<img src="anyImage.jpg" width="500" height="350"/>
</div>
<div id="leftRetail">
Retail Menu
</div>
</div>
You need to change your .button to use display: block or inline-block:
.button {
display: block;
width: 250px;
height: 200px;
background: #ed2626;
border-radius: 2px;
color: white;
font-weight: bold;
text-decoration:none;
}
CHANGED ANSWER after copying the original code into a snippet:
I just realized that the whole thing is inside a flex container, which makes all child elements flex items automatically. (BTW: The float parameters have no effect in this case)
So, one method to add width and height to your .button is to give it some padding, as shown below:
.flexcontainer {
display: flex;
align-items: center;
overflow: hidden;
}
img[width="500"] {
border: 3px solid #5F5F5F;
border-radius: 3px;
}
#leftRetail {
height: 354px;
width: 1308px;
text-align: center;
vertical-align: middle;
line-height: 354px;
}
.button {
background: #ed2626;
border-radius: 2px;
color: white;
font-weight: bold;
text-decoration: none;
padding: 8px 12px;
}
<div class="flexcontainer">
<div>
<img src="anyImage.jpg" width="500" height="350" />
</div>
<div id="leftRetail">
Retail Menu
</div>
</div>
You cannot modify the width and height of inline elements, manually.
Add display: block; (or inline-block) to your .button block, and you can observe that the height and width changes are you define it.
Only block elements may have their width and height set specifically.
Your button should now look like:
.button {
width: 250px;
height: 200px;
background: #ed2626;
border-radius: 2px;
color: white;
font-weight: bold;
text-decoration:none;
display: block;
}
Just make it block-level element by adding display:bock to its style. Then you can apply whatever style you want!
I have the following simple code snippet. It is taken out from my application where .a1 is a container button, which has an icon. The icon should be vertically middle aligned to the parents line-height/height, but it is shifted with 1px from top. Could you explain me why this is the behavior? Is there any solution?
.a1 {
display: inline-block;
width: 28px;
line-height: 28px;
background-color: #000;
text-align: center;
vertical-align: middle;
}
.i {
display: inline-block;
width: 16px;
height: 16px;
background-color: #f00;
vertical-align: middle;
}
<div class="a1"><i class="i"></i>
</div>
Why?
Because inline-block elements render with "white-space". You can see this in this demo where no height/width is set on the parent element.
When you use vertical-align:middle; the "white space" is rendered before the element (on top) (black line in the demo). This space moves the child element down and therefore it doesn't appear verticaly centered.
how to fix :
You can use display:block; and calculate the margin to apply to the child element so it centers verticaly and horzontaly.
You can also take a look at this question which talks about white space and ways to avoid them.
Well, it seems like font-size:0; for .a1 seems also a fix for such issue.
.a1 {
display: inline-block;
width: 28px;
line-height: 28px;
background-color: #000;
text-align: center;
vertical-align: middle;
font-size: 0;
}
.i {
display: inline-block;
width: 16px;
height: 16px;
background-color: #f00;
vertical-align: middle;
}
<div class="a1"><i class="i"></i>
</div>
.a1 {
display: inline-block;
background-color: #000;
}
.i {
display: block;
width: 16px;
height: 16px;
margin: 6px 6px;
background-color: #f00;
}
<div class="a1"><i class="i"></i>
</div>
.a1 {
display: inline-block;
background-color: #000;
}
.i {
display: block;
width: 16px;
height: 16px;
margin: 6px 6px;
background-color: #f00
Hi I'm trying to center the text in the first circle div. I think it's currently in the center of the div but when there is more than one characters like '200', it looks funky as below. I have the red circle background and trying to make the text in the center regardless of the characters. thank you in advance!
.main {
vertical-align: top;
margin-top: 3px;
margin-bottom: 5px;
display: inline-block;}
.main .label {
display: inline-block;}
.bg {
background: red;
padding: 10px;
font-weight: bold;
color: #fff;
display: inline-block;
border-radius: 60%;}
.bg .label {
vertical-align: top;
margin-top: 3px;
margin-bottom: 5px;
width: 10px;
display: inline-block;
margin: auto;}
<div class="main">
<div class="bg"><span class="label">200</span></div>
<span class="label">This is the other text need to be</span>
<div class="bg"><span class="label">0</span></div>
<span class="label">This is the other text need to be</span>
</div>
Try to set width:100% on .bg .label as follows:
.main {
vertical-align: top;
margin-top: 3px;
margin-bottom: 5px;
display: inline-block;}
.main .label {
display: inline-block;}
.bg {
background: red;
padding: 10px;
font-weight: bold;
color: #fff;
display: inline-block;
border-radius: 60%;}
.bg .label {
vertical-align: top;
margin-top: 3px;
margin-bottom: 5px;
width: 100%;
display: inline-block;
margin: auto;}
<div class="main">
<div class="bg"><span class="label">200</span></div>
<div class="bg"><span class="label">0</span></div>
</div>
EDIT: if you want to keep the same width for the circle and still center the text, you could replace width:10px; in .bg with the following:
.bg {
/* ... */
width: 35px;
padding: 10px 0;
text-align: center;
/* ... */
}
So the full snippet would look something like this:
.main {
vertical-align: top;
margin-top: 3px;
margin-bottom: 5px;
display: inline-block;}
.main .label {
display: inline-block;}
.bg {
background: red;
width: 35px;
padding: 10px 0;
text-align: center;
font-weight: bold;
color: #fff;
display: inline-block;
border-radius: 60%;}
.bg .label {
vertical-align: top;
margin-top: 3px;
margin-bottom: 5px;
width: 100%;
display: inline-block;
margin: auto;}
<div class="main">
<div class="bg"><span class="label">200</span></div>
<div class="bg"><span class="label">0</span></div>
</div>
Try something like this. I'm guessing you are ok with fixing the width and height of your little circles? If so, this solution should work for you. The benefit here is your circles stay consistent visually regardless of the values placed within them.
You can adjust the width/height of the circle to your liking, and whatever value you place in there will remain centered. Keep in mind, with this solution, your circles won't scale to match the value's length should it expand beyond their bounds. I assume this is the behavior you're looking for, though, given your original code.
Also, note, you might need to adjust the top margin to position the values according to the height of the circles if you change them. Hope this helps!
.bg {
background: red;
font-weight: bold;
color: #fff;
display: inline-block;
border-radius: 60%;
width: 38px;
height: 38px;
}
.bg .label {
display: inline-block;
margin: 9px auto 0;
text-align: center;
width: 38px;
}
<div class="main">
<div class="bg"><span class="label">200</span></div>
<span class="label">This is the other text need to be</span>
<div class="bg"><span class="label">0</span></div>
<span class="label">This is the other text need to be</span>
</div>
html
<ul class="tabs-bottom">
<li>Booking</li>
<li>Special Offers</li>
</ul>
css
.tabs-bottom{
list-style: none;
margin: 0;
padding: 0;
}
.tabs-bottom li{
width: 216px;
height: 55px;
background: #000;
display: inline-block;
text-align: center;
}
demo
I can do this with display table-cell but I strongly searching for with display inline-block
here is working demo with display table-cell demo
.tabs-bottom{
list-style: none;
margin: 0;
padding: 0;
border-spacing: 10px 0;
}
.tabs-bottom li{
width: 216px;
height: 55px;
background: #000;
display: table-cell;
text-align: center;
vertical-align: middle;
}
If you are ok with other display property, you can use diaplsy:table-cell like this
.tabs-bottom li{
width: 216px;
height: 55px;
background: #000;
display: table-cell;
text-align: center;
vertical-align:middle;
}
This is the complete css you need to have it working.
This also handles very long texts, which wrap around (multi-line link texts):
.tabs-bottom{
list-style: none;
margin: 0;
padding: 0;
}
.tabs-bottom li{
width: 216px;
height: 55px;
line-height: 50px;
background: #000;
display: inline-block;
text-align: center;
}
.tabs-bottom li a
{
line-height:16px; /*This is in place, to prevent inheriting the li's line-height*/
display:inline-block;
vertical-align: middle;
}
You can test it in this fiddle:
http://jsfiddle.net/vVnR5/
Notice
jsFiddle doesn't seem to work in IE 7 and 8, so i couldn't test it.
If they're only ever going to be one line, you can center them with line-height,
.tabs-bottom li{
width: 216px;
height: 55px;
line-height:55px
background: #000;
display: inline-block;
text-align: center;
}
It's trickier if you want a multi-line center, but it can be achieved using display:table-cell;
EDIT Here is a version where it will center regardless of number of lines http://codepen.io/robsterlini/pen/btqjv but remember that it will default back to appearing at the top for IE7 and below (see here).
I need to vertically align in middle labels of menu. Allso if client will change the value of label and it will break in two lines, it'll stays in middle. As i know vertical-align: middle works for table cells, but I need to generate menu from <li> elements. See example code below.
html
<ul>
<li>qwe</li>
<li>asd</li>
<li>zxcvbnm asdfgh</li>
</ul>
css
ul {
list-style: none;
}
li {
float:left;
border: 1px solid;
margin: 1px;
height: 60px;
width: 60px;
background: tomato;
text-align: center;
vertical-align: middle;
}
a {
vertical-align: middle;
}
And working prototype at jsbin
Hi now give to li display:table-cell and remove float:left
as like this
li {
display:table-cell;
vertical-align: middle;
float:left; // remove this line
}
Demo
Just add line-height:60px to your a tag and it works.
See Demo
Use display: inline-block for <a> elements.
ul {
list-style: none;
}
li {
float:left;
border: 1px solid;
margin: 1px;
height: 60px;
line-height: 60px;
width: 60px;
background: tomato;
text-align: center;
vertical-align: middle;
}
a {
vertical-align: middle;
display: inline-block;
line-height: normal;
}
Example fiddle : http://jsbin.com/ohazot/1/edit