Align span text within li element - html

Using dataTables, I have a responsive table that produces the following HTML within the child row
<ul data-dtr-index="0" class="dtr-details">
<li data-dtr-index="0" data-dt-row="0" data-dt-column="0">
<span class="dtr-title">Pay No</span>
<span class="dtr-data">999</span>
</li>
<li data-dtr-index="3" data-dt-row="0" data-dt-column="3">
<span class="dtr-title">DOB</span>
<span class="dtr-data">14 Sep 1951</span>
</li>
</ul>
Using CSS, I would like the second span in each li (dtr-data) to have the text aligned to the right. Is this possible in CSS without having to amend the DOM to put the span in a div tag? Ideally I do not want to create my own custom rendere, so to be able to align the span text through CSS only would be ideal

You could use flexbox for this.
This would reset the li display property, which removes the bullet point, so you would need to reintroduce it.
Example:
li {
display: flex;
margin-left: -1em;
}
li:before {
content: '•';
padding-right: .5em;
}
.dtr-data {
margin-left: auto;
}
<ul data-dtr-index="0" class="dtr-details">
<li data-dtr-index="0" data-dt-row="0" data-dt-column="0">
<span class="dtr-title">Pay No</span>
<span class="dtr-data">999</span>
</li>
<li data-dtr-index="3" data-dt-row="0" data-dt-column="3">
<span class="dtr-title">DOB</span>
<span class="dtr-data">14 Sep 1951</span>
</li>
</ul>

You can make the li relative, and the span absolute right:0px. (or use float:right)
li {
position: relative;
}
li span:nth-child(2){
position: absolute;
right: 0px;
}

Method 1
float the nested element in question (.dtr-data).
Code Snippet Demonstration:
.dtr-data {
float: right;
}
li {
max-width: 300px; /* for the sake of demonstration */
clear: both; /* clear floats for good measure */
}
<ul data-dtr-index="0" class="dtr-details">
<li data-dtr-index="0" data-dt-row="0" data-dt-column="0">
<span class="dtr-title">Pay No</span>
<span class="dtr-data">999</span>
</li>
<li data-dtr-index="3" data-dt-row="0" data-dt-column="3">
<span class="dtr-title">DOB</span>
<span class="dtr-data">14 Sep 1951</span>
</li>
</ul>
Method 2
float the nested sibling element (.dtr-title) and declare text-align on containing parent.
Code Snippet Demonstration:
.dtr-title {
float: left;
}
li {
max-width: 300px; /* for the sake of demonstration */
clear: both; /* clear floats for good measure */
text-align: right;
}
<ul data-dtr-index="0" class="dtr-details">
<li data-dtr-index="0" data-dt-row="0" data-dt-column="0">
<span class="dtr-title">Pay No</span>
<span class="dtr-data">999</span>
</li>
<li data-dtr-index="3" data-dt-row="0" data-dt-column="3">
<span class="dtr-title">DOB</span>
<span class="dtr-data">14 Sep 1951</span>
</li>
</ul>
Method 3
Declare flex on the containing parent element and align accordingly with justify-content property.
Code Snippet Demonstration:
li {
max-width: 300px; /* for the sake of demonstration */
display: flex; /* required for following property to apply */
justify-content: space-between; /* to align end-to-end */
}
<ul data-dtr-index="0" class="dtr-details">
<li data-dtr-index="0" data-dt-row="0" data-dt-column="0">
<span class="dtr-title">Pay No</span>
<span class="dtr-data">999</span>
</li>
<li data-dtr-index="3" data-dt-row="0" data-dt-column="3">
<span class="dtr-title">DOB</span>
<span class="dtr-data">14 Sep 1951</span>
</li>
</ul>
A note on flex-box:
Since flex-box has limited to no support for legacy browsers (like I.E) for full browser support and compatibility refer to either one of the aforementioned alternatives.
See browser compatibility:
caniuse.com
flex - CSS | MDN
Method 4
position the nested element in question (.dtr-data) as absolute and offset alignment by declaring a right property value, declare relative positioning on the containing parent element so that the absolutely nested child element is positioned relative to its parent.
note: this solution will most likely require responsive adjustments
Code Snippet Demonstration:
li {
max-width: 300px; /* for the sake of demonstration */
position: relative; /* required */
}
.dtr-data {
position: absolute;
right: 0;
}
<ul data-dtr-index="0" class="dtr-details">
<li data-dtr-index="0" data-dt-row="0" data-dt-column="0">
<span class="dtr-title">Pay No</span>
<span class="dtr-data">999</span>
</li>
<li data-dtr-index="3" data-dt-row="0" data-dt-column="3">
<span class="dtr-title">DOB</span>
<span class="dtr-data">14 Sep 1951</span>
</li>
</ul>

