Skip to main content

CSS Tips and Tricks on How to Get Common Things Done


CSS stands for Cascading Style Sheet and is the styling part of HTML (though not just limited to HTML). It is the primary part of modern WWW alongside HTML and JavaScript. CSS is designed to separate the presentation or the appearance part of a web page so that there is more control and flexibility, and less repetition owing to the fact that the same CSS file can be shared across all or many of the pages of a website. You can make changes just once and it will reflect in all the pages as modern websites use a certain style for most if not all the pages.

In this article we are going to learn how to make certain styling with CSS in an HTML page. See this article for ways to change CSS with JavaScript.

How to Center an Image

HTML

<img src="https://picsum.photos/400/300" class="center-img" />

CSS

.center-img {	
display: block;
margin: 0 auto;
}

Please note that the image needs to be smaller in width to be centered. You can also change the width by using width: 80% or the width in percentage you want. Defining a width would automatically change the height as well while maintaining the aspect ratio.

How to Center Text

HTML

<div class="center-text">Text Here</div>

CSS

.center-text {
text-align: center;
}

How to Center Text Vertically

HTML

<div class="center-text-vert">Text Here</div>

CSS

.center-center-text-vert {
display: table-cell;
vertical-align: middle;
height: 500px;
}

One caveat with the above is that you'll have to specify the height of the element in pixels. In dynamic web applications the height can be set with JavaScript, though.

How to Change Text Color

HTML

<div class="color-blue">Text Here</div>

CSS

.color-blue {
color: #2222CC;
}

How to Change Font

HTML

<div class="font-monospace">Text Here</div>

CSS

.font-monospace {
font-family: monospace;
}

CSS (Alternate web-based font import)

@import url('https://fonts.googleapis.com/css2?family=Major+Mono+Display&display=swap');	
.font-monospace {
font-family: 'Major Mono Display', monospace;
}

How to Make Text Bold

HTML

<div>Text Here <strong>bold text</strong></div>

It is better to use the HTML <strong> tag to mark bold text. But, you can also use CSS font-weight property as in the following example.

HTML

<div class="bold-text">Text Here</div>

CSS

.font-monospace {
font-weight: bold;
}

How to Change Background Color

You can set the background color of the whole page by using the following:

CSS

body {
background-color: #333333;
}

You can also set the background color of any element as in the following example:

HTML

<div class="ele">Text Here</div>

CSS

.ele {	
background-color: #CCCCCC;
}

How to Set Background Image

You can set an image for the whole page by using the following:

CSS

body {
background: url(https://picsum.photos/1600/900); background-repeat: no-repeat; background-size: cover;
}

background-repeat is set to no-repeat as the default value is to repeat the the same image to fill the whole container. The other property background-size can be left to default (which is auto) in which case the image will be displayed as-is whether or not it fills up or is short of the size of the container. The value cover on the other hand will resize the image (maintaining the aspect ratio) to fill the container (in this case the body element). This is the value which would make the most sense in a typical case, but you can always experiment.

How to Center a Button

HTML

<form class="form">
    <button type="button" class="center-button">Click</button>
</form>

CSS

.center-button {
    margin: 0 auto;
    display: block;
}

How to Rotate an Image

HTML

<img src="https://picsum.photos/400/300" class="center-img" />

CSS

.center-img {
    display: block;
    margin: 100px auto;
    transform: rotate(45deg);
}

Popular posts from this blog

Fix For Toshiba Satellite "RTC Battery is Low" Error (with Pictures)

RTC Battery is Low Error on a Toshiba Satellite laptop "RTC Battery is Low..." An error message flashing while you try to boot your laptop is enough to panic many people. But worry not! "RTC Battery" stands for Real-Time Clock battery which almost all laptops and PCs have on their motherboard to power the clock and sometimes to also keep the CMOS settings from getting erased while the system is switched off.  It is not uncommon for these batteries to last for years before requiring a replacement as the clock consumes very less power. And contrary to what some people tell you - they are not rechargeable or getting charged while your computer or laptop is running. In this article, we'll learn everything about RTC batteries and how to fix the error on your Toshiba Satellite laptop. What is an RTC Battery? RTC or CMOS batteries are small coin-shaped lithium batteries with a 3-volts output. Most laptops use

The Best Way(s) to Comment out PHP/HTML Code

PHP supports various styles of comments. Please check the following example: <?php // Single line comment code (); # Single line Comment code2 (); /* Multi Line comment code(); The code inside doesn't run */ // /* This doesn NOT start a multi-line comment block /* Multi line comment block The following line still ends the multi-line comment block //*/ The " # " comment style, though, is rarely used. Do note, in the example, that anything (even a multi-block comment /* ) after a " // " or " # " is a comment, and /* */ around any single-line comment overrides it. This information will come in handy when we learn about some neat tricks next. Comment out PHP Code Blocks Check the following code <?php //* Toggle line if ( 1 ) {      // } else {      // } //*/ //* Toggle line if ( 2 ) {      // } else {      // } //*/ Now see how easy it is to toggle a part of PHP code by just removing or adding a single " / " from th

Introduction to Operator Overloading in C++

a1 = a2 + a3; The above operation is valid, as you know if a1, a2 and a3 are instances of in-built Data Types . But what if those are, say objects of a Class ; is the operation valid? Yes, it is, if you overload the ‘+’ Operator in the class, to which a1, a2 and a3 belong. Operator overloading is used to give special meaning to the commonly used operators (such as +, -, * etc.) with respect to a class. By overloading operators, we can control or define how an operator should operate on data with respect to a class. Operators are overloaded in C++ by creating operator functions either as a member or a s a Friend Function of a class. Since creating member operator functions are easier, we’ll be using that method in this article. As I said operator functions are declared using the following general form: ret-type operator#(arg-list); and then defining it as a normal member function. Here, ret-type is commonly the name of the class itself as the ope