CSS: text-indent and text-align are ignored - html

I guess the answer to this may be simple, but I can't figure it out on my own.
I've got the following HTML:
<div id="excerpt">
<p class="chapter">Chapter One</p>
<p>Text</p>
<div class="copyright-notice">
<p>Copyright © 2014 Name. All rights reserved.</p>
</div>
<!--end copyright-notice-->
</div>
<!--end excerpt-->
and the following CSS to go with it:
#excerpt {
padding:20px;
color:#000000;
}
#excerpt p {
line-height:1.4em;
text-indent:45px;
text-align:justify;
}
p.chapter {
text-align:center;
text-indent:0;
font-size:16pt;
text-transform:uppercase;
}
.copyright-notice {
border-bottom: 1px solid #999999;
border-top: 1px solid #999999;
margin:20px auto;
padding:20px;
}
.copyright-notice p {
display: block;
color:#666666;
text-align: center;
text-indent:0;
}
JS Fiddle reproduction.
As you can see I try to center the text and set the indent to 0 for the paragraph with the chapter class as well as the text within the copyright notice. But it doesn't work.
If I apply the style to the paragraph directly in the HTML file like:
<p style="text-align:center;text-indent:0;">text</p>
JS Fiddle reproduction.
It'll work. But as soon as I try to style those paragraphs through CSS text-align and text-indent get ignored.
Any idea what I'm doing wrong? Thanks for your help!

This is just a specificity issue.
The selector #excerpt p is more specific than p.chapter. Therefore text-indent:0 isn't applied. The reason it was applied when using the style attribute, is because inline CSS is more specific.
More specifically, (pun intended), #excerpt p has a specificity calculation of 101. Whereas p.chapter has a specificity of 11. (An id is 100 points, a class is 10, and an element is 1).
As for a solution, use either of the following to avoid the specifity conflict.
p {
text-indent:45px;
}
p.chapter {
text-indent:0;
}
or..
#excerpt p {
text-indent:45px;
}
#excerpt p.chapter {
text-indent:0;
}
(Other styling omitted from brevity.)
The latter example is probably what you should go with because you don't want all paragraph elements to be indented, just those that are a descendant of #excerpt. I'd avoid using id's in CSS as much as possible though - save those for JS.

Related

the adjacent sibling selector is not working on my mark up

I have used the following HTML and CSS codes:
p.head-1 {
font-size:250%;
color:#696969;
}
p.head-2 {
font-size:100%;
}
p.head-1+p.head-2 {
text-align:center;
display:block;
}
<div id="header">
<p class="head-1">
This is main heading
</p>
<p class="head-2">
this is another header component
</p>
</div>
but despite of using the sibling selector only head-2 is accepting CSS properties while head-1 remains in-effected
ie the 'text-align' property is only accepted by the head-2 class but not by the head-1
You misunderstood the Adjacent sibling selector.
What it does, and did successfully in your case, is to identify an element which is adjacent to another.
In your example it would identify head-2 only if it is adjacent to head-1. But head-1 itself is not included.
You may simply wrap your css like this:
#header p{
font-size:250%;
color:#696969;
text-align: center;
/*display:block -- not needed as p is block level element by default*/
}
Or, using more complex selector:
p[class^="head"]{
text-align: center;
}
If you want to combine the selector then use a comma not plus operator (plus operator is used for next sibling):
p.head-1, p.head-2
{
text-align:center;
}

How to deal with default padding and margins

I am fairly comfortable with html5/css3 now, so I am trying to make a site using same and make it responsive.
So far things are going smoothly except for these two problems:
the use of em i dont understand the calculations at all, especially why i have to put this font: .81em/150% i am following a guide from a tutorial online.
i am having some imaginary padding on my div, you can see it here http://jsfiddle.net/NhZ2A/
e.g. I have on the body:
body{padding:0px; margin:0px;}
Then I have a div with an image like this:
<div id="slider">
<img src="images/slider.jpg"/>
</div>
Then in my css I have:
#slider{
width:100%;
height:auto;
}
#slider img{
width:60%;
height:auto;
}
With the above css I still have padding on the slider div below or maybe it's a margin on the image below.
I don't understand why and its killing me.
For the second issue :
The space is not padding, it is created because the <img> tag is an inline element and therefore has a whitespace use display:block; on the <img> tag to remove it.
Use css resets , To get consistent cross-browser experience,it should be included,any one among these.
Eric Meyer’s Reset CSS
HTML5 Doctor CSS Reset
Yahoo! (YUI 3) Reset CSS
Normalize.css
Get it from here --> http://www.cssreset.com/
Yes, CSS reset is important to set default initial value for each element.
reset.css Source - Reset5
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,audio,canvas,details,figcaption,figure,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,summary,time,video
{
border:0;
outline:0;
font-size:100%;
vertical-align:baseline;
background:transparent;
margin:0;
padding:0
}
body
{
line-height:1
}
article,aside,dialog,figure,footer,header,hgroup,nav,section,blockquote
{
display:block
}
nav ul
{
list-style:none
}
ol
{
list-style:decimal
}
ul
{
list-style:disc
}
ul ul
{
list-style:circle
}
blockquote,q
{
quotes:none
}
blockquote:before,blockquote:after,q:before,q:after
{
content:none
}
ins
{
text-decoration:underline
}
del
{
text-decoration:line-through
}
mark
{
background:none
}
abbr[title],dfn[title]
{
border-bottom:1px dotted #000;
cursor:help
}
table
{
border-collapse:collapse;
border-spacing:0
}
hr
{
display:block;
height:1px;
border:0;
border-top:1px solid #ccc;
margin:1em 0;
padding:0
}
input[type=submit],input[type=button],button
{
margin:0!important;
padding:0!important
}
input,select,a img
{
vertical-align:middle
}
em - Unit measurement values (1em is equal to the current font-size,same as 2em = 2*font-size)
Font Syntax:
font: font-style font-variant font-weight font-size/line-height font-family;
In your question value .81em/150%
.81em/150% - font-size/line-height
Every browser has a default behaviour and configuration
If you want a clean start from all of them, you must set it with a "reset.css" style sheet, to avoid undesirable behaviours and have all homogeneous.
Check this SO answer to get a proper reset CSS stylesheet:
https://stackoverflow.com/questions/167531/best-practice-for-css-reset-style-sheet
The first choice will be
Css Resets
Most Used Css Reset
JUSR USE CSS RESET
* {
margin:0;
padding:0;
box-sizing:border-box;
}

