Enter key and tab not working for focus class - html

I am trying to tab the buttons for accessibility reasons in the header however I am not able to tab over them.
I have the following css
.nmcnav_clickable:focus {
background-color: yellow;
}
.nmcnav_clickable {
.noButton;
display: flex;
align-items: center;
height: var(--primary-height);
cursor: pointer;
// don't show an outline when not tabbing
&:focus:not(:focus-visible) {
outline: none;
}
&:focus {
outline: currentColor solid;
background-color: yellow;
}
&:hover {
.nmcnav_text {
color: #color-teal;
}
}
}
This is my HTML
{% macro navbutton(item) %}
<button tabindex="0"
class="nmcnav_clickable nmcnav_button"
aria-controls="nmcnav_dropdown-{{ item.id }}"
aria-expanded="false"
id="nmcnav_button-{{ item.id }}">
<span class="nmcnav_text">{{ item.title|raw }}</span>
</button>
{% endmacro %}
{% macro navlink(item) %}
<a
class="nmcnav_clickable {{ item.meta('button') ? 'nmcnav_btn' : '' }}"
href="{{ item.get_link }}"{% if item.target %} target="{{ item.target }}"{% endif %}>
<span class="nmcnav_text {{ item.meta('button') ? 'btn' : '' }}">{{ item.title|raw }}</span>
</a>
{% endmacro %}
There was a point where I was able to tab over the buttons but then I realized I was not able to press enter to actually access the links because tabindex only works with <a> or <button> tags. But when I try and change the tabindex to the buttons, nothing happens. I need to be able to tab over and enter for the buttons. I have also attached a ss of what the header should look like.
Thanks in advance.

Related

why is flask jinja giving me error when I use this {{bg_color}} as a background Color

this is my code i want to give different colors to comments
like grey(#eee) to odd and blue (#e6f9ff) to even
here this line is giving me error
background-color: {{ bg_color }}
{% extends 'base.html' %}
{% block content %}
<h1>{% block title %} Comments {% endblock %}</h1>
<div style="width: 50%; margin: auto">
{% for comment in comments %}
{% if loop.index % 2 == 0 %}
{% set bg_color = '#e6f9ff' %}
{% else %}
{% set bg_color = '#eee' %}
{% endif %}
<div style="padding: 10px; background-color: {{ bg_color }}; margin: 20px">
<p>#{{ loop.index }}</p>
<p style="font-size: 24px">{{ comment }}</p>
</div>
{% endfor %}
</div>
{% endblock %}
I think jinja version is updated and my course which Im following is old
so anyone who knows to answer me
You should note the scope for the variables. If you define a variable within an if-else block, it is only valid there.
<div style="width: 50%; margin: auto">
{% for comment in comments -%}
{% set bg_color = '#e6f9ff' if loop.index % 2 == 0 else '#eee' %}
<div style="padding: 10px; background-color: {{ bg_color }}; margin: 20px">
<p>#{{ loop.index }}</p>
<p style="font-size: 24px">{{ comment }}</p>
</div>
{% endfor -%}
</div>
Instead of using jinja you can also use css to assign a different background color to every other line.
<div class="comments">
{% for comment in comments -%}
<div>
<p>#{{ loop.index }}</p>
<p class="text">{{ comment }}</p>
</div>
{% endfor -%}
</div>
<style type="text/css">
.comments {
width: 50%;
margin: auto;
}
.comments div {
padding: 10px;
background-color: #e6f9ff;
margin: 20px
}
.comments div:nth-child(odd) {
background-color: #eee;
}
.comments .text {
font-size: 24px
}
</style>

Django - After overriding the base_site.html, i was unable to publish my post and causing CSRF error

Here is my code for base_site.html
{% extends "admin/base.html" %}
{% load static %}
{% block title %}{% if subtitle %}{{ subtitle }} | {% endif %}{{ title }} |
{{ site_title|default:_('Admin') }}
{% endblock %}
{% block extrastyle %}
<style type="text/css">
#branding h1{
color: #fff !important;
}
#branding h1 a:link, #branding h1 a:visited {
color: #fff !important;
}
.submit-row input{
background-color: #00b388;
}
#header {
background-color: #000000;
color: #fff !important;
}
.module caption{
background-color: #00b388;
}
div.breadcrumbs {
background: #00b388;
}
.object-tools a.addlink{
background-color: #00b388;
}
input[type=submit].default, .submit-row input.default{
background-color: #00b388;
}
</style>
{% endblock %}
{% block branding %}
<form method="post">{% csrf_token %}
<h1 id="site-name"><img src="{% static 'img/logo.PNG' %}" height="30px" /></h1>
{% endblock %}
{% block nav-global %}
{% endblock %}
unfortunately i have added the <form> tag in the above code, just to over come with CSRF issue and it does.
unfortunately, this approach gives me another error,
i.e, i have 3 file fields in my Model, and even though, i have mentioned the files during the upload and when i hit submit, it is not working and keep showing me This field is required error.
I am new to Django and your response helps me great.
versions:-
Django - 2.2
python - 3.6.2
You need to close the form tag :
<form method="post">{% csrf_token %}
</form>
<h1 id="site-name"><img src="{% static 'img/logo.PNG' %}" height="30px" /></h1>
{% endblock %}
{% block nav-global %}
{% endblock %}

