How to make custom css button responsive? - css

I have created a CSS button but it is not responsive. I have tried adding EM or % but still not working. I want to change width and height according to the screen resolution.
Following is the CSS code I am actually using:
#media only screen and (min-width:321px) and (max-width:480px) { }
HTML
<div>
<p>
<button class="btn btn-1 btn-1a">Click Here To Enter The Future</button>
</p>
</div>
CSS
.btn {
font-size:0.875em;
display:block;
left:-60px;
margin-top:35px;
}
Right now the button is moving from left to right, but I want the button to appear at the exact same place.

You could add width:100%; to achieve your objective.
.btn {
font-size:0.875em;
display:block;
left:-60px;
margin-top:35px;
width:100%;
}

This should work
.btn {
width: 100%;
min-width: 50px; // add this if you want
max-width: 300px; // add this if you want, adjust accordingly
}

This works for me:
<script>
$(document).ready(function() {
$('#select_preferences').multiselect({
buttonText: function(options, select) {
return 'Look for users that:';
},
buttonTitle: function(options, select) {
var labels = [];
options.each(function() {
labels.push($(this).text()); //get the options in a string. later it would be joined with - seprate between each.
});
if(!labels.length ===0 )
{
$('#range-div').removeClass('hide');
// The class 'hide' hide the content.
//So I remove it so it would be visible.
}
if (labels.length ===0)
$('#range-div').addClass('hide');
//If the labels array is empty - then do use the hide class to hide the range-selector.
return labels.join(' - ');
// the options seperated by ' - '
// This returns the text of th options in labels[].
}
});

Related

How to hide a section and show another section in one click in elementor?

I want to hide a section and show another section in one click.... is there any way to do that in elementor.
I searched on google but I get the result that only hide a section and show it again on another click.
But I want to hide a section and show a hidden section in one click....
please, help me with the solution.
thank you.
make sure to set a classname/id for each of your sections and also for your button.
eg. let's say i have set .sc1 and .sc2 for sections and .btn for the button.
drag and drop HTML widget to your header template or the page/template you want the results in.
copy the following javascript code to your html, make sure to put it between script tag:
var btn = document.querySelector('.btn');
var sc1 = document.querySelector('.sc1');
var sc2 = document.querySelector('.sc2');
btn.addEventListener("click", function() {
if (sc1.style.display === "none") {
sc1.style.display = "block";
sc2.style.display = "none";
} else {
sc1.style.display = "none";
sc2.style.display = "block";
}
});
.sc1, .sc2 {
width: 50px;
height: 50px;
}
.sc1 {
background: #000;
display: block;
}
.sc2 {
background: #e2062c;
display: none;
}
<div>
<div class="sc1"></div>
<div class="sc2"></div>
</div>
<button class="btn">click</button>

How to dynamically apply CSS in vue.js?

This is my situation:
I have a web app that allow users to change the UI size. (i.e. small, medium or large button)
Users can change how it looks like dynamically during runtime. All text and form input boxes will be resized.
My project is in Vue.js.
What is the best way to solve this? Loading different css when user click?
Load different CSS while user click the button similar to this . Codepen : https://codepen.io/anon/pen/NJEoVM
HTML
<div id="app" :class="size">
<div class="text">Text</div>
<input class="ipt"/><br/><br/>
<button class="btn" #click="change('small')">Small</button>
<button class="btn" #click="change('medium')">Medium</button>
<button class="btn" #click="change('large')">Large</button>
</div>
CSS
.small .ipt{
width: 100px;
height:30px;
}
.small .text{
font-size: 18px;
}
.medium .ipt{
width: 300px;
height:50px;
}
.medium .text{
font-size: 32px;
}
.large .ipt{
width: 600px;
height:100px;
}
.large .text{
font-size: 64px;
}
Javascript
new Vue({
el: '#app',
data:()=>({
size:'small'
}),
methods:{
change(val){
this.size = val
}
}
});
Actually, you can make use of Custom Properties aka CSS variables.
Firstly, define the button CSS styles
/* button.css */
#buttonRef {
--fontSize: 16px;
font-size: var(--fontSize)
}
The overall flow would be something like the following one e.g
methods: {
changeButtonSize: function(size, event) {
event.preventDefault();
/* size might be either of 's', 'm', 'l' */
/* let the button ref is stored in refs */
const buttonRef = this.$refs[“buttonRef”];
let fontSize = 16;
switch(size) {
case 's':
fontSize = 12;
case 'm':
fontSize = 18;
case 'l':
fontSize = 22;
}
/* dynamically change the value for custom property */
buttonRef.style.setProperty("--fontSize", fontSize);
}
}
you can set up 3 classes:
.small {font-size:12px}
.medium {font-size:18px}
.large {font-size:24px}
Then add or remove them to your main parent div onClick.
If you have discreetly set the font-size on the elements, you'll have to target those elements as such:
.small .description { font-size:12px }
.small .title{ font-size:16px }

Is there a way to show / hide a <div> depending on the size of the browser?

Angular JS code I am working on has media queries that can be used to limit the display of blocks with code like this:
#media screen and (max-width: 370px) {
#testGrid {
.gridHeader {
div:nth-child(2),
div:nth-child(3),
div:nth-child(n+7) {
display: none;
}
div:nth-child(6) {
border-top-right-radius: 0.4rem;
}
}
.gridBody {
div {
div:nth-child(2),
div:nth-child(3),
div:nth-child(n+7) {
display: none;
}
}
}
}
}
My comment here was that it's not good to use things like div:nth-child(2) as this would easily break if another column was added. Plus it's also difficult to maintain. I suggested to give the column names class names that matched the contents of the columns.
Still this means that I have the code that defines what shows and what does not show far removed from the HTML. Does anyone have any suggestions on a way that I could do this with AngularJS that would have the showing and hiding of columns next to the actual <div>s
You can get the current width from the $window service so you could try something like this:
DEMO
app.controller('MainCtrl', function($scope, $window) {
$scope.name = 'World';
angular.element($window).bind('resize', function(){
$scope.hideThing = ($window.innerWidth < 400);
// have to manually update $scope as angular won't know about the resize event
$scope.$digest();
});
});
Then in your HTML
<body ng-controller="MainCtrl">
<p ng-hide="hideThing" >Hello {{name}}!</p>
</body>

