Is it possible to address the first element in a div when you don't know what the first element is.
I have for example two different divs
<div class="templateOne">
<h1>Header 1</h1>
</div>
<div class="templateOne">
<h2>Header 2</h2>
</div>
That I can then say
.templateOne > * {
margin-top: 0em;
}
or something like that.
If you want to use adress the first child element, you can use the :first-child or the :nth-child(1) pseudo-selector.
.templateOne :first-child {
color: red;
}
<div class="templateOne">
<h1>Header 1</h1>
<p>Content 1</p>
</div>
<div class="templateOne">
<h2>Header 2</h2>
<p>Content 2</p>
</div>
If you want to address only the first element with a specific class name you can use :first-of-type or nth-of-type(1):
.templateOne:first-of-type {
color: red;
}
<div class="templateOne">
<h1>Header 1</h1>
<p>Content 1</p>
</div>
<div class="templateOne">
<h2>Header 2</h2>
<p>Content 2</p>
</div>
> child combinator
* universal selector
:first-child
.templateOne > *:first-child {
margin-top: 0em;
}
I have a fixed table-layout div parent with two horizontal table-cell children.
On the left cell, I just simply put in some text. However, when the right cell has a tall image, the text on the left cell is pined to the bottom no matter what html padding & margin styles I put in. When the right cell has some multi-line text, the text on the left cell is placed on top.
I want the text from the left cell to stay on top all the time. Where am I missing?
html
<div class="row-post">
<div class="col-cell col-fixed" style="margin-top: 0px;padding-top: 0px;">
<p>1</p>
</div>
<div class="col-cell pl-3">
<img src="https://i.kym-cdn.com/photos/images/facebook/001/295/524/cda.jpg" style="height:300px">
</div>
</div>
<hr>
<div class="row-post">
<div class="col-cell col-fixed">
<p>1</p>
</div>
<div class="col-cell pl-3">
<p>Some long text</p>
<p>Some long text</p>
<p>Some long text</p>
<p>Some long text</p>
<p>Some long text</p>
<p>Some long text</p>
<p>Some long text</p>
<p>Some long text</p>
<p>Some long text</p>
<p>Some long text</p>
<p>Some long text</p>
</div>
</div>
css
.row-post {
table-layout: fixed;
width: 100%;
word-break: break-all;
}
.row-post .col-cell {
display:table-cell;
}
.row-post .col-cell img {
max-width: 100%;
}
.col-fixed {
width: 26px;
}
js fiddle if you want to play around
https://jsfiddle.net/f1zr6o93/
js fiddle snip
For table render types, you can use:
vertical-align: top;
on any "table cell".
Edited fiddle
For reference, there's a fuller explanation of when vertical align applies in this other SO article.
How can I select the first of the highest h* elements present in a DOM?
Something like
(h1, h2, h3, h4, h5, h6):first-of-ordered-set
I.e. if a DOM tree has, in this order, h2, h3, h1, h2, h1, it would select the first h1;
and if the DOM has h3, h3, h2, h2, h4, it would select the first h2.
Let's assume h* elements are not nested.
I suspect CSS doesn't have that power, right?
Somethink potentially usable: https://css-tricks.com/extremely-handy-nth-child-recipes-sass-mixins/
Edit: Why I want it: A CMS system takes this "first top heading" as a title of the document (post, page, ...). But it leaves it in the page. And then it shows the title twice - once as the post title and once in the body. JavaScript is removed. The top h* level may differ.
I found something. CSS can't do it. Yet.
W3C is drafting new features:
.post-content:has(h1, h2, h3, h4, h5, h6)
Together with :not(), this will allow what I need:
.post-content:has(h1) h1:first-of-kind,
.post-content:not(:has(h1)) h2:first-of-kind,
.post-content:not(:has(h1,h2)) h3:first-of-kind,
...
There's a catch - the :not() currently can only have a single "simple selector". If it supported more, then it would be achievable even without :has.
Greetings to the future readers, I hope it worked out.
For now, I am leaving this open, maybe someone will figure out with CSS 3.1.
The main issue is that we don't have previous selector in CSS. For example, we can get the first h2 but if later we find a h1 we cannot have a selector to go backwards.
Here is the best you can do with CSS. You can hide all the elements after the needed one (so the element you want is the last visible one) but you cannot do the same with the previous elements.
h1:first-of-type,
h2:first-of-type,
h3:first-of-type,
h4:first-of-type {
color:red;
}
h1:first-of-type ~ * {
display:none;
}
h2:first-of-type ~ *:not(h1) {
display:none;
}
h3:first-of-type ~ h4 {
display:none;
}
.container {
border:1px solid;
}
<div class="container">
<h3>text 3</h3>
<h1>text 1*</h1>
<h2>text 2</h2>
<h1>text 1</h1>
</div>
<div class="container">
<h3>text 3</h3>
<h2>text 2*</h2>
<h2>text 2</h2>
<h4>text 1</h4>
</div>
<div class="container">
<h3>text 3</h3>
<h3>text 2</h3>
<h2>text 2*</h2>
<h4>text 1</h4>
</div>
<div class="container">
<h1>text 3*</h1>
<h3>text 2</h3>
<h2>text 2</h2>
<h4>text 1</h4>
</div>
You may then combine with a small JS code to keep only the needed element:
$('h3:first-of-type').prevAll('h4').hide();
$('h2:first-of-type').prevAll('*:not(h1)').hide();
$('h1:first-of-type').prevAll('*').hide();
h1:first-of-type,
h2:first-of-type,
h3:first-of-type,
h4:first-of-type {
color:red;
}
h1:first-of-type ~ * {
display:none;
}
h2:first-of-type ~ *:not(h1) {
display:none;
}
h3:first-of-type ~ h4 {
display:none;
}
.container {
border:1px solid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<h3>text 3</h3>
<h1>text 1*</h1>
<h2>text 2</h2>
<h1>text 1</h1>
</div>
<div class="container">
<h3>text 3</h3>
<h2>text 2*</h2>
<h2>text 2</h2>
<h4>text 1</h4>
</div>
<div class="container">
<h3>text 3</h3>
<h3>text 2</h3>
<h2>text 2*</h2>
<h4>text 1</h4>
</div>
<div class="container">
<h1>text 1*</h1>
<h3>text 2</h3>
<h2>text 2</h2>
<h4>text 1</h4>
</div>
I used to make the element hidden but you can do the same with other styles:
$('h3:first-of-type').prevAll('h4').addClass('initial');
$('h2:first-of-type').prevAll('*:not(h1)').addClass('initial');
$('h1:first-of-type').prevAll('*').addClass('initial');
h1:first-of-type,
h2:first-of-type,
h3:first-of-type,
h4:first-of-type {
color:red;
font-family:cursive;
font-style:italic;
}
h1:first-of-type ~ *,
h2:first-of-type ~ *:not(h1),
h3:first-of-type ~ h4,
h1.initial,h2.initial,h3.initial,h4.initial{
color:initial;
font-family:initial;
font-style:initial;
}
.container {
border:1px solid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<h3>text 3</h3>
<h1>text 1*</h1>
<h2>text 2</h2>
<h1>text 1</h1>
</div>
<div class="container">
<h3>text 3</h3>
<h2>text 2*</h2>
<h2>text 2</h2>
<h4>text 1</h4>
</div>
<div class="container">
<h3>text 3</h3>
<h3>text 2</h3>
<h2>text 2*</h2>
<h4>text 1</h4>
</div>
<div class="container">
<h1>text 1*</h1>
<h3>text 2</h3>
<h2>text 2</h2>
<h4>text 1</h4>
</div>
I have a div with the below content.
<div class="outsideDiv">
<div>
<p>Some text</p>
</div>
<div class="imgContainer">
<img />
</div>
</div>
The imgContainer is right of the div which contains the p
The outsideDiv doesn't have a pre-defined height. It adjusts to the height of the p element. So if the p element has 500px height then the height of outside div will be 500px. My website is responsive, therefore the height of p (and so the height of outsideDiv) it isn't fixed and it changes depending on screen. This section works perfect.
I want the height of imgContainer to be equal with the height of the outsideDiv.
I have tried with position absolute and relative and it works but I don't want to use this way. Also I tried inherit height but no luck.
.imgContainer {
background: rgba(0,0,0,.5);
position: absolute;
width:100%;
height:100%;
bottom: -100%;
}
.outsideDiv {
position: relative;
}
<div class="outsideDiv">
<div>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p><p>Some text</p><p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
</div>
<div class="imgContainer">
<img />
</div>
</div>
You can achieve this using flexbox.
body{
margin:0;
}
.outsideDiv{
background:red;
display:flex;
}
.outsideDiv > div{
float:left;
width:50%;
}
.outsideDiv p{
height:500px;
}
.imgContainer{
background:blue;
float:right;
width:50%;
height:100%:
}
<div class="outsideDiv">
<div>
<p>Some text</p>
</div>
<div class="imgContainer">
<img />
</div>
</div>
Here is Demo : https://jsfiddle.net/r73kh3bo/
If you don't want to use Flexbox because of browser support, here's another example: https://jsfiddle.net/r73kh3bo/1/
Basically you can just create a parent div with display: table and each child inside with display: table-cell will have the same height.
I am trying to build an interface with Bootstrap 3 which looks like this:
--------Fieldset-------- -----Fieldset-----
Col 1 Col 2 Some data
Some data Some data Some data
Some data Some data Some data
Some data Some data Some data
------------------------Fieldset------------------------
Some large <textarea>
I am not sure how to go about accomplishing this, though. My concerns are the multiple columns in one fieldset, and the bottom fieldset spanning the length of the two fieldsets above it. I know to make stacked fieldsets all I need to do is nest them, but that then restricts the length of the children fieldsets.
Is what I'm doing even possible? Is it going to get ugly like I feel like it will?
Thanks in advance for any assistance!
I should have provided this anyway - sorry about that. Here's some markup that (should) get you rolling.
HTML
<div class="row">
<div class="col-md-6">
<div class="mycol-1">
<p>Fieldset</p>
<div class="row">
<div class="col-md-6">
<p>Col 1</p>
<p>Some data</p>
<p>Some data</p>
<p>Some data</p>
</div>
<div class="col-md-6">
<p>Col 2</p>
<p>Some data</p>
<p>Some data</p>
<p>Some data</p>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="mycol-2">
<p>Fieldset</p>
<p>Some data</p>
<p>Some data</p>
<p>Some data</p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="mycol-3">
<p>Fieldset</p>
<p>Some large textarea</p>
</div>
</div>
</div>
CSS:
.mycol-1 { background-color: red; }
.mycol-2 { background-color: pink; }
.mycol-3 { background-color: orange; }
I only used the css as a way to show the boundires. You will probably want to at least rename the classes (or delete them all together). Hope this helps!