To redefine a general look for an entire HTML tag, what would be the proper selector? Would it be a class, head or id?
Also, not sure which attribute that is used for defining an inline style in HTML? class, font, type, input?
Finally, if I wanted to add a width attribute into a specific item within the html code, would it be: width="10" or width:"10".
Thanks
To redefine a general look for an entire HTML tag, what would be the proper selector? Would it be a class, head or id?
-> To redefine a general look of a tag for the entire page you just need to use the tag name, for example: if you want to change the anchor tag to not have an underline for the entire page you just type this,
a {
text-decoration: none;
}
And if you want to change everything inside a particular tag you could do something like
div * {
padding: auto;
}
Also, not sure which attribute that is used for defining an inline style in HTML? class, font, type, input?
-> As of now it is a good practice not to do inline styling frequently in your code, but if you have to you could style 'almost' all the tags which follow HTML5 syntax. To do that you could write something like this:
<div style="width:200px"><span>Some text here</span></div>
Finally, if I wanted to add a width attribute into a specific item within the html code, would it be: width="10" or width:"10".
-> For styling anywhere and anything you have to use property : value; this syntax, except in javascript. For javascript you have to follow these steps: clickable link
To style an html tag, you just use the tag as a reference:
p {
color: red;
}
h1 {
font-size: 20px;
}
Inline HTML styling is done with the style attribute, but is not recommended.
<h1 style="color:blue">This is a Blue Heading</h1>
To style a tag in general, you wouldn't need to use id, class, or head.
Say you wanted to style all 'p' elements, in CSS you would put:
p {
Property:Value
}
If you wanted to specify an elements width within the HTML code the correct way would be:
width="10" (Within an elements tags of course)
img{
width="100px"
}
.image{
width="100px"
}
#image{
width="100px"
}
<img src="" width="100px" />
<img src=""/>
<img src="" class="image" />
<img id="image" src=""/>
If you wish to define a general look for the whole of the webpage that you wish to create, you can type the body and then define it accordingly.
For example
body{
padding:20px;
color: blue;
}
This will define the styling for all the elements under the body tag.
An alternate way for this is:
*{
padding:20px;
}
By using a Universal selector, you can automatically select all the elements and apply them to the style.
Coming to your last question,
if you are using Inline CSS, you can use both the ways that you have mentioned-
"width=200"and "width:200;"
But if you are directly applying in a tag or under style tag:
<img src="" width=" ">
This is the apt way of doing so.
Thank you
A unique selector is the id.
the class selector is for more than one tag. You use it if you would have that two tags are viewed in the same way. (from your css file)
inline styles:
<p style="border: 1px solid #000000">Hello World</p>
Last que: width="100px" for example:
<img src="pics/pic1.jpg" width="100px" />
The Tag "html" is "only for defining that here starts the document"
You have to style "each" tag (p,div,h1,h2,span,....)
http://de.selfhtml.org/css/
Related
I am trying to modify the link color inside a <span>. (I know that this is not optimal/conforming, but I am stuck in a situation where I don't have access to the header and also cannot play with the <a> tags.) I tried something like this, which doesn't work
<span style="a {color:#C04040} ">
Try to change in a <span>: This link
</span>
I also tried other variations, like:
<span style="a.color:#C04040">
I admit that I don't understand the syntax of <style> as a tag, as opposed to inside the header. Would appreciate any help, or links to complete documentation.
Thanks!
If you want to apply this rule to all your span tags, you can just place this in the content of your html document. Then all tags inside a span tag will get red with blue background.
<style>
span a { color:red; background-color:blue; }
</style>
If you want to apply this only to a specific span tag, just use a named class like
<style>
span.colorlink a { color:red; background-color:blue; }
</style>
And then you can do <span class='colorlink> for the spans you want colored.
In regard to the first CSS rule, it works when i use the 'p' tag by itself. When I apply the 'article' class with or without the 'p' tag, it doesn't work. Why is that? Also the 'hr' tag with the class of 'one' works (which means CSS file is working). This seems so basic. I don't understand why it isn't working. Any ideas?
HTML
<p class=article>{{ post.body|truncatewords:30|linebreaks }}</p>
-- Also tried this
<p class="article">{{ post.body|truncatewords:30|linebreaks }}</p>
external CSS file
p.article {
color:red;
}
hr.one {
border:none;
height: 2px;
background: #cec4c4;
}
HTML Output
<div>
<h1 class=display-4>gdddsasddsg</h1>
<h6><span class="font-italic font-weight-normal">By: </span>gdorman619 <span
class="font-italic font-weight-normal">Published Date: </span> May 28, 2020, 12:24 p.m.</h6>
<p class="article"><p>sdadfsdsfdsfa</p></p>
<hr class="one">
</div>
Are you printing content from a WYSIWYG-editor or something else that is not a pure string? In that case, that content will likely enforce its own markup as inline HTML and external css is not going to work as inline CSS inside HTML has a higher specificity then CSS placed in an external stylesheet, unless you apply !important to the color, which makes me cringe on my behalf.
Your code looks mostly good. To add a class attribute, you must specify the name of the class with quotes, like this:
<p class="article"> Your code here</p>
Hope this helps
Try with this css.
.article {
color:red;
}
.one {
border:none;
height: 2px;
background: #cec4c4;
}
it may help you
I have some text where the inline styling is as follows:
<a style="margin-left:87px;color:white;">3mm</a>
For some reason, it's being underlined on hover. It looks like this:
The containing div is:
<div class="next-slide-container">
Poke ID<br>
Choose your Pokemon<br>
Details<br>
Size<br>
</div>
<div class="next-slide-container" style="margin-top:-7px">
<a style="margin-left:87px;color:white;">3mm</a>
<img class="texture-icon" role="button" src="Resources/Cutups/Texture_Icons/Pikachu.png">
</div>
I tried recreating it with a JSFiddle but I cannot get the text to underline is JSFiddle, even though the css for the container is not different than my own source code. Additionally, the text inside the first div class next-slide-container, like "Choose your Pokemon", does not highlight in my code. It's only just "3mm"; so it would seem that the inline styling is the problem? Thanks.
By default, most browsers set anchor tags <a> to have the css text-decoration: underline; If you want to remove any browser default underlining you need to add text-decoration: none; the the css for anchor tags.
Basically... add this to your CSS file or area.
a { text-decoration: none; }
Some browsers also add a default outline or pseudo "glow" to anchor tags (Mozilla).
If you want that to also go away, you would add this to your css for anchor tags:
a { text-decoration: none; outline: none; }
Valid HTML would mean you need to add either an href= value or a name= value to the anchor tag. Without one of these, the a tag is invalid.
In your fiddle, this invalid anchor tag may be the reason you aren't seeing the same thing as in your browser. Add href="#" to the anchor tag in your fiddle and you'll see the same issue. jsFiddle isn't as forgiving with improper markup the way browsers can be. That's kind of the purpose of jsFiddle. Browsers will guess at what is meant sometimes, jsFiddle really doesn't.
If you merely want to style the text, you can use <p>, <span>, <div>, <h1>, etc. tags. You don't need an anchor tag simply to style text.
It appears some browsers (Chrome at least) put a partial underline under images that are nested inside of an anchor tag, like this:
<img src="/foo.jpg" />
So I'm looking for a way to add text-decoration: none; to any anchor tags that contain an img tag. My first thought was this:
a img {
text-decoration: none;
}
Of course that doesn't work, because the style gets applied to the img tag, and not the anchor. So is there a selector I can use to apply the text-decoration style to any anchor tag with a img child?
EDIT:
My HTML typically looks like this:
<a href="#">
<img src="/foo.jpg" />
</a>
The way I space and tab the elements is adding extra whitespace between the anchor tag, and image tag. It's that white space that's being underlined.
If you're against adding a class to this <a> tag (which is the simple solution), your next best CSS solution would be to remove text-decoration on the <a> tag, and wrap the text you want to have underlined in an inline element. See below:
For images:
<a href="#">
<img src="/foo.jpg" alt="etc" />
</a>
For text:
<a href="#">
<span>Text that you probably want underlined</span>
</a>
Combined:
<a href="#">
<img src="/foo.jpg" alt="etc" /> <span>Text that you probably want underlined</span>
</a>
CSS:
a { text-decoration: none; }
a:hover span { text-decoration: underline; }
Unfortunately there is no way currently of selecting the parent of an element using just CSS.
You would need to resort to javascript or jQuery.
Personally I would do something in jQuery like
$('a>img').parent().addClass('noTextDecoration');
then in css have the following:
a.noTextDecoration {test-decoration:none;}
I just use
img {
border:none;
}
So far as I can tell, there is no way to select an element's parent in CSS. You could try applying some class, i.e. imagelink to A elements that contain IMG elements, though.
If the href attribute of these anchors always points to images, and no anchors point to images besides the one with actually an img tag inside, then you can use:
a[href$=".gif"],
a[href$=".png"],
... ,
a[href$=".jpg"] {
text-decoration: none;
}
Here is what I am trying to accomplish in HTML/CSS:
I have images in different heights and widths, but they are all under 180x235. So what I want to do is create a div with border and vertical-align: middle them all. I have successfully done that but now I am stuck on how to properly a href link the entire div.
Here is my code:
<div id="parentdivimage" style="position:relative;width:184px;height:235px;border-width:2px;border-color:black;border-style:solid;text-align:center;">
<div id="childdivimage" style="position:absolute;top:50%;height:62px;margin-top:-31px;">
<img src="myimage.jpg" height="62" width="180">
</div>
</div>
Please note that for the sake of copy pasting here easily, the style code is inline.
I read somewhere that I can simply add another parent div on top of the code and then do a href inside that. However, based on some research it won't be valid code.
So to sum it up again, I need the entire div (#parentdivimage) to be a href link.
UPDATE 06/10/2014: using div's inside a's is semantically correct in HTML5.
You'll need to choose between the following scenarios:
<a href="http://google.com">
<div>
Hello world
</div>
</a>
which is semantically incorrect, but it will work.
<div style="cursor: pointer;" onclick="window.location='http://google.com';">
Hello world
</div>
which is semantically correct but it involves using JS.
<a href="http://google.com">
<span style="display: block;">
Hello world
</span>
</a>
which is semantically correct and works as expected but is not a div any more.
Why don't you strip out the <div> element and replace it with an <a> instead? Just because the anchor tag isn't a div doesn't mean you can't style it with display:block, a height, width, background, border, etc. You can make it look like a div but still act like a link. Then you're not relying on invalid code or JavaScript that may not be enabled for some users.
Do it like this:
Parentdivimage should have specified width and height, and its position should be:
position: relative;
Just inside the parentdivimage, next to other divs that parent contains you should put:
<span class="clickable"></span>
Then in css file:
.clickable {
height: 100%;
width: 100%;
left: 0;
top: 0;
position: absolute;
z-index: 1;
}
The span tag will fill out its parent block which is parentdiv, because of height and width set to 100%. Span will be on the top of all of surrounding elements because of setting z-index higher than other elements. Finally span will be clickable, because it's inside of an 'a' tag.
Going off of what Surreal Dreams said, it's probably best to style the anchor tag in my experience, but it really does depend on what you are doing. Here's an example:
Html:
<div class="parent-div">
Test
Test
Test
</div>
Then the CSS:
.parent-div {
width: 200px;
}
a {
display:block;
background-color: #ccc;
color: #000;
text-decoration:none;
padding:10px;
margin-bottom:1px;
}
a:hover {
background-color: #ddd;
}
http://jsbin.com/zijijuduqo/1/edit?html,css,output
Two things you can do:
Change #childdivimage to a span element, and change #parentdivimage to an anchor tag. This may require you to add some more styling to get things looking perfect. This is preffered, since it uses semantic markup, and does not rely on javascript.
Use Javascript to bind a click event to #parentdivimage. You must redirect the browser window by modifying window.location inside this event. This is TheEasyWayTM, but will not degrade gracefully.
I'm surprised no one suggested this simple trick so far! (denu does something similar though.)
If you want a link to cover an entire div, an idea would be to create an empty <a> tag as the first child:
<div class="covered-div">
<a class="cover-link" href="/my-link"></a>
<!-- other content as usual -->
</div>
div.covered-div {
position: relative;
}
a.cover-link {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
This works especially great when using <ul> to create block sections or slideshows and you want the whole slide to be a link (instead of simply the text on the slide). In the case of an <li> it's not valid to wrap it with an <a> so you'd have to put the cover link inside the item and use CSS to expand it over the entire <li> block.
Do note that having it as the first child means it will make other links or buttons inside the text unreachable by clicks. If you want them to be clickable, then you'd have to make it the last child instead.
In the case of the original question:
<div id="parentdivimage" style="position:relative;width:184px;height:235px;border-width:2px;border-color:black;border-style:solid;text-align:center;">
<a class="cover-link" href="/my-link"></a> <!-- Insert this empty link here and use CSS to expand it over the entire div -->
<div id="childdivimage" style="position:absolute;top:50%;height:62px;margin-top:-31px;">
<img src="myimage.jpg" height="62" width="180">
</div>
<!-- OR: it can also be here if the childdivimage divs should have their own clickable links -->
</div>
Make the div of id="childdivimag" a span instead, and wrap that in an a element. As the span and img are in-line elements by default this remains valid, whereas a div is a block level element, and therefore invalid mark-up when contained within an a.
put display:block on the anchor element. and/or zoom:1;
but you should just really do this.
a#parentdivimage{position:relative; width:184px; height:235px;
border:2px solid #000; text-align:center;
background-image:url("myimage.jpg");
background-position: 50% 50%;
background-repeat:no-repeat; display:block;
text-indent:-9999px}
<a id="parentdivimage">whatever your alt attribute was</a>
This can be done in many ways.
a. Using nested inside a tag.
<a href="link1.html">
<div> Something in the div </div>
</a>
b. Using the Inline JavaScript Method
<div onclick="javascript:window.location.href='link1.html' ">
Some Text
</div>
c. Using jQuery inside tag
HTML:
<div class="demo" > Some text here </div>
jQuery:
$(".demo").click( function() {
window.location.href="link1.html";
});
I simply do
onClick="location.href='url or path here'"
What I would do is put a span inside the <a> tag, set the span to block, and add size to the span, or just apply the styling to the <a> tag. Definitely handle the positioning in the <a> tag style. Add an onclick event to the a where JavaScript will catch the event, then return false at the end of the JavaScript event to prevent default action of the href and bubbling of the click. This works in cases with or without JavaScript enabled, and any AJAX can be handled in the Javascript listener.
If you're using jQuery, you can use this as your listener and omit the onclick in the a tag.
$('#idofdiv').live("click", function(e) {
//add stuff here
e.preventDefault; //or use return false
});
this allows you to attach listeners to any changed elements as necessary.
A link with <div> tags:
<div style="cursor: pointer;" onclick="window.location='http://www.google.com';">
Something in the div
</div>
A link with <a> tags:
<a href="http://www.google.com">
<div>
Something in the div
</div>
</a>