March 12, 2012

Is there a way to change the width of the <code> HTML tag?

Question by Yeseanul

I’ve noticed that any modification of the <code>‘s style with respect to its width doesn’t have any effect. It’s seems to always be set to “auto”.

I’m just trying to have some code written inside a <code> tag (this tag is mandatory due to some known iBooks bugs) that has 100% width. One workaround is to put the <code> inside a <div> which has a 100% background style. This works OK but I’ll have to deal with a couple of hundred <code> tags… This is the reason I would prefer just to be able to modify the <code>‘s width.

Any thoughts?
Thanks.

Answer by Rob W

<code> elements are inline elements. Setting a height or width on these do not have any effect on their size.

Use display:inline-block::

code {
    display: inline-block; 
    width: 100px; /* Whatever. The <code>'s width will change */
}

Answer by Starx

Add a display: block or inline-block as per you requirement to the code element. Rest should work as planned

See an example

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!