Using CSS to style textarea input fields in Django-generated forms

The image below displays the code from forms.py that generates the textarea input box on the right side, with a placeholder text of "Your comment to the world".

https://i.sstatic.net/8CAGO.png

However, the CSS is only affecting the standard HTML textarea box on the left, and I am struggling to apply it to the Django-generated textarea. The CSS has been automatically applied to the 'Name' text input field at the top.

Here is the styles.css code:

body {
  background-color: white;
}
h1 {
  color: red;
  text-shadow: 3px 2px grey;
  font-family: Arial
}
p {
  color: black;
  font-family: Arial
}


input[type=text] {
  width: 20%;
  padding: 21px 20px;
  margin: 14px 0;
  box-sizing: border-box;
  font-size: 33;
  border: 2px solid red;
  border-radius: 4px;
  font-family: Arial
}

textarea[type=textarea] {
  width: 20%;
  padding: 21px 20px;
  margin: 14px 0;
  box-sizing: border-box;
  font-size: 33;
  border: 2px solid red;
  border-radius: 4px;
  font-family: Arial
}

Below is the code snippet for forms.py (which includes the rendered HTML):

from django import forms

class CommentForm(forms.Form):
    name = forms.CharField(max_length=20,widget=forms.TextInput(attrs={'placeholder':'Your Name Please'}))
    comment = forms.CharField(widget=forms.Textarea(attrs={'placeholder':'Your comment to the world'}))

Finally, here is the HTML code for the sign.html page:

{% load static %}
<!DOCTYPE html>
<html>

<head>

  <link rel="stylesheet" href="{% static 'guestbook/styles.css' %}">
</head>
<body>

<h1>Tell the world how you're doing!</h1>
<h2>Sign the guestbook</h2>
<form action="/action_page.php">
  Enter your name:<br>
  <!--<input type="text" name="name" placeholder="Your Name here">-->
   {{form.name}}
  <br>
 Enter your comment:<br>
  <textarea name="message" type="Textarea" placeholder="Your comment here" rows="10" cols="30"></textarea>
  {{form.comment}}
  <br><br>

  <input type="button" value="Submit">

</form> 

<p>If you click the "Submit" button, the form-data will be sent to a page called "/action_page.php".</p>
<p>Go to the <a href="{% url 'index' %}"> guestbook </a> itself</p>
</body>
</html>

In essence, my goal is to understand how to create a functional HTML object and apply CSS to it seamlessly.

Answer №1

From my understanding, you might be experiencing no problems with applying CSS to the HTML that you manually code, but encountering difficulties when trying to style the HTML generated by Django's templating system. If this is the case, I suggest examining the rendered HTML of the Django-generated "comment" field. It is likely that your CSS rules are not taking effect because the structure of the HTML is not as expected. For instance, if your selector is textarea[type=textarea], it may not match the comment field due to it not being a textarea or lacking a type=textarea attribute.

If you are struggling to apply a CSS rule, you can also include attributes in the widget definition itself. Additionally, I often utilize this library, which allows for adding attributes directly in the template.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Preventing Image Resize within a Div: Best Practices

I am facing an issue with my image slider where the images automatically resize in the same ratio when I adjust my browser size. What I want to achieve is to keep the height constant while adjusting the width slightly until a specific breakpoint using @med ...

Steps for implementing an <h1> element with the React Material UI theme

I have chosen the 'dark' theme and would like to apply it to an h1 element. How can I achieve this? The method below does not yield the desired result: <MuiThemeProvider theme={theme}> <h1>Page Title</h1> ... The following ...

What is preventing me from using JavaScript to remove this class?

Struggling to implement a skeleton loading screen with CSS classes and JavaScript. The idea is to apply the 'skeleton' class to elements, style them accordingly, then remove the class using a timeout set in JavaScript. However, I'm encounter ...

What methods are available to manage the ordering of the JSON output provided by the Django REST framework?

Understanding the control of field order in JSON returned by Django serializers is crucial. Is it to be managed at the template, model, or serializer level? What influences the arrangement of fields in the serialized JSON data? Let's consider the foll ...

