Applying custom CSS designs to images using Thymeleaf

Currently working on a web application using Spring MVC and Thymeleaf for templating. I'm struggling to figure out how to apply rules to an image, especially when using an external CSS file.

I have tested code that works:

page.html

<a class="navbar-brand" href="#">
  <img src="../resources/images/logo.png" class="img-responsive" />           
</a>

style.css

.navbar-brand > img {
    max-height: 100%;
}

h1 { text-align:center; }

However, when incorporating Thymeleaf into my HTML, it looks like this:

thymeleaf.html

[Bootstrap navbar...]
<a class="navbar-brand" href="#">
  <img th:src="@{/resources/images/logo.png}" class="img-responsive" />            
</a>
[...]
[/Bootstrap navbar]

<h1>Test</h1>

When using the same CSS, the rule doesn't get applied to the image but instead centers the h1 text.

EDIT: Rules applied to the content div within the Bootstrap navbar are not being recognized (not showing up in "inspect element"). Other rules set to a normal div without Bootstrap classes work fine.

EDIT2: CSS import

In servlet.xml

<mvc:resources mapping="/resources/**" location="/resources/"/>

In thymeleaf.html

<link rel="stylesheet" type="text/css" media="all"  
      href="/resources/css/style.css" th:href="@{/resources/css/style.css}" />

Any suggestions on how to resolve this issue? Thank you in advance.

Answer №1

Could you please include the th:class attribute like this?

<a th:class="navbar-brand" href="#">
  <img th:src="@{/resources/images/logo.png}" th:class="img-responsive" />            
</a>

Let me know if this solution worked for you

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

Ways to interact with elements lacking an identifier

Currently, I'm faced with an HTML challenge where I need to interact with an element labeled tasks. *<td class="class_popup2_menuitem_caption" unselectable="on" nowrap="">Tasks</td>* In my attempt to do so, I've implemented the foll ...

Access a designated tab within a CSS-designed tab system

Can anyone offer some assistance? I currently have a tab structure created using CSS. The first tab is set as the default when the page loads. I am looking to create a link from another page that will take users directly to the page with the tabs and ope ...

What is the method to choose an invisible link without an identifier using Selenium WebDriver?

I am attempting to click on the link labeled 'User Requested'. This link only appears when hovering over the menu. This is the HTML code: <ul class="k-widget k-reset k-header k-menu k-menu-horizontal" id="menu" data-role="menu" tabindex="0" ...

Broken links preventing page navigation

As embarrassing as it is, I seem to have forgotten the basics of linking pages in HTML. I've been focusing so much on CSS that now, when it's time to create a website with multiple pages, I'm stumped. The links in my navbar aren't worki ...

Ensure that divs can adjust in size while maintaining the same dimensions of their contained

I'm currently facing a slight issue with these two divs. I want them to be scalable, which I know can be achieved by using percentages, but every time I do so, the divs end up out of position. When I try setting widths, they look fine in Google Chrome ...

js include exclude switch a category

Although this question has been asked before, I have a query on completely removing an "id" from an element. Let's consider the following scenario: <div id="page"> some content here </div> I want to eliminate the "id" attribute from the ...

Tips for switching images in a grid using jQuery

I have implemented an image grid using flexbox on a website I am developing. The grid consists of 8 boxes, and my goal is to randomly select one box every 2 seconds and assign it one of 12 random images. My strategy involves creating an array containing UR ...

How to use PHP and JavaScript to update a location marker on Google Maps

I'm new to web development and in need of some help, please. I have a code that is supposed to update the marker location with coordinates retrieved from a database. <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AP ...

Create a custom overlay for an image that is centered horizontally and does not have a fixed width

I'm working with this HTML setup: <div class="container"> <img class="image" /> <div class="overlay"> <div class="insides">more content here</div> </div> &l ...

What causes the disappearance of CSS styles when attempting to modify the className in react js?

I am currently working on a basic react application, and I am trying to dynamically change the name of a div element using the following code snippet - <div className={'changeTab ' + (page.login ? 'leftSide' : 'rightSide')} ...

Incorporate dynamic body color changes across all pages using AngularJS

On the home page, I want to change the color scheme so that it is consistent across all pages. The user should be able to choose a color from a list on the home page and have it apply to every page. Currently, the color selection only applies to the home p ...

Including CSS in an HTML file does not function properly

I've been attempting to import a CSS file into an HTML document, but for some reason it's not working. I have also tried linking the path directly, but that hasn't worked either. <!DOCTYPE html> <html lang="en"> <head> < ...

Guide to saving a Python docx file on the client side with the help of Ajax

I am currently using Python 3.6 with Django, and my code snippet is shown below: from docx import Document document = Document() document.add_heading('My docx', 0) document.save('myFile.docx') return HttpResponse(document, content_typ ...

What is the best way to resolve the CSS border styling issue surrounding the anchor element?

I created a simple sign-up form for my school project where users can choose their roles. I'm facing an issue with the border under the anchor tag and I need help fixing it. How can I ensure that the space at the top is equal to the bottom as well? T ...

Adjusting the angular routerLink based on an observable

When working with HTML in Angular, I am looking for a way to use an <a> tag that adjusts its routerlink based on whether or not a user is logged in. Is it possible to achieve this functionality within a single tag? <a *ngIf="!(accountService ...

What is the best way to update a CSS class in React JS?

Suppose I have a CSS class called 'track-your-order' in my stylesheet. Whenever a specific event occurs, I need to modify the properties of this class and apply the updated values to the same div without toggling it. The goal is to replace the ex ...

Best practices for coding in HTML and CSS

Creating my first website for an internship has been quite a learning experience. Throughout my training, I was always advised to avoid embedding styles directly into the HTML code. However, now that I am actually developing a site, I can't help but f ...

Variations in CSS Stylesheets for Various Parts of the Body

Currently, I am in the process of creating a single page website. One issue that I have encountered is needing a Bootstrap "modal" class popup to appear when a button is clicked. However, the template I am utilizing was designed using Bootstrap Version 1, ...

Ways to ensure your page stays confidential

Concept: Exploring the idea of making specific pages on a website private. For example, having 4 navigation links where one link leads to a private page that requires a username and password for access. Currently, my practice website has 7 links directing ...

How to modify the appearance of the md-tab-header component in Angular2

Currently, I am working on a project that was passed down to me by a former colleague. It is a gradual process of discovery as I try to address and resolve any issues it may have. One particular element in the project is a md-tab-header with a .mat-tab-he ...