CSS - Change image on hover?

I'm working with a shopify theme, and I'm trying to display an Add To Cart button on top of my product image when a user hovers over it. That said, the 'hover' aspect is already active on the product image using the following CSS. In short, when I currently hover over a product image, it simply lowers the opacity to 0.7. That said, I can't seem to figure out how to write HTML for .grid-product__image-link if I want a button or an image to appear on hover instead? Help is appreciated!
Current HTML
<div class="grid__item grid-product {{ grid_item_width }}{% if sold_out %} is-sold-out{% endif %}">
<div class="grid-product__wrapper">
<div class="grid-product__image-wrapper">
<a class="grid-product__image-link{% unless featured_image.src == blank %} grid-product__image-link--loading{% endunless %}" href="{{ product.url | within: collection }}" data-image-link>
{% if featured_image.src == blank %}
<img class="grid-product__image" src="{{ featured_image.src | img_url: '1024x' }}" alt="{{ featured_image.alt | escape }}">
{% else %}
{% include 'image-style' with image: featured_image, small_style: true, width: width, height: height, wrapper_id: img_wrapper_id, img_id_class: img_id_class %}
<div id="{{ img_wrapper_id }}" class="product--wrapper">
<div style="padding-top:{{ 1 | divided_by: featured_image.aspect_ratio | times: 100 }}%;">
<img class="product--image lazyload {{ img_id_class }}"
data-src="{{ img_url }}"
data-widths="[180, 370, 590, 740, 900, 1080, 1296, 1512, 1728, 2048]"
data-aspectratio="{{ featured_image.aspect_ratio }}"
data-sizes="auto"
alt="{{ featured_image.alt | escape }}"
data-image>
</div>
</div>
<noscript>
<img class="grid-product__image" src="{{ featured_image.src | img_url: '1024x' }}" alt="{{ featured_image.alt | escape }}">
</noscript>
{% endif %}
</a>
{% if sold_out %}
<div class="grid-product__sold-out">
<p>{{ 'products.product.sold_out_html' | t }}</p>
</div>
{% elsif on_sale %}
<div class="grid-product__on-sale">
{% capture saved_amount %}{{ product.compare_at_price | minus: product.price | money_without_trailing_zeros }}{% endcapture %}
<p>{{ 'products.general.save_html' | t: saved_amount: saved_amount }}</p>
</div>
{% endif %}
</div>
CSS
.grid-product__wrapper {
text-align: center;
margin-bottom: $gutter;
.grid-collage & {
margin-bottom: 0;
}
}
.grid-product__image-wrapper {
position: relative;
width: 100%;
display: table;
table-layout: fixed;
}
.grid-product__image-link {
position: relative;
display: block;
width: 100%;
background-color: $colorProductBackground;
#include transition(opacity 0.4s ease-out);
.grid-collage & {
padding: 0 20px;
}
&:hover,
&:focus {
opacity: 0.7;
#include transition(opacity 0.15s ease-in);
}
}
If I correctly understand you, maybe this will help you :)
Html:
<a class="grid-product__image-link>
<div class = "product-overlay"></div>
</a>
Scss:
.grid-product__image-link {
position: relative;
display: block;
width: 100%;
background-color: $colorProductBackground;
#include transition(opacity 0.4s ease-out);
.grid-collage & {
padding: 0 20px;
}
.product-overlay{
display:none;
}
&:hover,
&:focus {
opacity: 0.7;
#include transition(opacity 0.15s ease-in);
.product-overlay{
display:block;
}
}
}
I would use javascript to make another html <img> or <button> element popup with a higher z-index property when your mouse gets on top of that image, but I'm not sure if that works for you.

