how to override element.style using css - html

I have a xhtml page and i am trying to override the element.style.
source page view when i inspect
I had tried to apply my own CSS but it didn't work at all.
I have search through the similar question that i face but no one is working for me.
.element .style {
text-align: center;
background-color: green;
}
Anyone have any idea how can i apply my own CSS so that i could override this style.element?
<h:panelGrid id ="cibBulk" styleClass="interfaceDashboard" >
<h:outputText style="text-align: center;background-color: green;" styleClass= "interfaceTransactionTitle" value="CIB BULK UPLOAD TO HOST" />
<..../>
</h:panelGrid>
The output of my screen page (the background color not fit at all) would not be what i expected for as the screen attached.
Output screen image

That is inline CSS style which is used to apply a unique style to a single HTML element.They come from the style="" attribute that sets element.style.someProperty.
For example :
<h1 style="color:blue;">Blue Heading</h1>
This will set heading to color blue. If you inspect then you will see like this:
Like in your snapshot if you want to change the style, you can put style attribute in tbody. Learn more here.

div{
background-color: green;
}
.element.style {
text-align: center;
color: #fff;
}
<div>
<span class="element style">Hello</span>
</div>

Related

How do you style an image in an <a> tag?

Here's some code that contains a tags, one which contains an image.
<div class="container">
Not styled
<img src="image.png">
</div>
If I only want to style the image, how would I do it (without creating a class or something similar)?
For example, if I want to style all the a tags, I could use the following CSS:
.container a {
/* styles here */
}
If I want to style all the img tags, I could use this:
.container img {
/* styles here */
}
Is there a way to apply this same logic to an img in an a tag?
Edit: Here are the styles I'm applying. For some reason, when I use .container a img it adds extra padding/margins.
.container a {
padding: 9px 10px 5px 10px;
}
Edit 2: I think the problem lies elsewhere. Whenever I try any of the suggested responses (i.e. .container a img, #img, src="image.png") they all lead to the amount of vertical padding/margin increasing. Should I delete my post? It seems all it is getting is downvotes right now.
Yes You can do that, Have a look into the demo, it will be applied to all the images under a tag
.container a img {
/* styles here */
}
If you just want a single image to be applied for css, try giving it an ID, then apply css to an id
Demo which applies to all
.container a img{
filter: sepia(100%);
}
<div class="container">
Not styled
<img src="https://www.whistler.com/images/placeholders/200x200.gif" />
<img src="https://www.whistler.com/images/placeholders/200x200.gif" />
</div>
Demo which applies to single id
#img{
filter: invert(100%);
}
<div class="container">
Not styled
<img src="https://www.whistler.com/images/placeholders/200x200.gif" />
<img src="https://www.whistler.com/images/placeholders/200x200.gif" id='img' />
</div>
You can do a nested CSS
.container a img {
/* styles here */
}
.container a img {} is the best way to do it, but every IMG will use the amount of padding/margin that you've given in the .container a {padding: etc }. So try to position the IMG with margins.
I think you can simply use CSS to point exactly to the image like below:
img[src="image.png"]{
}
your question: If I only want to style the image, how would I do it (without creating a class or something similar)?
Now, if you only want that specific image no problem, but if later more and more images will behave the same way you better create a class
note: you didnt specify what styles you wanted for the image, so I asummed you wanted this ones padding: 9px 10px 5px 10px
<div class="container">
Not styled
<img style="padding: 9px 10px 5px 10px;" src="image.png">YES styled
</div>
Give your image a seperated class and then style it in your css
You just to need to write CSS with a in heirarchy as
.container a img {
// your code
}

Why is * given more specificity than property inheritance in CSS?

Put simply, I have a page with these two styles:
* {
color: black;
}
div.error {
color: red
}
And a page structure like:
<html>
...
<div class="error">
<div class="row form">
<div class="column">
Error text.
</div>
</div>
</div>
...
</html>
You would expect "Error text" to be red, wouldn't you. But it is, in fact, rendered black in all browsers. Is this the expected behavior?
My second question, is contingent on whether this is the expected behavior. if it is, then why would a designer ever color every element on his whole website with "black" or some other color if that means it cannot be overridden with inheritance in specific places?
--EDIT--
The question is asked in the context of where you'd want a default color to be placed across the whole website, but wherever you want, you could say "this whole section inherits color #ffeeff". For example, a special form, contained by a divider of class "form." You don't want to label every sub-element of form with a special class like "white-text" to color everything white. You just want to set the "form" class's color and have it propagate to sub-elements.
* is more specific than agent stylesheets (the default stylesheets that come with the browser), and inherited properties are nothing more than something like this:
div {
/* ... */
color: inherit;
/* ... */
}
In the agent stylesheet, so your * with color: black is more specific than agent:div with color: inherit, thus it wins.
It is the expected behavior, for the text to be red, you want to specify:
div.column {
/* ... */
color:red;
/* ... */
}
Do check: (why) is the CSS star selector considered harmful? as suggested by 4castle.
just do that instead:
<style>
* {
color: black;
}
div.error {
color: red
}
</style>
<div>
<div class="row">
<div class="column error">
Error text.
</div>
</div>
</div>

How to Assign on demand CSS Styles to some div or class?