Related

CSS alternate row order for display:table

I have an HTML page with a list of items and their descriptions. The styling is done exclusively in CSS, in the pursuit of keeping information separate from layout.
Is it possible to use only CSS so that for every other row, the columns are reversed? I'd like to avoid using an HTML table, or manually reversing the columns in HTML, and I'd like to keep using <ul> and <li>, since it matches the kind of data that is displayed.
What I have:
img {
max-width: 50px;
height: auto;
}
ul {
list-style-type: none;
border-collapse: collapse;
display: table;
}
.item-name, .item-details {
display: table-row;
}
.item-pic, .item-text {
display: table-cell;
vertical-align: top;
padding: 10px;
}
.item-name {
font-weight: bold;
}
<html>
<body>
<ul>
<li><span class="item-pic"><img src="green.gif"></span>
<span class="item-text">
<span class="item-name">Green</span>
<span class="item-details">This is a green item</span>
</span>
</li>
<li><span class="item-pic"><img src="red.gif"></span>
<span class="item-text">
<span class="item-name">Red</span>
<span class="item-details">This is a red item</span>
</span>
</li>
<li><span class="item-pic"><img src="blue.gif"></span>
<span class="item-text">
<span class="item-name">Blue</span>
<span class="item-details">This is a blue item</span>
</span>
</li>
<li><span class="item-pic"><img src="yellow.gif"></span>
<span class="item-text">
<span class="item-name">Yellow</span>
<span class="item-details">This is a yellow item</span>
</span>
</li>
</ul>
</body>
</html>
What I would like:
You can use the following CSS code. It uses display:flex on the li tags, with reverse order on every second one (:nth-child(even)) , and display: block on the two text spans to make them go across the whole width of their parent and therefore place them below each other. Other details see below:
(The width of the container can be adjusted as desired, it could as well be 100% to span the whole width of its parent)
img {
max-width: 50px;
height: auto;
}
ul {
list-style-type: none;
display: block;
width: 200px;
padding: 0;
}
ul>li {
display: flex;
justify-content: space-between;
align-items: flex-start;
}
ul>li:nth-child(even) {
flex-direction: row-reverse;
}
.item-text > * {
display: block;
}
.item-pic,
.item-text {
padding: 10px;
}
.item-name {
font-weight: bold;
}
<html>
<body>
<ul>
<li>
<span class="item-pic"><img src="green.gif"></span>
<span class="item-text">
<span class="item-name">Green</span>
<span class="item-details">This is a green item</span>
</span>
</li>
<li><span class="item-pic"><img src="red.gif"></span>
<span class="item-text">
<span class="item-name">Red</span>
<span class="item-details">This is a red item</span>
</span>
</li>
<li><span class="item-pic"><img src="blue.gif"></span>
<span class="item-text">
<span class="item-name">Blue</span>
<span class="item-details">This is a blue item</span>
</span>
</li>
<li><span class="item-pic"><img src="yellow.gif"></span>
<span class="item-text">
<span class="item-name">Yellow</span>
<span class="item-details">This is a yellow item</span>
</span>
</li>
</ul>
</body>
</html>
You can do it with a combination of nth-child and flex. With nth-child, you can easily target alternating rows with odd or even keywords. Then it just a matter of setting the li to flex and changing the order for even row. (EDIT: updated this to use row reverse instead as it is indeed easier)
I removed all the able related styles
img {
max-width: 50px;
height: auto;
}
ul {
list-style-type: none;
border-collapse: collapse;
}
.item-pic,
.item-text {
padding: 10px;
}
.item-name {
font-weight: bold;
display: block; // no need for this if you use a div
}
li {
display: flex;
}
li:nth-child( even ) {
flex-direction: row-reverse;
}
<html>
<body>
<ul>
<li><span class="item-pic"><img src="green.gif"></span>
<span class="item-text">
<span class="item-name">Green</span>
<span class="item-details">This is a green item</span>
</span>
</li>
<li><span class="item-pic"><img src="red.gif"></span>
<span class="item-text">
<span class="item-name">Red</span>
<span class="item-details">This is a red item</span>
</span>
</li>
<li><span class="item-pic"><img src="blue.gif"></span>
<span class="item-text">
<span class="item-name">Blue</span>
<span class="item-details">This is a blue item</span>
</span>
</li>
<li><span class="item-pic"><img src="yellow.gif"></span>
<span class="item-text">
<span class="item-name">Yellow</span>
<span class="item-details">This is a yellow item</span>
</span>
</li>
</ul>
</body>
</html>

