Hightlight buttons on click with CSS - html

I have a multiple selection and would like to be able to click on it to manage the answers on a /getmatch page. At the moment I have no visual cue that I have selected things:
[]]1
So how to make the selection of multiple items responsive?
Here is the code from the page that gives these buttons:
{% extends "todo/base.html" %}
{% block content %}
<style>
.elements {
display: block;
}
ul.items {
display: flex;
margin: 0;
padding: 0;
list-style: none;
}
li.item {
flex: 1 0 20%;
padding: 8px;
margin: 2px;
border-radius: 8px;
border: 1px solid rgba(61, 86, 233, 0.3);
text-align: center;
}
.col {
display: flex;
flex-direction: column;
align-items: center;
}
</style>
<!-- Header -->
<header class="intro-header">
<div class="container">
<div class="col-lg-5 mr-auto order-lg-2">
<h3><br>Tell me something about you... </h3>
</div>
</div>
</header>
<!-- Page Content -->
<section class="content-section-a">
<div class="container">
<dt>
<span>Pick the keywords you want to express when wearing perfume</span>
</dt>
<form action = "/getmatch" method="POST">
{% for keyword in keywords %}
<div class="elements">
<ul class="items ">
<li class="item col-xs-6 col-sm-3 col-md-3 col-lg-3">
<div class="box">
<div data-toogle="buttons" class="col">
<span>{{ keyword.title }}</span>
</div>
</div>
</li>
</ul>
</div>
{% endfor %}
{% csrf_token %}
<button type="submit">Envoyer</button>
</form>
</div>
</section>
<!-- Bootstrap core JavaScript -->
<script src="static/vendor/jquery/jquery.min.js"></script>
<script src="static/vendor/popper/popper.min.js"></script>
<script src="static/vendor/bootstrap/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flexboxgrid/6.3.1/flexboxgrid.min.css" type="text/css" >
{% endblock %}
I tried to add things in the balisa .
button:hover{background-color:orange;}
button:focus{background-color:red;}
But it didn't modified anything

