How to centering Content - css

I make some homepage using wordpress.
i use this css code
.container { width: 960px; margin-left: auto; margin-right: auto; }
actually looking good, content's position is center.
but sometimes change content, change margin
my resolution is 1920 width, content's width is 960px fixed.
content 1 has 960px width and margin is 475.75px
content 2 has 960px width and margin is 480px
i want 480px fixed.
what is problem?

Try to add content1 half the width of container , give margin auto .
.content1 { width:475x; margin:auto; word-wrap:break-word;}

Related

How to set width 100% but not bigger than value

Which is the proper way to set width to an element, setting width to let's say 960px and max-width to 100% or the other way arround width 100% and max-width to 960px?
div { width: 960px; max-widht: 100% }
div { width: 100%; max-width: 960px }
set width 100% but not bigger than value,
div{ max-width: 960px; width: 100%;}
You can use bootstrap for responsive pages. It will help you to have best width for mobile and tablet. Also use media quires for best. Always go for a max width for a div or container.

Horizontal centering of a div can be done by margin-left and right auto, together with a width, or max-width, but not min-width?

In the old days before having max-width and min-width, we have to set the width to some value other than auto, together with margin-left and margin-right to auto, for the div to be horizontally centered.
Now that we have max-width and min-width, we can also set the max-width to some value other than none to horizontally centered it? Is there any spec for it?
P.S. min-width won't work as the div will span the whole width, so you can consider it horizontally centered, except that the left and right margin are both 0, so there is no horizontally centering effect.
Example:
width: https://jsfiddle.net/dtr7t4z7/1/
max-width: https://jsfiddle.net/dtr7t4z7/2/ and https://jsfiddle.net/dtr7t4z7/3/
min-width: https://jsfiddle.net/dtr7t4z7/4/
margin: auto will just fill the remaining space equally on both sides. If a width or max-width are not specified, a div will take up as much horizontal space as possible. Because the div is the full width of its container when only min-width is specified, there isn't any space left for margin: auto to distribute.
https://jsfiddle.net/ehnvy1xz/5/
body{
display:flex;
justify-content:center;
}
#line {
text-align:center;
background: yellow;
min-width: 200px;
}
If you really need the min-width. I would recommend using flex on the parent div and centering it that way instead.

Resize image to parent div

I have an image with a 600px width.
It needs to be inside a bootstrap col-xs-6 or 12 div with a width of 300px and has to be resized if the screen width is smaller than 300px.
How can I realize that using width and max-width in CSS?
just do this:
img {
width: 100%;
max-width: 300px;
height: auto;
}
I think this will solve your problem.
img {
width: 100%;
max-width: 300px;
}
If the div width is bigger than 300px image will have maximum width of 300px. If div is smaller than 300px image should fill 100% width of div.
.my-image {
max-width:300px;
width:100%;
}
These rules basically mean, display the image at the full width of its container unless the container is larger than 300px, in which case, limit the image to 300px.

Vertical Pagination of Divs

What I want to do is setup multiple div's that each contain the contents of an entire page. Each div should be centered in the viewport and fill it entirely. Each successive div should be evenly spaced vertically based on the variable height of the viewport. For example say I have a view of 800x600 then each div should be this size and stacked. So, if I scrolled down exactly 600px I would only see page 2 div, 1200px I'd only see page 3 div. I don't have any code or example to share and my exhausted searches have turned up nothing of this sort. Is this possible with just css?
You simply have to give your html, body & divs a height: 100%;:
html, body {
height: 100%;
}
div {
height: 100%;
margin-bottom: 5px;
}
http://jsfiddle.net/KMMjv/
Because this would vary depending on the size of the user's browser window you would need to JavaScript to detect screen height and position them accordingly. Although you could set the height of each div with just CSS (height: 100%;) you would need to set the top with JS.
Here is a working JSBin: http://jsbin.com/ogokef/edit#preview
Use the following units:
vh for viewport height
vw for viewport width
html, body {
height: 100vh;
}
div {
height: 100vh;
width: 100%; /* you can use 100vw too, but for height it must be vh */
}

CSS: Block width should stay fixed until sidebars reach little width

I have <div> block, that have some fixed width. Sidebars stretch until their width reach some value. Then block should stretch instead of sidebars. How can I implement this in CSS?
"Sidebars" are just <div>'s margin (space).
Solution:
div
{
/* Left and right margin stretch from zero to infinity */
margin:0 auto;
/* 2em is minimal side width */
padding: 0 2em;
/* Expected width */
max-width: 640px;
/* Minimal width */
min-width: 320px;
}
I think you're looking for the min-width property: http://www.w3schools.com/CSS/pr_dim_min-width.asp. Or max-width: http://www.w3schools.com/css/pr_dim_max-width.asp.
However this doesn't have full support in all browsers.
One way would be to surround all the <div>'s in a larger div's which is a total size of all of them, then perhaps impose a min-width on all three. But Internet Explorer doesn't work well with this.

Resources