CSS : align at a certain position - css

I know what I want but I'm not sure how to best express it, so please be forgiving.
I have numbered headlines in my page, and paragraphs following the headlines. I wish to align the text of the paragraphs the same as the start of the title of the headline, and the numbers of the headlines being aligned to the right, at for example 0.5em of the title of the headline.
Here is an example in monospace font:
1. Introduction
This is the beginning of the introduction.
1.1. Sub header
Another paragraph here and when it comes to having
another line, it is indented as the first one.
1.1.1. Sub-sub header
Notice how the headlines and paragraphs are exactly
aligned, whereas the numbers in the headlines are shifted
to the right ?
1.2. Sub header 2
I'm sure you get the picture...
What is the best way to achieve this in HTML/CSS ?
It would be trivial to use tables but I wish to do otherwise if there is a cleaner way.

This is how I would do it:
HTML:
<ul id="list">
<li>
<h2>Intro<em></em></h2>
<p>This is the beginning of the introduction.</p>
</li>
<li>
<h3>Sub header<em></em></h3>
<p>Another paragraph here and when it comes to having
another line, it is indented as the first one.</p>
</li>
<li>
<h4>Sub-sub header<em></em></h4>
<p>Notice how the headlines and paragraphs are exactly
aligned, whereas the numbers in the headlines are shifted
to the right ?</p>
</li>
<li>
<h3>Sub header 2<em></em></h3>
<p>I'm sure you get the picture...</p>
</li>
</ul>
CSS:
#list {
counter-reset: level1 0 level2 0 level3 0;
margin-left:50px;
}
#list h2,
#list h3,
#list h4 { margin-left:-50px; }
#list em { float:left; width:40px; padding-right:10px; text-align:right; }
#list h2 em:before {
counter-increment: level1 1;
content: counter(level1, decimal) ".";
}
#list h3 em:before {
counter-increment: level2 1;
content: counter(level1, decimal) "." counter(level2, decimal) ".";
}
#list h4 em:before {
counter-increment: level3 1;
content: counter(level1, decimal) "." counter(level2, decimal) "." counter(level3, decimal) ".";
}
Live demo: http://jsfiddle.net/9bkwQ/
Notice that:
There are no CSS classes set on the HTML elements - there is no need for that (Result: cleaner code)
The numbering is auto-generated via CSS counters which means that you don't have to update the numbering whenever you want to insert an item between other items.

I imagine you're using custom numbers? You're placing them there yourself without the use of ? I'd imagine so with that system.
Either way, I'd probably set up some divs to look like this:
Quick Note: You'd need to add clear:both to the main container to have them stack nicely.
<div style="display:inline-block; width:100%; clear:both")
<div style="float:left; margin-right:5px">
1.
</div>
<div style="float:left">
The first headline
</div>
</div>
Something like this would work fine and you could use your own styles to manipulate the structure/design, but again, I don't know if it's the absolute best way.

Check out a JSFiddle of it here: http://jsfiddle.net/uvQsR/2/
<div class="section">
<span class="number">1.1.</span>
<h4 class="heading">Sub header</h4>
<p> Another paragraph here and when it comes to having
another line, it is indented as the first one. </p>
</div>
with css
.section {
padding-left:40px;
}
.number{
text-align:right;
margin-left:-40px;
float:left;
width:40px;
}

You could do something like this, adjusting the width and margin as necessary (and depending on the depth/length of the header numbers)
<h2 class="number">1.</h2><h2 class="text">Introduction</h2>
<p>This is the beginning of the introduction.</p>
<h2 class="number">1.1</h2><h2 class="text">Sub header</h2>
<p>Another paragraph here and when it comes to having<br />
another line, it is indented as the first one.</p>
CSS
h2.number{display:inline-block; width:30px;}
h2.text{display:inline-block;}
p{margin-left:30px; margin-bottom:10px;}
http://jsfiddle.net/jasongennaro/VKP9Y/

Related

Style span only on the first line of div