I can't add a comment since i don't have the required rep points (new to stack overflow, sorry if this isn't allowed), but I am not sure what you're trying to do, if you want to give a visual cue that a button has been selected for more than one button at a time, you need to grab the list of buttons, add an event listener on click that toggles a class on/off, then add styling to that class, so that when the item has that class it has a specific style.
Edit on how to do that:
P.S i've never used Django, but i think you should do this, since the way you're doing it is generating a ul for each keyword
<form action = "/getmatch" method="POST">
<div class="elements">
<ul class="items ">
{% for keyword in keywords %}
<li class="item col-xs-6 col-sm-3 col-md-3 col-lg-3">
<div class="box">
<div data-toogle="buttons" class="col">
<span>{{ keyword.title }}</span>
</div>
</div>
</li>
{% endfor %}
</ul>
</div>
{% csrf_token %}
<button type="submit">Envoyer</button>
</form>
Now you need to select the list elements, an easier way to do this would be something called event delegation https://javascript.info/event-delegation
so create a script tag/or file and import it, inside your script you neeed the following :
//Select the UL list
const myItemsList = document.querySelector(".items");
//function passed to event listener as callback, toggles the selected class if the target is a list
const toggleListClass = (event) => {
let target = event.target;
if (target.tagName !== "li") return;
target.classList.toggle("highlight");
};
//Add an event listener to the ul list
myItemsList.addEventListener("click", toggleListClass);
Now in your css add a selector for class hightlight
.hightlight {
color: whatever
}

Related

How do i make my drop down menu visible beyond the navbar?

I'm trying to have a drop down menu within a navbar; however, it only displays until the limit of my navbar when, ideally, id like it to be "in front" of the navbar so to speak.
Dropdown is cut off
I am new to html and CSS so my exploration is somewhat limited. I have implemented one of bootstrap's navbar's and also used their dropdown button html. I expected this to work okay but, as stated, the dropdown seems to be bound within the navbar (assuming this is because it is within the navbar div?) I have also attempted to follow w3schools guide but I didn't succeed with that either.
Sidenote: because of the limited visibility, the "my account" button logs the user out, this is intentional for now lmao.
<body>
<nav class="navbar navbar-expand-lg bg-light">
<div class="container-fluid">
<a class="logo" href="/"><img src="static/Logo_2.png" alt="Logo"></a>
<div class="navbar-nav">
{% if not session["id"] %}
<a class="nav-link" href="login">Log In</a>
{% endif %}
{% if session["id"] %}
<a class="nav-link" href="languages">Languages</a>
<a class="nav-link" href="faq">FAQs</a>
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
Account
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
<li><a class="dropdown-item" href="logout">My Account</a></li>
<li><a class="dropdown-item" href="logout">Log Out</a></li>
</ul>
</div>
{% endif %}
</div>
</div>
</nav>
{% if get_flashed_messages() %}
<header>
<div class="alert alert-primary mb-0 text-center" role="alert">
{{ get_flashed_messages() | join(" ") }}
</div>
</header>
{% endif %}
<main class="container-fluid py-5 text-center">
{% block main %}{% endblock %}
</main>
</body>
.navbar {
height: 100;
overflow: hidden;
}
.navbar-nav {
align-items: baseline;
display: flex;
float: right;
gap: 3em;
}
.nav-link {
color: black;
display: flex;
float: right;
}
.nav-link:hover {
color: red;
}
.btn-secondary {
background: none;
border: none;
color: black
}
.btn-secondary:hover {
background: none;
border: none;
color: red;
}
.logo {
display: flex;
scale: 0.4;
transform-origin: left;
}
your example should be cut down to just the minimum that displays the issue. you have extra classes, some ASP-like code, etc - none of that is needed and just makes it harder to diagnose.
the issue appears to be that your submenu is contained inside the top-level .navbar element, but you are constraining that to have a height of 100 (and you should include the metric here - 100px? 100cm?) with overflow being hidden. this means that the submenu gets cut off.
you could just remove those constraints.
a better solution would involve a rewrite, so the submenu items are positioned absolutely, in a relatively-positioned placeholder. there are examples of this online. example: https://gist.github.com/SamWM/901853
{% if not session["id"] %}
Log In
{% endif %}
{% if session["id"] %}
Languages
FAQs
Account
My Account
Log Out
{% endif %}
...
Remove the "overflow: hidden;" style from .navbar css, so your code should be-
.navbar {
height: 100;
}

How to select a selector when a class does not exists

I have a doubt about the "sibling selectors" for CSS.
This is the situation:
if in some countries we can show the header but in other countries must not show the header, for example:
<!-- USA page -->
<div class="body">
<div class="header">HEADER</div> <!-- it exists -->
<div class="wrapper">BODY</div>
</div>
<hr>
<!-- Canada page --> <!-- does not have header -->
<div class="body">
<div class="wrapper">BODY</div> <!-- I want to modify this wrapper -->
</div>
I am not sure if it's possible because I can't create a new style for the <div class="wrapper">BODY</div> only for Canada because it's one template for all countries.
it's a twig template
<div class="body">
{% if show-header %} <!-- if true show -->
<div class="header">HEADER</div>
{% endif %}
<div class="wrapper">BODY</div>
</div>
so...I need a purely CSS solution... How I can have a specific style to apply for <div class="wrapper">BODY</div> if the <div class="header">HEADER</div> does not exist in Canada. let's say to change the color of the BODY but not for USA.
I thought that with selector to the :not() pseudo-class like so:
:not(.printable) {
/* Styles */
}
but, what is the approach?
I'm not a pro in twig, but you can play around with conditional classes:
<div class="body">
{% if show-header %} <!-- if true show -->
<div class="header">HEADER</div>
{% endif %}
<div class="{{ country == 'Canada' ? 'wrapper-canada' : 'wrapper-usa' }}">BODY</div>
</div>
Or even use function where you will identify country and return corresponding class name.
If I understand your question, a sibling selector would still work for you. Your sibling selector (the presence of .header) would be an override.
So something like this:
.body .wrapper {
color: red; // Default color
}
.body .header + .wrapper {
color: blue; // Override default color when sibling exists
}
JSFiddle: https://jsfiddle.net/fch3drve/

Django css background-image

My css file is working but i can't add bacgorund image. I try 3 way but don't work.
This is my style.css
.first-info{
width: 100%;
height: 300px;
background-image: url( "{% static 'img/bg.jpg' %}");
}
This is my base.html ( img is working)
{% load static %}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}VPW{% endblock %}</title>
<link href="{% static 'css/style.css' %}" rel="stylesheet">
<link href="{% static 'css/bootstrap.css' %}" rel="stylesheet">
<link href="{% static 'css/font-awesome.css' %}" rel="stylesheet">
<script href="{% static 'js/bootstrap.js' %}"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container-fluid">
<div class="row head">
<div class="col-md-12">
<div class="col-md-4">
</div>
<div class="col-md-4 logo">
<img src="{% static 'img/logo1.png' %}">
</div>
<div class="col-md-4">
</div>
</div>
</div>
</div>
<div id="content">
{% block content %}
{% endblock %}
</div>
</body>
</html>
And this is my main.html where i have class first-info.
{% extends "shop/base.html" %}
{% load static %}
{% block title %}VPW{% endblock %}
{% block content %}
<div class="container-fluid">
<div class="row first-info">
<div class="col-md-4">
<div class="box">
Lorem Ipsum
</div>
</div>
<div class="col-md-4">
<div class="box">
</div>
</div>
<div class="col-md-4">
<div class="box">
</div>
</div>
</div>
</div>
{% endblock %}
I try change in css background-image: url( "{% static 'img/bg.jpg' %}"); to background-image: url( "img/bg.jpg"); but don't work it
This is the expected behaviour. Django templating engine populates all the variables while rendering the html template. It will not populate values in your css file which is included in your html.
Your base.html because {% static 'img/logo1.png' %} is replaced into something like /static/img/logo1.png and browser is able to find the image, loads it and renders it.
If you want to make your background-image dynamic you can either .
You can add css class either directly in template i.e.
{% extends "shop/base.html" %}
{% load static %}
{% block title %}VPW{% endblock %}
<style>
.first-info{
width: 100%;
height: 300px;
background-image: url( "{% static 'img/bg.jpg' %}");
}
</style>
{% block content %}
<div class="container-fluid">
<div class="row first-info">
<div class="col-md-4">
<div class="box">
Lorem Ipsum
</div>
</div>
<div class="col-md-4">
<div class="box">
</div>
</div>
<div class="col-md-4">
<div class="box">
</div>
</div>
</div>
</div>
{% endblock %}
Or use a inline css directly in your html tag.
<div class="row first-info" style="background-image: url({% static 'img/bg.jpg' %})" >
Or
Just hardcode the complete path in your css file. But it removes the dynamic behaviour i.e. your static path. I am assuming it is just /static/
.first-info{
width: 100%;
height: 300px;
background-image: url( '/static/img/bg.jpg' );
}
Try this:
background-image:url('{{ STATIC_URL }}/img/bg.jpg');
Late answer, but I wanted to share this with someone trying to dynamically load a background image. My example is rendering a database request generated in views.py. The request contains three elements: background.photo, background.title and background.body.
Although not necessary, I wanted to apply opacity to the photo; however, using opacity property applies the effect to all children of the element in the DOM, so I used rgba to isolate the effect to the photo. The text is overlaid without being affected by the opacity of the photo. To make the text stand out, I used a horizontal gradient to darken the photo on the left.
{% extends 'base_layout.html' %}
{% block content %}
<div class="background-image" style="background: url({{ background.photo.url }} ) no-repeat; background-size: 100%;">
<div class="container">
<h2 class="title-text">{{ background.title }}</h2>
<p class="body-text">{{ background.body }}</p>
</div>
</div>
{% endblock %}
And the css:
.container{
background: linear-gradient(to right, rgba(0,0,0,1), rgba(255,255,255,0));
}
.background-image{
margin: 1em;
border-style: solid;
border-width: 1px;
border-color: #ff0080;
}
.title-text{
font-family: serif;
color: #ff0080;
margin: 0;
padding: .5em 1em;
}
.body-text{
font-family: serif;
color: #ff0080;
margin: 0;
padding: 1.5em 4em;
}
Django is not processing .css file includes just renders html templates.
Your CSS is static file and url should there be included through processing of some kind of management command if you want it to be extensible.
Othewise just use path of url
Just replace in static with '..'.
It will solve:
background:url('../images/Course.jpg');
Images must be in static/app/images/ and .css files must be located in static/app/content/.
Further in the .css file write like this:
#home {
background: url(../images/1.jpg);
}
This just worked for me:
<style="background-image: url({% static 'images/cover.jpg' %})">
Source:
https://simpleit.rocks/python/django/call-static-templatetag-for-background-image-in-css/
.bgded{
background-image: url( "{% static 'images/ur.jpg' %}");
}
.wrapper {
display: grid;
}
Add this

