简体   繁体   中英

floating, left aligned, centered div

I would like to have a div, that is centered in its parent, but children inside of it would be left aligned. Wanting this, I obtained the following:

 .centered { text-align: center; } .container { background: red; padding: 10px; display: inline-block; } .child { width: 100px; height: 100px; float: left; } .child:nth-child(even) { background: green; } .child:nth-child(odd) { background: blue; } 
 <div class="centered"> <div class="container"> <div class="child"></div> <div class="child"></div> <div class="child"></div> </div> </div> 

http://jsfiddle.net/SbPRg/ (source: Having the floating children elements determine parent width ).

The problem is that when you resize the page, there still is a problem whereas the main "red" div is wider than what I want, and shows a big part of red instead of resizing to a smaller container that 'fits' its children.

Would you have any idea of how to obtain something like this, without javascript ?

The main idea is to display a "gallery" of images, in a responsive way (if the screen is too small, display one image per row, up until three per row if the screen is wide).

Just add CSS Media Query rules. Add the following rule to your CSS.

CSS

@media screen and (max-width: 337px) {
    .child
    {
        float:none;
        text-align:center;
        display:block;
    }
}

Working : Demo

Note: CSS Media Query is use to effect the styling to your HTML based on screen width's and height's

Here in your example i have added media query rule which gives style to .child class when the screen width reaches the 337px .

There could be max-width or min-width for width's similar for heights .

What is max-width : Here i have given it 337px , so it says that the css inside the rule should apply only when screen width is <=337px . If screen width is >337px then it would not apply the css within that rule.

Just set the Container width to the exact width you want (in the JSFidlle: you need to add width:300px; )

and when you resize the window nothing happen.

to make it responsive use:

@media screen and (max-width:480px)

and set the width of the container for every screen size(or for the three block and for two then for just one)

You could try a media query like this:

@media screen and (max-width: 350px) {
  .container {width: 100px;}
}

Check the demo

The reason that the parent container "shows a big part of red instead of resizing to a smaller container that 'fits' its children" is that you've explicitly given it ten pixels of padding on all sides. Remove that padding from .container, and the div will shrink to the width of its children.

I guess you want something like this . Use media query to apply relevant css to the elements based on the various screen sizes.

Here is the css you need to add:

@media only screen and (max-width: 350px) {
    .child {
        clear:both;
    }
}

One alternative would be to use a framework like Bootstrap, which is good enough for making responsive webpages.

Here are the links you can refer to:

  1. Media query reference .

  2. Bootstrap .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM