簡體   English   中英

CSS - 同一個類在其他元素上看起來不同

[英]CSS - same class looks different on other element

我創建了一個名為“primary”的按鈕類。 我注意到,它看起來不同的一個buttona標簽,他們有其他尺寸的例子。

 .primary { border-radius: 0; color: white; background: #EC7404; padding: 9px 29px; font-size: 1.5rem; vertical-align: middle; border: none; }
 <button class="primary">This is a test</button> <a class="primary">This is a test</a>

我怎樣才能讓課程在兩者上看起來一樣?

JSFIDDLE

使用 HTML 標簽,瀏覽器會向標簽添加自己的默認樣式(是的,它可能因瀏覽器而異)。 在您的情況下,您需要在.primary類中添加 2 個屬性:' display: inline-block and font` 屬性。

根據您共享的 JSFiddle。 這是更新主類代碼后兩個元素高度的更新代碼和屏幕截圖:

.primary{
    border-radius: 0;
    color: white;
    background: #EC7404;
    padding: 9px 29px;
    /*font-size: 1.5rem;*/
    vertical-align: middle;
    border: none;

    /* these 2 lines to be added */
    display: inline-block;
    font: 400 1.5rem Arial;
}

在此處輸入圖片說明

 .primary { border-radius: 0; color: white; background: #EC7404; padding: 9px 29px; font-family: "Goudy Bookletter 1911", sans-serif; font-size: 1.5rem; vertical-align: middle; border: none; }
 <button class="primary">This is a test</button> <a class="primary">This is a test</a>

這是因為瀏覽器已經為 HTML 元素賦予了自己的樣式。 您將使用自己的 CSS 覆蓋此樣式。 但是默認情況下, button將具有與a元素不同的樣式。

例如 line-height 和 font-family 可能不同。 在您的情況下, display: inline-block似乎可以解決問題。

這是因為元素繼承了默認樣式,如果您希望錨點看起來相同,只需在類中聲明字體系列和字體大小即可;

.primary {
border-radius: 0;
color: white;
background: #EC7404;
padding: 9px 29px;
font-size: 1.5rem;
vertical-align: middle;
border: none;
font-family: sans-serif;
font-size: 26px;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM