Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

css

width

tags

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.

like image 816
Yeseanul Avatar asked Mar 12 '12 16:03

Yeseanul


People also ask

How do I fix width in HTML?

To convert it to a fixed-width layout, simply add a fixed with to the #wrapper and set the margins to auto. Setting the margins to auto will cause the left and right margins to be equal no matter how wide the browser window is, which will cause your fixed-width layout to be positioned in the center of the browser.

How do you change the width of text in HTML?

In HTML, you can use the width attribute to set the width of an element. Alternatively, you can also use the size attribute to define the width of the <input> .

What is the width in HTML?

The width attribute specifies the width of the element, in pixels. Note: For input elements, the width attribute is used only with <input type="image"> .


2 Answers

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

See an example

like image 123
Starx Avatar answered Oct 26 '22 18:10

Starx


<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 */
}
like image 24
Rob W Avatar answered Oct 26 '22 17:10

Rob W