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

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 {   ...

How to Create an HTML Form Linked with MySQL Database in PHP

If you're looking for example code and detailed discussion on how to create an HTML form that stores data in a MySQL database using PHP then this post might be what you're looking for. I assume that you're familiar with basic HTML, CSS, PHP coding, and  MySQL. I am going to divide this small project into two parts: The HTML form itself that takes input from the user and the PHP script that saves it into the database A table that displays the user-added data that has been saved in the database. We'll be dealing with the first part in this tutorial. Again I'd like to break this problem into a few parts so that it's easier for you to understand and probably gives you an insight into how breaking up a problem into smaller chunks can help make things clearer in your mind. Let's think about it, there is an HTML form (that is HTML code), then there is the PHP code that deals with the user-input data, and the MySQL database itself. For this tutorial, we'll b...

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 ...