I have multiple li items floating left and I want to set a right border around them so that all of them get the same height for the border. Therefor I'm using the display:table; However it ignores the width of the li. How can I force to use same width for all li items?
HTML:
<ul>
<li>a</li>
<li><img src="http://farm8.staticflickr.com/7437/9982948185_19ae813ee0_n.jpg"/></li>
<li>b</li>
</ul>
CSS:
ul{
display: table;
}
ul li{
display: table-cell;
width: 50px !important;
border-right: 1px solid red;
}
Demo:
http://jsfiddle.net/xbmEQ/
You can try "table-layout: fixed"
https://developer.mozilla.org/en-US/docs/Web/CSS/table-layout
Your image is bigger so you are having issues. Set max-width to image:
ul li img {
max-width:100%;
}
Also use word break on li,
Demo: http://jsfiddle.net/shekhardesigner/xbmEQ/7/
You'll also need to define the width for the img tag because img tag is taking full width So you can set the image width to 50px but better way to define width 100%.
If you don't want to specify anything inside the li tag then you may set display: inline-block; overflow: hidden; to your li instead of using display: table-cell; to li
demo
Do you want to make the image 50px wide? In that case you just have to add this piece of code:
ul li img{
width: 50px;
}
http://jsfiddle.net/xbmEQ/3/
You can set the min-width of the li items so that they do not shrink below 50px.
http://jsfiddle.net/xbmEQ/5/
It keeps the heights of all the li items matching (and dynamic) as you want for matching border heights. It also keeps all the li items 50px wide unless their content exceeds 50px (like your image).
(I have not tested this in any other browser than Chrome)
not real sure what you're trying to achive but what about this fiddle
ul{}
ul li{
display: inline-block;
width: 50px;
border: 1px solid red;
overflow:hidden;
}
EDITED FIDDLE AND CSS, added height attribute.
fiddle 2.0
ul li{
display: inline-block;
height:100px;
width: 50px;
border: 1px solid red;
overflow:hidden;
}
Related
Let's keep this simple and short, I've made 2 dummy examples of my problem.
I have a container section which is 600px wide, this section will container list of products.
each product is a 100x100 block, and there is a margin of 62px between each product.
The ul is set on display: inline-block so it won't go one under each other.
Now in the following pen example: http://codepen.io/anon/pen/yujLf
You can see what I want to do, can you see how the first row of squares, touch the border of the container & then the next element goes under?
(source: gyazo.com)
You can see how it's perfectly aligned for the width, as there's a perfect equal margin between each element.
Now this solution is a problem, because now the second row will have extra margin on the left side:
(source: gyazo.com)
I do have a solution for that, simply by changing margin-left to margin-right and disable margin-right for ul li:last-child.
But if I do that, I will not be able to align the last element with the border, like I did with the first example, take a look: http://codepen.io/anon/pen/wdhrJ
As you see, I had to change the margin to 40px instead of 62px to make it 4 elements per row.
Is it possible to implement what I want with using ul?
If your container is fixed at 600px, then the following solution will work:
ul li {
width: 100px;
height: 100px;
background-color: red;
margin-right: 62px;
display: inline-block;
}
ul li:nth-child(4n+4) {
margin-right: 0;
}
What I've done is change margin-left to margin-right in both of the above selectors. I've also changed your second selector from first-child to nth-child, to select the 4th element and every 4th element after that.
CodePen Example
Have you tried this hack instead? http://codepen.io/anon/pen/IgKtD
* {
padding: 0;
margin: 0;
}
ul {
list-style: none;
padding: 0;
margin: 0;
text-align:justify;
vertical-align:top;
font-size:0.001px;
}
ul::after {
content:'';
display:inline-block;
width:100%;
height:3px;
background:blue;
}
ul li {
width: 100px;
height: 100px;
background-color: red;
display: inline-block;
box-shadow: inset 0 0 0 1px purple;
margin:0 20px;
}
ul li:nth-child(4n), ul li:last-child {
margin-right:0;
}
ul li:nth-child(4n-3) {
margin-left:0;
}
http://jsfiddle.net/SyKnv/
I am trying to get rid of the additional space after each li item, to make the blocks the same size as their content. I tried to display them as inline, but that removes the bullets.
HTML
<div>
<ul>
<li>banana</li>
<li>orange</li>
<li>cherry</li>
</ul>
CSS
div {
width: 40%;
min-height: 50%;
border:1px solid black;
}
li {
border:1px solid black;
}
As mention in my comment you have to make ul display: inline-block; like this:
ul{
display: inline-block;
}
fiddle
try this code DEMO
div{
width: 40%;
min-height: 50%;
border:1px solid black;
}
ul {
margin:0;
padding:0;
}
li{
border:1px solid black;
list-style:inside;
}
CSS
ul{
display: inline-block;
}
Inline-Block
Basically, it’s a way to make elements inline, but preserving their block capabilities such as setting width and height, top and bottom margins and paddings etc.
More Info Regarding Inline-block
Updated Fiddle
I have the following code. It's essentially a simple grid view that uses <ul> and <li> tags. I wanted to make it responsive such that the <li> elements are always centered no matter what the width of the screen is. How can I do so? I've tried setting the padding-left and padding-right as percentages, however it doesn't work. Right now if I adjust the width of the screen it doesn't always stay centered.
Simply add text-align: center to the parent <ul>
Fiddle
I'll extend on the previous answer...
Also center the UL
#home-listing {
text-align: center;
width:80%; /*Pick Your Own Width*/
margin:16px auto;
}
http://jsfiddle.net/Zd9Gf/3/
It depends on what you mean by centered. In order to center the entire list you could do something like:
ul {
padding: 0px;
width:500px;
margin:auto;
}
See this jsFiddle.
add this class:
ul.search-results{
width:100%;
}
and update this class:
ul.search-results li {
border-right: 1px solid #cecdcb;
border-bottom: 1px solid #cecdcb;
border-top: 1px solid #fff;
list-style: none;
padding: 0;
display: inline-block;
vertical-align: top;
width:60%;
margin:0 20%;
text-align:center;
}
I have a <ul> with several <li> items in it all in a single row. The <li> has a nested <span> and an <img>. The <img> height is all the same across all items. But the <span> items contain text which can span on a single line or two lines (depends on text).
I have tried appying display:block, float:left, height:100%, and few other suggestions I found, but the shorter <li> items will not take the same height as the taller <li> items. So I am left with a space below the shorter <li> items.
Anyone has any suggestions on this?
In this case, when you set height:100% it isn't inheriting any height from its parent. If you want the list items to have 100% height of the div #wrapper, then you should set the ul's height to 100% and set a height on the div #wrapper in pixels or em's:
http://jsfiddle.net/SF9Za/1/
#wrapper {
background: transparent;
width: 350px;
color: white;
height:250px;
}
#wrapper ul {
list-style-type: none;
display: block;
float: left;
background: green;
height:100%;
}
If you'd rather have it stretch to the full height of the browser window, then you need to set the height of html, body in your css to 100%, and then all of the elements down to the li (html, body, div#wrapper, ul.list, and li) must have 100% height:
http://jsfiddle.net/YdGra/
html, body{
height:100%;
margin:0;
padding:0;
}
#wrapper {
background: transparent;
width: 350px;
color: white;
height:100%;
}
#wrapper ul {
list-style-type: none;
display: block;
float: left;
background: green;
height:100%;
}
Here's some other links that you might want to check out that talk about this:
CSS 100% height layout
Setting height: 100% on my label element doesn't work
http://webdesign.about.com/od/csstutorials/f/set-css-height-100-percent.htm
An easy fix is to add display:table on your ul element. The hieght will align with the content.
maybe it fix if you change list-style-position of the container ul
I am struggling to make a list always display elements on 1 line only.
To understand this better let me first show some code:
html structure is this:
<div id="tab-content">
<div id="part-list">
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</div>
CSS style is this:
#tab-content{
overflow: auto;
height: 100%;
position:relative;
background-color: #373837;
color: white;
padding-left:20px;
padding-right:20px;
}
#tab-content ul{
display: block;
list-style: none outside none;
overflow: auto;
white-space: nowrap;
}
#tab-content ul li{
display: block;
float: left;
padding: 7px;
margin-right: 4px;
font-style: italic;
color: #cccccc;
}
I need the ul to have its items on 1 line no matter if it gets bigger than the containing div. If it does get bigger than the scrolling bar should appear. Right now it just gets wrapped..
I am using jscrollPane on #part-list because i have a custom scrollbar there.
PS: specifying a bigger width than the containing div, say 150%, is not a solution as the list has its content pulled out from a db and its length is dynamic.
Change float:left to display:inline. Live Example: http://jsfiddle.net/LgKsY/
#tab-content ul li{
display: inline;
padding: 7px;
margin-right: 4px;
font-style: italic;
color: #cccccc;
}
However in changing it from block to inline your padding/height will seem different. They key here is set a height on the ul of say 30px and then a line-height of the same value i.e. 30px on the li. I have also removed overflow:auto from the ul in this version as it is redundant because the div already does that job. 2nd example: http://jsfiddle.net/LgKsY/1/
Use css display:inline-block; instead of display:block and with display:inline-block use *float:left !important; /* For IE 7*/ and _float:left !important; /* For IE 6*/. Because disaply:inline-block; doesn't support IE 6 & 7.
Another solution, you can use float:left for all browsers instead off using display:inline-block