Having corrected your invalid HTML (as noted in the comment to your question no element other than an li
is a valid child of a ul
or ol
, including those elements themselves) to the following:
<div>
<ul>
<li class="items">
<input type="button" value="" class="cancel" />
<input type="button" value="" class="save" />
<input type="text" value="Big list item" class="name" />
</li>
<li>
<ul>
<li id="category_2" id_category="2" class="items">
<input type="button" value="" class="cancel" />
<input type="button" value="" class="save" />
<input type="text" value="Medium list item" class="name" />
</li>
<li>
<ul>
<li class="items">
<input type="button" value="" class="cancel" />
<input type="button" value="" class="save" />
<input type="text" value="1st small item" class="name" />
</li>
<li class="items">
<input type="button" value="" class="cancel" />
<input type="button" value="" class="save" />
<input type="text" value="2nd small item" class="name" />
</li>
<li class="items">
<input type="button" value="" class="cancel" />
<input type="button" value="" class="save" />
<input type="text" value="3rd small item" class="name" />
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
Note that I had to add a class to those li
elements that contain children (since the previous, simple, li
selector now matches the multiple elements in the hierarchy), in this case the class items
, but adjust to taste.
The following CSS works, albeit only with a pretty up-to-date browser:
li {
width: 80%;
clear: both;
float: right;
margin: 0 0 0.2em 0;
}
li.items {
background-color: #f90;
}
li .save, li .cancel {
float: right;
width: 20px;
height: 20px;
}
input.name {
width: -webkit-calc(100% - 45px);
width: -moz-calc(100% - 45px);
width: -o-calc(100% - 45px);
width: -ms-calc(100% - 45px);
width: calc(100% - 45px);
}
This uses the CSS calc()
function to determine the width of the input
element as a calculation.
To be cross-browser compatible with the older browsers you could use, instead, absolute (for the .save
and .cancel
elements) and relative (for the li
elements) positioning:
li {
width: 80%;
clear: both;
float: right;
margin: 0 0 0.2em 0;
position: relative;
}
li.items {
background-color: #f90;
padding: 0 45px 0 0;
}
li .save, li .cancel {
position: absolute;
width: 20px;
height: 20px;
}
.save {
top: 0;
right: 21px;
}
.cancel {
top: 0;
right: 0;
}
input.name {
width: 100%;
}
You'll note that the padding
gets added to the width of the li
elements, of course, so you may wish to add the box-sizing
CSS property to compensate for that:
/* all other CSS remains untouched */
li {
width: 80%;
clear: both;
float: right;
margin: 0 0 0.2em 0;
position: relative;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
-o-box-sizing: border-box;
box-sizing: border-box;
}
References: