css style tag not working - html

when i add this code to my css file it does not work ? However when i add this to my jsp file in the Head tag it works ?
Any idea what am i missing ?
<style>
input[type="text"]
{
width:500px;
display:block;
margin-bottom:10px;
background-color:yellow;
}
input[type="button"]
{
width:200px;
margin-left:35px;
display:block;
}
</style>

Remove <style> </style> tags .Not required for external CSS.

No need for the style open and close in external style sheets.

if you are using an external CSS file then you should not use <style> tags. Use style tags only when your using the CSS in the same HTML document

The style tags tell the browser how to treat the contents when included directly within your page and are necessary because of the potential for mixed content types within a single html document.
However when you are defining styles directly within a CSS file you should not include the style tags as the contents are determined by the file type (i.e. an external style sheet).
Your CSS contents should look as follows:
input[type="text"]
{
width:500px;
display:block;
margin-bottom:10px;
background-color:yellow;
}
input[type="button"]
{
width:200px;
margin-left:35px;
display:block;
}

Related

Is it possible to add a class from external stylesheet into the css of another class?

This has been bugging me for so long.
Is this possible?
<link rel="stylesheet" href="https://raw.githubusercontent.com/una/CSSgram/master/source/css/cssgram.min.css">
<style>
.posts img{
color:black;
background:red;}
</style>
How do i apply a class from the external stylesheet into the .posts{} css ?
If external class is using the same classes or IDs and if it's loaded after your CSS classes, it will overwrite them.
In this particular case, the following code:
<style>
.posts{
color:black;
background:red;}
</style>
will be overwritten by imported style sheet in case it comes before the external stylesheet has been loaded.
You can find out more about CSS Hierarchy here.

Header not changing style using CSS, however other div is

I am trying to get my header background to change color using css, however I am unable to change anything other than the position of my nav <div>.
https://jsfiddle.net/70d40nnt/2/
<style type="text/css">
#header {
background:#7D72F7;
text-align:center;
padding:5px;
}
.clearfix {
overflow: auto;
}
#nav {
line-height:30px;
background-color:#eeeeee;
height:200px;
width:150px;
float:right;
padding:5px;
text-align:center;
}
#section {
width:350px;
padding:10px;
float:left;
}
</style>
Remove the <style></style> from your stylesheet and your jsfiddle works. Indeed, they are needed only for css directives inside a html page.
Just remove the <style> tags from the "CSS" box (and <body> from the "HTML" one), and it will work.
remove the style tag in css block in js fiddle. JS fiddle doesn't need style tag to be defined. Directly write the styles.

Why is style *{display:*} included in the HTML body?

When I add such CSS into <style> tags:
* {
display:block;
}
It is never interpreted correctly. Instead, what do I see? Somehow everything inside <style> becomes the part of html body. E.g.:
* {
display:block;
}
<p>paragraph</p>
<phrase>phrase</phrase>
<pet>pet</pet>
This happens anywhere. For the first time, I thought this is the problem with StackSnippets. (i.e. the live demo for Stack Overflow, the one I've provided above), but then I checked with code pen. Then with jsfiddle. Then I've gone ahead and made a file on my server, giving it all contents I inserted in the snippet above.
The outcome is always the same. The CSS gets included in the html, though it is applied. (the only fix is to create a stylesheet and include it using <link>)
The most interesting thing, is, that it seem to happen only with display:*. E.g., the following works:
* {
color:green;
background:red;
border:2px solid orange;
border-radius:5px;
}
<p>paragraph</p>
<phrase>phrase</phrase>
<pet>pet</pet>
But once I put in the styles of the last snippet display:*, the styles are, again, magically included in HTML.
* {
color:green;
background:red;
border:2px solid orange;
border-radius:5px;
display:inline-block;
}
<p>paragraph</p>
<phrase>phrase</phrase>
<pet>pet</pet>
Why does it happen?
It's styling the <head> element and everything in it, including the very <style> element your CSS resides in, because the CSS appears as character data within the <style> element. A <link> element on the other hand doesn't have any content — it points to a separate resource altogether, so there is nothing inside the element itself to be displayed.
Most browsers implement <head> as display: none (and some propagate that value to every descendant), which you are able to override by targeting them with a display style. The rest of the properties are still applied to <head> and its descendants regardless of whether you do this, but without it, they simply won't show up in your page so you don't really see it happening. That's really all there is to it — there isn't anything else that's special about <head> or its related elements.
In other words, as far as CSS is concerned, the following (yes, a <style> element with a style attribute...):
<style style="display: block; font-family: monospace">
p { color: red; }
</style>
Is no different from this:
<code style="display: block; font-family: monospace">
p { color: red; }
</code>

Apply CSS to certain part of website

I'm building a WISYWIG in which I have an iframe that needs to have content.css applied to it.
content.css: (will be applied to the iframe (src: ./content/home.php))
#content h4
{
font-size:30px;
color:brown;
text-shadow: 1px 1px #000000;
text-align:center;
}
editpage.php: // wisywig
<iframe id="content" src="./content/home.php"></iframe>
home.php contains content that needs to be shown on the homepage of my website. As you can imagine the #content in front of my h4 is used to apply this same css to a div inside my website. However, when I apply this same css to my iframe this css wont find a #content inside the iframe. Hence my previous question.
How can I apply this CSS to both a <div id="content"> and an iframe?
Yes. To apply your style to everything inside #lol, just use the * selector:
#lol * {
display:inline-block;
color:green;
}
If you only want direct descendants of #lol:
#lol > * {
display:inline-block;
color:green;
}

