Posts

How to indicate where a word should wrap in HTML/CSS

By using word-wrap, word-break and overflow-wrap in CSS you can control the browser’s behaviour when it comes to word wrapping. However, it is not possible to control where the word breaks. It just breaks when the word no longer fits in the container.

To indicate where a word can be broken, the soft hyphen can be used. The soft hyphen is a html code:

­

For example:

Thisisaverylongword

and we want it to break just after “very”, then we type:

Thisisavery­longword

When the container doesn’t fit the word, it will be displayed on two lines:

Thisisavery-
longword

Anchor links does not scroll to the correct position

There are several issues that can cause local anchor links to not scroll to the right position. The first thing to check is if you have an id-tag that has the same name as the anchor. In this case the browser will scroll to the id position instead. The solution is to name the anchor and id with different names.

Example:

<div name="myanchor">
<p>Some random text</p>
</div>
<a href="#myanchor">Go to some random text</a>

The above example will behave as expected.

But in this example the link will scroll to the h1 instead of the div:

<h1 id="myanchor">
<div>
<p>Some other random text</p>
</div>
<div name="myanchor">
<p>Some random text</p>
</div>
<a href="#myanchor">Go to some random text</a>

To avoid this, make sure the anchors (name) and id have different names.

There are also other sitiuations where the local anchor does not scroll correctly covered in this article.

How to vertically center text in a div

This is a neat way of centering text in a div. Live demo here.

 

<html>
<head>
 <title>Vertical text center demo</title>
</head>
<body>
<style>
.textbox {
 height: 200px;
 width: 300px;
 text-align: center;
 font-size: 14px;
 font-weight: bold;
 background-color: blue;
}
.textbox p {
 height: 100%;
 display: flex; /* vertically center text */
 justify-content: center;
 align-content: center;
 flex-direction: column;
 margin: 0 50px;
 color: white;
}
</style>
<h1>Vertical text center demo</h1>
<div class="textbox">
 <p>This text is vertically centered in the box</p>
</div>
</body>
</html>

Centering a horizontal bullet list

Horizontal bullet lists are often used to create a horizontal menu. This is how to make the horizontal bullet list centered.

  1. Create a <div> container and set the text-align: center style on it.
  2. Create a <ul> <li> inside the div and set display: inline-block style on it.

This is an example of how it is done:

<html>
<head>
  <style type="text/css">
   div#container {
     text-align: center; /* center the horisontal list */
   }
   ul.list {
     list-style: none; /* no bullets */
     display: inline-block; /* this will cause the list to be centered */
   }
   ul.list li {
     float: left; /* horizontal list */
     margin-left: 10px; /* some space between items */
     margin-right: 10px;
   }
  </style>
</head>
<body>
<div id="container">
 <ul class="list">
   <li>Item 1</li>
   <li>Item 2</li>
   <li>Item 3</li>
 </ul>
</div>
</body>
</html>

Start embedded Youtube video at specific time

When you share a video on Youtube you can select to start the shared video at a certain point of time in the video. Like this (selected to start at 0:40).

Youtube share with start at

This option however is not availible when embedding a video. It can be solved manually though. The embed code from Youtube looks something like this:

<iframe width=”560″ height=”315″ src=”//www.youtube.com/embed/6bHSP9Ddw5w” frameborder=”0″ allowfullscreen></iframe>

To start the video from a specific time, you add the ?start=XXX where XXX is the point counted in seconds. So if the start time includes minutes you have to convert into seconds. If I want the embedded video to start at 0:40 I add ?start=40 like this:

<iframe width=”560″ height=”315″ src=”//www.youtube.com/embed/6bHSP9Ddw5w?start=40” frameborder=”0″ allowfullscreen></iframe>

This is the embedded video starting at 0:40 (and yes, that is me reverse driving an articulated 18 meter bus – sometimes, when I’m not busy ruling Nerdia or working as an IT consultant I walk out into the real world where I sometimes work as a bus driver and it is great fun! :)):

Vertical and horizontal center img in div

This example shows how an image (img) can be vertically and horizontally centered inside a div. At the same time it illustrates how to resize the image to fit inside the div.

A live view of this code can be seen here. 

<html>
  <head>
    <title>Vertically aligned img in div</title>
    <style type="text/css">
      #container {
        position: absolute;
        width: 600px;
        height: 600px;
        background-color: lightyellow;
        vertical align: middle;
      }  

      #container img {
        max-width: 80%;
        max-height: 80%;
        margin: auto;
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
      }


      p {
        text-align: center;
      }
    </style>
  </head>
  <body>
    <div id="container">
      <img src="sl-buss-6050.jpg" alt="SL-buss 6050" title="SL-buss 6050">
      <p>The yellow box is here to show the div</p>
    </div>
  </body>
</html>

Div with width 100% and margins or padding gets wider than 100% [solved]

If a div is set to width (or height) 100% and margins or padding are added, it will be wider than 100% causing the scrollbars to appear in the browser. 

For example like this (click here to view live sample):

<html>
<head>
  <style type="text/css">
    body, html {
      width: 100%;
      border: 0;
      margin: 0;
      padding: 0;
    }
    #wrapper {
      width: 100%;
      margin: 0;
      padding: 0;
      border: 0;
    }
    #container {
      width: 100%;
      margin: 10px;
      padding: 0;
      border: 0;
      background-color: lightblue;
    }
  </style>
</head>
<body>
  <div id="wrapper">
    <div id="container">
      <h1>Div taking more than 100% space</h1>
      <p>It demonstrates a div that is taking up more than 100%. Notice the horizontal scroll bar below.</p>
    </div>
  </div>
</body>
</html>

The solution is to set the inner container width to auto instead of 100%, like this (click here to view live sample):

<html>
<head>
  <style type="text/css">
    body, html {
      width: 100%;
      border: 0;
      margin: 0;
      padding: 0;
    }
    #wrapper {
      width: 100%;
      margin: 0;
      padding: 0;
      border: 0;
    }
    #container {
      width: auto;
      margin: 10px;
      padding: 0;
      border: 0;
      background-color: lightblue;
    }
</style>
</head>
<body>
  <div id="wrapper">
    <div id="container">
       <h1>Div NOT taking more than 100% space</h1>
       <p>It demonstrates a div that is NOT taking up more than 100%. Notice the absence of the horizontal scroll bar below.</p>
       <p>The difference/solution is to set the inner div with width: auto.</p>
    </div>
  </div>
</body>
</html>

Make a weblink (a href) not clickable

Sometimes you have a need to prohibit a visitor from clicking on a link on your site. For example if you are including content from another soruce via Atom or RSS and they contain links.

The best way is of course to remove the link tag (<a href="" >) from the content. But if this is not possible an alternate solution can be to cover it with a transparent div placed on top of the content using the z-index.