How to highlight div on click

I would like to highlight a div when it's clicked.
Heres the example: www.spidex.org
On this website if you hover any of the navigation buttons a div on the top of the page is highlighted.
You may use jQuery for achieving this.
get jQuery here.
now consider that you have a div that you want to highlight on mouseover called item.
do this by adding an overlay div.
div.overlay{
opacity:0;
background:#000;
width:100%;
height:100%;
position:absolute;
top:50px;left:0;
}
then use jquery
jQuery(document).ready(function($){
$('.item').mouseover(function(){
$('.overlay').css({opacity:0.3});
});
});
You can change the appearance of elements when hovered using the :hover pseudo-class.
For example
div:hover {
color: red;
}
Secondly, you can change the text color via using the color property and the background color using the background-color property.
Both are shown below:
div:hover {
color: black;
background-color: white;
}
In your given example, when you hover over the primary navigation items in the super-header, then the body dims. I agree with your analysis that this is managed with some cover div of the body.
One cross-browser approach (using jQuery in this example) you might consider would be the following:
EXAMPLE HTML:
<div class="header">
Some Link
</div>
<div class="body">
<div class="body-content">
[ CONTENT HTML ]
</div>
<div class="body-cover"></div>
</div>
EXAMPLE CSS:
.body {
position: relative; /* container needs position */
}
.body-cover {
position: absolute;
top: 0px;
left: 0px;
background-color: blue;
/*
you could use a sligtly transparent background here,
or tween your opacity in your javascript
*/
}
EXAMPLE JavaScript:
// on dom ready
jQuery(function ($) {
// closures
var $links = $('.header a');
var $body = $('.body');
var $content = $body.find('.body-content');
var $cover = $body.find('.body-cover');
var sCoverHiddenCssClassName = 'body-cover-hidden';
var sCoverTweeningCssClassName = 'body-cover-tweening';
var sCoverShowingCssClassName = 'body-cover-showing';
// closure methods
var fMouseOver = function () {
// check to see if hidden (not already tweening or showing)
if ($cover.hasClass(sCoverHiddenCssClassName)) {
// check content, may have changed.
$cover.css({
height: $content.outerHeight(),
width: $content.outerWidth()
});
// animate or tween cover (do this however you want)
$cover
.removeClass(sCoverHiddenCssClassName)
.addClass(sCoverTweeningCssClassName)
.fadeIn(function () {
// when completed, mark as showing/visible
$cover
.removeClass(sCoverTweeningCssClassName)
.addClass(sCoverShowingCssClassName);
});
}
};
var fMouseOut = function () {
// check to see if visible (not already tweening or hidden)
if ($cover.hasClass(sCoverShowingCssClassName)) {
// animate or tween cover (do this however you want)
$cover
.removeClass(sCoverShowingCssClassName)
.addClass(sCoverTweeningCssClassName)
.fadeOut(function () {
// when completed, mark as showing/visible
$cover
.removeClass(sCoverTweeningCssClassName)
.addClass(sCoverHiddenCssClassName);
});
}
};
var fClick = function (e) {
// prevent default if needed for anchors or submit buttons
// e.preventDefault();
if ($cover.hasClass(sCoverHiddenCssClassName)) {
fMouseOver();
}
else if ($cover.hasClass(sCoverShowingCssClassName)) {
fMouseOut();
}
};
// init interaction
$cover.hide().addClass(sCoverHiddenCssClassName);
$links.each(function () {
// wire links
jQuery(this)
.mouseover(fMouseOver)
.mouseout(fMouseOut);//
//.click(fClick); // use click event if desired
});
});
JQuery UI is also gives an good option to quickly highlight div .
https://jqueryui.com/effect/
$( "#divId" ).effect( "highlight", 500 );

