How to pass parameters to css classes - html

I want to know is it possible to add some flexibility to css via this:
<div class='round5'></div>
where .round is a class with round corners and '5' determines the amount of radius. Is it possible? I have seen some where, but I don't know how the implementation takes place.

For anyone stumbling across this in 2018, whilst not fully supported CSS variables now give you the ability to pass a variable directly into your class.
<div class="round" style="--radius: 100%;"></div>
<style>
.round {
display: block;
height: 40px;
width: 40px;
border: 1px solid #BADA55;
border-radius: var(--radius);
}
</style>
You can also define root variables and pass them in as well
<div class="round" style="--radius: var(--rad-50);"></div>
<style>
:root {
--rad-0: 0%;
--rad-50: 50%;
--rad-100: 100%;
}
.round {
display: block;
height: 40px;
width: 40px;
border: 1px solid #BADA55;
border-radius: var(--radius);
}
</style>
This is also scoped to the element as well. If you set the --radius in one element is wont effect another element. Pretty jazzy right!

You can't define the border radius separate from its value because it's all one property. There's no way to tell an element to have rounded corners "in general" without also specifying how much to round them by.
However, you can do something kind of similar with multiple classes and different properties:
HTML:
<div class="rounded blue"></div>
<div class="rounded green"></div>
CSS:
.rounded {
border-radius: 5px;
}
.blue {
background: blue;
}
.green {
background: green;
}
The .rounded class adds the border radius and the .blue and .green classes add the background color.
(I like to name and order the classes such that they read logically, like <div class="large box"></div>, etc.).

