I just did an UL list with 4 LI elements and I did this in CSS:
ul li {
width:200px;/*problem*/
display:inline;
color:white;
background-color:red;
border-radius:5px;
font-family:verdana;
font-size:2em;
box-shadow:0 7px 0 #600;
padding:5px 20px;
margin:15px;
}
The problem is that the width property has no influency with the width of the li element, and if I want to do it larger I have to add padding to the right and left but that is not good because there are different lenghts in each box since some have more letters than others.
Why is that happening?
Thanks,
In order to set block type properties (i.e. width), you'll have to set the display to inline-block on the li element
ul li {
display:inline-block;
}
The selector above will only affect the li element. If you want to style child elements, like a button, you'll also have to set the width property there.
ul li button {
width:200px;
}
fiddle
Since you asked why this is happening - inline elements width is determined by their content so your width css has no effect. Setting the item to display as inline-block or block will make it determine it's width based on your styles rather than the content.
All HTML elements are rendered in one of the following ways:
Block: Takes up the full width available, or a defined width, with a new line before and after. This can be forced using display:block;
Inline: Takes up only as much width as it needs, based on its contents, and does not force new lines. This can be forced using display:inline;
Not displayed: Some tags, like <meta />, <style> and <link> are not visible. This can be forced using display:none;
Inline elements can not have a defined width. Only block element can do this. There is a way to have a block level element to be inline by using display: inline-block. This is much like an emoticon or an image would be displayed in a word document. You can define a width because it is a block, but it will fall inline with the rest of the elements if no width is set. Inline blocks will not respect line-heights, meaning that if an image is inline with text it will adjust the line-height to allow itself to fit inline.
You should use display: inline-block not display: block;
DEMO http://jsfiddle.net/kevinPHPkevin/SyBBu/
ul li {
width:200px;/*problem*/
display:inline-block;
color:white;
background-color:red;
border-radius:5px;
font-family:verdana;
font-size:2em;
box-shadow:0 7px 0 #600;
padding:5px 20px;
margin:15px;
}
Related
I am developing a configuration page for my plugin in WordPress. I created an <ul> element inside a <div> element and placed it on my config page. The problem is, whenever I apply margin-right and width:100% to that div it causes the scroll bar to appear, the width of the list exceeds the total width of the page. As you can see at the bottom of the screenshot.
Here are the only styles I am applying (LESS):
div#pworks-popular-posts-list {
display:block;
margin:20px;
width:100%;
ul {
width:100%;
margin:0;
background-color:white;
li {
display:block;
div {
display:inline-block;
}
}
}
}
This is the HTML structure pulled from Chrome Dev Tools:
Could you please help me with this? Thank you.
First of all you don't need width:100%; because a display:block; div will fill its parent's width by default. But if you want to specify it for some reason (or you plan on making it display:inline-block; or something) you can use calc() function like this: width:calc(100% - 20px);.
What's happening is that you are setting the div's width to be the body's width. After that you are moving it so it causes your div to go even further and that causes an overflow-X.
I wouldn't recommend setting a block element width to 100%. Block elements automatically have 100% width of their parents.
I would set a container div with a padding: 20px; instead.
Set max-width:100% instead width:100%.
It will reduce width according to padding.
I have a problem that I don't understand. Inside parent div I have a link and I want to fill full parent width.
#changePassword {
width:100%;
height:5.28%;
border-bottom:1px solid #a9aaa7;
background:#3c3c3c;
}
#changePassword a {
width:100%;
height:100%;
background:url(iconPassword.png) no-repeat;
color:#FFF;
font-size:1vw;
text-decoration:none;
padding-left:20%;
}
But only fill text inside link. I've test this width a border.
Anchors are inline elements which means, among other things, that they will only be as big as their contents and horizontal padding.
You can change this by changing the display property in CSS. In this case, you would need to set it to block or inline-block.
More information on the display property
Add display:block to a tag class
DEMO
I have a simple HTML vertical menu, with such a structure:
<div id="menu">
<div class="item">
...
</div>
</div>
I've set the .item div to be rendered as text elements, so they're ordered next to each other:
#menu div.item {
margin: 0px;
padding: 0px;
display: inline;
}
This kinda works:
However, I want to apply a :hover effect on each link, such as vertical black borders will come from sides and connect with the bottom dashed border:
So I did the following:
#menu div.item a{
padding: 5px;
border-width: 0px 1px 0px 1px;
border-color: transparent;
border-style: dashed;
}
#menu div.item a:hover{
border-color: black;
background: #B3D2FF;
}
But the link seems to be bigger than it's parent element. I didn't think this was possible:
What's wrong? Why doesn't the parent DIV stretch to be able to contain whatever's inside?
(not) Working fiddle.
Rest of the CSS:
#menu {
text-align: center;
margin: 0px;
padding:0px;
background-color: #CEE2FF;
border-bottom: 1px dashed #1868FF;
}
Add the following css:
#menu div.item a {
display: inline-block;
}
Before this code, the a tags are not being displayed as blocks and the container doesn't want to be friends with them.
The padding 5px you used on the a tag will be partially applied but only left-right in respect to other sibling inline elements.
You're trying to set padding to inline elements, which is visually not the desired. The padding on inline elements will refer to the textual line, but not in respect to the containing block-level parent.
Remember that padding can be set to block-level elements such as
<div>, <h1>, <p> etc...
One fix is to use some simple math and set line-height to your a
The other is to set your inline anchors as display-block elements,
doing that way your elements will act contextually as inline elements while being allowed to take block-level elements properties.
https://developer.mozilla.org/en-US/docs/Web/CSS/display
http://www.w3.org/TR/CSS2/propidx.html
Also setting just like that an inline any element to be inline-block is not the best choice and is not fully compatible with older browsers.
display-inline for span, a, b, i, s, q, u will work in all browsers,
but generally speaking span is the perfect one to be used in that case cause even if an inline element by properties is similar to div.
https://developer.mozilla.org/en-US/docs/HTML/Block-level_elements
https://developer.mozilla.org/en-US/docs/HTML/Inline_elements
From CSS 2.1 section 10.6.1 Inline, non-replaced elements
The vertical padding, border and margin of an inline, non-replaced box
start at the top and bottom of the content area, and has nothing to do
with the 'line-height'. But only the 'line-height' is used when
calculating the height of the line box.
The height of the block div is the height of the stack of line boxes, which in this case is the height of the one and only line box. Which means that if you want to contain the <a> with padding, you need to set the line-height of the <a> element.
Add to your CSS
#menu div.item a{
line-height:30px;
vertical-align:top;
}
Like this: http://jsfiddle.net/Z63gs/4/
You can try putting overflow:hidden; on your #menu item. http://jsfiddle.net/Z63gs/1/
This will just hide the extra fluff. I think your padding is making the <a> elements larger than you would like.
Elliot's answer is much cleaner than this.
I just can't set the height and width of a elements of my navigation.
#header div#snav div a{
width:150px;
height:77px;
}
#header div#snav div a:link{
width:150px;
height:77px;
}
#header div#snav div a:hover{
height:77px;
background:#eff1de;
}
Any ideas what I am doing wrong?
add display: block;
a-tag is an inline element so your height and width are ignored.
#header div#snav div a{
display:block;
width:150px;
height:77px;
}
Anchors will need to be a different display type than their default to take a height.
display:inline-block; or display:block;.
Also check on line-height which might be interesting with this.
Your problem is probably that a elements are display: inline by nature. You can't set the width and height of inline elements.
You would have to set display: block on the a, but that will bring other problems because the links start behaving like block elements. The most common cure to that is giving them float: left so they line up side by side anyway.
From the definition of height:
Applies to: all elements but non-replaced inline elements, table columns, and column groups
An a element is, by default an inline element (and it is non-replaced).
You need to change the display (directly with the display property or indirectly, e.g. with float).
Thanks to RandomUs 1r for this observation:
changing it to display:inline-block; solves that issue. – RandomUs1r May 14 '13 at 21:59
I tried it myself for a top navigation menu bar, as follows:
First style the "li" element as follows:
display: inline-block;
width: 7em;
text-align: center;
Then style the "a"> element as follows:
width: 100%;
Now the navigation links are all equal width with text centered in each link.
Basically, I want to be able to specify the height of my "li" element for a horizontal list I'm making. "line-height" and "height" have no impact on the visible height of the "li". The only thing that changes the height of the "li" is changing the size of the font used within the "li". This even happens when the "list-style" is set to "none".
What gives?
Set the li to display: inline-block or float: left.
list-style basically just determines the bullets to be used for the list, and doesn't really affect the inner elements otherwise.
Do this:
ul {
overflow: hidden;
}
li {
float: left;
height: 200px; /* as required */
}
The above is another method for getting list items to display inline, without using display: inline; which is a very restrictive property and should be applied sparingly.
Try setting
display:block on the li
font-size could affect the computed height of an li element (depending on what's inside) IF line-height is proportional or relative AND no height is set on li (or li is display:inline).
line-height could affect the computed height of an li element (depending on what's inside) IF no height is set on li (or li is display:inline).
height will affect the computed height of an li element if it's display:inline-block or display:block only (height has no effect at all on display:inline).
See http://moorer.me/mmKVHM for examples of how font-size and line-height can affect the height of li in different ways depending on the technique used. The differences are somewhat obviated if a is set to display:block, so for example purposes I did not do so.
Others have already shared the two most common solutions to horizontal menus -- inline-block or floated blocks. Inline can be used, but likely won't give you the control you need.