CSS Sibling Selector Alternative

I'm trying to cause one element to change when another element is hovered. I know this can be done using the sibling selector (~) but it doesn't seem to be working. I tried to find an alternative to using the sibling selector but only found solutions in javascript which I don't know.
I think the problem may come from the fact that I'm trying to tie multiple elements to one sibling, that is to say, hovering over 3 different divs all change one div in three different ways. I don't think there's a mistake though I could be wrong, the code is here...
CSS
#internalContainer {
width:900px;
height:400px;
}
#sectionLeft {
float:left;
height:400px;
width:300px;
}
.leftInternal {
height:100px;
width:300px;
text-align:right;
}
#titleA {
font-size:11pt;
font-weight: bold;
text-transform: uppercase;
letter-spacing:1px;
position:relative;
top:40%;
transition:.2s
}
#sectionRight {
float:left;
width:568px;
height:400px;
margin-left:32px;
background-color:#f2f2f2;
}
#titleA:hover {
top:45%;
transition:.2s
}
#titleA:hover ~ #sectionRight {
background: #ccc;
}
HTML
<div id="internalContainer">
<div id="sectionLeft">
<div class="leftInternal"><div id="titleA">Title of One</div></div>
<div class="leftInternal"><div id="titleA">Title of Two</div></div>
<div class="leftInternal"><div id="titleA">Title of Three</div></div>
<div class="leftInternal"><div id="titleA">Title of Four</div></div>
</div>
<div id="sectionRight">
</div>
</div>
http://jsfiddle.net/xs7h8/
Currently nothing changes when the links are hovered but they're all set to do the same thing right now. I was going to make subclasses for titleA and connect each to the sectionRight but that didn't work either.
You cannot have duplicate IDs. It simply won't work. That is your problem
Also, the #titleAs are not siblings of #sectionRight so the sibling selector will not work. #sectionRight is an uncle to them and, since there is no parent selector at the moment, there is no way to select it using CSS on hover
You also don't need to repeat the transition in the hover, it is inherited from the default state
This is the closest you can get using your current setup and no javascript, applying the hover to #sectionLeft instead

Why this only works with "!important"?

I have this piece of code, that it refuses to work without the !important (which I never want to use, because I know there is always a way to do without it).
The interesting thing is that the CSS line is after everything else (and as far as I know, this should overwrite the other stuff)
live demo jsFiddle
HTML Structure:
<div id="body">
<div class="box">
<p>...</p>
</div>
<p>...</p>
</div>
CSS:
#body{
padding:18px 35px;
}
#body p{
margin-bottom:18px;
}
.box{
background:#ddd;
border:1px solid #000;
padding:5px;
}
.box p{
margin:0;/*works with !important*/
}
It's because the ID of #body p is a more specific selector than the class of .box p. The important simply overrides that cascade.
Matching p with #body has higher specificity than matching p with .box. Read the specificity section of the CSS spec for help. Try
#header .box p { margin: 0; }
The space between #header and .box is important.
Your #body p has a greater specificity value. You can read more on generally how specificity values are calculated here.

CSS rule isn't applying

I have the following markup and CSS:
<div id="contactarea">
<p class="heading">Anuncios Premium</p>
<p class="tag">Asegure que su venta se complete!</p>
</div>
#contactarea
{
min-height:150px;
border:1px solid cyan;
}
#contactarea p .heading
{
Color:Yellow;
background-color:Green;
}
#contactarea p .tag
{
min-height:150px;
border:1px solid cyan;
}
The contactarea alone is working, the cyan border displays, but the font color of the p doesn't work.
Thanks!
Too many spaces:
#contactarea p.heading
The way you've got it, it means "any element with class 'heading' that is a descendant of a <p> element that is a descendant of the element with id 'contactarea'". Thus it didn't affect the <p> tags themselves.
The SelectORacle site is a great friend!
Get rid of the extra spaces. #contactarea p .heading should be #contactarea p.heading.
This isn't an answer to your direct question but it may be helpful to you in the future. First, if you find that one rule is superceding another, pay attention to the natural priority of CSS rules within a stylesheet and for the prioritization of inline css > external css. Second, if you ever want a rule to take priority, you can do:
#contactarea p.heading { color:yellow !important; }