CSS list item width/height does not work - css

I tried to make a navigation inline list. You can find it here: http://www.luukratief-design.nl/dump/parallax/para.html
For some reason it does not display the width and height of the LI. Here is the snippet. What is wrong with this?
.navcontainer-top li {
font-family: "Verdana", sans-serif;
margin: 0;
padding: 0;
font-size: 1em;
text-align: center;
display: inline;
list-style-type: none;<br>
width: 117px;
height: 26px;
}
.navcontainer-top li a {
background: url("../images/nav-button.png") no-repeat;
color: #FFFFFF;
text-decoration: none;
width: 117px;
height: 26px;
margin-left: 2px;
margin-right: 2px;
}
.navcontainer-top li a:hover {
background: url("../images/nav-button-hover.png") no-repeat;
color: #dedede;
}

Declare the a element as display: inline-block and drop the width and height from the li element.
Alternatively, apply a float: left to the li element and use display: block on the a element. This is a bit more cross browser compatible, as display: inline-block is not supported in Firefox <= 2 for example.
The first method allows you to have a dynamically centered list if you give the ul element a width of 100% (so that it spans from left to right edge) and then apply text-align: center.
Use line-height to control the text's Y-position inside the element.

Inline items cannot have a width. You have to use display: block or display:inline-block, but the latter is not supported everywhere.

I think the problem is, that you're trying to set width to an inline element which I'm not sure is possible. In general Li is block and this would work.

Using width/height on inline elements is not always a good idea.
You can use display: inline-block instead

Remove the <br> from the .navcontainer-top li styles.

I had a similar issue trying to fix the item size to fit the background image width. This worked (at least with Firefox 35) for meĀ :
.navcontainer-top li
{
display: inline-block;
background: url("../images/nav-button.png") no-repeat;
width: 117px;
height: 26px;
}

Related

Resize ul and li element with text to window size

So I'm developing a web page, and I'm making it now responsive. I managed to get everything responsive except the main menu nav.
It's a ul element and it has li inside with text. Here are some pictures about the problem
Full webpage:
On mobile:
I just want to adjust the text or the ul element to fit without making another line.
Here's the css ul element:
.main-menu ul {
display: table !important;
background-color: #98B709;
padding: 0;
margin: 0;
text-align: center;
}
And the li element:
.main-menu ul li {
display: inline-block;
float: left;
position: relative;
list-style: none;
}
I tried a lot of things but nothing works...
Thanks and hope you guys can help me!
I found a very useful Stackoverflow post that should answer your question:
Responsive Font Size
And I experienced that changing font size and other problematic parts from px to em generally helps to make is more responsive too.
Try to use different font size e.g
vw Relative to 1% of the width of the viewport
vh Relative to 1% of the height of the viewport
Here is a variation that does not use display:table, which I always avoid.
The important bit you can play with is the 'width' of the 'li' element. If you really want to squash them all on one line, you can make this a very small %.
If you do use the second line, the 'text-align:center' in the 'ul' element will keep everything centered, instead of floating left as you have it now.
I use this code block all the time; it's a common problem.
#main{
width:100%;
}
#main ul {
display: block;
text-align: center;
width: 100%;
padding: 0;
margin: 0;
}
#main li {
width:10%;
list-style: none;
display: inline-block;
padding: 0;
margin: 8px;
}

Inline block adding bottom space

I have one of my <a> links set to inline-block and there is some space added to the bottom of the containing div. I am not sure how to get rid of this and was wondering if someone could help.
Here is a fiddle: http://jsfiddle.net/RQ69r/1/
Thanks in advance.
You can fix that adding the following style to the inline-block element:
vertical-align: middle;
Demo
Why dont you change it to display: block; ?
Check the updated fiddle:
http://jsfiddle.net/RQ69r/3/
When you want more <a> elements next to each other (horizontal), you could use list-items and / or float:left;
This is the default behavior of inline-block elements. Set the parent div font-size: 0px;
DEMO http://jsfiddle.net/RQ69r/7/
.row_20 {
width: 20%;
font-size: 0px;
}
And set the correct font-size of the child element
.header .logo {
font-size: 13px; <-- set font size
height: 45px;
width: 100%;
display: inline-block;
background: blue;
}

How to make navbar-links fill out 100% of the width of a page

