Project Assignment 1: Create a Tabs Component

This is a simple tab project. The original code can be found in Code Pen

Screen Shot 2017-01-07 at 7.41.25 PM.png

Let's start from HTML structure. Instead of traditional ul, li list, I use radio type of input to switch between tabs.

<div id='tabComponent'>
  <div class='tabComponent'>
    <input type="radio" id='homeDec' name='tab' />
    <label for="homeDec">Home Dector</label>
    <div class='content'>Browse through our list of Home Decor Items</div>
  </div>
  <div class='tabComponent'>
    <input type="radio" id='furniture' name='tab' />
    <label for="furniture">Furniture</label>
    <div class='content'>Flash Sale on Living Room and Bedroom Furniture</div>
  </div>
  <div class='tabComponent'>
    <input type="radio" id='kitchen' name='tab' />
    <label for="kitchen">Kitchen</label>
    <div class='content'>Bring the best out of the masterchef in you!!</div>
  </div>
  <div class='tabComponent'>
    <input type="radio" id='clearance' name='tab' />
    <label for="clearance">Clearance</label>
    <div class='content'>Red hot deals at affordable prices. Check out our clearance section for more details.</div>
  </div>
</div>

From the above example, we can see that all HTML code is included in a div with id=tabComponent. We can treat every tab along with its content as one whole set, so every tabComponent` class will be displayed when we click on one tab. Let's take a look at one tab for example:

<div class='tabComponent'>
    <input type="radio" id='homeDec' name='tab' />
    <label for="homeDec">Home Dector</label>
    <div class='content'>Browse through our list of Home Decor Items</div>
</div>

Firstly, there is a radio input, so we can check the radio to select one tab. After input tag, we have a label to show as tag info. Following label tag, there is a content div. One thing needs to be mentioned is that name attributes in input tag, all the tab should have a same name so that they can switch between each other.

Okay. It is time for us to take a look at our CSS file.

#tabComponent {
  margin: 25px 0;
  min-height: 200px;
  clear: both;
  position: relative;
}

div.tabComponent {
  float: left;
}

div.tabComponent [type=radio] {
  display: none;
}

div.tabComponent label {
  padding: 10px;
  background: #eee;
  border: 1px solid #ccc;
}

div.tabComponent .content {
  position: absolute;
  left: 0;
  top: 28px;
  bottom: 0;
  right: 0;
  background: #fff;
  padding: 20px;
  border: 1px solid #ccc;
}

[type=radio]:checked ~ label {
  background: #000;
  color: #fff;
  border: 1px solid #fff;
}

[type=radio]:checked ~ .content {
  z-index: 2;
}

Forget about CSS styling for now. Let's look at some basic functions for tabs. Firstly, we need to float all the label tabs, so they can be on the same line. display: none; line is aim to hide the radio check circle. Please pay attention to the .content class, because we need all content share the same space so they need to be placed in the same place. Here we use position: absolute to fulfill our needs. Do not forget to give background: #fff;, otherwise, the extra length of the content will be shown.

How we click on one tab and let the according content shows up? Okay, here goes our important rules.

[type=radio]:checked ~ .content {
  z-index: 2;
}

From the code above, we let input be checked every time and then we select the checked input's content, we set it z-index:2; so it will be on top of others.

I hope this will help someone in need. =) Happy Coding~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末说铃,一起剝皮案震驚了整個(gè)濱河市瓤球,隨后出現(xiàn)的幾起案子憋飞,更是在濱河造成了極大的恐慌,老刑警劉巖循狰,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件窟社,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡绪钥,警方通過(guò)查閱死者的電腦和手機(jī)灿里,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)程腹,“玉大人匣吊,你說(shuō)我怎么就攤上這事〈缌剩” “怎么了色鸳?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)甸祭。 經(jīng)常有香客問(wèn)我缕碎,道長(zhǎng),這世上最難降的妖魔是什么池户? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任咏雌,我火速辦了婚禮,結(jié)果婚禮上校焦,老公的妹妹穿的比我還像新娘赊抖。我一直安慰自己,他們只是感情好寨典,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布氛雪。 她就那樣靜靜地躺著,像睡著了一般耸成。 火紅的嫁衣襯著肌膚如雪报亩。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,125評(píng)論 1 297
  • 那天井氢,我揣著相機(jī)與錄音弦追,去河邊找鬼。 笑死花竞,一個(gè)胖子當(dāng)著我的面吹牛劲件,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播约急,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼零远,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了厌蔽?” 一聲冷哼從身側(cè)響起牵辣,我...
    開(kāi)封第一講書(shū)人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎奴饮,沒(méi)想到半個(gè)月后服猪,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體供填,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年罢猪,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了近她。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡膳帕,死狀恐怖粘捎,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情危彩,我是刑警寧澤攒磨,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站汤徽,受9級(jí)特大地震影響娩缰,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜谒府,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一拼坎、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧完疫,春花似錦泰鸡、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至芳誓,卻和暖如春余舶,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背锹淌。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來(lái)泰國(guó)打工欧芽, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人葛圃。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像憎妙,于是被迫代替她去往敵國(guó)和親库正。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容