I have the following html:
<div class="pp">TWO <span>WORDS</span>
</div>
And I want to separate them by "#" for example, but only if they are on the same line.
I don't want to use any JS, only CSS.
This is not working:
.pp{
width:20%;
}
.pp::first-line span:before{
content: " # "
}
And I couldn't come up with another idea.
Note that I want one and the same element to either have or not # depending on if it appears on the same lane. E.g.
TWO # WORDS
But
TWO
WORDS
Depending on the size of the window when the 20% can't fit the TWO # WORDS, the WORDS gets on a second line, and then I want the "#" to disappear
The most obvious solution would be to use two spans (text should in text tags anyway and not text nodes).
Then you can target the second child and use :before.
.pp {
width: 20%;
}
.pp span:nth-child(2):before {
content: '#';
}
<div class="pp">
<span>TWO</span>
<span>WORDS</span>
</div>
<div class="pp">
<span>ONE</span>
</div>

Target multiple specific elements that appear within a div of a certain class

I am sure this question has been asked before, but I have searched for awhile an can't find the answer-- so here goes:
lets say I have a div of a certain class, in this case we can call it .nyg-section-darkblue, and I want any H tag element to appear a certain color when it is nested in a div of this class, for example: this color:#19A5FF;
What is the most efficient way to accomplish this with CSS?
I have tried this:
.nyg-section-darkblue h6,h3,h4 {color:#19A5FF;}
but that appears to select H elements universally, and not just ones that are nested in "nyg-section-darkblue" class elements.
Anyone know how to fix this problem?
<style>
/*css*/
.nyg-section-darkblue h6,h3,h4 {color:#19A5FF !important;}
</style>
<!-- html-->
<div class="nyg-section-darkblue">
<h3>I want this to be color #19A5FF</h3>
<h4>I want this to be color #19A5FF</h4>
<h6>I want this to be color #19A5FF</h6>
</div>
<div class="nyg-section-white">
<h3>I dont want this to be #19A5FF</h3>
<h4>I dont want this to be #19A5FF</h4>
<h6>I dont want this to be #19A5FF</h6>
</div>
Try with
.nyg-section-darkblue h6, .nyg-section-darkblue h3, .nyg-section-darkblue h4 { ....
Use the CSS selector > to select children elements. You can select the children of .nyg-section-darkblue like this: nyg-section-darkblue > a.
Example follows:
.nyg-section-darkblue > a {
color: #19A5FF;
}
a {
color: red;
}
<div class="nyg-section-darkblue">
Home
About
Products
Contact
</div>
Home
About
Products
Contact
.nyg-section-darkblue h6,h3,h4 {color:#19A5FF;}
is same as
.nyg-section-darkblue h6 {color:#19A5FF;}
h3 {color:#19A5FF;}
h4 {color:#19A5FF;}
you have to target that specific header tags that are inside a specific div , you can use the '>' selector
here is a code example
.asd{
font-size:13px;
color:green;
}
h2{
font-weight:bold;
}
h4{
font-family:cursive;
}
.asd > h2{
color:red;
font-size:18px;
}
.asd h4{
color:cyan;
}
<div class="asd">
<h3>hello</h3>
<p>are you</p>
<h2> reading this</h2>
<div id="inner">
<h4> ahaa</h4>
</div>
</div>
Now the '>' selector targets the h2 and h3 elements who are the direct children of the parent class. here is a link to css selectors from w3schools http://www.w3schools.com/cssref/css_selectors.asp
hope this helps

Unable to align paragraphs to their respective images. (3 paragraphs in a row)

Image that describes my situation:
How does it currently look like:
**I have an issue with aligning the text to the images, under the Express Yourself Page.
I have tried setting the paragraphs to be of the same width as each other, based on the wrapper's total width.
The Pictures were able to float left successfully; with the images side by side. However, the text which is supposed to be centrally aligned with it's respective image aren't shifting.
When I experiment with the text, it's either all the text stays at the left or it's centralised; all in a row, below each other. How am I able to shift the respective paragraphs to be side by side each other, centrally aligned to their images?**
<Style>
#wrapper {
width:970px;
margin-left:auto;
margin-right:auto; /* Most crucial thing to align the whole page plus margin-left */
}
#section1 img, #section2 img, #section3 img, #section4 img {
max-width:100%;
float:left;
display:block;
padding-bottom:30px;
}
#section1, #section2, #section3, #section4 {
font:Arial, Helvetica, sans-serif;
font-size:
text-align:center;
}
p.title {
width:323px;
}
hgroup {
font:Arial, Helvetica, sans-serif;
font-size:1em;
font-weight:bold;
padding-top:24px; /*To give space inbetween image and Text*/
width:323px;
}
</style>
<body>
<div id="wrapper">
<section id="section1">
<img src="img/showcase_project1.jpg">
<img src="img/showcase_project2.jpg">
<img src="img/showcase_project3.jpg">
<hgroup>PAVÉ</hgroup>
<p class="title">Illustration Design<br>Avina Tan</p>
<hgroup>REBRANDING HAW PAR VILLA</hgroup>
<p>Branding Design<br>Tan Chin Hwee</p>
<hgroup>DREAMS</hgroup>
<p class="title">Photography<br>Jessie Lam</p>
</section>
<section id="section2">
<img src="img/showcase_project4.jpg">
<img src="img/showcase_project5.jpg">
<img src="img/showcase_project6.jpg">
<hgroup>PAPER NEVER FORGETS</hgroup>
<p class="title">Graphic Design<br>Janelle Qua</p>
<hgroup>‘O THAT TWISTED STORY</hgroup>
<p class="title">Illustration Design<br>Jennifer Soon</p>
<hgroup>HOMEBREW</hgroup>
<p class="title">Branding Design<br>Fiona Lim</p>
</section>
<section id="section3">
<img src="img/showcase_project7.jpg">
<img src="img/showcase_project8.jpg">
<img src="img/showcase_project9.jpg">
<hgroup>FIVE (MUSIC IN PRINT)</hgroup>
<p class="title">Photography<br>Jonathan Liu</p>
<hgroup>PRECIOUS</hgroup>
<p class="title">Graphic and Editorial Design <br>Tzeng Li Syuan</p>
<hgroup>26 HOURS</hgroup>
<p class="title">Digital Design<br>Evelyn Chew & Rachall Tay</p>
</section>
<section id="section4">
<img src="img/showcase_project10.jpg">
<img src="img/showcase_project11.jpg">
<hgroup>JOY IN A CUP</hgroup>
<p class="title">Digital Design<br>Chua Pei En & Lim Si Hui</p>
<hgroup>AZURE AIRLINE APP</hgroup>
<p class="title">Digital Design<br>Tulasi & Lock Phutthaphon</p>
</section>
</div>
</body>
There are a few issues here, but to solve your problem, I'd recommend grouping each image wih its associated <hgroup> and <p class="title"> by wrapping them in a <div> or something and then floating the wrapper. Something like this: http://jsfiddle.net/Lp32mtkm/ .
The reason your version isn't working is because <p> tags and <hgroup> tags are block elements by default (this can be set explicitly with the css property display), which means they automatically start a new line, even if there is space for them on the previous line. <img> tags are actually inline elements by default, which means they will line up next to each other.
EDIT: missed the part about centering the text. All you have to do is put text-align:center on the wrapping element, like so: http://jsfiddle.net/Lp32mtkm/1/ .

Centring a div box when you cannot adjust html

I am trying to adjust the CSS so the "product" and product information is centered and not floated to the left I cannot adjust the HTML as its given via a shortcode thats added into the WP post but maybe I could wrap it in a div?
HTML:
<ul class="products ribbon">
<li class="product last first">
<a href="http://dailybabydeals.co.nz/shop/estella-rose-designs/">
<div class="thumbnail">
<span class="onsale">Sale!</span>
<img width="325" height="325" src="http://dailybabydeals.co.nz/wp-content/uploads/2013/02/Front-Page-325x325.jpg" class="attachment-shop_catalog wp-post-image" alt="Front Page" />
<div class="thumb-shadow"></div>
<strong class="below-thumb">Estella Rose Designs</strong>
</div>
<span class="price"><span class="from">From: </span><del><span class="amount">$25</span></del> <ins><span class="amount">$19.95</span></ins></span>
</a>
<div class="buttons">
Select options</div>
</li></ul>
CSS:
CSS
Okay, let's try this again now that I understand your question better. So you want to center the <ul> element as a whole? That is a lot simpler than what I originally thought you were going for.
To do that, all you need to do is wrap the <ul> element in a span tag that is set to display as an inline-block. Then set the containing element so that its text is centered.
Try this:
<html>
<head>
<style language="text/css">
/*<![CDATA[ */
#test1 {
text-align: center;
}
#test2 {
display: inline-block;
text-align: left;
}
/* ]]> */
</style>
</head>
<body>
<div id="test1">
<span id="test2">
<!-- Place your <ul> element here -->
</span>
</div>
</body>
</html>
how it works
The "test2" element is set to display as an inline-block, which means it displays inline with the text. This means that it can then be affected by properties that manipulate lines of text, such as "text-align".
Setting "text-align" to "center" on the "test1" element makes the inline content -- the "test2" element in this case -- within it become centered on the screen.
The standard way to center a block is to set the "margin-right" and "margin-left" properties to "auto", but that only works for elements that are displayed as blocks and that have a width that is less than 100%.
I would just put it in a div and float it next to another div with nothing in it.
http://www.barelyfitz.com/screencast/html-training/css/positioning/
Like in step 8 in this link.
The reason that it looks like the text "Sale!" is floated to the left is that <img> elements display as inline blocks by default, so the image is on the same line of text as the words "Sale!". A <br /> tag immediately following the text "Sale!" would solve that problem; but you said you can't modify this HTML, right?
Given that restriction, here is how I was able to solve the problem...
Surround the HTML from your example in a <span> tag and assign it a class. I used "test" as my class name".
Then Place this CSS in the head of the HTML document:
<style language="text/css">
/*<![CDATA[ */
.thumbnail img {
display: block;
}
.test {
display: inline-block;
}
.test .price, .test .below-thumb {
display: block;
text-align: center;
}
/* ]]> */
</style>
why it works
The selector for making the image display as a block solves the problem of the missing <br /> tag.
The span tag with which you surrounded the HTML displays as an inline block. This means that it will collapse around its contents, giving you a box within which you can center the text.
Making the items that contain the text display as a block causes them to take a width of 100%, filling the surrounding box
The inclusion of "text-align: center" is what finally makes the text display as centered within its container

how do I prevent other elements from being affected by overflow hidden?

I have a parent element that has dynamic text in it that needs to be confined into a certain area. I had users that where writing sentences without using spaces (example:yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy etc...) This was breaking the structure of my styled text area.
#ProfileCommentBody{ width:500px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#333333; margin:15px 0px 0px 20px; float:left; background-color:#EAF7FB; padding:10px 10px 10px 10px; position:relative; overflow:hidden;}
I used overflow:hidden,as you can see above in the css code, and it solved the problem but created a new problem. I have another another element that is a image that is absolutely positioned that completes the style of my speech bubble ( the above element is the speech bubble) and when I have the above element on overflow:hidden it make that image disappear. I have tried z-index and that does not work.
.ProfileCommentTail{background:url(images/speechBubbleTail.png) no-repeat top left; width:15px; height:10px; position:absolute; top:20px; left:-15px;}
Is there a different option to use besides overflow:hidden or is there something I can do to counteract the unwanted part of what the overflow:hidden is doing?
html structure:
<div id='ProfileCommentBody' class='round_10px'>
<div id='CommentNameProfile'>
<div class='ProfileCommentTail'> </div>
<a href='http://www.blahblahblah.org/Profile.php?id=<?php echo $poster->id; ?>'>
<?php echo $poster->first_name. " ". $poster->last_name; ?> says...
</a>
</div>
</div>
Here is an example of an imgage of what it is doing now,which is wrong (missing speech bubble tail to the right of the polaroid, and the left of the speech bubble) overflow:hidden is causing this to disappear, but fixing another problem I was having:
(source: cysticlife.org)
here is what it is SUPPOSED to look like, notice the tiny blue speech bubble tail connected to the speech bubble:
(source: cysticlife.org)
<div id="blah">
<div class='ProfileCommentTail'> </div>
<div id='ProfileCommentBody' class='round_10px'>
<div id='CommentNameProfile'>
<a href='http://www.blahblahblah.org/Profile.php?id=<?php echo $poster->id; ?>'>
<?php echo $poster->first_name. " ". $poster->last_name; ?> says...
</a>
</div>
</div>
</div>
This is what I meant by my initial comment. CSS such as:
#blah { position:relative; }
Then again, because there is no layout/design/comp links it's hard to see what you are describing. I suggest posting visual details for visual problems.

Resources