Google CSE样式表

Luk*_*uka 6 css google-custom-search

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

当我将Google的风格放在body标签内时,一切正常.但问题是,这种方式不符合万维网联盟的规则,如果我在页面正文中插入如此长的CSS代码,我的代码会变得非常"脏"和不整洁.

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

小智 6

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

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

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

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

然后我从"获取代码"页面获取代码的脚本部分并将它们粘贴到主页的HTML中,然后我更改了短语:

google.load('search', '1', {language : 'en', style : google.loader.themes.MINIMALIST});
Run Code Online (Sandbox Code Playgroud)

对此:

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

如果知道java脚本的人可以告诉我一个更好的方法(只是因为这是有效的并不意味着它是最好的方式),请告诉我. 带有外部样式表的Google CSE 带有外部样式表的Google CSE


Gni*_*ski 6

只需在加载Google自定义搜索时提供其他参数" nocss:true ",以防止Google加载任何用于搜索的CSS,因此您可以自己定义所有gcse- ,gs-等css类而不受干扰.

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

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

码:

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

结果(没有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%;
}
Run Code Online (Sandbox Code Playgroud)

结果(使用自定义样式):

只有自定义样式的Google自定义搜索.