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

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

Related

Hightlight buttons on click with CSS

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
}

How do I create a card grid view HTML, CSS and Flask

I am using the code in my index.html file.
{% for post in blog_posts.items %}
<div class="col-sm-6">
<div class="card" >
<div class="card-body">
<h2><a class="card-title" href=" {{ url_for('blog_posts.blog_post', blog_post_id=post.id) }}">{{ post.title }}</a></h2>
Written By: {{ post.author.username }}
<p>Published on: {{ post.date.strftime('%Y-%m-%d') }}</p>
<p class="card-text">{{ post.text[:100] }}...</p>
Read Blog Post
</div>
</div>
</div>
{% endfor %}
My understanding from bootstrap is that it would create a 6 column grid, however it is just stacking one card below the next. I am sure it is something I am missing. Thank you for your help.
You probably need to use a card deck or card group to achieve this:
I'd lose this div:
<div class="col-sm-6">
Then put the for loop within a card-group div:
<div class='card-group'>
{% for post in blog_posts.items %}
<div class="card" >
<div class="card-body">
...
</div>
</div>
{% endfor %}
</div>
If you want some spacing between the cards, you can change the class of the outer div to card-deck.
See the Card docs for more options.
.col-sm-6{ display: flex } add this css property to the parent that contains all cards.
alternatively you can use floats .col-sm-6 > .card{ float: left}

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/

how to extend background color on column layouts css

I seem to be having trouble to extend my background color for my 2 column layout. The user can add categories so the background color has to be able to adjust with the amount of categories the user wants to add in.
html file:
{% for category in categories %}
<div class="row__2 sub-pages--background">
<div class="sub-pages--categories-background">
<div class="sub-pages--categories">
<a href="{% url 'blogging_logs:topics' category.id %}" class="sub-pages--categories-position ">{{ category }}
<img class="sub-pages--img" src="{{ category.category_image.url }}">
</a>
</div>
</div>
</div>
{% empty %}
<p>No categories entered yet.</p>
{% endfor %}
css file:
&--background {
background-color: $mainBackground;
padding-bottom: 2rem;
}
I feel like it has to do with the way I'm formatting my divs
Since you mentioned in comments that you are using floats, simply add a parent element that will have the background and then add something known as clearfix, at the end of it.
HTML:
<div class="parent--background">
{% for category in categories %}
<div class="row__2 sub-pages--background">
<div class="sub-pages--categories-background">
<div class="sub-pages--categories">
<a href="{% url 'blogging_logs:topics' category.id %}" class="sub-pages--categories-position ">{{ category }}
<img class="sub-pages--img" src="{{ category.category_image.url }}">
</a>
</div>
</div>
</div>
{% empty %}
<p>No categories entered yet.</p>
{% endfor %}
<div class="clearfix"></div>
</div>
CSS:
.clearfix::after {
content: "";
clear: both;
display: table;
}

How can I display the details in one line in HTML?

I have a script written in Python which will pass a list to a HTML file. I have a problem in displaying the elements in the html page. As per the code written below, the elements are displayed vertically down as a list. I wanted the elements to be displayed in a single line horizontally.
<!doctype html>
<html lang="en">
<body>
<div id="content">
{% for item in VOD1 %}
<li>ID: {{ item[0] }}</li><li> Title: {{ item[1] }}</b></li>
<img src="{{ item[2] }}" alt="dummy.jpg"> </img>
{% endfor %}
{% for item in VOD2 %}
<li>ID: {{ item[0] }}</li><li> Title: {{ item[1] }}</li>
<img src="{{ item[2] }}" alt="dummy.jpg"> </img>
{% endfor %}
</div>
</body>
</html>
I tried by adding a '-' after the '%' in the for loop to trim the white spaces but don't work. If I remove the line break then assets are displayed in a line but it's in a auto fit manner.3 assets details are displayed in each line instead of the whole details in a single line. Can someone please shed some light into this?
If I get you correctly, you want the <li> elements to be displayed inline rather than as block elements:
li { display: inline-block }
<ul>
<li>ID: 1</li>
<li>Title: foo</li>
<li><img src="//lorempixel.com/400/200"</li>
</ul>
Edit: as I think of it, you could also want each ID/Title as a header to the image, and every of those components horizontally aside each other. Like so:
div, li { display: inline-block }
img { display: block }
<div>
<ul>
<li>ID: 1</li>
<li>Title: foo</li>
</ul>
<img src="//lorempixel.com/400/200">
</div>
<div>
<ul>
<li>ID: 2</li>
<li>Title: bar</li>
</ul>
<img src="//lorempixel.com/400/300">
</div>