【问题标题】:Spring + Thymeleaf + BootstrapSpring + Thymeleaf + Bootstrap
【发布时间】:2017-01-03 16:27:44
【问题描述】:

我想知道如何从 Bootstrap 覆盖 css。这是我到目前为止所尝试的。我创建了一个单独的 css 文件,并在那里调用 Bootstrap css 类并进行了一些更改,但我没有看到我已更改的类的差异。

HTML 和 css 文件

.table {
    border-color: crimson;
}

.table-striped {
    border-color: crimson;
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Spring Core Online Tutorial - List Products</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

    <link href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.4/css/bootstrap.min.css"
          th:href="@{/webjars/bootstrap/3.3.5/css/bootstrap.min.css}"
          rel="stylesheet" media="screen"/>

    <script src="http://cdn.jsdelivr.net/webjars/jquery/2.1.4/jquery.min.js"
            th:src="@{/webjars/jquery/2.1.4/jquery.min.js}"></script>

    <link href="../../static/css/spring-core.css"
          th:href="@{css/spring-core.css}" rel="stylesheet" media="screen"/>
</head>
<body>
<div class="container">
    <div th:if="${not #lists.isEmpty(products)}">
        <h2>Product List</h2>
        <table class="table table-striped">
            <tr>
                <th>Id</th>
                <th>Description</th>
                <th>Price</th>
                <th>Image URL</th>
                <th>Show</th>
                <th>Edit</th>
                <th>Delete</th>
            </tr>
            <tr th:each="product : ${products}">
                <td th:text="${product.id}"></td>
                <td th:text="${product.description}"></td>
                <td th:text="${product.price}"></td>
                <td th:text="${product.imageUrl}"></td>
                <td><a th:href="${'/product/show/' + product.id}">View</a> </td>
                <td><a th:href="${'/product/edit/' + product.id}">Edit</a> </td>
                <td><a th:href="${'/product/delete/' + product.id}">Delete</a> </td>
            </tr>
        </table>
    </div>
    <div class="row">
        <div class="col-sm-3">
            <a href="/product/new">New Product</a>
        </div>
    </div>
</div>

</body>
</html>

Pom 文件

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.designfreed</groupId>
<artifactId>spring-mvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-mvc</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.5</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>2.1.4</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

希望任何人都可以提供帮助!

非常感谢!

【问题讨论】:

  • 确保您的自定义 css 在引导文件之后加载

标签: html css spring twitter-bootstrap thymeleaf


【解决方案1】:

我解决了,我在导入我的 css 文件时在我的 html 上遗漏了一个斜杠

这是我的原始 html 文件

<link href="../../static/css/spring-core.css"
          th:href="@{css/spring-core.css}" rel="stylesheet" media="screen"/>

这是我改正错误的html

<link href="../../static/css/spring-core.css"
          th:href="@{/css/spring-core.css}" rel="stylesheet" media="screen"/>

感谢您的回答,因为他们帮助我找到了我想要的特定部分!

【讨论】:

    【解决方案2】:

    您看不到效果,因为您没有指定 border typeborder width 属性。设置该属性以查看效果。我添加了一个sn-p 下面。 使用边框:边框宽度边框类型边框颜色;设置你的边框

    .table {
        border:1px solid  crimson;
    }
    
    .table-striped {
        border:1px solid  crimson ;
    }
    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>Spring Core Online Tutorial - List Products</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    
        <link href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.4/css/bootstrap.min.css"
              th:href="@{/webjars/bootstrap/3.3.5/css/bootstrap.min.css}"
              rel="stylesheet" media="screen"/>
    
        <script src="http://cdn.jsdelivr.net/webjars/jquery/2.1.4/jquery.min.js"
                th:src="@{/webjars/jquery/2.1.4/jquery.min.js}"></script>
    
        <link href="../../static/css/spring-core.css"
              th:href="@{css/spring-core.css}" rel="stylesheet" media="screen"/>
    </head>
    <body>
    <div class="container">
        <div th:if="${not #lists.isEmpty(products)}">
            <h2>Product List</h2>
            <table class="table table-striped">
                <tr>
                    <th>Id</th>
                    <th>Description</th>
                    <th>Price</th>
                    <th>Image URL</th>
                    <th>Show</th>
                    <th>Edit</th>
                    <th>Delete</th>
                </tr>
                <tr th:each="product : ${products}">
                    <td th:text="${product.id}"></td>
                    <td th:text="${product.description}"></td>
                    <td th:text="${product.price}"></td>
                    <td th:text="${product.imageUrl}"></td>
                    <td><a th:href="${'/product/show/' + product.id}">View</a> </td>
                    <td><a th:href="${'/product/edit/' + product.id}">Edit</a> </td>
                    <td><a th:href="${'/product/delete/' + product.id}">Delete</a> </td>
                </tr>
            </table>
        </div>
        <div class="row">
            <div class="col-sm-3">
                <a href="/product/new">New Product</a>
            </div>
        </div>
    </div>
    
    </body>
    </html>

    【讨论】:

      【解决方案3】:

      边框颜色不适用于.table.table-striped 类,它是在 Bootstrap 中使用以下选择器设置的:

      .table>tbody>tr>td, .table>tbody>tr>th, .table>tfoot>tr>td,     
      .table>tfoot>tr>th, .table>thead>tr>td, .table>thead>tr>th {
          padding: 8px;
          line-height: 1.42857143;
          vertical-align: top;
          border-top: 1px solid #ddd;
      }
      

      确保使用 Bootstrap 使用的选择器进行自定义。

      【讨论】:

        猜你喜欢
        • 2018-08-15
        • 2020-11-08
        • 2019-03-04
        • 2018-08-05
        • 2018-06-19
        • 2020-08-15
        • 2017-05-23
        • 2018-01-13
        • 1970-01-01
        相关资源
        最近更新 更多