IE9 not removing :hover style on DOM change

I am trying to make a button that has a :hover state on a popup box, when you one of the buttons I am removing the box from the DOM and saving it for future interacts. the problem is when I reattach it to the DOM in IE9 it has not cleared the :hover state until you next hover it then mouse out.
Obviously this is not present on any other browser, but is reproducible here: http://jsfiddle.net/5dXSp/
I cant find a manual way of clearing a css :hover state, and I really dont want to have to rebuild the menu every time because of this. Any thoughts?
Try controlling the hover with a class and jQuery. This seemed to work for me:
http://jsfiddle.net/5dXSp/25/
CSS:
.box{
height:200px;
margin:10px 0;
}
.button{
display:block;
width:200px;
height:20px;
background:#ccc;
}
.hover {
background-color: #000;
}​
jQuery:
$(".button").hover(
function () {
$(this).addClass("hover");
},
function () {
$(this).removeClass("hover");
}
);
$(".button").click(function(ev){
ev.preventDefault();
$(ev.target).appendTo($(".catch"));
$(this).removeClass("hover");
});
There is one additional way to fix it.
You can hide element before detaching it from DOM, but in a different event processing.
Something like that:
// HTML structure: <div id="aaa"> <a id="bbb"> Text </a> </div>
var bbb = document.getElementById('bbb');
var container = document.getElementById('aaa');
bbb.attachEvent("onclick", function() {
bbb.style.display = "none";
window.setTimeout(function() {
container.removeChild(bbb);
bbb.style.display = "";
// Some time later
window.setTimeout(function() {
container.appendChild(bbb);
}, 2000);
}, 1);
});
bbb.style.visibility = "hidden" worked too.
using jquery you can do ugly things like:
if($.browser.msie)
$('el').html($(el).html());
to de and attach the element

Resources