Here is an answer that I came up with that requires a small amount of jQuery, and a small knowledge of Regex.
$(function() {
var number = $("div").attr("class").match(/\d+$/);
$("div").css({
"width": "100px",
"height": "100px",
"background-color": "green",
"border-radius": number + "px"
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='round54'>hello</div>
The .match() function uses Regex. Regex is used to detect parts of strings. The \d detects any digits. The + matches the previous selector 1 or more times. In other words, the number can be a multi digit number. And the $ means it has to be at the end.
So then the jQuery uses that in the border-radius property later. All you have to do is append px, and you are good to go.
Fiddle

You could do something similar but not exactly the way you've put it.
CSS
.radius{
border-radius: 10px;
border: 1px solid red;
}
.r5{
border-radius:5px;
}
HTML
<div class="radius">Hello World</div>
<br/>
<div class="radius r5">Hello World</div>
Working Example
In the example above the red border will be retained but the border-radius will change.
Note that you don't start class names with numbers, hence r5 rather than 5

You can use multiclassing on the element. Eg.:
HTML:
<div class="round">Box without border radius</div>
<div class="round rounded-5">Box with 5px border radius</div>
<div class="round rounded-10">Box with 10px border radius</div>
CSS:
.round {
border: 1px solid #000;
width: 100px;
height: 100px;
}
.round.rounded-5 {
border-radius: 5px;
}
.round.rounded-10 {
border-radius: 10px;
}

you can do this. but you have to create the css in the html document(not linked, but between the <style> tag). you can use php or javascript to make a loop. for example try this:
<style>
<?php
$round = 5;
for ($round = 50; $round <= 150; $round+=25){
echo "#round$round{
height: 300px;
width: 300px;
background: #f00;
border-radius : ".$round."px;
margin: 2px;
}
";
}
?>
</style>
<?php
for ($round=50;$round<=150; $round+=25){
echo "<div id='round$round'>
</div>
";
}
?>
hope this helps! :D

Maybe what you want is like this
CSS
.round {
border-radius: 4px; /*it's default when you juse using .round*/
}
.round.five {
border-radius: 5px;
}
.round.ten {
border-radius: 10px;
}
HTML
<div class="round five">something</div>

You can do what you are saying but you would have to reserve the keyword "round" for only this purpose. If you look at the following.
div[class*="round"] {
background-color: green;
color: white;
padding: 10px;
}
And then targeting specific variants of it using...
div[class="round5"] {
border-radius: 5px;
}
The first block of code selects all class names which contain the word round, this can be both a good thing and a bad thing.

Related

Avoid using the !important css attribute for showing my container

I show/hide a container with boxes inside as showed below.
I use a simple mechanism with toggleClass to show/hide the container.
$("#btn").click(
function () {
$("#switch-apps-panel").toggleClass('flex-apps-panel');
}
);
The problem is I had to use the important attribute on the css and I prefer to avoid it.
.flex-apps-panel {
display: flex !important;
}
Any help on slightly changing my code to avoid using the important attribute ?
jsFiddle: https://jsfiddle.net/hg60e8gf/
You defined switch-apps-panel as an ID. IDs are always higher ranked and more specific than class names.
In order to get rid of your !important statement, either change the ID to a class or make your selector more specific and add the ID selector to your .flex-apps-panel like this:
#switch-apps-panel.flex-apps-panel {
display: flex;
}
Here I changed it to be a class:
$(document).ready(function() {
$("#btn").click(
function() {
$(".switch-apps-panel").toggleClass('flex-apps-panel');
}
);
});
.switch-apps-panel {
display: none;
z-index: 9999;
position: fixed;
top: 70px;
left: 10px;
background-color: white;
border: 1px solid #b6b6b6;
box-sizing: content-box;
box-shadow: 0 1px 15px rgba(0, 0, 0, .4);
padding: 10px 10px 10px 10px;
}
.flex-apps-panel {
display: flex;
}
.box-1 {
margin: 8px;
width: 100px;
background-color: yellow;
}
.box-2 {
margin: 8px;
width: 100px;
background-color: blue;
}
.box-3 {
margin: 8px;
width: 100px;
background-color: orange;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
By default, boxes are hidden.
<button id="btn">Click here to show/hide boxes</button>
<div class="switch-apps-panel">
<div class="box-1">
<span>First</span>
</div>
<div class="box-2">
<span>Second</span>
</div>
<div class="box-3">
<span>Third</span>
</div>
</div>
You need to increase your selector's specificity. ID selectors have higher specificity than class selectors, so you can just try to select your ID with class like this:
#switch-apps-panel.flex-apps-panel {
display: flex;
}
You can read more about specificity on MDN, and also try to avoid styling by IDs in the future.

How to change properties of CSS thing in HTML at usage?

Sorry if the question is not really relevant, I'm french and I don't know the right terms of what I am asking exacly :)
Here is the 'thing' (module?) I've created to create a circle.
.fake-avatar {
width: 70px;
height: 70px;
}
But I want to outline this one without creating this :
.fake-avatar-outline {
width: 72px;
height: 72px;
}
Here is where I use it :
.fake-avatar-outline.rounded.flex-center.b-success.m-auto.bg-pink
.fake-avatar.rounded.flex-center.b-success.text-xl.m-auto.bg-pink-light.icon-star
so the goal is to have only fake-avatar and change manually the size. How is it possible? Should I do something like that :
.fake-avatar.rounded.flex-center.b-success.m-auto.bg-pink(width='72px')
.fake-avatar.rounded.flex-center.b-success.text-xl.m-auto.bg-pink-light.icon-star
Thank you,
Nicolas
If I am understanding this correctly you are looking for a border
.fake-avatar {
width: 70px;
height: 70px;
/* Add this*/
border: 2px solid #FFF; /* color can go here */
/* If you wanted a circle */
border-radius: 50%;
}
EDIT: As stated in the comments, the poster was also looking for a way to change the width of the element without having to change the class in the css file. I said he could use inline-styles.
<div class="fake-avatar" style="width: 72px"></div>
I also noted that setting styles this way is usually frowned upon as it makes css maintenance a nightmare.
Expanding on #DavidLee's answer, here a snippet with running code for both options.
.fake-avatar {
width: 70px;
height: 70px;
border: 2px solid white;
border-radius: 50%;
background-color:blue;
}
.fake-avatar-outline {
width: 70px;
height: 70px;
border: 1px solid red;
border-radius: 50%;
}
<div class="fake-avatar"></div>
<div class="fake-avatar-outline"></div>

How can I modify bootstrap text input fields to have text cut off the top on focus?

I imagine this is going to be a bit of a difficult problem, but I'm curious how I can use CSS (potentially animations) to modify bootstrap to have functionality similar to this:
I've found a few different examples, but they only really help with getting a material design-esque look, like this:
Any suggestions on how to implement this? I'm stuck
This might help you. Check the jsfiddle https://jsfiddle.net/xo5gdp8r/
span {
position: absolute;
z-index: 1;
top: 5px;
color: red;
background: #fff;
left: 15px;
padding: 0 10px;
}
input {
margin: 15px 10px;
border: 1px solid red;
padding: 10px;
width: 200px;
}
You need to wrap it inside a div and provide position as relative to the div.
I would also say something similar
your HTML should look
<div class="form-group">
<div class="error-message">is required field.</div>
<input class="form-control" required placeholder="Input field" />
</div>
CSS should be :
.form-group .error-message{
display:none;
}
.form-group.required .form-control{
border-color:red;
}
.form-group.required .error-message{
display:inline-block;
color:red;
position:absolute;
background-color:#fff;
margin-top: -10px;
margin-left: 20px;
}
and JS can be :
(function(){
$('.form-control[required]').on('blur',function(){
if(!$(this).val()){
$(this).parent().addClass('required');
}
});
})();
You can see it in action : https://codepen.io/FaridNaderi/pen/EXQzJK

How does one draw a box around some sibling HTML elements?

I'd like to visually highlight a set of sibling elements that share the same attribute.
#boxed_contents span[data-boxme] {
display: inline-block;
border: 2px solid #00F;
border-radius: 5px;
}
<div id="boxed_contents">
<span>hello</span><!--
--><span>world</span><!--
--><span data-boxme>look</span><!--
--><span data-boxme>at</span><!--
--><span data-boxme>me</span>
</div>
This almost works like I want it to, but I'd like to join the boxes around each of the boxme elements, leaving just one box around all three elements. I know I can wrap the adjacent boxme elements in a wrapper div, but since this is conceptually a visual (rather than a structural) choice, I'd really like to do this without modifying the HTML.
Is there a way to do this in pure CSS? Failing that, with the addition of some straightforward Javascript?
Actually it is not possible to wrap elements in a another one by pure CSS. But we can somehow fake the effect by adding border to each adjacent element and putting an absolutely positioned pseudo-element over the middle borders.
As an aside, note that custom attributes are not valid in HTML unless they are formatted as data-*.
#boxed_contents [data-boxme] {
display: inline-block;
border: 2px solid #00F;
}
#boxed_contents [data-boxme] + [data-boxme] {
margin-left: -.25em;
padding-left: .25em;
position: relative;
}
#boxed_contents [data-boxme] + [data-boxme]:after {
content: "";
position: absolute;
top: 0; bottom: 0; left: -4px;
width: 4px;
background: white;
}
<div id="boxed_contents">
<span>hello</span>
<span>world</span>
<span data-boxme>look</span>
<span data-boxme>at</span>
<span data-boxme>me</span>
<span>not me</span>
</div>
Does this work for you?
var borderProps='2px solid #f00',borderRadius='5px',boxMeAttr='data-boxme';
var spans=document.querySelectorAll('span'),boxMeArrays=[],dummyArray=null,iterator=0;
var currSpan=null,prevSpan=null;
var i,length=spans.length,adjacentSpans=0;
for(i=0; i<length; i+=1){
prevSpan=currSpan;
currSpan=spans[i];
if(currSpan.hasAttribute(boxMeAttr)){
if(prevSpan!==null&&prevSpan.hasAttribute(boxMeAttr)){
dummyArray[dummyArray.length]=currSpan;
}else{
dummyArray=[currSpan];
boxMeArrays[iterator]=dummyArray;
iterator+=1;
}
}
}
length=boxMeArrays.length;
for(i=0; i<length; i+=1){
adjacentSpans=boxMeArrays[i].length;
for(var j=0; j<adjacentSpans; j+=1){
currSpan=boxMeArrays[i][j];
if(adjacentSpans>1){
if(j===0){
currSpan.innerText+=' ';
currSpan.style.borderLeft=currSpan.style.borderTop=currSpan.style.borderBottom=borderProps;
currSpan.style.borderTopLeftRadius=currSpan.style.borderBottomLeftRadius=borderRadius;
}else if(j===adjacentSpans-1){
currSpan.style.borderTop=currSpan.style.borderBottom=currSpan.style.borderRight=borderProps;
currSpan.style.borderTopRightRadius=currSpan.style.borderBottomRightRadius=borderRadius;
}else{
currSpan.innerText+=' ';
currSpan.style.borderTop=currSpan.style.borderBottom=borderProps;
}
}else{
currSpan.style.border=borderProps;
currSpan.style.borderRadius=borderRadius;
}
}
}
Definitely not straightforward but I think it scales well enough. And if you are OK with it, it does modify your HTML with just one bit: adding an extra space using innerText so borders could join each other nicely.
Take a look at the implementation on jsFiddle. Hope I haven't missed out any detail and really hoping this solution works for you.
Thanks to Hashem for putting me on the right track with the sibling selector (to uniquely style consecutive elements), and a pseudo-selector to add border elements.
I have had to add a line of JavaScript to ensure that there is a blank span element without the boxme attribute at the very end. By doing so, I can use the :before pseudo element on any non-boxme element following a boxme element. The main advantage to this strategy (over the one given by Hashem) is that I get to keep the rounded corners from my original CSS.
document.getElementById('boxed_contents').appendChild(document.createElement('span'));
#boxed_contents span[data-boxme] {
border: 2px solid #00F;
border-right: none;
border-radius: 5px 0 0 5px;
padding-left: 4px;
}
#boxed_contents span[data-boxme] + span[data-boxme] {
border-left: none;
border-radius: 0;
padding-left: 0;
}
#boxed_contents span[data-boxme] + span:not([data-boxme]):before {
content: "";
border: 2px solid #00F;
border-left: none;
border-radius: 0 5px 5px 0;
padding-right: 4px;
}
<div id="boxed_contents">
<span>hello</span><!--
--><span>world</span><!--
--><span data-boxme>look</span><!--
--><span data-boxme>at</span><!--
--><span data-boxme>me</span>
</div>
I'm not sure how cross-platform this solution is, but it seems to work well on my target platform of Chrome.
I have edited your code to work how you describe you want it to work, the .boxme tag in the class element applies the style whereby the boxme class is inside the #box_contents tag.
#boxed_contents.boxme {
display: inline-block;
}
#boxed_contents.borderBox {
border: 2px solid #00F;
border-radius: 5px;
}
:)
<div id="boxed_contents">
<span>hello</span>
<span>world</span>
<div class='borderBox'>
<span class='boxme'>look</span>
<span class='boxme'>at</span>
<span class='boxme'>me</span>
</div>
</div>
If you know the layout of the boxme structure, then you can use it to force by adding an extra DIV around the chosen elements.