Can't move an inline style out to a style sheet?

I'm sure this is a dumb question, but I can't move this inner (button) inline style to a style sheet:
<label style='white-space:nowrap; line-height:14px; height:14px; vertical-align:bottom; '>
<input type = 'button'
id = 'sButton2'
style = 'cursor:pointer; width:30px; height:13px; float:left; margin-bottom:2px; margin-right:12px' >
The quick brown fox
</label>
I've tried this:
<style type="text/css">
#sButton2 { cursor:pointer; width:30px; height:13px; float:left; margin-bottom:2px; margin-right:12px; }
</style>
...
<label style='white-space:nowrap; line-height:14px; height:14px; vertical-align:bottom; '>
<input type = 'button'
id = 'sButton2' >
The quick brown fox
</label>
and this:
<style type="text/css">
.sButton { cursor:pointer; width:30px; height:13px; float:left; margin-bottom:2px; margin-right:12px; }
</style>
...
<input type = 'button'
id = 'sButton1'
class = 'sButton' >
but neither of these works; no style is applied. Any idea what I'm doing wrong?
Thanks.
EDIT
Sorry folks, it's midnight, I (was!) just off to bed, and I screwed up the cut-and-paste. The syntax of the style sheet is actually correct - I've fixed it above. Other info:
1- the style sheet is in the head section of a single html file. There are already a couple of jQueryUI ones above it, and I copied the syntax from them;
2 - I think that there is another style applied to the buttons, but I was hoping that #sButton2 would override this;
3 - I can't get even the most trivial style from a style sheet to apply
to the buttons. If I just set the button width, for example, nothing happens, although an inline width works;
4 - it's not browser-specific - seen in F/F, Opera, Chrome;
Thanks again.
EDIT2
Just tried appending a !important to the end of the style sheet, with no change in F/F & Chrome:
...margin-right:12px; !important }
<style type="text/css">
#sButton2 { 'cursor:pointer; width:30px; height:13px; float:left; margin-bottom:2px; margin-right:12px' >
</style>
should be
<style type="text/css">
#sButton2 { cursor:pointer; width:30px; height:13px; float:left; margin-bottom:2px; margin-right:12px }
</style>
You need to end the first { with a }, not a >. And the styles are not in quotes, they are simply enclosed in curly braces. You should also look into what is standard for your HTML code. Usually elements are laid out like this:
<input type="button" id="sButton2" />
It is a good idea to follow standards like this.
Change
#sButton { 'cursor:pointer; width:30px; height:13px; float:left; margin-bottom:2px; margin-right:12px' >
to
#sButton { cursor:pointer; width:30px; height:13px; float:left; margin-bottom:2px; margin-right:12px }
That should do it.
(remove the ' at the start and the end, and replace the > by a } )
your inner style ends with > instead of }
furthermore it should be in your head tag
lastly why are you wrapping your style with single quotes?
Stupid question, but are you sure the style sheet is being loaded? I've seen where people have multiple stylesheets and they're loading the wrong one, or they edited the wrong one.
You might also have an overriding style later in your stylesheet. Do you have any input styles? ID should override less specific styles, but there are bugs in some browsers that you might have to work around.
Try using !important
DOH! Yes, as others have said, your problem is the single quotes around your css attributes.
<style type="text/css">
.sButton {
cursor:pointer;
width:30px;
height:13px;
float:left;
margin-bottom:2px;
margin-right:12px
</style>
<input type="button" id="sButton1" class="sButton">
http://jsfiddle.net/charlescarver/aQHyK/
Fixed, with a lot of help from Firebug (what a great bit of software). The input tag is in a jQueryUI dialog, and was inheriting a 'width:95%' from '#dialog input'. The 'width:30px' in my style sheet was being ignored, until I changed it to 'width:30px !important', in all of F/F, Chromium, and Opera. It was ignored in both the div (#sButton2) and class (.sButton) styles.
I don't understand this. What's the point of inheritance if you can't over-ride it without adding arbitrary !important's? Is css inheritance broken? Isn't !important just admitting that it's broken?
I had originally thought that the entire style sheet was being ignored, because the input button expanded to 95%, and made it look like everything else was being ignored as well. However, I eventually realised that part of the style was actually working - 'cursor:pointer' did show a cursor.
Thanks everybody.
# is the correct prefix for IDs (as you actually use in your second code example), but you shouldn't wrap quotes around the css code. Try it like this:
<style type="text/css">
#sButton2 { cursor:pointer; width:30px; height:13px; float:left; margin-bottom:2px; margin-right:12px; }
</style>
Executing a validator on the code (which is always a good idea) would have told you all this as well!