Dynamic CSS based on database info - ruby-on-rails

I'd like to know how to dynamically change my CSS styling based on the number of elements in my database.
Users can choose a certain number of columns, and the number of columns they choose determines the width of the columns (obviously the more columns chosen, the smaller the width of each column would be so that they are evenly spaced horizontally across the page.
How do you do something like this?

Sounds like a good use of ahem TABLEs!
The issue is you can't do this by changing the CSS, you need to change the HTML
<table width="100%">
<tr>
<td>..</td> (repeat for the number of columns)
</tr>
</table>
By default all table cells (TD) will be of equal width.

You need to dynamically assign styles in your .html.erb. You can use, tables, divs, lists - whatever you want. Just assign different classes depending on the number of columns (class="small", class="wide") and define those in your CSS file OR (possibly less pretty) you can use inline-styles.
For example, in this code I assign the width of an element (to do a five-star rating):
<ul class="stars floatstars">
<li class="yellowstars" style="width: <%= #article.avg_rating * 25 %>px !important;"></li>
<li class="text"><%= #article.avg_rating %> average from <%= pluralize(#article.count_ratings, "vote") %></li>
</ul>
Edit: if you set the class as a variable in the controller this would be an example for the view:
<li class=<%= #myclass %>>...</li>

Related

Controlling width of MatTable Column

How can I control the width of the columns in MatBlazor MatTable
I have fiddled a bit around, and by disabling the css class mdc-table in the browser 'inspector'... it seems to be possible to get 'normal' table behavior.
right now all column except the last column is quite narrow, (actual widths: 119, 153, 460)
the last columns is just two icons, so it should be about 100px width, and the middle column is a quite long text string, so I would like that to be much wider.
There isn't much drama about the code, but here it is anyway...
<MatDialog #bind-IsOpen="#ShowBeginCreateNewSLA">
<MatDialogTitle>Service level agremeents</MatDialogTitle>
<MatDialogContent>
<MatTable ShowPaging="false" Items="SLAOptions">
<MatTableHeader>
<th>Id</th>
<th>Description</th>
<th>Action</th>
</MatTableHeader>
<MatTableRow>
<td>#context.Id</td>
<td>#context.Description</td>
<td><MatIconButton Icon="delete" /></td>
</MatTableRow>
</MatTable>
</MatDialogContent>
<MatDialogActions>
<MatButton>Close</MatButton>
</MatDialogActions>
</MatDialog>
Any idea what I can do?
I found that the way to set it, also mentioned in the documentation, is simply to insert a div with a fixed with in the table header, such as
<th><div style="width: 200px">Options</div></th>

What is the class for min width using Bootstrap 5?

I looked at the documentation for Bootstrap 5 but I can't seem to find the min-width class using pixels? Is there any?
https://v5.getbootstrap.com/docs/5.0/utilities/sizing/
Bootstrap CSS Grid layout does the heavy lifting in terms of setting responsive breakpoints.
Each view/page/front-end element can be divided into 12 parts. and you can choose how much space a container takes out of 12 vertical columns.
Bootstrap 5 gives you 6 breakpoints pre-defined, with the following syntax
col-xs-{number-between-1-to-12} width <= 540px
col-sm-{number-between-1-to-12} max-width = 540px
col-md-{number-between-1-to-12} max-width = 720px
col-lg-{number-between-1-to-12} max-width = 960px
col-xl-{number-between-1-to-12} max-width = 1140px
col-xxl-{number-between-1-to-12} width <= 1400px
Example 1: You get a parent div with 2 child divs, these children will take equal divided space inside for all screen sizes
<div id="i-am-a-parent-div">
<div class="col-6"> This takes First Half the Space inside parent element</div>
<div class="col-6"> This takes Second Half the Space inside parent element</div>
</div>
Example 2: The Children divs will share 6/12 space each till max-width=540, but then they will change space occupation to 4/12 and 8/12 respectively.
NOTE: I've added two separated classes for each child div, you can add as many as needed for each breakpoint.
<div>
<div class="col-sm-6 col-md-4"> This takes First Half (6/12) the Space inside parent element till max-width = 540px,
BUT it will only take One-Thirds (4/12) the space between 540px & 768px </div>
<div class="col-sm-6 col-md-8"> This takes Second Half (6/12) the Space inside parent element till max-width = 540px,
BUT it will only take Two-Thirds (8/12) the space between 540px & 768px </div>
</div>
If this doesnt satisfy what you need to do, Then there are more customization options to their breakpoints, Feel free to checkout: https://v5.getbootstrap.com/docs/5.0/layout/grid/#grid-options
Let me know if this helps you out!

Angular Material vertical percentage space

How can I make one div to take certain percent of parent space verically?See the code below:
<div layout="column">
<div id="div1" flex="70%"></div>
<div id="div2" flex="30%"></div>
</div>
I want to make div1 70% height and div2 30% height of parent div. But it does not work. Div 1 and div 2 collapsed. But if the parent layout is row, it works fine---div1 takes 70% percent space horizontally.
I think you need to make the following modifications:
Specify a "height" to your outside div. This could be a percentage or pixels.
Do not use the % sign with the flex attribute
See this codepen: http://codepen.io/sebastiengiroux/pen/jPovxG
<div layout="column" style="height: 100%;">
<div id="div1" flex="70" style="background-color:red;"></div>
<div id="div2" flex="30" style="background-color:blue;"></div>
</div>
If you look into Layout docs https://material.angularjs.org/latest/layout/introduction
there is this statement that I think is relevant to your problem.
The flex attribute value is restricted to 33, 66, and multiples of five.
For example: flex="5", flex="20", flex="33", flex="50", flex="66", flex="75", ....
So I guess if you use allowed values, you can achieve results that you need. For example using 66 and 33.

CSS, HTML: How can i make the div or <p> width dynamic as the text line increses/decreses

There is a line of text inside a <div></div> or inside <p></p>.
The text length changes when clicking some links.
If the text length decreases the div or p will decrease.
If the text length increases the div or p will increase.
How can i do this ?
The problem is the div or p width looks fixed and the length of text is 30% of that div or p.
The div or p has a background color. So whole div or p is colored. But what i'm trying to do is to color the portion of background of div or p, the portion that contains text of line.
Wrap the text in a <span></span> and style that instead?
It sounds to me like you're struggling with the difference between an inline and block level element, but forget about that a second and let's get to what you want. Without seeing your code... an easy way to fix that is to make your block level elements p and div float to the left. From what you've said, this should be close to what you want (check the jsFiddle).
div {
float:left;
background:red;
}
Ok, so what is a block level element? A div and p are block level elements. As per the Mozilla Developer Network "Their most significant characteristic is that they typically are formatted with a line break before and after the element (thereby creating a stand-alone block of content)." This is why with default styling it expands to the entire line. This sounds like exactly what you didn't want. Where as a span will shrink down to fit just the content that it contains.
If you want a more complicated "real-world" example, I've thrown together this jsFiddle that might help you down your path.
Ah, you're doing Rails! Ok, here's your real answer. Just check to see if the flash[:notice] exists before displaying it to the screen.
<% if flash[:notice] %>
<p class="customnotice"><%= flash[:notice] %></p>
<% end %>

Flex 3 htmlText calculates height way wrong and strips span tags

I currently have two problems with Flex 3 htmlText.
1) When I am setting my text's htmlText:
myText.htmlText = <html text stored in my mysql database>
It calculates the height way wrong. In most cases, a ton of padding (or whitespace) is added above and below the text. I am not sure what happens, but it calculates the textHeight way higher than normally.
2) If I put <span> tags in my html, it automatically strips them out (instead of just ignoring them). I am using the span tags to be able to dynamically find certain pieces of my text. For instance:
<span class="salutation">Dear,</span> <span class="tag">[First Name]</span>
is inserted in my htmlText, and I use them to parse out the salutation and tag of my variable data.
Does anyone know of any solutions for these two issues? Any alternative ideas on how to parse out pieces of html? Any way to improve htmlText? How to correctly measure textHeight and/or remove padding?
Thanks!
Basically, the tag needs width and height explicitly set in order for flex to calculate the height correctly on these elements. Also, you can use:
invalidateSize();
validateSize();
To force size validation to occur, which will at times help!
Add these two lines before you use any XML:
XML.ignoreWhitespace = true;
XML.prettyPrinting = false;
This should take care of the added lines above and below your text.
ignoreWhitespace strips leading and trailing whitespace from your text nodes.
prettyPrinting, when set to true, formats the XML output, indenting nodes and adding newline characters to make it more readable - but also affecting text output. For example, when you have a structure like this:
<p><span class="test">This is a test.</span></p>
Flash will output:
<p>
<span class="test">
This is a test.
</span>
</p>
This leads to your TextField displaying:
(newline)
(newline)
This is a test.
(newline)
(newline)
The Flash plugin will remove any HTML tags, if the styleSheet property isn't properly set. So in your case, it will remove the <span> tags and add its own <P> and <FONT> tags instead, according to any font information you've specified using defaultTextFormat or setTextFormat (try tracing htmlText after you've set it - you'll see). You have to set the TextField's styleSheet property to an actual StyleSheet, if you want to keep your own tags.
Parsing out the information you need by using HTML tags is a valid way to go. I usually use numbered placeholders like {0} and string.replace () to insert dynamic values.

Resources