Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce an element's width by a pixel when it is defined in percent

Tags:

html

css

I have four elements each of whom's width is set to 25%. They fill the width of the page perfectly.

I want to put a 1px gap between each one. If I do this (margin-right: 1px; for example), the last element overflows onto the next line. How can I reduce the width of each element by 1px without calculating the width in pixels in the first place?

like image 289
Nick Brunt Avatar asked Nov 01 '12 17:11

Nick Brunt


People also ask

How do I change the width of a percentage in HTML?

Try It: Set the width of an image using a percentage and resize your browser window. For the width attribute, you can also use percentages, where 100% is the total space available. So, if you wanted an image to be one quarter the width of the window, you could set width to 25% (one quarter of 100% is 25%).

Can we give width in percentage in CSS?

The <percentage> CSS data type represents a percentage value. It is often used to define a size as relative to an element's parent object. Numerous properties can use percentages, such as width , height , margin , padding , and font-size . Note: Only calculated values can be inherited.

Can we give width more than 100%?

Yes, as per the CSS 2.1 Specification, all non-negative values are valid for width, that includes percentage values above 100%. Show activity on this post. Percentage values simply represent a percentage of the length of the element's container.


1 Answers

I have just found a solution myself, with the help of @Lubnah in the comments.

.tab-list li {
  margin-right: -1px;
  border-left: 1px solid #fff;
}

.tab-list li:first-of-type {
  border-left: none;
  margin-right: 0px;
}
like image 137
Nick Brunt Avatar answered Nov 04 '22 11:11

Nick Brunt