Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use HTML Div to add border only on top and bottom?

Tags:

html

I use HTML DIV tag to format my header with border like this:

<h3 style="background-color:red; padding:2% 0 2% 0; border:5px solid green"> This is a header </h3> 

However, I would like to have the border appears only on top and bottom, but not on left and right. I would like border in the same way as padding for top left bottom and right, but border does not have this feature(or I don't know?). Is there any way in CSS style to do that?

like image 398
David.Chu.ca Avatar asked Jun 15 '11 16:06

David.Chu.ca


People also ask

How do I put a border on top and bottom only?

You can use the border-left , border-right , border-top and border-bottom properties to give different border styles to each side of your container. In your case, you want to apply your style to border-top and border-bottom .

Can you add border to div in HTML?

You can use: border-style: solid; border-width: thin; border-color: #FFFFFF; You can change these as you see fit, though.


1 Answers

For future reference, the following is a an alternative method that avoids having to define border properties (colour, width) more than once and allows declarations similar to those used for margin and padding (as requested).

border: 5px green; border-style: solid none; 

As with margin or padding, border-style is defined in order of top, right, bottom, left. The above case applies a solid border to top and bottom of an element and no border to the left or right of the element.

Using this method avoids creating redundancies in the declaration.

like image 158
user1823021 Avatar answered Sep 28 '22 09:09

user1823021