horizontal line and right way to code it in html, css

I need to draw a horizontal line after some block, and I have three ways to do it:
1) Define a class h_line and add css features to it, like
#css
.hline { width:100%; height:1px; background: #fff }
#html
<div class="block_1">Lorem</div> <div class="h_line"></div>
2) Use hr tag
#css
hr { width:100%; height:1px; background: #fff }
#html
<div class="block_1">Lorem</div> <hr />
3) use it like a after pseudoclass
#css
.hline:after { width:100%; height:1px; background: #fff; content:"" }
#html
<div class="block_1 h_line">Lorem</div>
Which way is the most practical?
hr {
display: block;
height: 1px;
border: 0;
border-top: 1px solid #ccc;
margin: 1em 0;
padding: 0;
}
<div>Hello</div>
<hr/>
<div>World</div>
Here is how html5boilerplate does it:
hr {
display: block;
height: 1px;
border: 0;
border-top: 1px solid #ccc;
margin: 1em 0;
padding: 0;
}
I'd go for semantic markup, use an <hr/>.
Unless it's just a border what you want, then you can use a combination of padding, border and margin, to get the desired bound.
.line {
width: 53px;
height: 0;
border: 1px solid #C4C4C4;
margin: 3px;
display:inline-block;
}
<html>
<body>
<div class="line"></div>
<div style="display:inline-block;">OR</div>
<div class="line"></div>
</body>
</html>
In HTML5, the <hr> tag defines a thematic break. In HTML 4.01, the
<hr> tag represents a horizontal rule.
http://www.w3schools.com/tags/tag_hr.asp
So after definition, I would prefer <hr>
If you really want a thematic break, by all means use the <hr> tag.
If you just want a design line, you could use something like the css class
.hline-bottom {
padding-bottom: 10px;
border-bottom: 2px solid #000; /* whichever color you prefer */
}
and use it like
<div class="block_1 hline-bottom">Cheese</div>
I wanted a long dash like line, so I used this.
.dash{
border: 1px solid red;
width: 120px;
height: 0px;
}
<div class="dash"></div>
My simple solution is to style hr with css to have zero top & bottom margins, zero border, 1 pixel height and contrasting background color.
This can be done by setting the style directly or by defining a class, for example, like:
.thin_hr {
margin-top:0;
margin-bottom:0;
border:0;
height:1px;
background-color:black;
}
it is depends on requirement , but many developers suggestions is to make your code as simple as possible .
so, go with simple "hr" tag
and CSS code for that.
hr {
display: block;
height: 1px;
border: 0;
border-top: 1px solid #ccc;
margin: 1em 0;
padding: 0;
}
<div>Hello</div>
<hr/>
<div>World</div>
emphasized text
This is relatively simple example and worked for me.
hr {
width: 70%;
margin-left: auto;
margin-right: auto;
}
Resource: https://www.w3docs.com/snippets/css/how-to-style-a-horizontal-line.html