Trouble getting vertical alignment to work using display:table-cell within display:table

This is my initial inquiry, so I apologize if the formatting is incorrect. Despite attempting various methods from Stack Overflow, I am still unable to properly display this content vertically centered. CSS: div.img-wrapper { display: table; height: ...

Is the height of each column the same as the deepest column?

If my div layout looks like this: <div id="stretchyheader"></div> <div id="fixedwidthwide"><div> <div id="fixednarrow></div> <div id="footer"></div> Resulting in the following structure: ------------------ ...

The issue of background-attachment: fixed not functioning properly on Safari

Currently, I have the following code implemented on an element that extends 100% of the browser: #section_white { background-attachment:fixed; background-image:url(image_url_here.jpg); background-position:100% 100%; background-repeat:no-repeat no- ...

Is it possible to create a touch menu on an iPhone by utilizing basic markup and implementing the :hover-state feature?

After finally launching my WordPress blog, I spent a significant amount of time developing the theme. When I attempted to view the site on my iPhone for the first time, I was surprised by what I saw. Initially, I thought creating a touch menu would be as s ...

Words are extending beyond the boundaries of the box

Is there a way to create a 128px by 192x box with a title and text inside it, where the text can scroll if it's too long but the title remains fixed? The issue I'm facing is that the text is exceeding the boundaries of the box. If you have any s ...

Header that stays in place even when scrolling - Styling with CSS and jQuery

My goal is to implement a sticky header that appears when the user scrolls down and the original header disappears. Here is the current script I'm using: $(function(){ // Check the initial Position of the Sticky Header var stickyHeaderTop = $ ...

placing a dropdown menu below the search bar

Let's say I have a container with an input search field. My goal is to position a select dropdown right below the search input, ensuring that the dropdown has the same width as the search input (similar to autocomplete functionality). Here's what ...

How can I create a fixed-top navbar with CSS that sticks when scrolling?

Having two top navbars on a single page can be tricky. One is fixed in its position, while the other is sticky. But when scrolling down, the sticky navbar fails to stay on top of the fixed one: https://i.sstatic.net/4mUAZ.png Furthermore, as you scroll d ...

Div-based table design

I'm facing an issue that I can't seem to resolve on my own, so if anyone has any advice or solutions, I would greatly appreciate it: ________________________________________________________________________________ | body ...

Sliding horizontally with a responsive touch

I am looking to implement a horizontal responsive page navigation, similar to the illustration shown below: This is my current progress: DEMO $(document).ready(function () { var slideNum = $('.page').length, wrapperWidth = 100 * s ...

Automatically Logging out of Django Admin and Answers About Web Hosting

Issue 1 Imagine I am starting a new Django project and creating a new app. How can I make sure that users are logged out after being inactive or when they close all tabs related to the website in the admin section? [I bring up this scenario because existi ...

PNG images lose their transparency when altered using Django and PIL

I am currently utilizing sorl-thumbnail, PIL, and Django on a web server to dynamically generate thumbnails in templates. Despite having PNG support installed with PIL, I am encountering strange artifacts on the transparent areas of the images after trans ...

The Operational Error in Django DB: No password provided for authentication as fe_sendauth

After cloning a repo from github and diving into its django project that uses postgres as the database, I found myself in need of making changes on the production side. The database details are as follows: DATABASES = { 'default': { ...

Utilizing MongoDB in Django's class-based views

I am attempting to implement a class-based view as shown below class DetailBookVIew(DetailView): model = Book template_name = 'book/detail_book.html' The only challenge is that the model Book is a mongodb model. How can I successfully u ...

Switching a Polymorphic Wrapper from type T to type U?

Consider the purpose behind the following illegal C++11 code: struct Base { template<typename U> virtual U convert() = 0; }; template<typename T> struct Derived : Base { T t; template<typename U> virtual U convert() ...

Server issues causing Laravel 6 CSS and JS to fail loading

I compressed my project folder and uploaded it to the Document Root for the subdomain. All of my CSS and JS files are located inside the Public folder. The project works perfectly fine on XAMPP, but when I transfer it to my shared hosting, the CSS and JS f ...