Apply css on div

How do i apply css to the inner level of an element?

Suppose there are number of div tags and if there are an element at the innermost level then how do I access that one or declare css for that element?

How do I access the element

or set the background image for it in a separate css file?

7 Answers 7

It depends on how specific you want the style to be, say if you have:

You can style the

as generic/specific as you want:

p < color:red; /* Affects all p */ >p.description < color:blue; /* Affects all p with class description */ >.some-container p < color:black; /* Affects all p that is a descendant of an element with some-container class */ >.page .content .some-container p.description < color:white; /* You get the idea */ >

thanx for such a nice reply, but if there are in similar manner a div has id then the attribute will be accessed in the same manner or not?

Easiest way may be defining a css class and using that class for element.

A space between CSS selectors allows you to reference descendant elements, no matter how far down the tree they are.

So if your top-level has a class of topdiv , you can access all the

elements beneath it in the tree with a simple CSS selector like this:

If you need to be more specific, you can use a class from one of the divs further down the tree, either instead of the .topdiv reference, or alongside it:

will reference all the

elements contained within a middlediv element that is itself contained within a topdiv element.

You can continue this pattern to make is as specific as you need to, in order to be sure you only reference the elements you want.

If you need to get more specific than that, you can replace the space separator with an angle bracket > . This specifies that the next item in the selector is an immediate child element of the previous one, rather than being anywhere down the tree.

There are a number of other CSS selectors that might be handy to know, but those are the most common ones in use, and the most likely to help in your situation.

Источник

How To Style the HTML element with CSS

This tutorial will introduce you to styling the HTML Content Division element—or element—using CSS. The element can be used to structure the layout of a page and break up a webpage into separate components for individual styling. In this tutorial, you will create and style elements, as well as learn how to add and style other elements inside a container. These skills will prepare you to use elements as layout tools later on in the series when you begin recreating the demonstration website.

The

element is used by adding opening and closing

tags to an HTML document. On its own, the

element typically has little visual effect on the presentation of a webpage. To specify the size, color, and other properties of a
element, you can assign it style rules using CSS.

Prerequisites

To follow this tutorial, make sure you have set up the necessary files and folders as instructed in a previous tutorial in this series How To Set Up You CSS and HTML Practice Project.

Exploring the Element in Practice

Let’s try a hands-on exercise to study how the element works. Erase everything in your styles.css file (if you added content from previous tutorials). Next, add the following CSS rule for the tag selector:

div  background-color: green; height: 100px; width: 100px; > 

Save the styles.css file. Next, return to your index.html file, erase everything that’s there (except for the first line of code: ) and add the following code snippet:

Notice that the element has opening and closing tags but does not require any content. Save the index.html file and reload it in your browser. (For instructions on loading an HTML file, please visit our tutorial step How To View An Offline HTML File In Your Browser).

Your webpage should display a green box 100 pixels wide and 100 pixels tall as specified by the CSS rule:

Webpage with green <div data-lazy-src=

Note that you have added the class as an attribute to the tag by adding the class attribute and class name to each opening tag. Save the file and reload it in your browser. You should receive something like this:

Adding and Styling Text in a Container

You can put text inside a container by inserting text in between the opening and closing tags. Try adding text inside each of the elements in your index.html file:

div class="div-1">Bluediv> div class="div-2">Reddiv> div class="div-3">Yellowdiv> 

Save the file and reload it in your browser. You should now have text displayed in each of your containers:

Webpage with elements containing text

You can add additional HTML elements to your text inside the elements. For example, try adding the HTML heading tags ( to ) to your text inside the tags in your index.html file:

div class="div-1">h2>Blueh2>div> div class="div-2">h3>Redh3>div> div class="div-3">h4>Yellowh4>div> 

Save the file and reload it in your browser. The text inside the containers should now be styled according to the default properties of the to tags:

Webpage with header text inside containers

Note that the elements have also adjusted their positions slightly. This repositioning is caused by the default margin properties of the through elements. You’ll learn more about margins in the next tutorial on the CSS Box Model, but for now it is fine to ignore them

To style text inside the containers, you can specify text property values in the rulesets for your classes. Try adding the properties and values to your rulesets in your styles.css file as highlighted in the in the following code snippet:

.div-1  background-color: blue; height: 50px; width: 50px; font-size: 10px; color: white; iu > .div-2  background-color: red; height: 100px; width: 100px; font-size: 20px; color: yellow; > .div-3  background-color: yellow; height: 200px; width: 200px; font-size:30px; color: blue; > 

Save your styles.css file and reload the index.html file in your browser. The text inside the containers should now be styled according to the CSS rules in your styles.css file:

Webpage with styled header text inside <div data-lazy-src=

Jquery — apply css style to all elements within specifed div?

I have a situation where I am setting up a mobile theme for a wordpress website. Now what I would like to do is, grab any elements (p, divs, etcc) within the «#content» div, and apply css «width: 100%» to each of those child elements. The reason I want to this is, in event somebody sets a fixed width for a div, I need it to overwrite that and revert it to 100% so it does not get cutoff when viewing on a mobile device with a smaller screen. I would like to know how this can be achieved using Jquery. I appreciate any help with this. Thanks

If you’re setting up a «mobile theme», surely you have a «mobile stylesheet»? Use CSS to make this change, not JavaScript..

6 Answers 6

Sometimes, jQuery is the wrong way.

You shouldn’t use jQuery unless it’s offers a legitimate advantage. Often times using standard JavaScript will give you enormous performance advantages. With your situation, you could do something like the following:

var i, tags = document.getElementById("content").getElementsByTagName("*"), total = tags.length; for ( i = 0; i

That being said, the jQuery method is pretty simple as well.

This will run down into each level of #content , affecting all elements.

Performance Differences

Using http://jsperf.com to compare the peformance difference here we can see the magnitude of speed raw JavaScript has over the jQuery alternative. In one test JavaScript was able to complete 300k operations in the time it took jQuery to complete 20k.

But, Why JavaScript?

Ultimately the question of whether jQuery or Raw JavaScript is better is a red-herring, distracting from the real question — why use scripting at all? If you detect a mobile browser, load a new stylesheet containing mobile rules:

Источник

Читайте также:  Python shutil удалить файл
Оцените статью