How to have a parent treeitem in a treeview be a link

The treeview example of the WAI-ARIA Authoring Practices shows how to have a tree with some items being expandable parent nodes, and some being hyperlinks:
<ul role="tree" aria-label="Foods">
<li role="treeitem" aria-expanded="true">
<span> Fruits </span>
<ul role="group">
<li role="none"> <a role="treeitem" href="/orange"> Oranges </a> </li>
<li role="none"> <a role="treeitem" href="/pineapple"> Pineapple </a> </li>
</ul>
</li>
</ul>
However, what if I want to have a "Fruits" page too, and have a treeitem that is both a parent node and a hyperlink?
(For keyboard navigation, the pattern of Right Arrow/Left Arrow opening/closing the parent node, and Enter following the hyperlink would be used. This resembles the interaction of a combobox with a tree popup.
For mouse interaction, the current pattern would be kept: having a clickable icon (aria-hidden) indicating expanded state, which opens/closes the node, and the text of the link itself, which follows the link.)
For example:
<ul role="tree" aria-label="Foods">
<li role="none">
<a role="treeitem" aria-expanded="true" href="/fruits">
<span> Fruits </span>
<ul role="group">
<li role="none"> <a role="treeitem" href="/orange"> Oranges </a> </li>
<li role="none"> <a role="treeitem" href="/pineapple"> Pineapple </a> </li>
</ul>
</a>
</li>
</ul>
This, while technically correct, violates the HTML spec, which disallows <a> tags being nested within other <a> tags.
After much searching, I found this answer, which is about a different topic and only hints at my solution.
To have the <a> element be the treeitem, while simultaneously "containing" other <a> elements nested within groups, the aria-owns attribute can be used.
Like this, the requirement that
Each parent node contains or owns an element with role group.
is satisfied, while at the same time following the HTML spec.
<ul role="tree" aria-label="Foods">
<li role="none">
<a role="treeitem" aria-expanded="true" aria-owns="fruit-group" href="/fruits">
<span> Fruits </span>
</a>
<ul role="group" id="fruit-group">
<li role="none"> <a role="treeitem" href="/orange"> Oranges </a> </li>
<li role="none"> <a role="treeitem" href="/pineapple"> Pineapple </a> </li>
</ul>
</li>
</ul>
This is not exactly what you are looking for but I thought I would share it for anyone that is looking for something similar that accomplishes the same thing at least from the end user's perspective.
The following code has been modified from w3school's Tree View Example to have clickable arrows and parent link items besides them.
Original code: https://www.w3schools.com/howto/howto_js_treeview.asp
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
ul, #myUL {
list-style-type: none;
}
#myUL {
margin: 0;
padding: 0;
}
.caret {
cursor: pointer;
-webkit-user-select: none; /* Safari 3.1+ */
-moz-user-select: none; /* Firefox 2+ */
-ms-user-select: none; /* IE 10+ */
user-select: none;
}
.caret::before {
content: "\25B6";
color: black;
display: inline-block;
margin-right: 6px;
}
.caret-down::before {
-ms-transform: rotate(90deg); /* IE 9 */
-webkit-transform: rotate(90deg); /* Safari */'
transform: rotate(90deg);
}
.nested {
display: none;
}
.active {
display: block;
}
</style>
</head>
<body>
<h2>Tree View</h2>
<p>A tree view represents a hierarchical view of information, where each item can have a number of subitems.</p>
<p>Click on the arrow(s) to open or close the tree branches.</p>
<ul id="myUL">
<li><span class="caret">Beverages</span>
<ul class="nested">
<li>Water</li>
<li><span class="caret">Juice</span>
<ul class="nested">
<li>Orange Juice</li>
<li>Apple Juice</li>
</ul>
</li> <!-- End of caret Tea -->
<li><span class="caret">Tea</span>
<ul class="nested">
<li>Black Tea</li>
<li>White Tea</li>
<li><span class="caret"></span>Green Tea
<ul class="nested">
<li>Sencha</li>
<li>Gyokuro</li>
<li>Matcha</li>
<li>Pi Lo Chun</li>
</ul>
</li>
</ul>
</li> <!-- End of caret Tea -->
</ul> <!-- End of nested -->
</li> <!-- End of caret Beverage -->
</ul> <!-- End of myUL -->
<script>
var toggler = document.getElementsByClassName("caret");
var i;
for (i = 0; i < toggler.length; i++) {
toggler[i].addEventListener("click", function() {
this.parentElement.querySelector(".nested").classList.toggle("active");
this.classList.toggle("caret-down");
});
}
</script>
</body>
</html>

