CSS selector or CSS classNames? [closed] - css

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 days ago.
Improve this question
Is it preferable to use selectors or classNames for nested code in CSS? In the example below, where there are parent divs with text inside, is it better to just use selectors to style the children? Or should they have classNames? Does it matter? And if it matters, is it because of readability/clean code, performance/optimization or something else?
Example 1 - using className:
Html
<div className="flexbox-parent">
<div className="flexbox-header">
<h1 className="primary-title">Title</h1>
</div>
<div className="content">
<h3 className="tertiary-title"> Tertiary title </h3>
<p className="text-content"> A paragraph with some text</p>
<br/>
<p className="text-content"> A paragraph with some text</p>
</div>
</div>
Css
.flexbox-parent {
display: flex;
justify-content: center;
.flexbox-header {
display: flex;
justify-content: center;
.primary-title {
font-size: 2rem;
color: grey;
}
}
}
Or example 2 - using selectors:
Html
<div className="flexbox-parent">
<div className="flexbox-header">
<h1>Title</h1>
</div>
<div className="content">
<h3> Tertiary title </h3>
<p>A paragraph with some text</p>
<br/>
<p>A paragraph with some text</p>
</div>
</div>
css:
.flexbox-parent {
display: flex;
justify-content: center;
.flexbox-header {
display: flex;
justify-content: center;
h1 {
font-size: 2rem;
color: grey;
}
p {
font-size: 0.8rem;
color: green;
}
}
}
Which is better?

One thing I would say, is that you should read up on specificity within CSS. Try to avoid nesting classes at all costs.
There isn't really a wrong or right way of doing what you want to achieve, it'll be opinions, but read here: https://css-tricks.com/specifics-on-css-specificity/
It'll help you avoid running into problems later.

Related

How can I use CSS to make a div invisible when there are no divs with a specific class under it?

I want to use CSS to hide entire groups of items when they don't have a threshold of named div items inside of them. I have 6 sections of faq 'questions', and a filtering mechanism that shows/hides these questions according to matching search terms. If after a search one or more of these sections does not have visible questions in it I don't want that section to be visible (ie. display: none;)
Is it possible to accomplish this purely with CSS? So that the jquery-driven reactive page search can rely on CSS to hide faq sections that don't have matching results inside of them? I have a general idea of how I'd accomplish this with JS, is it possible to do only using CSS?
To give an idea of what I'm thinking, I'd have:
<div class="group">
<div class="question" style="display: none;">
Question goes here
</div>
</div>
And I'd like to use CSS to apply something (display: none; or otherwise) to make the entire "group" invisible BECAUSE every div with the CSS class of 'question' is not visible from the display: none;
Based on answer in
check the parent div has the div with child class
You can control your divs if it is empty using .question:empty
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
}
body{
min-height: 100vh;
display: grid;
place-content: center;
font-family: Arial, Helvetica, sans-serif;
background-color: bisque;
}
.group{
display: flex;
flex-direction: row;
gap:1rem;
}
.question{
width: 5rem;
height: 5rem;
display: grid;
/* display:none; you can change this */
place-content: center;
background-color: aquamarine;
}
.question:empty {
display:block;
background-color: brown;
}
<div class="group">
<div class="question">
<p>Question</p>
</div>
<div class="question"></div>
<div class="question">
<p>Question</p>
</div>
</div>
EDIT AFTER COMMENT
The thing that you need is about selecting parent element. There is one actually which is :has pseudo class but still experimental. But i create a scenerio for you.
While you are applying inline style = display: none; in .question divs you can also apply them type="hidden"
and others which have question inside(visible divs) type:not-hidden
<div class="group">
<div class="question" style="display: none" type="hidden"></div>
<div class="question" type="not-hidden"></div>
<div class="question" style="display: none" type="hidden" ></div>
</div>
<div class="group">
<div class="question" style="display: none" type="hidden"></div>
<div class="question" style="display: none" type="hidden"></div>
<div class="question" style="display: none" type="hidden"></div>
</div>
Then u can use
.group{
display:none;
}
.group:has(div[type="not-hidden"]) {
display: block;
}
kind of selector(.group:has(div[type="not-hidden"])) to get the parent which has a child div have type="not-hidden"; then you can apply parent a display:block to get visible.
:has pseudo class only supported in Safari. Maybe you can try to check this in Safari.
https://developer.mozilla.org/en-US/docs/Web/CSS/:has

Adjust letter spacing to size of container [duplicate]

