How can a div get centered when i don't specify a fixed width?
When a design is fluid (there is no fixed width), how can wrapper
div using margin:0 auto;
be centered?
#wrapper {
max-width: 1000px;
min-width: 767px;
margin: 0 auto;
}
html css
|
show 4 more comments
When a design is fluid (there is no fixed width), how can wrapper
div using margin:0 auto;
be centered?
#wrapper {
max-width: 1000px;
min-width: 767px;
margin: 0 auto;
}
html css
1
If your question is "how can it get centered when I don't enter a width?" then I can answer that quickly: You have given a maximum and minimum. So it'll first check if the maximum value1000px
is possible to set. If that is the case, it'll be1000
and the other part of the screen will be filled with white space). As usedmargin:0 auto
the div will be centered.
– JeroenE
Nov 19 '18 at 13:45
@JeroenE Thanks you got my question...Then if screen width be less than 1000px,there would be no white space
– Zahra19977
Nov 19 '18 at 13:52
1
yes, that's the case.
– JeroenE
Nov 19 '18 at 13:53
Posted an answer with several examples to be complete.
– JeroenE
Nov 19 '18 at 14:17
Tell me the reason of downvote,i get banned from asking question because of reasons that i don't know
– Zahra19977
Nov 19 '18 at 16:12
|
show 4 more comments
When a design is fluid (there is no fixed width), how can wrapper
div using margin:0 auto;
be centered?
#wrapper {
max-width: 1000px;
min-width: 767px;
margin: 0 auto;
}
html css
When a design is fluid (there is no fixed width), how can wrapper
div using margin:0 auto;
be centered?
#wrapper {
max-width: 1000px;
min-width: 767px;
margin: 0 auto;
}
html css
html css
edited Nov 19 '18 at 16:50
Zahra19977
asked Nov 19 '18 at 13:29
Zahra19977Zahra19977
407
407
1
If your question is "how can it get centered when I don't enter a width?" then I can answer that quickly: You have given a maximum and minimum. So it'll first check if the maximum value1000px
is possible to set. If that is the case, it'll be1000
and the other part of the screen will be filled with white space). As usedmargin:0 auto
the div will be centered.
– JeroenE
Nov 19 '18 at 13:45
@JeroenE Thanks you got my question...Then if screen width be less than 1000px,there would be no white space
– Zahra19977
Nov 19 '18 at 13:52
1
yes, that's the case.
– JeroenE
Nov 19 '18 at 13:53
Posted an answer with several examples to be complete.
– JeroenE
Nov 19 '18 at 14:17
Tell me the reason of downvote,i get banned from asking question because of reasons that i don't know
– Zahra19977
Nov 19 '18 at 16:12
|
show 4 more comments
1
If your question is "how can it get centered when I don't enter a width?" then I can answer that quickly: You have given a maximum and minimum. So it'll first check if the maximum value1000px
is possible to set. If that is the case, it'll be1000
and the other part of the screen will be filled with white space). As usedmargin:0 auto
the div will be centered.
– JeroenE
Nov 19 '18 at 13:45
@JeroenE Thanks you got my question...Then if screen width be less than 1000px,there would be no white space
– Zahra19977
Nov 19 '18 at 13:52
1
yes, that's the case.
– JeroenE
Nov 19 '18 at 13:53
Posted an answer with several examples to be complete.
– JeroenE
Nov 19 '18 at 14:17
Tell me the reason of downvote,i get banned from asking question because of reasons that i don't know
– Zahra19977
Nov 19 '18 at 16:12
1
1
If your question is "how can it get centered when I don't enter a width?" then I can answer that quickly: You have given a maximum and minimum. So it'll first check if the maximum value
1000px
is possible to set. If that is the case, it'll be 1000
and the other part of the screen will be filled with white space). As used margin:0 auto
the div will be centered.– JeroenE
Nov 19 '18 at 13:45
If your question is "how can it get centered when I don't enter a width?" then I can answer that quickly: You have given a maximum and minimum. So it'll first check if the maximum value
1000px
is possible to set. If that is the case, it'll be 1000
and the other part of the screen will be filled with white space). As used margin:0 auto
the div will be centered.– JeroenE
Nov 19 '18 at 13:45
@JeroenE Thanks you got my question...Then if screen width be less than 1000px,there would be no white space
– Zahra19977
Nov 19 '18 at 13:52
@JeroenE Thanks you got my question...Then if screen width be less than 1000px,there would be no white space
– Zahra19977
Nov 19 '18 at 13:52
1
1
yes, that's the case.
– JeroenE
Nov 19 '18 at 13:53
yes, that's the case.
– JeroenE
Nov 19 '18 at 13:53
Posted an answer with several examples to be complete.
– JeroenE
Nov 19 '18 at 14:17
Posted an answer with several examples to be complete.
– JeroenE
Nov 19 '18 at 14:17
Tell me the reason of downvote,i get banned from asking question because of reasons that i don't know
– Zahra19977
Nov 19 '18 at 16:12
Tell me the reason of downvote,i get banned from asking question because of reasons that i don't know
– Zahra19977
Nov 19 '18 at 16:12
|
show 4 more comments
3 Answers
3
active
oldest
votes
The max-width:1000px;
means: make a container that has a maximum with of 1000px
. If the page is larger (as an users uses a wider window) it'll fill the other part with white space.
The min-width:767px
will then set the minimum. So if the users has a smaller window then 1000px
then the container doesn't fit. So it'll decrease it's size automatically to a minimum of (in this case) 767px
. If the users still has a smaller window, then a scrollbar will appear. The container will be set to 767px
.
If an user is loading the page in between the max-width
and min-width
, then it'll take the maximum width available. Please see the "To be more precise an example per case" section below for more information about this topic.
As you're using margin:0 auto;
on this #wrapper
. The #wrapper
will be centered with no margin on top of bottom. So instead of the whitespace on the right side which will be set on default, now the white space will be shared on both: left and right side of the container.
I made an example with a lower width then your question in the example below to show that it'll become centered. This is all because of the combination of: max-width
which is smaller then the window-size of the user (box below) and the margin: 0 auto;
which will try to center the div when possible.
#wrapper {
max-width: 200px;
min-width: 100px;
margin: 0 auto;
height:50px; /* added this as example */
background:red; /* add this as example */
}
<div id="wrapper"></div>
To be more precise an example per case:
Note (pre-condition): The cases below are when the div is as main-element in the page. So no other elements that have effect on the #wrapper
-element. Just like the code examples in this post.
- User has a browser that has a size of
1100px
width: The#wrapper
will have a width of1000px
and there will be50px
of whitepace left and50px
of whitespace right. (see the above code example). - User uses a browser that has a size of:
920px
width: The#wrapper
will have width of920px;
and there will be no whitespace on the left and right side. - User uses a browser that has a size of:
600px
width: The#wrapper
will have width of767px;
and there will be no whitespace on the left and right side. Beside that the user will have a scrollbar on the bottom of it's page to be able to see the complete#wrapper
. See the code example below for the scrollbar:
#wrapper {
max-width: 5000px;
min-width: 1000px;
margin: 0 auto;
height:50px; /* added this as example */
background:red; /* add this as example */
}
<div id="wrapper"></div>
add a comment |
You can try this:-
#wrapper {
display: block;
max-width: 1000px;
min-width: 767px;
margin: 0 auto;
}
1
How do you know the poster is not just using a div? Because then it would just work anddisplay:block
won't be necessary.
– JeroenE
Nov 19 '18 at 13:37
Yes you are right, if using divdisplay;block
won't be necessary.
– Akshãy Paghdar
Nov 19 '18 at 13:40
add a comment |
You can use Flexbox.
Set the following properties on the parent element of the element you want to center
display: flex;
justify-content: center; // for horizontally center
align-items: center; // for vertically center
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375695%2fhow-can-a-div-get-centered-when-i-dont-specify-a-fixed-width%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The max-width:1000px;
means: make a container that has a maximum with of 1000px
. If the page is larger (as an users uses a wider window) it'll fill the other part with white space.
The min-width:767px
will then set the minimum. So if the users has a smaller window then 1000px
then the container doesn't fit. So it'll decrease it's size automatically to a minimum of (in this case) 767px
. If the users still has a smaller window, then a scrollbar will appear. The container will be set to 767px
.
If an user is loading the page in between the max-width
and min-width
, then it'll take the maximum width available. Please see the "To be more precise an example per case" section below for more information about this topic.
As you're using margin:0 auto;
on this #wrapper
. The #wrapper
will be centered with no margin on top of bottom. So instead of the whitespace on the right side which will be set on default, now the white space will be shared on both: left and right side of the container.
I made an example with a lower width then your question in the example below to show that it'll become centered. This is all because of the combination of: max-width
which is smaller then the window-size of the user (box below) and the margin: 0 auto;
which will try to center the div when possible.
#wrapper {
max-width: 200px;
min-width: 100px;
margin: 0 auto;
height:50px; /* added this as example */
background:red; /* add this as example */
}
<div id="wrapper"></div>
To be more precise an example per case:
Note (pre-condition): The cases below are when the div is as main-element in the page. So no other elements that have effect on the #wrapper
-element. Just like the code examples in this post.
- User has a browser that has a size of
1100px
width: The#wrapper
will have a width of1000px
and there will be50px
of whitepace left and50px
of whitespace right. (see the above code example). - User uses a browser that has a size of:
920px
width: The#wrapper
will have width of920px;
and there will be no whitespace on the left and right side. - User uses a browser that has a size of:
600px
width: The#wrapper
will have width of767px;
and there will be no whitespace on the left and right side. Beside that the user will have a scrollbar on the bottom of it's page to be able to see the complete#wrapper
. See the code example below for the scrollbar:
#wrapper {
max-width: 5000px;
min-width: 1000px;
margin: 0 auto;
height:50px; /* added this as example */
background:red; /* add this as example */
}
<div id="wrapper"></div>
add a comment |
The max-width:1000px;
means: make a container that has a maximum with of 1000px
. If the page is larger (as an users uses a wider window) it'll fill the other part with white space.
The min-width:767px
will then set the minimum. So if the users has a smaller window then 1000px
then the container doesn't fit. So it'll decrease it's size automatically to a minimum of (in this case) 767px
. If the users still has a smaller window, then a scrollbar will appear. The container will be set to 767px
.
If an user is loading the page in between the max-width
and min-width
, then it'll take the maximum width available. Please see the "To be more precise an example per case" section below for more information about this topic.
As you're using margin:0 auto;
on this #wrapper
. The #wrapper
will be centered with no margin on top of bottom. So instead of the whitespace on the right side which will be set on default, now the white space will be shared on both: left and right side of the container.
I made an example with a lower width then your question in the example below to show that it'll become centered. This is all because of the combination of: max-width
which is smaller then the window-size of the user (box below) and the margin: 0 auto;
which will try to center the div when possible.
#wrapper {
max-width: 200px;
min-width: 100px;
margin: 0 auto;
height:50px; /* added this as example */
background:red; /* add this as example */
}
<div id="wrapper"></div>
To be more precise an example per case:
Note (pre-condition): The cases below are when the div is as main-element in the page. So no other elements that have effect on the #wrapper
-element. Just like the code examples in this post.
- User has a browser that has a size of
1100px
width: The#wrapper
will have a width of1000px
and there will be50px
of whitepace left and50px
of whitespace right. (see the above code example). - User uses a browser that has a size of:
920px
width: The#wrapper
will have width of920px;
and there will be no whitespace on the left and right side. - User uses a browser that has a size of:
600px
width: The#wrapper
will have width of767px;
and there will be no whitespace on the left and right side. Beside that the user will have a scrollbar on the bottom of it's page to be able to see the complete#wrapper
. See the code example below for the scrollbar:
#wrapper {
max-width: 5000px;
min-width: 1000px;
margin: 0 auto;
height:50px; /* added this as example */
background:red; /* add this as example */
}
<div id="wrapper"></div>
add a comment |
The max-width:1000px;
means: make a container that has a maximum with of 1000px
. If the page is larger (as an users uses a wider window) it'll fill the other part with white space.
The min-width:767px
will then set the minimum. So if the users has a smaller window then 1000px
then the container doesn't fit. So it'll decrease it's size automatically to a minimum of (in this case) 767px
. If the users still has a smaller window, then a scrollbar will appear. The container will be set to 767px
.
If an user is loading the page in between the max-width
and min-width
, then it'll take the maximum width available. Please see the "To be more precise an example per case" section below for more information about this topic.
As you're using margin:0 auto;
on this #wrapper
. The #wrapper
will be centered with no margin on top of bottom. So instead of the whitespace on the right side which will be set on default, now the white space will be shared on both: left and right side of the container.
I made an example with a lower width then your question in the example below to show that it'll become centered. This is all because of the combination of: max-width
which is smaller then the window-size of the user (box below) and the margin: 0 auto;
which will try to center the div when possible.
#wrapper {
max-width: 200px;
min-width: 100px;
margin: 0 auto;
height:50px; /* added this as example */
background:red; /* add this as example */
}
<div id="wrapper"></div>
To be more precise an example per case:
Note (pre-condition): The cases below are when the div is as main-element in the page. So no other elements that have effect on the #wrapper
-element. Just like the code examples in this post.
- User has a browser that has a size of
1100px
width: The#wrapper
will have a width of1000px
and there will be50px
of whitepace left and50px
of whitespace right. (see the above code example). - User uses a browser that has a size of:
920px
width: The#wrapper
will have width of920px;
and there will be no whitespace on the left and right side. - User uses a browser that has a size of:
600px
width: The#wrapper
will have width of767px;
and there will be no whitespace on the left and right side. Beside that the user will have a scrollbar on the bottom of it's page to be able to see the complete#wrapper
. See the code example below for the scrollbar:
#wrapper {
max-width: 5000px;
min-width: 1000px;
margin: 0 auto;
height:50px; /* added this as example */
background:red; /* add this as example */
}
<div id="wrapper"></div>
The max-width:1000px;
means: make a container that has a maximum with of 1000px
. If the page is larger (as an users uses a wider window) it'll fill the other part with white space.
The min-width:767px
will then set the minimum. So if the users has a smaller window then 1000px
then the container doesn't fit. So it'll decrease it's size automatically to a minimum of (in this case) 767px
. If the users still has a smaller window, then a scrollbar will appear. The container will be set to 767px
.
If an user is loading the page in between the max-width
and min-width
, then it'll take the maximum width available. Please see the "To be more precise an example per case" section below for more information about this topic.
As you're using margin:0 auto;
on this #wrapper
. The #wrapper
will be centered with no margin on top of bottom. So instead of the whitespace on the right side which will be set on default, now the white space will be shared on both: left and right side of the container.
I made an example with a lower width then your question in the example below to show that it'll become centered. This is all because of the combination of: max-width
which is smaller then the window-size of the user (box below) and the margin: 0 auto;
which will try to center the div when possible.
#wrapper {
max-width: 200px;
min-width: 100px;
margin: 0 auto;
height:50px; /* added this as example */
background:red; /* add this as example */
}
<div id="wrapper"></div>
To be more precise an example per case:
Note (pre-condition): The cases below are when the div is as main-element in the page. So no other elements that have effect on the #wrapper
-element. Just like the code examples in this post.
- User has a browser that has a size of
1100px
width: The#wrapper
will have a width of1000px
and there will be50px
of whitepace left and50px
of whitespace right. (see the above code example). - User uses a browser that has a size of:
920px
width: The#wrapper
will have width of920px;
and there will be no whitespace on the left and right side. - User uses a browser that has a size of:
600px
width: The#wrapper
will have width of767px;
and there will be no whitespace on the left and right side. Beside that the user will have a scrollbar on the bottom of it's page to be able to see the complete#wrapper
. See the code example below for the scrollbar:
#wrapper {
max-width: 5000px;
min-width: 1000px;
margin: 0 auto;
height:50px; /* added this as example */
background:red; /* add this as example */
}
<div id="wrapper"></div>
#wrapper {
max-width: 200px;
min-width: 100px;
margin: 0 auto;
height:50px; /* added this as example */
background:red; /* add this as example */
}
<div id="wrapper"></div>
#wrapper {
max-width: 200px;
min-width: 100px;
margin: 0 auto;
height:50px; /* added this as example */
background:red; /* add this as example */
}
<div id="wrapper"></div>
#wrapper {
max-width: 5000px;
min-width: 1000px;
margin: 0 auto;
height:50px; /* added this as example */
background:red; /* add this as example */
}
<div id="wrapper"></div>
#wrapper {
max-width: 5000px;
min-width: 1000px;
margin: 0 auto;
height:50px; /* added this as example */
background:red; /* add this as example */
}
<div id="wrapper"></div>
edited Nov 20 '18 at 16:43
answered Nov 19 '18 at 13:58
JeroenEJeroenE
509318
509318
add a comment |
add a comment |
You can try this:-
#wrapper {
display: block;
max-width: 1000px;
min-width: 767px;
margin: 0 auto;
}
1
How do you know the poster is not just using a div? Because then it would just work anddisplay:block
won't be necessary.
– JeroenE
Nov 19 '18 at 13:37
Yes you are right, if using divdisplay;block
won't be necessary.
– Akshãy Paghdar
Nov 19 '18 at 13:40
add a comment |
You can try this:-
#wrapper {
display: block;
max-width: 1000px;
min-width: 767px;
margin: 0 auto;
}
1
How do you know the poster is not just using a div? Because then it would just work anddisplay:block
won't be necessary.
– JeroenE
Nov 19 '18 at 13:37
Yes you are right, if using divdisplay;block
won't be necessary.
– Akshãy Paghdar
Nov 19 '18 at 13:40
add a comment |
You can try this:-
#wrapper {
display: block;
max-width: 1000px;
min-width: 767px;
margin: 0 auto;
}
You can try this:-
#wrapper {
display: block;
max-width: 1000px;
min-width: 767px;
margin: 0 auto;
}
answered Nov 19 '18 at 13:33
Akshãy PaghdarAkshãy Paghdar
2,93521429
2,93521429
1
How do you know the poster is not just using a div? Because then it would just work anddisplay:block
won't be necessary.
– JeroenE
Nov 19 '18 at 13:37
Yes you are right, if using divdisplay;block
won't be necessary.
– Akshãy Paghdar
Nov 19 '18 at 13:40
add a comment |
1
How do you know the poster is not just using a div? Because then it would just work anddisplay:block
won't be necessary.
– JeroenE
Nov 19 '18 at 13:37
Yes you are right, if using divdisplay;block
won't be necessary.
– Akshãy Paghdar
Nov 19 '18 at 13:40
1
1
How do you know the poster is not just using a div? Because then it would just work and
display:block
won't be necessary.– JeroenE
Nov 19 '18 at 13:37
How do you know the poster is not just using a div? Because then it would just work and
display:block
won't be necessary.– JeroenE
Nov 19 '18 at 13:37
Yes you are right, if using div
display;block
won't be necessary.– Akshãy Paghdar
Nov 19 '18 at 13:40
Yes you are right, if using div
display;block
won't be necessary.– Akshãy Paghdar
Nov 19 '18 at 13:40
add a comment |
You can use Flexbox.
Set the following properties on the parent element of the element you want to center
display: flex;
justify-content: center; // for horizontally center
align-items: center; // for vertically center
add a comment |
You can use Flexbox.
Set the following properties on the parent element of the element you want to center
display: flex;
justify-content: center; // for horizontally center
align-items: center; // for vertically center
add a comment |
You can use Flexbox.
Set the following properties on the parent element of the element you want to center
display: flex;
justify-content: center; // for horizontally center
align-items: center; // for vertically center
You can use Flexbox.
Set the following properties on the parent element of the element you want to center
display: flex;
justify-content: center; // for horizontally center
align-items: center; // for vertically center
answered Nov 19 '18 at 13:35
YousafYousaf
4,5432823
4,5432823
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375695%2fhow-can-a-div-get-centered-when-i-dont-specify-a-fixed-width%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
If your question is "how can it get centered when I don't enter a width?" then I can answer that quickly: You have given a maximum and minimum. So it'll first check if the maximum value
1000px
is possible to set. If that is the case, it'll be1000
and the other part of the screen will be filled with white space). As usedmargin:0 auto
the div will be centered.– JeroenE
Nov 19 '18 at 13:45
@JeroenE Thanks you got my question...Then if screen width be less than 1000px,there would be no white space
– Zahra19977
Nov 19 '18 at 13:52
1
yes, that's the case.
– JeroenE
Nov 19 '18 at 13:53
Posted an answer with several examples to be complete.
– JeroenE
Nov 19 '18 at 14:17
Tell me the reason of downvote,i get banned from asking question because of reasons that i don't know
– Zahra19977
Nov 19 '18 at 16:12