I'm working on a Classifieds/Ads Page for a client using content from a specific Database. I'm close to get the look I'm looking for but now I have a problem. I don't want CSS Styles for the empty boxes to the available spaces.I just want the CSS Styles on the ones that will appear once the information arrive to the database and it's ready to show on the Classifieds/Ads page.
Is there a way to have a CSS style that only is applied when content is present?
This is my test page.
notice how I kind of cheated the CSS style so the blank boxes (spaces) show a text saying: "place your ad here!" but I really don't want those blank boxes there. Any help will be apreciate!
thanks in advance.
All you need to do is to find the parent div which does not have content on its header (for instance) using jQuery. Then you can set its display property to none. I have created a working example ****HERE****
HTML:
class="ad">
<h2>This is an ad</h2>
<p>this is the ad which has been added by the database</p>
</div>
<div class="ad">
<h2></h2>
<p></p>
</div>
CSS:
* {box-sizing: border-box;}
h2, p {margin:0; padding:0;}
.ad {
display: block;
float: left;
height:300px;
width:130px;
background: #999;
margin: 10px 20px;
padding: 5px;
}
jQuery:
$(".ad").each(function() {
if ($(this).find("h2").text() == "") {
$(this).css("display","none");
};
})
This seems more of a backend issue than a CSS one. Your php script shouldn't be outputting html for ads that don't exist.

how to add more specificity to css class .background-color-blue so it overrides default div's background color

I have this CSS class
.background-color-blue {
background-color: #00C0FF;
}
and I want to be able to use it on some elements if needed.
I now it is not very semantic, but it is used for HTML template that is meant to be easy to use, small in size, and universal.
I want to be able to use it on elements like some panels, sidebars, modals, top-bar menu , or whatever I want
of course it works, but only for divs that do not have background-color already specified.
in this case:
<div class="modal background-color-blue"></div>
.modal has already specified bg color to #fff. now it does not work - it stays white.
I have found two solutions for that:
.background-color-blue {
background-color: #00C0FF !important;
}
and
div.background-color-blue {
background-color: #00C0FF;
}
I am not sure about these... which solution is better? Or is there any other solution that would work better?
I think this method:
.modal.background-color-blue {
background-color: #00C0FF;
}
is not good since I would have to do it with any similar element.
and
<div class="modal">
<div class="background-color-blue">
</div>
</div>
also isn't good since modal already has some padding.
Just use !important it will help to override
.background-color-blue {
background-color: #00C0FF !important;
}
Take a look at this : when-using-important-is-the-right-choice
If you think that modal will be able to have different colors, i guess you'd like this (ITCSS - BEM inspired). This will help you to stay at a low specificity rate, preventing you to have some problems with future classes or have to overwrite them.
Exemple here :
http://codepen.io/AxelCardinaels/pen/ZGVKzp
HTML :
<div class="container">
<div class="modal modal--grey">
<h1 class="modal__title"> Modal Title</h1>
<p class="modal__text">Hello this is the content of the modal !</p>
</div>
</div>
CSS :
/*Base Class */
.modal{
width:40%;
margin:0px auto;
padding:20px;
text-align:center;
border-radius:10px;
box-shadow:1px 1px 1px rgba(1,1,1,0.3);
}
/*Attribute classes for the modal, just make a choice in your HTML ! */
.modal--grey{
background:rgb(220,220,220);
border:1px solid rgb(200,200,200);
}
.modal--blue{
background:rgb(65,105,225);
border:1px solid rgb(58,95,205);
}
The proper usage is
! important
Normally it should have been blue background. Perhaps something else that is preventing the elements of blue.
But don't forget that; the last !important taken into account.

Change image size within a division

I have a division placed on the bottom of the page. I put an image into this division, but I don't know how to modify the image. The problem may be, that the inline style for <img> is setting modification rules for all images. I have an inline style sheet that has this code and HTML code for <div>.
My CSS code looks like this:
<style type="text/css">
img {
image-align: center;
padding: 10px;
height: 200px;
width: 140px;
}
div {
position: absolute;
right: 10px;
}
</style>
And my HTML code is like that:
<div align="center" >
<img src="images/music_banner.jpg" >
</div>
you can do this:
div img{
}
or give the div a name and do this
#div img{
}
or you give the img an id as below
<div>
<img id="mg"/>
</div>
Use id as #mg in CSS code.
or you can do as define class name in img tag.
<div>
<img class="mg"/>
</div>
Use class as .mg in CSS Code.
You might try learning a little bit more about CSS selectors: these are the rules that tell the browser which element you'd like to apply the following rules to.
I would recommend Code Academy for an easy to follow course. You can skip down to the CSS section if you are already comfortable with HTML.
Note: if you google CSS, you'll get "w3schools" as the first results. That website is generally derided on Stack Overflow. I don't know if it's really that bad, but I tend to skip it just because everyone else has a bad opinion of it. Your call if you find it helpful of course.
I should note that I like to use the W3C (World Wide Web Consortium) website for reference, as they're the ones trying to make everything standard. It is a pretty technical read, though.
Create a div element in your HTML code:
<div class="parent">
<img src="image">
</div>
Than add this to your CSS code:
.parent {
width: 42px; /* I took the width from your post and placed it in css */
height: 42px;
}
/* This will style any <img> element in .parent div */
.parent img {
height: 100%;
width: 100%;
}