Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a auto margin in css but want a minimum margin

Tags:

html

css

I had checked Using a percentage margin in CSS but want a minimum margin in pixels?. It had how to implement a minimum margin when using percentage. But i am using

width:80%; margin:5px auto; 

How to set a minimum margin on the left side. i had used auto margin as per Reducing the space equally when resizing browser window

Thanks

like image 267
harikrishnan.n0077 Avatar asked Nov 23 '12 10:11

harikrishnan.n0077


People also ask

Can you set a minimum margin in CSS?

Yes, you can! In 2020 this is now very straightforward using the CSS math functions: min(), max(), and clamp(). A min calculation picks the smallest from a comma separated list of values (of any length). This can be used to define a max-padding or max-margin rule: padding-right: min(50px, 5%);

What happens when you set margin to auto?

The auto Value You can set the margin property to auto to horizontally center the element within its container. The element will then take up the specified width, and the remaining space will be split equally between the left and right margins.

What does margin 0 Auto in CSS do?

So in margin: 0 auto, the top/bottom margin is 0, and the left/right margin is auto, Where auto means that the left and right margin are automatically set by the browser based on the container, to make element centered.

Is Auto a valid value for margins?

The margin property may be specified using one, two, three, or four values. Each value is a <length> , a <percentage> , or the keyword auto . Negative values draw the element closer to its neighbors than it would be by default.


2 Answers

It might take another div to accomplish this task. I will refer to the div you are talking about as the #content div. I know that yours will have a bit more css. This is just my example of the margin in question. Next we would put the #content div in a div we will call #container. We will set the margins to auto in this div as well. The added aspect will be that we will add padding (right and left) to the #container div.

#content {     margin: auto; }  #container {     padding-right: 5px;     padding-left: 5px; } 

I think that this would achieve what you are looking for. Keep in mind a min-width for the #content div and it could work nicely.

like image 122
Patrick Avatar answered Oct 11 '22 12:10

Patrick


I'm a little late, but here's an answer. Don't forget that you can style the HTML element!

html {   padding: 20pt; }  body {   margin: auto; } 

That's it!

like image 29
Greg Searle Avatar answered Oct 11 '22 13:10

Greg Searle