Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all block level elements with css

Is there an easy way to select all block level elements with CSS?

I want to put am 1.5 em margin between all block level elements in the main content area of my site

Right now I have code like:

#wrapper .content p, #wrapper .content ul, #wrapper .content div, #wrapper .content ol, #wrapper .content blockquote, #wrapper .content table {margin-top: 1.5em;}

#wrapper .content p:first-child, #wrapper .content ul:first-child, #wrapper .content div:first-child, #wrapper .content ol:first-child, #wrapper .content blockquote:first-child, #wrapper .content table:first-child {margin-top: 1.5em;}

Which is a royal pain in the rear to read or maintain...

I would like to replace it with something like:

#wrapper .content *:block + *:block {margin-top: 1.5em;}

Is this possible?

I can't use * + * because that will also catch inline elements, table cells, etc, and I don't want random margins being applied in the middle of paragraphs. I also can't use #wrapper .content > * because then it won't get nested div etc.

like image 798
Jay Irvine Avatar asked Apr 08 '15 05:04

Jay Irvine


People also ask

How do you select all elements in CSS?

The * selector selects all elements. The * selector can also select all elements inside another element (See "More Examples").

How do you select multiple elements in CSS?

When you group CSS selectors, you apply the same styles to several different elements without repeating the styles in your stylesheet. Instead of having two, three, or more CSS rules that do the same thing (set the color of something to red, for example), you use a single CSS rule that accomplishes the same thing.

What are the block level elements in CSS?

Block level elements take up as much space as possible by default. Each block level element will start a new line on the page, stacking down the page. In addition to stacking vertically, block level elements will also take up as much horizontal space as possible. The p element is an example of a block level element.


2 Answers

This is not possible with CSS; you can't select an element based on one of its CSS properties. You'll need to use JavaScript to select all elements with something like getComputedStyle or something similar, and then run some script logic based on what that finds.

The closest thing you can get to in CSS is selecting HTML attributes; things like href or title.

like image 65
TylerH Avatar answered Oct 14 '22 21:10

TylerH


:not(a):not(b):not(label):not(form):not(abbr):not(legend):not(address):not(link):not(area):not(mark):not(audio):not(meter):not(b):not(nav):not(cite):not(optgroup):not(code):not(option):not(del):not(q):not(details):not(small):not(dfn):not(select):not(command):not(source):not(datalist):not(span):not(em):not(strong):not(font):not(sub):not(i):not(summary):not(iframe):not(sup):not(img):not(tbody):not(input):not(td):not(ins):not(time):not(kbd):not(var) {border:1px solid red;}

not sure if I missed an element, but you get the idea. I think this should work.

like image 40
jakov Avatar answered Oct 14 '22 21:10

jakov