How to style forms dynamically added from formsets

I have a base form which contains fields to be filled in by the user and a button which allows the user to add up to ten instances of a different form that is relevant to the base form (this is just dynamically adding forms through django formset). When the html is loaded the base form and all content looks great, but when I go to add a form from the formset, the CSS gets messed up because the form from the formset will neither conform to the base form CSS or any CSS I've added to it.
I've made a div to contain all forms added via the formset add button so that I can format all added forms with one CSS id. However, it seems no formatting works - the added forms' elements expand the base form's margins and seem to be floated.
HTML:
<div class="center-text jumbotron">
<h2>Incident Report Form</h2>
<form method="post" class="form-horizontal">
{% crispy incident_form %}
<div id="form_set_class">
{{ incident_formset.management_form }}
<table>
{% for form in incident_formset %}
{{form.non_filed_errors}}
{{form.errors}}
{% crispy form %}
{% endfor %}
</table>
</div>
<input type="button" id="add_def_report" value="Add Report">
<div id="empty_form" style="display:none">
{{incident_formset.empty_form}}
</div>
<input type="submit" class="btn btn-primary" value="Submit">
</form>
</div>
CSS:
.center-text{
display: block;
text-align: center;
}
form{
display: inline-block;
margin-right: auto;
margin-left: auto;
text-align: left;
}
legend {
float: left; /*allows for top margin */
border-bottom: 1px solid black;
margin-bottom: 20px;
margin-top: 20px;
}
#form_set_class{
clear: both;
display: block;
text-align: center;
}
Good Styling (image shows top part of form):
Messed Up Styling (image shows bottom part of form; the added formset starts at the "Supplier" dropdown):
I presume you have a parent and child model scenario. If that is so, then you may layout your child forms like this (laid out in tabular fashion):
<!-- ABOVE THIS WOULD BE YOUR PARENT FORMS -->
<!-- HERE WE CAN HAVE TABLE HEADER FOR THE FORMS ADDED DYNAMICALLY -->
<table class="table table-bordered table-striped table-sm">
{{ incident_formset.management_form }}
{% for form in incident_formset.forms %}
{% if forloop.first %}
<thead id="hdrID">
<tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<!-- HERE WILL BE THE FORMS ADDED BY THE USER -->
{% for field in form.visible_fields %}
<td>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{ field }}
</td>
{% endfor %}
{% endfor %}
</table>
<!-- BELOW THIS WOULD BE THE FORM SUBMISSION AND THE REST -->
You may then think of applying css as per your requirements using tags (eg. hdrID shown in the code).

Place checkbox/radio tags BEFORE labels

I use this Twig code to display checkboxes (or radio) of my Symfony form:
{% for o in my_form.magic_choices %}
{{ form_row(o) }}
{% endfor %}
This generates following output for each choice:
<div>
<label for="myform_magic_choices_0">Secret option</label>
<input type="checkbox" value="secret_option" ...>
</div>
This forces checkbox input element to be after its label visually, how do i fix this to make my choice lists better? I need to make checks to be in a left side.
You could simply float the input left.
Rather than render using form_row render the item individually like..
<div class="checkbox{% if o.vars.errors|length > 0 %} has-error{% endif %}">
{{ form_widget(o) }} {{ form_label(o) }}
{% if o.vars.errors|length > 0 %}
<span class="help_block">{{ form_errors(o) }}</span>
{% endif %}
</div>
The actual Bootstrap layout using the "checkbox" class has the input element inside the label and uses a margin-left for the input so that renders correctly so you might want to override the CSS for that.
.checkbox input[type=checkbox],
.checkbox-inline input[type=checkbox],
.radio input[type=radio],
.radio-inline input[type=radio] {
// margin-left: -20px;
margin-left: 0;
}
Alternatively
You could render the label as text so that it conforms to the Bootstrap thinking like..
<div class="checkbox{% if o.vars.errors|length > 0 %} has-error{% endif %}">
<label>
{{ form_widget(o) }} {{ o.vars.label }}
</label>
{% if o.vars.errors|length > 0 %}
<span class="help_block">{{ form_errors(o) }}</span>
{% endif %}
</div>
twig
{{ form_row(o, {'attr': {'class': 'checkboxy'}}) }}
css
checkboxy {
float:left;
}
Well, this was not mentioned in Symfony Docs and this is the epic fail.