使用外部 CSS 样式表自定义 Google CSE 的外观

发布于 2024-10-16 14:42:59 字数 272 浏览 6 评论 0原文

我使用 Google 网站管理员工具为我的网站创建了一个搜索引擎。现在我想自定义 CSE 给出的结果的格式。 Google 允许我下载整个 CSS 文件,但是当我将其附加到我的 PHP 文档的 head 部分时,没有任何反应 - 自定义样式不起作用。

当我将 Google 的样式放入 body 标记中时,一切正常,但问题是这种方式不符合万维网联盟的规则,而且如果我插入这样的代码,我的代码会变得非常“脏”和不整洁我的页面正文中有一长段 CSS 代码。

如何使我的外部样式表更改搜索引擎的默认外观?

I have created a search engine for my website using Google Webmaster Tools. Now I'd like to customise the format of results given by the CSE. Google offers me to download the CSS file in whole, but when I attach it to my PHP document inside the head section, nothing happens – the custom style doesn't work.

When I put the Google's style inside the body tag, everything worked normally, but the problem is that this way isn't according to the rules of the World Wide Web Consortium, plus my code gets very 'dirty' and untidy if I insert such a long block of CSS code inside the body of my page.

How can I make my external style sheet change the default appearance of the search engine?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东走西顾 2024-10-23 14:42:59

您是否能够确定外部 CSS 样式表是否可用于自定义 Google 搜索框?

这就是我今天所做的,它在 W3C 验证器中进行验证:

查看此链接:带有 Google 自定义搜索的主页和外部样式表。

如果您查看源代码,您可以看到我从 Google 自定义搜索的“获取代码”页面上的链接下载了“源 CSS”。然后我将其上传到我网站的服务器(根据我的喜好更改 CSS 后)。

然后我从“获取代码”页面中取出代码的脚本部分并将其粘贴到主页的HTML中,并将这句话:改为:

google.load('search', '1', {language : 'en', style : google.loader.themes.MINIMALIST});

如果

  google.load('search', '1', {language : 'en', style : src="http://hoodexc.com/css/google-search.css"});

任何了解java脚本的人可以告诉我一个更好的方法(只是因为这有效并不意味着这是最好的方法)请告诉我。
带有外部样式表的 Google CSE
带有外部样式表的 Google CSE

Were you able to figure out if an external CSS stylesheet can be used for a Custom Google Search box?

This is what I've done today, and it validates in W3C validator:

Check out this link: a homepage with Google Custom Search and external stylesheet.

If you view source, you can see that I downloaded Google Custom Search's "Source CSS" from the link on their "Get Code" page. Then I uploaded it to my website's server (after changing the CSS to my liking).

Then I took the script portions of the code from the "Get Code" page and pasted them in the HTML of the homepage, and I changed the phrase:

google.load('search', '1', {language : 'en', style : google.loader.themes.MINIMALIST});

to this:

  google.load('search', '1', {language : 'en', style : src="http://hoodexc.com/css/google-search.css"});

If anyone who knows java script can tell me a better way (just because this is working doesn't mean it's the best way) please let me know.
Google CSE with external stylesheet
Google CSE with external stylesheet

蓬勃野心 2024-10-23 14:42:59

只需在加载 Google 自定义搜索时提供附加参数“nocss: true”,即可防止 Google 加载任何 css 进行搜索,因此您可以定义所有 gcse-、gs- 等.css 类自己不受干扰。

nocss:告诉 API 是否加载任何样式表的布尔值
通常与其控件相关。如果您不打算使用
默认 CSS,您可以通过将其设置为 true 来减少加载时间。这
默认设置为 false。

https://developers.google.com/loader/#DetailedDocumentation

代码:< /strong>

google.load('search', '1', { 
    callback: OnGoogleSearchLoad,
    language : 'en',
    nocss: true,
    style : src="http://example.com/assets/css/gcse.css"
});

结果(无 CSS,网站默认规则除外):

使用我的网站 css 代替 GCSE 进行搜索主题,准备以我喜欢的任何方式进行自定义。

GCS 的示例 css 文件:

.gsc-tabsAreaInvisible,
.gsc-resultsHeader,
.gsc-branding,
.gcsc-branding,
.gsc-url-top,
.gs-watermark,
.gsc-thumbnail-inside,
.gsc-url-bottom {
    display: none;
}

.gsc-result {
    padding: 20px 0;
    border-bottom: 1px solid #E1E1E1;
}


.gs-image-box {
    width: 140px;
    height: 80px;
    overflow: hidden;
}

img.gs-image {
    min-height: 80px;
}

td.gsc-table-cell-thumbnail {
    vertical-align: top;
    padding: 0;
    width: 140px;
    display: block!important;
}

td.gsc-table-cell-snippet-close {
    vertical-align: top;
    padding: 0;
    padding-left: 20px;
}

.gsc-wrapper {
    font-size: 16px;
    line-height: 20px;
}

.gsc-control-cse a {
    color: #202020;
    font-family: HelveticaNeueLTPro-Cn, Helvetica, Arial, sans-serif;
}

.gs-snippet {
    color: #777;
    margin-top: 10px;
}