Prevent flexbox inheritance

I have the following HTML structure with CSS formatting.
What I want is the outer div using flex but the inner div not using flex but float style (by the reasons of the sortable drag-drop widget does not work under display as flex).
Specifically, the structure is as below:
<div id="a" style="display:flex;min-height: 100%;overflow:auto;padding-bottom: 150px;">
<div id="b" align="left" style="padding: 10px">
<div id="multi" style="display: NOT flex;"></div>
<div id="p" style=" float:left;"></div>
<div id="q" style=" float:left;"></div>
</div>
</div>
</div>
So, to be precise, I want flex for a. Particularly for div multi, p and q. I don't want flex, so that they can be side-by-side in the layout. Could anyone suggest how to do that? Many thanks.
The following is my working example run by Django on Python 3.5 for Michael_B's advice:
The template listing the RubaXa Sortable's widget for category ranking. What I want to do specifically is to list the blocks and Group A/B side by side, however, the Group A and B can be dragged to the block side for sorting/grouping:
{% extends "admin_content.html" %}
{% load static %}
{% block header_extra%}
<link rel="stylesheet" href="{% static 'django_tables2/themes/paleblue/css/screen.css' %}" />
{% include "fancybox/fancybox_css.html" %}
<!-- AngularJS -->
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<!-- Sortable.js -->
<script src="http://rubaxa.github.io/Sortable/Sortable.js"></script>
<!-- ng-sortable.js -->
<script src="http://rubaxa.github.io/Sortable/ng-sortable.js"></script>
<!-- app.js -->
<script src="http://rubaxa.github.io/Sortable/st/app.js"></script>
<link href="/static/app.css" rel="stylesheet" type="text/css">
{% endblock %}
{% block admin_right%}
<div id="multi" >
<div class="tile__list" style="float:left; min-height:100%; min-width=1200px">
{% for field in table %}
<p style="background-color:#C6DDF4; margin: 5px; padding: 10px 15px; cursor: move;"><font size="2px">{{ field.NewsPaper }}, {{ field.Section}}, {{field.Title}}</font></p>
{% endfor %}
</div>
<div class="layer tile" >
<div class="tile__name" style=" float:left;">Group A</div>
<div class="tile__list">
</div>
</div>
<div class="layer tile" >
<div class="tile__name" style=" float:left;">Group B</div>
<div class="tile__list">
</div>
</div>
</div>
</div>
<script id="jsbin-javascript">
angular.module('demo', ['ng-sortable'])
.controller('DemoController', ['$scope', function ($scope) {
$scope.firstList = ['foo 1', 'foo 2', 'foo 3'];
$scope.secondList = ['bar 1', 'bar 2'];
$scope.sortableOptions = {
group: 'ng',
animation: 200
}
}]);
var el = document.getElementById('multi');
var sortable = Sortable.create(el, {
animation: 150,
handle: ".tile__name",
draggable: ".tile"
});
[].forEach.call(document.getElementById('multi').getElementsByClassName('tile__list'), function (el){
Sortable.create(el, {
group: 'blocks',
animation: 150
});
});
</script>
{#{% load django_tables2 %}#}
{#{% render_table table %}#}
{% endblock %}
And this template is further extend to another template of "admin_content.html" which use display flex in div:
{% extends "main.html" %}
{% load static %}
{% block header_extra%}{% endblock %}
{% block content_main%}
<div style="display:flex;min-height: 100%;overflow:auto;padding-bottom: 150px;">
<nav class="w3-sidenav w3-border" style="width:15%;padding:0px;background:transparent;">
<a class="w3-border-bottom" href="#">Function A</a>
<a class="w3-border-bottom" href="#">Function B</a>
<a class="w3-border-bottom" href="#">Function C</a>
<a class="w3-border-bottom" href="#">Function D</a>
<a class="w3-border-bottom" href="#">Function E</a>
</nav>
<div align="left" style="display:flex; padding: 10px;">
{% block admin_right%}{% endblock %}
</div>
</div>
{% endblock %}

Make a div-row collapse in html/css/django

I have recently designed a really small django template looking like this :
The html code looks like this :
{% for category in categories %}
<div class="row">
<div class="column">
<h3>{{ category }}</h3>
<ul>
{% for elem in elems %}
<li><a href=...>{{ elem }}</a></li>
{% endfor %}
</ul>
</div>
</div>
{% endfor %}
And the CSS code :
.row:before,
.row:after {
content: " ";
display: table;
}
.row:after {
clear: both;
}
.column{
width: 50%;
}
I would like to have the rows to collapse if there is enough space.
In my example, I would like the Test column to go just behind the Pet one (only separated by the padding).
Is there any css property which could help me achieving this ? Or should I change my approach ?
Thanks !
The divs have no defined width. They will take up the entire page such that there is no horizontal room left. What you want is a responsive design that changes that width based on the size of the boxes, and the size of the screen.
Using the #media selector to limit a specific style to the size of the current browser window will allow the size of the divs to respond as you desire:
#media screen and (max-width : 600px) {
.box {
width: 50%;
}
}
#media screen and (min-width : 601px) {
.box {
width: 33.3%;
}
}
JS Fiddle
Hope that helps!
EDIT: Having read your comments I now see what you mean. Saying collapse confused me and I assumed you were asking for something dynamic.
The best way to solve this is to prioritise columns over rows. It's not a true table so they aren't even rows exactly.
<div class="column">
<div class="row">
<h3>Drinks</h3>
<ul><li>item 1</li><li>item 2</li></ul>
</div>
<div class="row">
<h3>Food</h3>
<ul><li>item 1</li><li>item 2</li></ul>
</div>
</div>
<div class="column">
<div class="row">
<h3>Pet</h3>
<ul><li>item 1</li></ul>
</div>
<div class="row">
<h3>Test</h3>
<ul><li>item 1</li><li>item 2</li></ul>
</div>
</div>
Using the following CSS will give the desired result.
.column {
width:50%;
float:left;
}
Using your code this would equate to:
{% for category in categories %}
<div class="column">
<div class="row">
<h3>{{ category }}</h3>
<ul> {% for elem in elems %}<li><a href=...> {{ elem }}</a></li>{% endfor %}</ul>
</div>
</div>
{% endfor %}
This answer has gotten quite long, I apologise!
New JS FIddle