why border bottom is wider than list content?

I have a list and I want to apply border bottom to it, here is what I want
here is what I have so far.
here is jsfiddle: https://jsfiddle.net/Mwanitete/vr1c8tgo/10/
.data-right-bottom ul li {
margin: 10px;
list-style-type: none;
display: flex;
width: 400px;
border-bottom: 1px solid green;
}
.data-right-bottom ul li span {
flex: 1 0 0;
width: 400px;
}
<div class="data-right-bottom">
<ul>
<li style="display: none;">
<span>Total wrapping (tax incl.)</span>
<span id="total_wrapping_right" class="total_wrapping_right">0,00 zł</span>
</li>
<li>
<span>Total products (tax incl.)</span>
<span id="total_product_right" class="total_product_wt_right">210,00 zł</span>
</li>
<li>
<span>Total shipping (tax incl.)</span>
<span class="total_shipping_right">
12,00 zł
</span>
</li>
<li class="order-subtotal">
<span>Total (tax excl.)</span>
<span class="total_price_without_tax_right">182,73 zł</span>
</li>
<li class="order-discounts" style="display:none">
<span>Total discounts</span>
<span class="total_discount_right">0,00 zł</span>
</li>
<li class="order-tax">
<span>Total tax</span>
<span class="total_tax_right">39,27 zł</span>
</li>
<li class="order-total">
<span>Total</span>
<span class="total_price_right">222,00 zł</span>
</li>
</ul>
</div>
what am i missing here? any help will be apreciated
Another option besides Lazar Nikolic's one is to use a CSS table instead of a flexbox. After all, that's what it is; it displays tabular data.
To make it responsive as you wanted, you can always use a media query to turn it back into a series of blocks so that everything is displayed underneath each other.
.data-right-bottom ul {
display: table;
}
.data-right-bottom ul li {
list-style-type: none;
display: table-row;
}
.data-right-bottom ul li span {
display: table-cell;
border-top: 1px solid green;
}
.data-right-bottom ul li span:last-child {
padding-left: 10px;
text-align: right;
}
#media all and (max-width: 17em) {
.data-right-bottom ul li {
display: block;
border-top: 1px solid green;
}
.data-right-bottom ul li span {
display: block;
border-top: none;
}
}
<div class="data-right-bottom">
<ul>
<li style="display: none;">
<span>Total wrapping (tax incl.)</span>
<span id="total_wrapping_right" class="total_wrapping_right">0,00 zł</span>
</li>
<li>
<span>Total products (tax incl.)</span>
<span id="total_product_right" class="total_product_wt_right">210,00 zł</span>
</li>
<li>
<span>Total shipping (tax incl.)</span>
<span class="total_shipping_right">
12,00 zł
</span>
</li>
<li class="order-subtotal">
<span>Total (tax excl.)</span>
<span class="total_price_without_tax_right">182,73 zł</span>
</li>
<li class="order-discounts" style="display:none">
<span>Total discounts</span>
<span class="total_discount_right">0,00 zł</span>
</li>
<li class="order-tax">
<span>Total tax</span>
<span class="total_tax_right">39,27 zł</span>
</li>
<li class="order-total">
<span>Total</span>
<span class="total_price_right">222,00 zł</span>
</li>
</ul>
</div>
The problem is within your css class for
.data-right-bottom ul li span. Delete it.
Also, under display: flex in .data-right-bottom ul li you should say justify-content: space-between to separate two spans that you have inside. This will give you borders that are as long as your content is. I suppose you used flex: 1 0 0; in order for your list to be responsive, but it could not be done considering that you said that the width of your list item should be 400px.
It looks like that you're trying to create a table - aren't you? If yes, you should use an HTML table instead of a list.
Especially if you want that the content in the last column has text-align: left; but the whole column be aligned to the right border of your block.
List items (li inside ul) are independent of each other and you can't make one span inside li to have the same variable width depend on the width of the span in other li. Table row, in contrast, changes the width based on its content.
table {
width: 400px;
border-collapse: collapse;
}
td {
border-top:1px solid green;
padding: 10px 0;
}
.price {
text-align: left;
width: 1%;
white-space: nowrap;
}
<div class="data-right-bottom">
<table>
<tr>
<td>Total products (tax incl.)</td>
<td class="price">
210,00 zł
</td>
</tr>
<tr>
<td>Total shipping (tax incl.)</td>
<td class="price">
12,00 zł
</td>
</tr>
</table>
</div>
Maybe you should change the width of your span to 200px (Span are two inside a "li").