b {
    font-weight: normal;
}
.gsc-cursor {
    width: 100%;
    height: 20px;
    text-align: center;
}

.gsc-cursor-page {
    display: inline-block;
    width: 20px;
    height: 20px;
    cursor: pointer;
    text-align: center;
    margin: 20px 0;
}


form.gsc-search-box {
    background: #d9dadd;
    border: 20px solid #d9dadd; 
    width: 100%;
    -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
    -moz-box-sizing: border-box;    /* Firefox, other Gecko */
    box-sizing: border-box;         /* Opera/IE 8+ */    
}

table.gsc-search-box {
    width: 100%;
}

td.gsc-search-button {
    vertical-align: middle;
    padding-left: 20px;
} 

td.gsc-input {
    vertical-align: top;
    width: 100%;
}

input.gsc-input {
    margin:0;
    width: 99%;
}

结果(具有自定义样式):

仅具有自定义样式的 Google 自定义搜索。

Simply provide additional parameter "nocss: true" when loading Google Custom Search, to prevent Google from loading any css for search, so you can define all gcse-, gs- etc. css classes yourself without interference.

nocss: A boolean that tells the API whether to load any style sheets
typically associated with its controls. If you don't intend to use the
default CSS, you can reduce the load time by setting this to true. The
default setting is false.

https://developers.google.com/loader/#DetailedDocumentation

Code:

google.load('search', '1', { 
    callback: OnGoogleSearchLoad,
    language : 'en',
    nocss: true,
    style : src="http://example.com/assets/css/gcse.css"
});

Result (no css, except site default rules) :

Search using my site css instead GCSE theme, ready to be customized in any way i like.

Example css file for GCS:

.gsc-tabsAreaInvisible,
.gsc-resultsHeader,
.gsc-branding,
.gcsc-branding,
.gsc-url-top,
.gs-watermark,
.gsc-thumbnail-inside,
.gsc-url-bottom {
    display: none;
}

.gsc-result {
    padding: 20px 0;
    border-bottom: 1px solid #E1E1E1;
}


.gs-image-box {
    width: 140px;
    height: 80px;
    overflow: hidden;
}

img.gs-image {
    min-height: 80px;
}

td.gsc-table-cell-thumbnail {
    vertical-align: top;
    padding: 0;
    width: 140px;
    display: block!important;
}

td.gsc-table-cell-snippet-close {
    vertical-align: top;
    padding: 0;
    padding-left: 20px;
}

.gsc-wrapper {
    font-size: 16px;
    line-height: 20px;
}

.gsc-control-cse a {
    color: #202020;
    font-family: HelveticaNeueLTPro-Cn, Helvetica, Arial, sans-serif;
}

.gs-snippet {
    color: #777;
    margin-top: 10px;
}

b {
    font-weight: normal;
}
.gsc-cursor {
    width: 100%;
    height: 20px;
    text-align: center;
}

.gsc-cursor-page {
    display: inline-block;
    width: 20px;
    height: 20px;
    cursor: pointer;
    text-align: center;
    margin: 20px 0;
}


form.gsc-search-box {
    background: #d9dadd;
    border: 20px solid #d9dadd; 
    width: 100%;
    -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
    -moz-box-sizing: border-box;    /* Firefox, other Gecko */
    box-sizing: border-box;         /* Opera/IE 8+ */    
}

table.gsc-search-box {
    width: 100%;
}

td.gsc-search-button {
    vertical-align: middle;
    padding-left: 20px;
} 

td.gsc-input {
    vertical-align: top;
    width: 100%;
}

input.gsc-input {
    margin:0;
    width: 99%;
}

Result (with custom styles):

Google Custom Search with only custom styling.

子栖 2024-10-23 14:42:59

我想对 Gniewomir 和 Kris 表示感谢。

我正在使用 V1 代码在我的 WordPress 博客上安装两页布局 google 自定义搜索。对于搜索框,我将

Loading

粘贴到文本小部件中,并将脚本的其余部分粘贴到标题中。

我将此部分更改

google.load('search', '1', {language: 'en', style: google.loader.themes.GREENSKY});

google.load('search', '1', {language: 'en', nocss:true, style:"http://www.domain-name/wp-content/themes/theme-name/css/gcse.css"});

同时,我下载了 GREENSKY.css 并将其保存为“gcse.css”,并将其上传到我的服务器。

通过 GTMETRIX 运行此命令,HTTP 重定向上的警告消失了。

I would like to say thanks to Gniewomir and Kris.

I am installing the two-page layout google custom search on my wordpress blog using the V1 code. For the search box, I pasted <div id="cse-search-form">Loading</div> in a text widget and the rest of the script in my header.

I changed this portion

google.load('search', '1', {language: 'en', style: google.loader.themes.GREENSKY});

to

google.load('search', '1', {language: 'en', nocss:true, style:"http://www.domain-name/wp-content/themes/theme-name/css/gcse.css"});

At the same time, I downloaded GREENSKY.css and saved it as 'gcse.css' which I uploaded to my server.

Ran this through GTMETRIX and the warnings on HTTP Redirects are gone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文