This question already has answers here:
Auto-size dynamic text to fill fixed size container
(21 answers)
Closed 2 years ago.
I am trying to create the following effect
How do I make it so the letter spacing of the bottom text makes to the end of the container?
HTML
<section class="nav">
<div class="logo">
<p id="top">Reagan</p>
<p id="bottom">Clayton</p>
</div>
</section>
I can offer such a solution:
.logo {
width: 300px;
}
#bottom {
display: flex;
justify-content: space-between;
}
#bottom_two {
display: flex;
justify-content: space-between;
text-transform: uppercase;
}
#bottom_three {
display: flex;
justify-content: space-between;
text-transform: uppercase;
color: blue;
}
<section class="nav">
<div class="logo">
<p id="top">Reagan</p>
<p id="bottom"><span>C</span><span>l</span><span>a</span><span>y</span><span>t</span><span>o</span><span>n</span></p>
<p id="bottom_two"><span>C</span><span>l</span><span>a</span><span>y</span><span>t</span><span>o</span><span>n</span></p>
<p id="bottom_three"><span>C</span><span>l</span><span>a</span><span>y</span><span>t</span><span>o</span><span>n</span></p>
</div>
</section>
Simple solution: just need to use letter-spacing css property.
To see output click on button "Run code snippet".
#logoText{
font-size:45px;
text-transform: uppercase;
margin-bottom:0;
padding-bottom:0
}
#sologunText{
font-size:20px;
letter-spacing: 30px;
margin-top:5px;
padding-top:0
}
<section class="nav">
<div class="logo">
<p id="logoText">Logo Text</p>
<p id="sologunText">Sologun</p>
</div>
</section>

Responsive images with Flex? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm super new to the world of coding, so please bear with me ;)
I want to know if I could use a flexbox around an image to make it responsive rather than the "img-responsive" class?
I would imagine this would also be the better option for resizing a box that has an image and text in it?
B.
Responsive image manipulation with flex properties
For production level code we have to add CSS Reset Code
.container {
max-width: 1200px;
display: flex;
align-items: center;
-webkit-justify-content: center;
/* Safari */
justify-content: center;
}
.item {
padding: 10px;
}
img {
width: 100%;
height: auto;
display: block;
}
<div class="container">
<div class="item">
<img src="http://i.imgur.com/EvzEpEi.jpg" alt="image1">
</div>
<div class="item">
<img src="http://i.imgur.com/EvzEpEi.jpg" alt="image2">
</div>
<div class="item">
<img src="http://i.imgur.com/EvzEpEi.jpg" alt="image3">
</div>
<div class="item">
<img src="http://i.imgur.com/EvzEpEi.jpg" alt="image4">
</div>
</div>

Using CSS, how to add a pseudo element before every odd child element that is "outside" of that child element?

