Edit: I have been able to solve the first part of the issue: having the text to be wrapped inside the button with automatic new lines if needed. Seems that adding white-space:normal
was sufficient.
I have a Django template mainly displaying accordions and we use w3 as css framework.
<style>
.myButtonClass{
display:inline-block;
padding:8px 16px;
vertical-align:middle;
overflow:hidden;
cursor:pointer;
width:100%;
text-align:left;
white-space:normal
border:1px solid #ccc;
display:block;
color:#000;
background-color:#f1f1f1;
margin-bottom:16px;
};
.myButtonClass:disabled{
cursor:not-allowed;
opacity:0.3;
}
.myButtonClass:hover{
color:#000!important;
background-color:#ccc!important;
}
</style>
{% for event in event_data.data %}
<button onclick="myFunction('{{forloop.counter}}')" class="myButtonClass">{{ event.summary|linebreaks }}</button>
<div id={{forloop.counter}} class="w3-container w3-hide">
<pre>{{ event.data_raw | pprint }}</pre>
</div>
{% endfor %}
<script>
function myFunction(id) {
var x = document.getElementById(id);
if (x.className.indexOf("w3-show") == -1) {
x.className += " w3-show";
} else {
x.className = x.className.replace(" w3-show", "");
}
}
</script>
The string to be displayed in the button is in "event.summary".
Right now, in python, this text is generated using f-string:
summary = (f"type: {event_type]}n"
f"description: {event_desc}")
I want to format how this text is displayed in the button. Right now it is extremely basic as I use Django built-in template filters and it look like that:
The first issue is that if the description is too long compared to the button size, then the text is cut at the button right edge. So I would like the text to be wrapped inside the button with automatic new lines if needed.
However, by doing just that, it would look like:
While I would like it to be like:
or even better if possible:
This probably means that there should be some html formating specific for the button text and that word "description" and the text components should be four separate variables, which is possible. However, I don’t see how to implement a specific html formating for the text within the button.
2
Answers
In the end, solving the second part of my issue (i.e. formating how the text is displayed within the button) was quite simple. I am not sure if that is the best solution but I modified
summary
to be a dict instead of a string and used a table.It looks neat now, but if someone has a more elegant approach, I would be happy to learn!
The updated
html
template:If it’s working then that’s great. You do have a missing semicolon after white-space:normal.
You are also using Display twice so the second Display value will overwrite the first.