Here's my struggle. So I've been wondering how I could make the navbar-links fill out 100% of the nav. So that they have an equal amount of spacing and the font-size will decrease if more links were added - this way it will always fill out 100%.
As it is now, I cannot seem to achieve this. I've only got a set padding, but I've tried doing stuff like:
display: block;
float: left;
width: 100%;
but it's giving me all kind of effects that doesn't work at all :S
Anyone able to help me out on this?
Codepen example
Remove width from #main-navigation and add with to #main-navigation li with a value equal to the 100/the_numberof_elements_in_the list. Your css should be:
#main-navigation {
height: 54px;
overflow: hidden;
border-bottom: 1px solid black;
}
#main-navigation ul {
height: 54px;
overflow: hidden;
}
#main-navigation li {
font-size: 1.0em;
text-transform: uppercase;
list-style: none;
float: left;
width: 20%;
}
This way you'll have your nav occupying all the available width, but your font-size will not automatically resize. You'll should change it also in function of the number of elements (similar the way you have to do to the li's width). If your are generating this list dynamically at server side, you could do the same with the css and then calculate the right values for these two attributes. If your are using ajax to populate the list, you could do change the attributes with javascript.
A way of doing this is using display: table on the parent and display:table-cell on the children. I believe it won't work on some versions of IE (of course). Here's an exaple
You can use the nav tag instead and treat it as table. Treat the <ul> tag as table-row and <li> tag as table-cell. Ex:
nav{
display: table;
text-align: center;
width: 100%
}
nav ul{
display: table-row;
}
nav ul li {
display: table-cell;
}
This will stretch the contents of <li> 100%
I learnt it from the following link: http://www.darkstardesign.com/darkstar-blog/2012/11/27/stretching-horizontal-navigation-menus-to-the-full-width-of-a-layout/

Centering heading with CSS sliding doors

I have an issue with the sliding doors technique here. The heading right after the description is floating left due to the sliding doors technique, but all I want is that is stands alone in the center, above the products.
Can you help me understanding how to do it?
Here is the CSS I used for the heading:
h3.offer-title, h3#comments, h3#reply-title {
background:url(images/offer-title-bg.png) no-repeat right bottom;
color:#434343;
display:block;
float:left;
font-size: 14px;
margin-right: 6px;
padding-right:6px;
text-decoration:none;
height: 43px;
line-height: 0;
position: relative; }
h3.offer-title span, h3#comments span, h3#reply-title span {
background:url(images/offer-title-bg.png) no-repeat;
display:block;
padding-left: 20px;
height: 43px;
line-height: 43px;
padding-right: 16px;
}
Thank you.
It's floating because you set float: left in your first CSS code block. To get rid of that behaviour you need to get rid of the float.
Once the float is gone, if you want the header's background to nicely fit the text like it did before, the element needs to have display: inline-block.
But with display: inline-block and no set width on the header (you could add a width, but then it might break if you want to change the text or font size), it's not centered. To get it centered, you need a wrapper element around it which has text-align: center.
So:
Add this block:
h3.offer-title {
display: inline-block; /* this makes the bg fit the text */
float: none; /* this overrides float:left */
}
Wrap the offer-title element in another div.
Style the wrapper with
.offer-title-wrapper {
text-align: center;
}

CSS Button - Vertically centre text

I have a Button that is a simple anchor tag styled with the following -
.buyBtn{
background:url(../images/buyBtn.png) no-repeat;
padding-top:4px;
width:97px;
height:28px;
margin-top:14px;
}
.buyBtn a{
color:#fff!important;
font-weight:normal!important;
text-decoration:none;
padding-left:27px;
padding-top:12px;
text-shadow:none!important;
}
I'm having problems vertically centering the text within the button, it appears fine in some devices, but off centre in others.
Can anybody recommend a way to fix this or a better solution to achieve the same result?
Cheers
Use line-height to center it vertically. I usually use the same value as its height.
HTML:
<div class="buyBtn">Button</div>
CSS:
.buyBtn{
background:url(../images/buyBtn.png) no-repeat;
width:97px;
height:28px;
display: table-cell;
vertical-align: middle;
}
.buyBtn a{
color:#fff!important;
font-weight:normal!important;
text-decoration:none;
padding-left:27px;
text-shadow:none!important;
}
No need to use padding-top or margin-top for vertical align. Just use display: table-cell; and vertical-align: middle;. Thats it.
Flexbox helped me nail it. Assume you have an excel button (an anchor tag, really).
HTML
<a class="btn-excel" href="/Export/ExportListToExcel">
Export To Excel
</a>
CSS
.btn-excel {
background-color: #32CD32; /*lime green*/
color: #fff;
outline: none;
border-radius: 0.3rem;
text-decoration: none;
width: 5rem;
height: 2.5rem;
/* Flex rules 'em all */
display: flex;
justify-content: center;
align-items: center;
}
.btn-excel:hover {
background-color: #808000; /*olive*/
cursor: pointer;
text-decoration: none;
}
I would use line-height as bchhun as mentioned. Also, padding-top & padding-bottom can help.
You can copy this code and put it as a CSS and adjust the things as you need
.btn-alignment{
width: 88px;
height: 40px;
margin: 0 auto;
padding: 0;
display: inline-block;
line-height: 40px;
margin-top: 9px;
text-align: center;
}
Did you try setting a font-size to your link ? Each browser probably has its own default size, so that may be an hint. Be careful too with padding and width/height, you need to decrease the block size if you add padding 'cause the padding is included in the width. For example, a simple block of 100px width without padding will have a size of 100px : ok. Add a padding-left: 10px; and your block now has a width of 110px. ;)

Resources