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

Designing a Simple Order Form Application in PHP

Ok guys, for this post we’re going to create a simple yet complete order form page. Order forms are used on many sites to take customers order online. Order forms should have the capability to take orders from visitors regarding what items they want to purchase and store the information for further processing. For this post’s example, we are going to create an order form for a Book Seller. The form will be designed to take order of five different items (books). Our order form application should be able to take order of five different items in any separate quantities tht user wants, it should also ask for shipping address and name of the customer. It should then store the information provided in a file along with the date and time order was placed. The application should also be able to take any number of orders and store them all linearly for further human processing. For this, we need a front end of a HTML form to which the user would interact a