I'm trying to get a style to indent like the following
H1
Content here
H2
Any content after the H2, paragraphs, images, etc
H3
Any content after the H2, paragraphs, images, etc
H2
content
H1 another top level heading
etc
Sample html:
<h1>heading 1</h1>
<p>content</p>
<h2>heading 2</h2>
<p>content</p>
<p>content</p>
<h3>heading 2</h3>
<p>content</p>
<img src="something.png" />
<p>content</p>
<h1>heading 1</h1>
<p>content</p>
I've tried the following
h2, h2 + * {
margin-left: 30px;
}
h3, h3 + * {
margin-left: 60px;
}
But this only sets a margin on the first element after the heading, I need all subsequent tags, until the next one.
Any questions please ask.
I'd like to point out that I can't rewrite the hml as I'm applying this to a website where many pages have already been created.
Some sample code https://codepen.io/User1972/pen/WZyKNR
I would do this something like this: https://codepen.io/andrasadam93/pen/dVKedR
This way you can easily scale it for further indentations, modify each and every part by adding id's or further classes and get your desired result in later specific cases as well.
.first{
margin-left:0;
}
.second{
margin-left:30px;
}
.third{
margin-left:60px;
}
<div class="first">
<h1>Hello</h1>
<p>Some content here</p>
<div class="second">
<h2>Hello second</h2>
<p>Also some content here</p>
<div class="third">
<h3>Hello third</h3>
<p>Also some content here</p>
</div>
<p>Some further content in the second indentation</p>
</div>
<p>This is also some content in the first indentation</p>
</div>
Why not just use a list <ul><li>...
ul, li { list-style: none; }
<ul>
<li>
<h1>hello h1</h1>
<p>Lorem ipsum dolor sit amet</p>
<ul>
<li>
<h2>hello h2</h2>
<p>Lorem ipsum dolor sit amet</p>
<ul>
<li>
<h3>hello h3</h3>
<p>Lorem ipsum dolor sit amet</p>
</li>
</ul>
</li>
</ul>
</li>
<li>
<h1>hello h1</h1>
<p>Lorem ipsum dolor sit amet</p>
</li>
</ul>
or if you do not want to use a list you can achieve the same with one single CSS rule and class like so:
.cheating-list .cheating-list {
margin-left: 40px;
}
<div class="cheating-list">
<h1>hello h1</h1>
<p>Lorem ipsum dolor sit amet</p>
<div class="cheating-list">
<h2>hello h2</h2>
<p>Lorem ipsum dolor sit amet</p>
<div class="cheating-list">
<h3>hello h3</h3>
<p>Lorem ipsum dolor sit amet</p>
</div>
</div>
</div>
<div class="cheating-list">
<h1>hello h1</h1>
<p>Lorem ipsum dolor sit amet</p>
</div>
The trick is do add the wrap <div class="cheating-list"> inside itself.
UPDATED CODE
With your sample HTML (which was added much later) something like this will do the trick (but if possible I would change the markup to one of the above examples)
h1,
h1 ~ *,
h2 ~ h1,
h2 ~ h1 ~ *,
h3 ~ h1,
h3 ~ h1 ~ * {
margin-left: 0px;
}
h2,
h2 ~ *,
h1 ~ h2,
h1 ~ h2 ~ *:not(h1):not(h3) {
margin-left: 40px;
}
h3,
h3 ~ *,
h1 ~ h3,
h1 ~ h3 ~ *:not(h1):not(h2) {
margin-left: 80px;
}
<h1>heading 1</h1>
<p>content</p>
<h2>heading 2</h2>
<p>content</p>
<p>content</p>
<h3>heading 3</h3>
<p>content</p>
<img src="something.png" />
<p>content</p>
<h1>heading 1</h1>
<p>content</p>
<h1 class="entry-title">Inputs</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean porttitor, lacus eget egestas pharetra.</p>
<h2><span id="Columns">Columns</span></h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean porttitor, lacus eget egestas pharetra.</p>
<ul>
<li>fghgfdh</li>
</ul>
<img src="http://via.placeholder.com/350x50" />
<h3><span id="another">another heading</span></h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean porttitor, lacus eget egestas pharetra.</p>
<h1 class="entry-title">2nd Heading One</h1>
<p>This should not have a margin</p>
<h2><span id="Columns">Columns XXXX</span></h2>
<p>This margin is too large. It has the h3 margin applied to it</p>
<h3><span id="another">another h3 heading</span></h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean porttitor, lacus eget egestas pharetra.</p>
Instead of using margin, we can achieve this style easily by using Un-Ordered list.
ul {
list-style-type: none;
}
h1,h2,h3,h4,h5,h4 {
margin: 0px;
}
<ul>
<li>
<h1>H1</h1>
<p>Content here</p>
<ul>
<li>
<h2>H2</h2>
<p>Any content after the H2, paragraphs, images, etc</p>
<ul>
<li>
<h3>H3</h3>
<p>Any content after the H2, paragraphs, images, etcContent here</p>
</li>
</ul>
</li>
<li>
<h2>H2</h2>
<p>Any content after the H2, paragraphs, images, etc</p>
</li>
</ul>
</li>
<li>
<h1>H1</h1>
<p>Content here</p>
<ul>
<li>
<h2>H2</h2>
<p>Any content after the H2, paragraphs, images, etc</p>
<ul>
<li>
<h3>H3</h3>
<p>Any content after the H2, paragraphs, images, etcContent here</p>
</li>
</ul>
</li>
<li>
<h2>H2</h2>
<p>Any content after the H2, paragraphs, images, etc</p>
</li>
</ul>
</li>
</ul>
Here I have attached my codepen link.
No need for extra classes. For any correctly structured HTML document following CSS is enough:
ul, ol {
list-style-position: inside;
} // This is necessary if you use lists. Otherwise just delete this rule.
h1 ~ *:not(h1) {
margin-left: 1.2rem;
}
h2 ~ *:not(h1, h2) {
margin-left: 2.4rem;
}
h3 ~ *:not(h1, h2, h3) {
margin-left: 3.6rem;
}
Related
This question already has answers here:
How can I select the last element with a specific class, not last child inside of parent?
(8 answers)
Closed 6 years ago.
I am trying to select the :last-child of a series of wordpress posts (with the same class) in my style.css file.
Here is a simplified version of my document structure:
<div id="container">
<div id="content">
<!--start .post-->
<div id="post-33" class="hentry p1 post">
<h2 class="entry-title">Post 1</h2>
<div class="entry-content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
</div>
<!--end .post -->
<!--start .post-->
<div id="post-24" class="hentry p1 post">
<h2 class="entry-title">Post 2</h2>
<div class="entry-content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
</div>
<!--end .post -->
<!--start .post-->
<div id="post-24" class="hentry p1 post">
<h2 class="entry-title">Post 3</h2>
<div class="entry-content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
</div>
<!--end .post -->
<div id="nav-below" class="navigation">
<div class="nav-previous"></div>
<div class="nav-next"></div>
</div>
</div><!-- #content -->
</div><!-- #container -->
I have tried this and although it works fine with the :first-child selector it does not work at all with the :last-child.
.post .entry-content {
background-color: pink;
border-bottom: 2px solid;
}
.post:first-child .entry-content {
background-color: red;
border-bottom: 0px solid;
}
.post:last-child .entry-content {
background-color: yellow;
border-bottom: 0px solid;
}
Any thoughts?
Here is the live version of my site: http://dev.visualvisual.com/
Use nth-of-type and nth-last-of-type.
.post:nth-of-type(1) .entry-content {
background-color: red;
border-bottom: 0px solid;
}
.post:nth-last-of-type(2) .entry-content {
background-color: yellow;
border-bottom: 0px solid;
}
first-child and last-child, are, like the name suggests, for operating on children, so you'd want to use #content:last-child to find last element (not necessarily .post though).
Codepen
I would like to target 'Person 2' without using nth-child/nth-of-type selector however I can't seem to narrow down using adjacent sibling selectors at the moment:
https://jsfiddle.net/fk5fxbm4/4/
<div id="myID">
<h2>Title</h2>
<blockquote title="quote">
<p>Statement 1</p>
<cite>Person 1</cite>
</blockquote>
<blockquote>
<p>Statement 2</p>
<cite>Person 2</cite>
</blockquote>
<blockquote>
<p>Statement 3</p>
<cite>Person 2</cite>
</blockquote>
</div>
blockquote[title="quote"] cite {
color: red;
}
Above code targets Person 1 however "cite + cite" or "blockquote[title="quote"] cite + cite" does not work. What am I missing here?
Also, could you explain how would I go about changing color of the last <em>? I have tried nth-child/nth-of-type selector however it selects multiple <em> each time:
https://jsfiddle.net/zs58xbch/7/
<main>
<h2>Paragraph 1</h2>
<p><em>Lorem ipsum dolor sit amet</em>, Lorem ipsum dolor sit amet. <em>Lorem ipsum dolor sit amet</em>.</p>
<h2>Paragraph 2</h2>
<p>Lorem ipsum dolor sit amet. <em>Lorem</em> ipsum dolor sit amet.
<h2>Paragraph 3</h2>
<p><em>Lorem </em> ipsum dolor sit amet.</p>
<p><em>Change my color!</em>
</main>
em:last-of-type {
color: red;
}
You want to select person2 right ? Try this simple code.
Just select adjacent blockquoteand style the cite.
blockquote[title="quote"] + blockquote cite{
color: green;
}
And this code use for targeting the last child.
blockquote:last-child cite{
color: cyan; //select last child
}
SNIPPET
blockquote[title="quote"] cite {
color: red;
}
blockquote[title="quote"] + blockquote cite{
color: green; //select 2nd child
}
blockquote:last-child cite{
color: cyan; //select last child
}
<div id="myID">
<h2>Title</h2>
<blockquote title="quote">
<p>Statement 1</p>
<cite>Person 1</cite>
</blockquote>
<blockquote>
<p>Statement 2</p>
<cite>Person 2</cite>
</blockquote>
<blockquote>
<p>Statement 3</p>
<cite>Person 2</cite>
</blockquote>
</div>
Answer for first question
blockquote[title="quote"]+blockquote cite { color: red; }
Updated fiddle https://jsfiddle.net/fk5fxbm4/6/
Answer for second question
p:last-of-type em:last-of-type { color: red; }
Updated fiddle https://jsfiddle.net/zs58xbch/9/
Nth selectors work within the current nesting context. So you'd need to go a level up to target the last grouping, like blockquote:last-of-type em {}
As for the first question, you can't target only the second of something without nth selectors. I suggest adding a class.
[edit] okay you can hack together an nth-child(2) approximation as Andrei commented on this answer. I still recommend using a class though.
I'm currently creating a web page with Bootstrap and I'm using columns. My page looks like that:
I'd like to center the last column (in the second row) but the page is dynamic and I don't know how many containers there are.
I found this two solutions on Google:
1) Add this to my css:
.col-centered{
float: none;
margin: 0 auto;
}
2) Add this to the class tag attribute
col-lg-offset-4
But both solutions look like this:
That is not what i want. I want it to look like this:
How can i achieve this?
Bootstrap's columns are floating by default with css float property. With float we can't middle align columns. However with display: inline-block we can. All we need is to remove float from styles of columns and change them to inline-block with vertical-align: middle and you will get what you want. But don't forget to remove extra space that comes with inline-block.
Here is the trick.
.wrapper {
background: green;
padding: 20px 0;
}
.box {
border-radius: 10px;
margin-bottom: 30px;
background: #fff;
padding: 10px;
color: #000;
}
.center-align {
letter-spacing: -4px;
text-align: center;
font-size: 0;
}
.center-align [class*='col-'] {
display: inline-block;
vertical-align: top;
letter-spacing: 0;
font-size: 14px;
float: none;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="wrapper">
<div class="container center-align">
<div class="row">
<div class="col-xs-4">
<div class="box">
<p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p>
</div>
</div>
<div class="col-xs-4">
<div class="box">
<p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p>
</div>
</div>
<div class="col-xs-4">
<div class="box">
<p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p>
</div>
</div>
<div class="col-xs-4">
<div class="box">
<p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p>
</div>
</div>
</div>
</div>
<div class="container center-align">
<div class="row">
<div class="col-xs-4">
<div class="box">
<p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p>
</div>
</div>
<div class="col-xs-4">
<div class="box">
<p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p>
</div>
</div>
<div class="col-xs-4">
<div class="box">
<p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p>
</div>
</div>
<div class="col-xs-4">
<div class="box">
<p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p>
</div>
</div>
<div class="col-xs-4">
<div class="box">
<p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</p>
</div>
</div>
</div>
</div>
</div>
Note: Setting font-size: 0; letter-spacing: -4px on parent and applying parent's font-size: 14px; letter-spacing: 0 back on child elements will remove white space that comes with inline-block.
Bootstrap has built-in functionality to achieve the layout you are after, without the introduction of additional CSS rules. Simply use the .col-md-offset-* class:
Move columns to the right using .col-md-offset-* classes. These classes increase the left margin of a column by * columns. For example, .col-md-offset-4 moves .col-md-4 over four columns.
Your layout would end up looking similar to this:
.show-grid {
margin-bottom: 15px;
}
.your-custom-div {
height: 50px;
background-color: green;
color: white;
text-align: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<div class="row show-grid">
<div class="col-md-4">
<div class="your-custom-div">
.col-md-4
</div>
</div>
<div class="col-md-4">
<div class="your-custom-div">
.col-md-4
</div>
</div>
<div class="col-md-4">
<div class="your-custom-div">
.col-md-4
</div>
</div>
<div class="clearfix visible-md"></div>
</div>
<div class="row show-grid">
<div class="col-md-4 col-md-offset-4">
<div class="your-custom-div">
.col-md-4 .col-md-offset-4
</div>
</div>
<div class="clearfix visible-md"></div>
</div>
</div>
</body>
</html>
EDIT #1: For your requirement of not knowing how many columns you will be fetching from your database for the second row, another option would be to use a conditional during the output of the HTML to also output a .col-md-offset-4 class if the modulo of the number of items in your collection divided by the number of columns is equal to 1, or proceed as usual otherwise. In ASP.NET with Razor, this would look something like this (the example below is kept simple on purpose to demonstrate the proposed logic, it can be refactored to it's own HTML helper class, accounting for other column sizes as well):
#{
bool lastItemShouldBeCentered = Foo.Count % 3 == 1;
for (int i = 0; i < Foo.Count; i++)
{
bool isLastItem = i == Foo.Count - 1;
if (isLastItem && lastItemShouldBeCentered)
{
<div class="col-md-4 col-md-offset-4">
// Foo[i] content here
</div>
}
else
{
<div class="col-md-4">
// Foo[i] content here
</div>
}
}
}
EDIT #2: Looks like I misread your requirement. For 1 left-over column, this solution will suffice. For more, I would go with #Muhammad's answer.
You need to add the last block of text into a different row and change the "col-md-4" to "col-md-12".
<div class="row">
<div class="col-md-4"> // column 1
bla bla bla
</div>
<div class="col-md-4"> //column 2
bla bla bla
</div>
<div class="col-md-4"> // column 3
bla bla bla
</div>
</div>
<div class="row">
<center>
<div class="col-md-12"> //last column, also note I changed it to 12
bla bla bla
</div>
</center>
</div>
I have been trying to adjust a div height so it would push all other content below it gracefully and with out overlapping, without any success.
I have two message box where each one of them is inside a wrapper ( timeslot ) div that works as a container for each.
I have tried setting the floating on the timeslot, and clearing what afterwards without success
I have tried setting min-height and display: block; without success either
<div class='timeslot'>
<div class='message'>
<span class='header'>Header text</span>
<span>Some really long long text</span>
</div>
</div>
The goal is trying to fit everything within the boxes and push things around nicely.
Here's a fiddle
http://jsfiddle.net/nawar/qjDPp/
Thanks!
Here you go mate, I rewrote it for you:
http://jsfiddle.net/wAhnB/
<style>
.clear {clear:both}
.message-wrap {width:500px}
.message {width:150px; font-size:12px; background:#EDF6FB; border:2px solid #67C2EF; padding:5px;
-webkit-border-radius: 10px; -moz-border-radius: 10px; border-radius: 10px;}
.message, .circle, .message-time {float:left; margin-right:20px}
.message-time {background:#F9F9F9; padding:5px; width:150px}
</style>
<h1>Lorem ipsum</h1>
<div class="message-wrap">
<div class="message">
<h2>Message</h2>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur sed tortor. Integer aliquam adipiscing lacus. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur sed tortor. Integer aliquam adipiscing lacus.</p>
</div><!-- message -->
<img class="circle" src="http://i47.tinypic.com/2pt1lag.jpg" alt="" />
<div class="message-time">
<p><span class="date">2012-10-19</span><br />
08:31:38</p>
</div><!-- message-time -->
</div><!-- message-wrap -->
<br class="clear" />
<br />
<div class="response-wrap">
<div class="message-time">
<p><span class="date">2012-10-19</span><br />
08:31:38</p>
</div><!-- message-time -->
<img class="circle" src="http://i47.tinypic.com/2pt1lag.jpg" alt="" />
<div class="message">
<h2>Response</h2>
<p>This is a response</p>
</div><!-- message -->
</div><!-- response-wrap -->
I have a #comments element which contains .comment elements. I would like to have 5 vertical lines from left to right, each 1px in width, 100% height (till the end of the #comments element), with 20px between them and without images. I tried to do that myself, but my CSS-fu isn't that high. Any help would be much appreciated.
HTML:
<div id="comments">
<div class="comment level1">Lorem ipsum dolor sit amet</div>
<div class="comment level2">Lorem ipsum dolor sit amet</div>
<div class="comment level3">Lorem ipsum dolor sit amet</div>
</div>
CSS:
#comments {
width: 400px;
border: 1px solid black;
}
.comment {
margin: 10px 0;
}
.level1 {}
.level2 { margin-left: 20px; }
.level3 { margin-left: 40px; }
Demo.
Here's how I imagine it:
|[comment ]
| |[comment ]
| |[comment ]
| | |[comment]
Is there some reason you need to have all the divs as direct children of the outer parent div? If you nest the divs you can accomplish this very easily:
css:
div div {
border-left: 1px solid black;
padding-left:20px;
}
nested html
<div id="comments">
<div>Lorem ipsum dolor sit amet</div>
<div>Lorem ipsum dolor sit amet
<br/>
<div>Lorem ipsum dolor sit amet
<br/>
<div>Lorem ipsum dolor sit amet
<br/>
<div>Lorem ipsum dolor sit amet</div>
<div>Lorem ipsum dolor sit amet
<br/>
<div>Lorem ipsum dolor sit amet</div>
</div>
</div>
</div>
</div>
</div>
updated fiddle showing how it would look here nested down to 5 levels:
http://jsfiddle.net/webchemist/tuZB6/4/