I would like to be able to have for example two img classes with different properties. For example in css I want to have:
img {
padding-left: 15pt;
float: right;
}
and then:
img1 {
padding-left: 15pt;
float: left;
}
where img1 should be a tag for image so I can use it in html for example as:
<img1 src="myimage.png" alt="m"/>
How can I do that?
What you are using like <img1 /> is invalid HTML tag, inorder to select the elements uniquely, we define class or id to the elements, even if for some reason you don't want to define a class or an id, you can define Custom Attributes to the element, having a prefix of data- but you cannot define Custom HTML Elements, ..
Defining class to each img tag.. (Declaring id, make sure they are unique for each, you cannot use the same id on multiple elements..)
<img src="" class="img1" />
<img src="" class="img2" />
And than we write respective CSS like
.img1 {
/* Selects element having a class of .img1 */
}
.img2 {
/* Selects element having a class of .img2 */
}
Supposing you have common properties to be declared for both, group them using a comma, and than redeclare the unique properties for the other class like
.img1, .img2 {
padding-left: 15pt;
/* Other common properties goes here */
}
.img2 {
/* Unique properties for .img2 */
}
If you are not looking to assign a class to each img tag, than you can use :nth-of-type or :nth-child pseudo to select each img, but make sure you wrap them using a div or section anything you feel is appropriate...
Suppose we wrap them inside a div tag, and we assign a class say .container
<div class="container">
<img src="" />
<img src="" />
</div>
So here you can select the first img as
.container img:nth-of-type(1) {
/* Properties here will apply to first img tag nested inside element having
class of .container */
}
.container img:nth-of-type(2) {
/* Properties here will apply to second img tag nested inside element having
class of .container */
}
Note: These are CSS3 pseudo, but these are widely supported but as usual, older IE versions will spoil the game, though polyfills are available, but at the end you have to decide you want to choose what.
Also, specificity matters in each of the selectors I've defined above, if you aren't aware of specificity, than give a quick read here...
You can not define custom tags in html, should use CSS classes for what you want to accomplish.
<img class=img1 src="myimage.png" alt="m"/>
<img class=img2 src="myimage2.png" alt="m"/>
.img1 {
padding-left: 15pt;
float: left;
}
.img2 {
padding-left: 15pt;
float: right;
}
Related
I want to wrap the content inside .container in another div, and give the new div class="content". As you can see, I've already started but I can't figure out how you're supposed to do it. I couldn't really find any help online regarding this so I could be doing it completely wrong.
.container {
div class="content" {;
width: 60%;
min-width: 640px;
margin: 0 auto;
background: #FFFFFF;
padding: 8px;
}
</div>
}
div class="content" { isn't valid CSS for specifying styles. Use this syntax instead:
.container div.content {
width: 60%;
min-width: 640px;
margin: 0 auto;
background: #FFFFFF;
padding: 8px;
}
Explanation
SELECTOR1 SELECTOR2 { RULES } is CSS syntax that means "Any element matching SELECTOR2 found as a child (or grandchild, etc) of any element matching SELECTOR1, give it RULES"
.CLASS is CSS syntax for a selector that means "Any element where the class property contains CLASS."
One specifies multiple classes by separating them with whitespace, e.g. <div class="foo bar"> would be hit by selector .foo as well as .bar, but not by .foobar.
ELEMENT.CLASS is CSS syntax for a selector that means "Any <ELEMENT> element where the class property contains CLASS."
So putting this all together, .container div.content { padding: 8px; } means "Any <div> element where class contains content, nested anywhere under any element where class contains container, give it padding=8px."
I have just started learning HTML and CSS and I am implementing all the techniques I have learnt. I have come across a problem where I have two images places under one id that have 2 different dimensions. However I don't know how to edit them separately.
This is my HTML code:
#main-header {
background-color: #453e32;
}
#main-header img {
height: 4%;
width: 4%;
}
#main-header h1 {
color: #F8D115;
padding-left: 1%;
font-family: Calibri;
}
<div id="main-header">
<img src="../resources/wsimplylogo.png" />
<img src="../resources/wsimply.png" />
</div>
When editing the image sizes I cannot edit one specifically and I don't know if I should create another id separately or what
Any help would be appreciated!
PS I've literally only just started!
When using the selector #main-header img you target all img tags within your div with the id main-header. This means, that you cannot target a specific image without using something a bit more sofisticated.
If you're just learning css and html, I would suggest you give each of your images a seperate id (or class).
BUT if you really want to learn, then you could use :nth-child() as a selector:
#main img:nth-child(1){
/*here be style of 1st image*/
}
For further reference, see https://www.w3schools.com/cssref/sel_nth-child.asp
Try to make use of adjacent sibling selector + css selector.
The adjacent sibling combinator (+) separates two selectors and matches the second element only if it immediately follows the first element, and both are children of the same parent element.
Also remove the closing </img> tag in your html. No need of it
#main-header {
background-color: #453e32;
}
#main-header img {
height: auto;
max-width: 100%;
vertical-align: middle;
}
#main-header img+img { /*for the second image followed by image*/
width: 50px;
}
<div id="main-header">
<img src="http://lorempixel.com/100/100/sports">
<img src="http://lorempixel.com/100/100/food">
</div>
So I'll have the following markup:
<div id="content-sidebar">
<div class="entry">
</div>
<div class="sidebar">
Sidebar Content
</div>
</div>
It is styled with the following:
#content-sidebar{
float: left;
padding-left: 10px;
position: relative;
width: 580px;
text-align: left;
padding-right: 20px;
background: url(../_images/content-line.gif) top right repeat-y;
}
#content-sidebar .entry{
position: relative;
padding-left: 16px;
}
#content-sidebar .entry a:hover {
color: #5CB414;
text-decoration: none;
}
I also have markup that uses a different styling for the parent div WITHOUT a sidebar (just adjusts the width), but needs the same for the child .entry class.
<div id="content-nosidebar">
<div class="entry">
</div>
</div>
the content-nosidebar id needs some slightly different styling, but the .entry child selector is exactly the same.
#content-nosidebar{
padding-left: 10px;
position: relative;
text-align: left;
padding-right: 20px;
}
What selectors will tell it to use content-sidebar and content-nosidebar with the .entry class without having to use redundant code? Is #content-sidebar .entry, #content-nosidebar .entry{} the best way or is there another way to select these?
Thanks!
If .entry covers more elements than #content-sidebar .entry, #content-nosidebar .entry would, then without knowing the exact structure of your document you'll have to make do with selecting by both IDs explicitly.
Or you could cheat by using an attribute prefix selector, if you really don't want to modify your HTML, but I wouldn't recommend it unless you know what you're doing (it's often cleaner and simpler to modify your HTML to suit your selectors instead):
div[id^="content-"] .entry
The selectors you can use depends on what you want to do.
.entry {} will apply to the class in both containers. Any CSS share between the two should be added in the simple selector, which should be listed in your CSS.
From there, if you want to customize either column, or have CSS that only applys to one column, but not the other, you can use either #content-sidebar .entry{} or #content-nosidebar .entry{}.
If there are other places that you have an .entry class, and you want CSS that applies to both #content-sidebar and #content-sidebar, you can use the combined selector #content-sidebar .entry, #content-nosidebar .entry{}. In this context, it might make more sense to add a common class of .content to #content-sidebar and #content-nosidebar. This would allow you to use a selector of .content .entry, instead of having to shove those IDs into all your CSS.
<div id="content-sidebar" class="content">
<div class="entry"></div>
<div class="sidebar">Sidebar Content</div>
</div>
<div id="content-nosidebar" class="content">
<div class="entry"></div>
</div>
The CSS would make the most sense in the following order:
.entry {
/** CSS common to all .entry elements **/
}
.content .entry {
/** CSS common to all .entry element in all .content areas **/
}
#content-sidebar .entry,
#content-nosidebar .entry{
/** CSS common to .entry elements inside #content-sidebar and #content-nosidebar **/
}
#content-sidebar .entry{
/** CSS specifically for .entry elements inside #content-sidebar **/
}
#content-nosidebar .entry{
/** CSS specifically for .entry elements inside #content-nosidebar **/
}
The reason you build your CSS this was is for
Efficiency - Common CSS is applied globally instead of repeated
multiple times thoughout the CSS
Organization - Common CSS is listed
first, while more specific CSS for specific sections are grouped and
listed afterwards, dispute the CSS specificity of the selectors
making the order irrelevant.
Good luck out there. -Matt
I'm using the <section> tag on several pages, but on ONE page I'm using an <aside> tag that is preceded by the <section> tag.
In the case that I have a <section> immediately followed by an <aside>, I would like to apply a width to both and have the section float left, and the aside float right.
Basically, if I have a <section> and <aside> I want the style to work like this:
section {
width: 500px;
float: left;
padding: 8px;
}
aside {
padding:8px;
width:400px;
float:right;
}
If I only have the <section> I want it to be like:
section {
padding: 8px;
}
Is there a way I can do this with either a conditional statement in CSS3, or a pseudo-class, without having to use JavaScript, custom classes, or separate CSS files?
This only works if the <section/> comes after the <aside/>:
<aside>content</aside>
<!-- if there is another node in between, use the '~' selector -->
<section>content</section>
In that case you could use aside ~ section or aside + section:
section {
padding: 8px;
}
aside + section {
width: 500px;
float: left;
}
In all other cases you'll have to use JavaScript because these selectors only work in one direction, top to bottom.
With CSS4 there might be a way using the Subject of a selector with Child combinator, but that's future. This selector was removed from the specification.
I'm having some difficulty with this example:
<img src="image1/<?php echo $file; ?>.jpg" style="width:500px" />
<p id="caption"><?php echo $caption; ?></p>
I'm trying to get the caption with CSS when hovering the image.
I tried to use a img{hover;} and p {hover;}.
Is there a way for me to get the caption when hovering the image? The example is in PHP and if it was in CSS or Javascript maybe I could search for it, but so far I can't find a solution for this.
I appreciate any explanation & examples.
/* by default, hide caption: */
#caption { display: none; }
/* if caption is next to a hovered img, show: */
img:hover + #caption { display: block; }
jsFiddle Demo
+ is the Adjacent Sibling Selector, supported from IE8.
:hover pseudoclass is used to style elements the mouse goes over
Note that if you want to use more than one caption in your document, you should use a class instead of an id. Ids must be unique in the document.
If you need something that works in IE7, consider HTML like this:
<div class="image-with-caption">
<img src="whatever.png" style="width:200px" />
<p class="caption">caption</p>
</div>
And the CSS would be:
.caption { display: none; }
.image-with-caption:hover .caption { display: block; }
jsFiddle Demo
To affect the style of the p while hovering over the img:
img:hover + #caption {
display: block; /* or whatever...*/
}
Or:
img:hover ~ #caption {
display: block; /* or whatever... */
}
It's worth noting that these examples assume that you have only one p element with an id of 'caption,' if you have multiple p elements with that id, then you need to use a class instead, as an id must be unique within the document.
The + is the CSS adjacent-sibling combinator, and selects the #caption that immediately follows the img over which the user is hovering.
The ~ is the CSS general-sibling combinator, and selects any sibling #caption element that is a later-sibling of the img which is hovered; regardless of any elements that might appear in between (though they do have to be siblings within the same parent element).
Reference:
CSS selectors, at the W3.org.
You want img:hover {/* css */}
Actually, you probably want to do something like this:
<div class="hoverme">
<img src="image1/<?php echo $file; ?>.jpg" style="width:500px" />
<span><?php echo $caption; ?></span>
</div>
and then in your CSS:
div.hoverme span{
display: none;
}
div.hoverme:hover span{
display: block;
}
Try using JavaScript events onMouseOver and onMouseOut to show/hide the caption, something like this:
<img src="image.jpg" style="width:500px"
onMouseOver="document.getElementById('caption').style.display='block'"
onMouseOut="document.getElementById('caption').style.display='none'" />
<p id="caption" style="display:none">Caption here</p>