There are tabs on pure CSS, but how to make the first link be active, have its own style, and when tabs are intertwined, the links also match the active ones. I try with :active :focus but this method only works while clicking on a tab. Using Js is, of course, easy to do, but is it possible without JS?

 .target:not(:target), :target ~ .target.default-target { display: none; } .target:target, .target.default-target { display: block; } a:active, a:focus{ background-color: #7B7B7B; padding:3px; } 
 <ul> <li><a href="#id1">Π‘Π»ΠΎΠΊ 1</a> </li> <li><a href="#id2">Π‘Π»ΠΎΠΊ 2</a> </li> <li><a href="#id3">Π‘Π»ΠΎΠΊ 3</a> </li> <li><a href="#id4">Π‘Π»ΠΎΠΊ 4</a> </li> </ul> <div class="target" id="id2"> <p>Sed lobortis placerat elit tincidunt tempor. Nam dignissim euismod quam nec tempor. Sed tortor lorem, ultricies a auctor nec, auctor ut neque. Aenean varius, urna eget adipiscing feugiat, nunc ligula molestie massa, id accumsan turpis metus ac ante. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p> </div> <div class="target" id="id3"> <p>Pellentesque quis tortor vitae elit cursus vulputate et vel dui. Nunc commodo pretium arcu in ultricies. Nunc vel velit enim, et tincidunt leo. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p> </div> <div class="target" id="id4"> <p>Quisque eget tempor sapien. Cras convallis tempor orci pulvinar scelerisque. Nullam et erat eu nibh sollicitudin congue sit amet id diam. Sed in lectus ut augue euismod porta. Quisque non lacus odio. Nunc ornare adipiscing egestas.</p> </div> <div class="target default-target" id="id1"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tempus, felis at varius eleifend, neque orci porta magna, consequat aliquam ligula velit quis erat. Aenean porttitor pellentesque risus, eu tincidunt ipsum blandit in.</p> </div> 

  • You know, I recently carefully researched, I did not see a nice, clean CSS solution. - Vadim Ovchinnikov
  • @VadimOvchinnikov rights, here are some examples of tabs, some become active: css-tricks.com/examples/CSSTabs but frankly they are not suitable for dynamic layout, and look ugly in terms of implementation - MasterAlex
  • I also thought of the place of references to use input type radio + label +: checked, but this approach is also not beautiful, so you still have to use a couple of lines of js. - Shperung
  • @Shperung, input type radio + label +: checked - not a single string js - user3178479

2 answers 2

 /* ΠžΡ‡ΠΈΡ‰Π°Π΅ΠΌ Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½ΠΈΠ΅ ΠΎΠ±Ρ‰Π΅Π³ΠΎ Π±Π»ΠΎΠΊΠ°, Ρ‡Ρ‚ΠΎΠ± float Π½Π΅ вилял Π½Π° Ρ€Π°Π·ΠΌΠ΅Ρ‰Π΅Π½Π½Ρ‹Π΅ элСмСнты послС Ρ‚Π°Π±ΠΎΠ² */ #tabs::after { display: block; content: ""; clear: both; } /* ΠŸΡ€Π΅Π΄ΠΎΡ‚Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΡƒ ΠΊ tab-content ΠΏΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ Π½Π° ссылку */ .tab-switch { position: fixed; } .tab-link { display: inline-block; } /* Π‘Π»ΠΎΠΊ с содСрТимым ΠΈΠ·Π½Π°Ρ‡Π°Π»ΡŒΠ½ΠΎ сокрытый ΠΈ ΠΈΠΌΠ΅Π΅Ρ‚ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ float:right для смСщСния ΠΏΠΎΠ΄ .tab-link. Если ΡƒΠ±Ρ€Π°Ρ‚ΡŒ float ΠΈΠ»ΠΈ Π·Π°ΠΌΠ΅Π½ΠΈΡ‚ΡŒ Π΅Π³ΠΎ Π½Π° flex, .tab-link Π±ΡƒΠ΄Π΅Ρ‚ ΡΠΌΠ΅Ρ‰Π°Ρ‚ΡŒΡΡ ΠΏΡ€ΠΈ ΠΏΠ΅Ρ€Π΅ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠΈ. Π’ΡƒΡ‚ ΠΆΠ΅ ΡƒΠΊΠ°Π·Π°Π½ΠΎ наслСдованиС стиля ΠΏΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ Π½Π° ссылку, Ρ‡Ρ‚ΠΎΠ± ΡΠΏΡ€ΡΡ‚Π°Ρ‚ΡŒ ΠΈΠ·Π½Π°Ρ‡Π°Π»ΡŒΠ½ΠΎ Π°ΠΊΡ‚ΠΈΠ²Π½Ρ‹ΠΉ Π±Π»ΠΎΠΊ. Если класс .active Ρ€Π°Π·ΠΌΠ΅ΡΡ‚ΠΈΡ‚ΡŒ Π² Π½Π°Ρ‡Π°Π»Π΅ ΠΊΠΎΠ΄Π°, Ρ‚ΠΎ ΠΏΡ€ΠΈ Π½Π°ΠΆΠ°Ρ‚ΠΈΠΈ Π½Π° ссылку Π½Π΅ Π±ΡƒΠ΄Π΅Ρ‚ ΡΡ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Ρ‚ΡŒ наслСдованиС ΠΌΠ΅ΠΆΠ΄Ρƒ родствСнными сСлСкторами, Ρ‚Π°ΠΊ ΠΊΠ°ΠΊ ссылка Π² ΠΊΠΎΠ΄Π΅ Π±ΡƒΠ΄Π΅Ρ‚ послС Π΄Π°Π½Π½ΠΎΠ³ΠΎ Π±Π»ΠΎΠΊΠ°. */ .tab-content, .tab-switch:target ~ .tab-content.active { box-sizing: border-box; float: right; display: none; width: 100%; } /* Π’ΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌ сокрытый Π±Π»ΠΎΠΊ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡ target ΠΈ id прописанный Π² href */ .tab-switch:target + .tab-link + .tab-content, .tab-content.active{ display: block; } #tabs { font-size: 0; } /* ОписаниС стиля ΠΊΠ½ΠΎΠΏΠΎΠΊ ΠΏΠ΅Ρ€Π΅ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ Ρ‚Π°Π±ΠΎΠ² ΠΈ аналогичная Ρ€Π°Π±ΠΎΡ‚Π° с ΠΎΡ„ΠΎΡ€ΠΌΠ»Π΅Π½ΠΈΠ΅ΠΌ этих ΠΊΠ½ΠΎΠΏΠΎΠΊ ΠΊΠ°ΠΊ Π² ΠΏΠ΅Ρ€Π΅ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ скрытых Π±Π»ΠΎΠΊΠΎΠ² */ .tab-link, :target ~ .tab-link.active { margin-left: 0; margin-top: 10px; padding: 10px; background: transparent; text-decoration: none; font-size: 18px; color: #333; } .tab-link:hover { background-color: white; color: black; } .tab-switch:target + .tab-link, .tab-link.active { background-color: #eee; color: black; } /* Π­Ρ‚ΠΎ просто внСшний Π²ΠΈΠ΄ */ .tab-content { background-color: #eee; padding: 5px 15px; font-size: 13px; } .tab-content p { line-height: 1.65; } .tab-content ul, .tab-content ol { padding: 0 0 0 15px; margin: 0; line-height: 1.25; } .tab-content ul > li, .tab-content ol > li { margin: 5px 0; } 
 <div id="tabs"> <span id="tab-1" class="tab-switch"></span> <a href="#tab-1" class="tab-link">Π’Π°Π± 1</a> <div class="tab-content"> <h2>Π’Π°Π± 1</h2> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias amet commodi cupiditate molestias laboriosam sit! Delectus magni esse magnam libero laboriosam aspernatur voluptates, natus aliquid sed doloremque architecto provident, impedit saepe. Mollitia eum qui, quae laboriosam, magni aut similique asperiores error dicta totam ad, deserunt nemo repudiandae maxime, expedita. Id, numquam, laboriosam. Voluptatibus, saepe! Eligendi assumenda ullam modi explicabo voluptas qui veniam repellat ipsam similique dolore, mollitia quam dolores quae cumque neque officia. Laudantium, ratione sint quo molestiae sapiente dolores, doloremque modi illo quia tempora asperiores ullam perspiciatis fuga quasi maxime nesciunt ab suscipit harum quam. Eligendi dicta suscipit dolorum.</p> </div> <span id="tab-2" class="tab-switch"></span> <a href="#tab-2" class="tab-link">Π’Π°Π± 2</a> <div class="tab-content"> <h2>Π’Π°Π± 2</h2> <ul> <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li> <li>Aliquam tincidunt mauris eu risus.</li> <li>Vestibulum auctor dapibus neque.</li> </ul> </div> <span id="tab-3" class="tab-switch"></span> <a href="#tab-3" class="tab-link active">Π’Π°Π± 3</a> <div class="tab-content active"> <h2>Π’Π°Π± 3</h2> <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p> </div> </div> 

The code is beautiful. The principle is the same. Making dopilita. The only drawback is that the active block can only be at the end because of the class inheritance to which the binding goes to hide the initial block. You can try to play with the float or with the structure as a whole, I think this point can be finished.

To understand the basic principles will need to read about:

  • Remains in the address bar id anchor, this is of course a good solution but because of the address bar cant - user33274
  • one
    A great option, played with flex-direction: row-reverse; and the first active now "Tab 1" - Shperung
  • @AlexeyGiryayev Excellent answer. Can you add comments on the code and explain the principles? 2. Replace float on flexbox? - Vadim Ovchinnikov
  • @VadimOvchinnikov Thank you. Added comments to the code. The narrator from me is useless, if something is not clear, ask. I see no sense in replacing flex, and most likely it will not work out (Just remove the float and understand what function it plays when building the whole structure). At leisure, I will also think about how to move the active tab to the top of the list. - Alexey Giryayev
  • @Shperung Thank you. It is so, only all the points will need to be formed in the reverse order, however, this is not a big problem. So far this is the only embodiment with the active block shifted to the left. Or a small script that selects the desired value when loading the page. Although, we still tried to get rid of the script. - Alexey Giryayev

You can use input radio :

 .target { display: none; } input#t1:checked ~ .target#id1 {display: block;} input#t2:checked ~ .target#id2 {display: block;} input#t3:checked ~ .target#id3 {display: block;} input#t4:checked ~ .target#id4 {display: block;} input[type="radio"] {display:none;} input#t1:checked ~ ul li label[for="t1"], input#t2:checked ~ ul li label[for="t2"], input#t3:checked ~ ul li label[for="t3"], input#t4:checked ~ ul li label[for="t4"], label:active { background-color: #7B7B7B; padding:3px; } 
 <input type="radio" name="tab" id="t1" checked> <input type="radio" name="tab" id="t2"> <input type="radio" name="tab" id="t3"> <input type="radio" name="tab" id="t4"> <ul> <li> <label for="t1">Π‘Π»ΠΎΠΊ 1</label> </li> <li> <label for="t2">Π‘Π»ΠΎΠΊ 2</label> </li> <li> <label for="t3">Π‘Π»ΠΎΠΊ 3</label> </li> <li> <label for="t4">Π‘Π»ΠΎΠΊ 4</label> </li> </ul> <div class="target" id="id2"> <p>Sed lobortis placerat elit tincidunt tempor. Nam dignissim euismod quam nec tempor. Sed tortor lorem, ultricies a auctor nec, auctor ut neque. Aenean varius, urna eget adipiscing feugiat, nunc ligula molestie massa, id accumsan turpis metus ac ante. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p> </div> <div class="target" id="id3"> <p>Pellentesque quis tortor vitae elit cursus vulputate et vel dui. Nunc commodo pretium arcu in ultricies. Nunc vel velit enim, et tincidunt leo. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p> </div> <div class="target" id="id4"> <p>Quisque eget tempor sapien. Cras convallis tempor orci pulvinar scelerisque. Nullam et erat eu nibh sollicitudin congue sit amet id diam. Sed in lectus ut augue euismod porta. Quisque non lacus odio. Nunc ornare adipiscing egestas.</p> </div> <div class="target default-target" id="id1"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tempus, felis at varius eleifend, neque orci porta magna, consequat aliquam ligula velit quis erat. Aenean porttitor pellentesque risus, eu tincidunt ipsum blandit in.</p> </div> 

But agree, this implementation does not look very nice. So it is better to use javascript , so that there would be less hassle and crutches