I want to create a grid with two columns whose width will be equal. My base HTML code looks like this:
<div class="linkgrid">
<div class="gridentry">
Loooooooooooooong
</div>
<div class="gridentry">
Short
</div>
<div class="gridentry">
Meeeedium
</div>
</div>
In this example, the first and the second gridentry should lie in the the first row. The thrid gridentry should lie in the second row. All gridentrys should have the same width.
~~~
I came up with a solution that uses a CSS table. However, to make sure the row "breaks" after every second cell, it currently requires non-semantic elements to force these "row breaks":
.linkgrid {
display: table;
border-spacing: 2px;
table-layout: fixed;
width: 50%;
}
.gridentry {
display: table-cell;
background-color: red;
padding: 5px;
text-align: center;
}
.gridentry a {
color: white;
}
.THIS-SHOULD-BE-A-PSEUDO-ELEMENT-BEFORE-EVERY-ODD-CHILD {
/* I imagine a selector that looks somewhat like this:
.linkgrid .gridentry:nth-child(odd):outsidebefore {
*/
display: table-row;
}
<div class="linkgrid">
<span class="THIS-SHOULD-BE-A-PSEUDO-ELEMENT-BEFORE-EVERY-ODD-CHILD"></span>
<div class="gridentry">
Loooooooooooooong
</div>
<div class="gridentry">
Short
</div>
<span class="THIS-SHOULD-BE-A-PSEUDO-ELEMENT-BEFORE-EVERY-ODD-CHILD"></span>
<div class="gridentry">
Meeeedium
</div>
</div>
Is there a way to remove my <span>s from my HTML (because they do not have any semantics) and use a clever CSS selector that adds them as pseudo elements at the right positions instead?
I do know that :before will "create" a pseudo-element within the selected element. Is there a non-JavaScript, CSS-only way to add a pseudo-element outside of the selected element like required in this example?
Another edit: For all those familiar with the Chrome developer tools, I want my result to look somewhat like this in the DOM tree:
<div class="linkgrid">
::outsidebefore
<div class="gridentry">
Loooooooooooooong
</div>
<div class="gridentry">
Short
</div>
::outsidebefore
<div class="gridentry">
Meeeedium
</div>
</div>
...where the ::outsidebefore pseudo-elements should have the CSS property display: table-row;.
Update 2016-01-04: While this specific question remains unanswered, my original problem was solved another way: https://stackoverflow.com/a/34588007/1560865
So please only post replies to this question that answer precisely the given question.
Display Level 3 introduces display: contents:
The element itself does not generate any boxes, but its children and
pseudo-elements still generate boxes as normal. For the purposes of
box generation and layout, the element must be treated as if it had
been replaced with its children and pseudo-elements in the document
tree.
Then, you can:
Wrap each cell in a container element
Set display: contents to those containers
Add ::before or ::after pseudo-elements to those containers
The result will look like as if the pseudo-elements were added to the cell, but outside it.
.wrapper {
display: contents;
}
.wrapper:nth-child(odd)::before {
content: '';
display: table-row;
}
.linkgrid {
display: table;
border-spacing: 2px;
table-layout: fixed;
width: 50%;
}
.wrapper {
display: contents;
}
.wrapper:nth-child(odd)::before {
content: '';
display: table-row;
}
.gridentry {
display: table-cell;
background-color: red;
padding: 5px;
text-align: center;
}
.gridentry a {
color: white;
}
<div class="linkgrid">
<div class="wrapper">
<div class="gridentry">
Loooooooooooooong
</div>
</div>
<div class="wrapper">
<div class="gridentry">
Short
</div>
</div>
<div class="wrapper">
<div class="gridentry">
Meeeedium
</div>
</div>
</div>
Note display: contents is not widely supported yet, but works on Firefox.
The most straightforward way is using an actual table structure. That is, one table divided into rows, in which the entries sit.
Also, you had width:50% on the table, but I believe from the question text that you meant every table cell to be 50% wide, rather than the table taking up 50% of the window width; so I corrected that.
.linkgrid {
display: table;
border-spacing: 2px;
}
.gridrow { /* new */
display: table-row;
}
.gridentry {
display: table-cell;
background-color: red;
padding: 5px;
text-align: center;
width: 50%; /* moved */
}
.gridentry a {
color: white;
}
<div class="linkgrid">
<div class="gridrow">
<div class="gridentry">
Loooooooooooooong
</div>
<div class="gridentry">
Short
</div>
</div>
<div class="gridrow">
<div class="gridentry">
Meeeedium
</div>
</div>
</div>

Is this layout achievable with flexbox? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I've been looking around at flex examples and having a go myself. I don't think this is possible with flexbox, but I thought I'd check before giving up on it.
The layout can be seen here:
All three elements are in the same parent div and unforntunately I'm stuck with this HTML structure so my options are limited. Sorry about the vague title. I couldn't really articulate the layout in words.
Thanks.
Since some kind soul has seen fit to answer...here's my version...no extra HTML required.
Codepen Demo
.wrapper {
width: 50%;
margin: 25px auto;
height: 300px;
border: 1px solid grey;
justify-content: space-between;
align-content: space-between;
display: flex;
flex-wrap: wrap;
flex-direction: column;
}
.box {
width: 49.5%;
background: #000;
}
.left {
flex: 0 0 100%;
/* equals height */
background: lightblue;
}
.right.top {
flex: 0 0 39%;
/* equals height */
}
.right.bottom {
flex: 0 0 39%;
/* equals height */
}
<div class="wrapper">
<div class="box left"></div>
<div class="box right top"></div>
<div class="box right bottom"></div>
</div>
As I'm trying to learn flex myself I thought I'd give this a go and came up with the following (otherwise this question should be closed for being too broad)
.container {display:flex;}
.column {flex:1;}
.row {flex:1; background-color:black;}
#outer {flex-direction:row; height:250px;} /* height for example purpose only */
#left {background-color:black; margin-right:20px;}
#right {flex-direction:column;}
#top {margin-bottom:50px;}
<div class="container" id="outer">
<div class="column" id="left"></div>
<div class="column container" id="right">
<div class="row" id="top"></div>
<div class="row" id="bottom"></div>
</div>
</div>

Resources