Aligning the elements using bootstrap css

I have created bootstrap menu items and kept FLIP option beside that.
FIDDLE
Here issue is that FLIP should be just beside the box, but it is appearing downside?
HTML:
<div class="magic-container">
<div class="input-group">
<input type="text" class="typeahead form-control" placeholder="Select category ..." />
<ul class="dropdown-menu">
<li id="one">Sports</li>
<li role="presentation" class="divider"></li>
<li id="two">Entertainment</li>
<li role="presentation" class="divider"></li>
<li id="three">Politics</li>
<li role="presentation" class="divider"></li>
<li id="four">Business</li>
<li role="presentation" class="divider"></li>
<li id="four">Travel</li>
<li role="presentation" class="divider"></li>
<li id="four">Celebrity</li>
<li></li>
</ul>
<span class="flip">
<span class="dropDownFlip">
FLIP
</span>
</span>
</div>
</div>
Your input is having a width of 100% being applied to it. It is being applied from the bootstrap.min.css file. By adding the following to your css it fixes your issue:
.input-group .form-control {
width: auto;
}
Here is your jsFiddle updated.
Update: Since this is a change to an existing class and could possibly affect elements elsewhere on your page/site, you could create a new class for this particular instance. Something like this would work:
.input-group .floatLeft {
float: left !important;
width: auto;
}
and just add the class floatLeft to your input.
Try to add following styles in you stylesheets
.magic-container .input-group input {
float: left;
margin-right: 2%;
width: 70%;
}
.flip {
float: left;
}

Menu Item break text

I have a problem. It might look simple, but I can't fix this.
I have a menu, that shows me 10 items. The 9th item is not aligned in right way, cause has 2 lines of text.
How can I fix this?
JSFIDLE
HTML:
<ul style="list-style:none; width: 615px;">
<li style="display: inline-block;"><a class="butao" href="http://www.ahighplay.org/pt/gca/index.php?id=34&preview=1">DFC Portugal</a>
</li>
<li style="display: inline-block;"><a class="butao" href="http://www.ahighplay.org/pt/gca/index.php?id=30&preview=1">Processo DFC</a>
</li>
<li style="display: inline-block;"><a class="butao" href="http://www.ahighplay.org/pt/gca/index.php?id=72&preview=1">Como Participar</a>
</li>
<li style="display: inline-block;"><a class="butao" href="http://www.ahighplay.org/pt/gca/index.php?id=70&preview=1">Calendário</a>
</li>
<li style="display: inline-block;"><a class="butao" href="http://www.ahighplay.org/pt/gca/index.php?id=71&preview=1">Regulamento</a>
</li>
<li style="display: inline-block;"><a class="butao" href="http://www.ahighplay.org/pt/gca/index.php?id=26&preview=1">FAQ's</a>
</li>
<li style="display: inline-block;">Vídeos
</li>
<li style="display: inline-block;">Notícias
</li>
<li style="display: inline-block;">Parceiros / Patrocinadores
</li>
<li style="display: inline-block;">Contactos
</li>
Add this settings to make the buttons stay on the same line.
ul li {
vertical-align: middle;
}
jsFiddle Demo
Further more, is not advisable to use inline styles (and it repeats itself on every list item). You should move that setting to the CSS, so your code would be:
ul li {
vertical-align: middle;
display: inline-block;
}