Document body style css

How To Style the Body of a Website With CSS

In this tutorial, you will style the body of a webpage with a CSS rule. You will use this rule to apply and style a background image and set the font family for the webpage. You will also create a style rule that changes the color of all hyperlinked text to a color that better matches the demonstration website’s color palette.

This exercise will be used to recreate the style of the demonstration site but you can apply and modify the same rules used here for other HTML/CSS website projects.

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.

For this tutorial, we suggest you use the background image from the demonstration site which you can download from this link. You may use another image as your background, but make that sure that the image is large enough to fill the screen.

Note: To download the background image of the demonstration site, visit this link and click CTRL + Left Click (on Macs) or Right Click (on Windows) on the image and select “Save Image As” and save it as background-image.jpeg to your «image’ folder.

Читайте также:  Php class extends plugin

Once you have selected an image, make sure it’s saved as “background-image.jpeg” in your images folder. You are now ready to proceed to the next step.

Adding a Background Image To Your Website With CSS

To declare style rules for the body of a webpage, you will need to create a CSS rule for the body tag selector. These rules will then be applied to all elements that are placed inside the opening and closing tags that you added to the index.html file in the earlier tutorial How To Set Up Your CSS and HTML Website Project.

To add a background image to your site, create a CSS rule using the tag selector. Erase everything in your styles.css file (if you have been following along with this series) and add the following ruleset:

/* General Website Style rules */ body  font-family: "Helvetica", Sans-Serif; background-image: url("../images/background-image.jpeg"); > 

Take note of the highlighted file path, which tells the browser where to locate the background image. If you have changed the name or location of the image then you will need to adjust the file path here accordingly.

Let’s pause briefly to understand each of the declarations in this ruleset:

  • /* General Website Style rules */ is a CSS comment, which is not displayed by the browser. Like HTML comments, CSS comments are useful for explaining and organizing your code for future reference. Notice that CSS comments open and close with /* and */ tags instead of tags used for HTML comments.
  • The font-family: «Helvetica», Sans-Serif; declaration sets the font family (Helvetica) and generic font family (Sans-Serif) for all the text on the webpage. (Note that you can specify different font families for text content on the same webpage by adding CSS rules later on). The generic font family is given as a backup in case the first font family isn’t available and the browser needs to pick a back up font. You can explore other fonts by replacing “Helvetica” with other font names, such as Times , Courier , or Palatino .
  • The background-image: url(» ../images/background-image.jpeg ;») declaration tells the browser to add a background image to the webpage using the file found with the specified file path. Note that you have prepended ../ to the file path name to tell the browser to locate the images folder in the directory above the directory that contains the file you are working in ( styles.css ).

Save your styles.css file and load the index.html page 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.

You should receive a page with no content except for the background image:

Webpage with background image only

If you don’t receive an image, check to make sure your file path is correct and that there are no errors in your index.html file and styles.css file.

Changing the Color of Hyperlinked Text

Next, we’ll add a CSS rule that changes the color of all hyperlinked text to a color that better matches the website color palette.

At the bottom of your styles.css file, add the following ruleset:

Conclusion

You should now have a webpage with a large background image. In addition, you declared a font family that will be applied when you begin to add text content. Using rulesets like these allow you to change the font and background image of a webpage by creating a ruleset for the body tag selector. Finally, you created a style rule that specifies the color of any hyperlinked text you add to the page.

In the next tutorial, you’ll recreate the header section of the demonstration website.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Tutorial Series: How To Build a Website With CSS

This tutorial is part of a series on creating and customizing this website with CSS, a stylesheet language used to control the presentation of websites. You may follow the entire series to recreate the demonstration website and gain familiarity with CSS or use the methods described here for other CSS website projects.

Before proceeding, we recommend that you have some knowledge of HTML, the standard markup language used to display documents in a web browser. If you don’t have familiarity with HTML, you can follow the first ten tutorials of our series How To Build a Website With HTML before starting this series.

Источник

How to apply CSS to HTML body element?

unfortunately I am not able to get this working in Firefox, chrome. Am I doing anything wrong here? Is there any way we could apply a CSS class to body?

3 Answers 3

You are doing two different things there. Your JavaScript is actually assigning the class «someclass» to your body element, while your CSS is styling a body element with the class «someclass», it won’t assign the class to it, that is not the task of CSS. You would do it in plain (X)HTML like this:

Part of the problem is that valid XHTML can only have one element per document. A class is generally ascribed to an element that occurs multiple times (or, rather, to a style that you want applied multiple times).

SINCE there’s only ever one body, I’ve never added an ID or a class to it. Just style:

If you have a class you want to reuse, use a comma in your selector:

All of this is potentially meaningless, however, because anything applied to the tag will be inherited by its children unless explicitly styled otherwise.

Maybe he is later changing the class using JavaScript and just wants to provide a default in a verbose manner? of course I am just making assumptions there.

I think it’s important to note that there’s only one body tag, but if you link to an external css file then that file could potentially encounter several different web pages that need the body tag styled differently. It’s also kind of moot if you use the document level css styling in the header instead of adding classes for body tags in the external css file.

There is no need for a class on body since you will only have one body in your document. Unless maybe you want to change the style of the body by changing with JavaScript class for some reason.

If you have to put a class on body do it like this:

And then access the class and modify the style in your css like this:

However you can change the style of all elements of a tag and since you will only have one body you could simply add this to the css:

And not add anything to the html. However with reoccuring tags do use class for a style that will be used several times and id for a style that will only be used one time on a page. You assign an id to a tag like this in the html:

Источник

Changing body tag style through JavaScript

I’m trying to write a script to change the width of the page, considering user’s client width. It’s something like this:

function adjustWidth() < width = 0; if (window.innerHeight) < width = window.innerWidth; >else if (document.documentElement && document.documentElement.clientHeight) < width = document.documentElement.clientWidth; >else if (document.body) < width = document.body.clientWidth; >if (width < 1152) < document.getElementsByTagName("body").style.width="950px"; >if (width >= 1152) < document.getElementsByTagName("body").style.width="1075px"; >> window.onresize = function() < adjustWidth(); >; window.onload = function() < adjustWidth(); >; 
document.getElementsByTagName("body").style is undefined 

Now my question is, how can i access the style of body? Because in the css sheet its selector and width property are defined.

try getComputedStyle(elements).[style] and example window.getComputedStyle(document.body).width . learn javascript.info/tutorial/styles-and-classes-getcomputedstyle

6 Answers 6

That function returns a list of nodes, even though there’s only one .

document.getElementsByTagName("body")[0].style = . 

Now, that said, you may want to look into doing this with CSS instead of JavaScript. With CSS media queries, you make such adjustments:

@media screen and (max-width: 1151px) < body < width: 950px; >> @media screen and (min-width: 1152px) < body < width: 1075px; >> 

Media queries work in IE9 and pretty much all other modern browsers. For IE8 you can fall back to JavaScript or just let the body be 100% of the viewport width or something.

Источник

Оцените статью