Align to center child DIVs inside parent DIV - html

I'm trying to align a DIV in the middle of the page when the size is less than 768px
I have one parent DIV that contains three different DIVs. So I used media queries and within one display size I added this "clear:both" to the display_type and buttons classes. So in that way each div will be in its own row.
<div class="category_header container">
<div class="products">
<label class="header t_left">Sorter efter:</label>
<div class="select t_left">
</div>
<div class="display_type">
<ul class="clearfix">
<li></li>
<li></li>
</ul>
</div>
<div class="buttons">
<span class="button"></span>
<span class="button"></span>
</div>
</div>
</div>
Then I tried several ways to align them in the center but they didn't work. Here what the CSS looks like:
.container {
width: 1180px;
margin: 0 auto;
padding: 0;
}
.category_header {
margin-top: 30px;
margin-bottom: 30px;
padding: 0 0 0 25px;
}
.products {
float: left;
width: 350px;
margin: 3.5px 0 0;
}
.products label {
margin: 0 30px 0 0;
line-height: 40px;
height: 40px;
display: block;
}
.display_type {
float: left;
width: 350px;
color: #A1ABB6;
margin: 12px 0 0;
}
.category_header .buttons {
float: right;
width: 427px;
}
I tried to add the following to the parent div "category_header" with no luck. I also tried the same to the child divs. I found a lot of solutions on google but noone seems to work. (display: inline-block; text-align: center etc etc)
#media screen and (max-width: Number px) and (min-width: Number px)
{
.category_header {
margin: 0 auto;
width: 50%;
}
}
Can anyone help me with this? Thanks

try this:
#media screen and (max-width: 768px)
{
.display_type, .buttons, .products
{
float:none;
margin:auto;
}
}

Related

How width in percentage is getting assigned to div?

I have main div and inside that i have 3 divs and each have individual width in percentage. So i got divs with 20%, 80% and 100% respectively.
Working with them so far i realised that if I have given then width of 200% all together then it divide browser width in two equal parts and first half is considered 100% and other one 100%. Then rest of percentage distribution happens. I am new to CSS but want to make this clear. Is my understanding correct and that's why my code is working correctly? Is there any disadvantage of doing so?
.main {
font-size: larger;
font-weight: bold;
}
.column1 {
position: relative;
line-height: 30px;
width: 20%;
text-align: left;
}
.column2 {
width: 80%;
}
.column3 {
position: relative;
width: 100%;
float: right;
text-align: right;
}
.clear-both {
clear: both;
}
.row {
border-width: 2px;
border-bottom-color: #f4f4f4;
border-style: none;
position: relative;
line-height: 30px;
border-bottom-style: solid;
margin-top: 0.2%;
font-weight: normal;
font-size: 12px;
display: flex;
}
<div class="row main">
<div class="column1">CODE</div>
<div class="column2 ">NAME</div>
<div class="column3">TOTAL</div>
</div>
if you use flexbox, it makes it clearer for you instead of using floats mixed with position,
body {
margin: 0
}
.main {
font-size: larger;
font-weight: bold;
display: flex;
flex-wrap: wrap;
}
.column1 {
flex: 0 20%;
background: red
}
.column2 {
flex: 0 80%;
background: green
}
.column3 {
flex: 0 100%;
background: yellow
}
<div class="main">
<div class="column1">CODE</div>
<div class="column2 ">NAME</div>
<div class="column3">TOTAL</div>
</div>
EDIT OP's comment
I want these 3 columns to be in one row. Working with my code too but
total width of main div is 200%. is there any possibility of breaking
of ui if i do it in this way
You can do this way:
body {
margin: 0
}
.main {
font-size: larger;
font-weight: bold;
display: flex
}
.column1,
.column3 {
flex: 0 20%;
background: yellow
}
.column2 {
flex: 1;
background: green
}
<div class="main">
<div class="column1">CODE</div>
<div class="column2 ">NAME</div>
<div class="column3">TOTAL</div>
</div>
You are doing wrong.
Keep following things in mind.
1- Parent/ main div should have appropriate width in percent or pixels. if you don't assign a width then it will take width automatically depending on contents of width.
2- If you are assigning width in percent to child divs (column1, column2 etc) then total of child divs should not exceed 100%. If it exceeds then you will not get desired/appropriate results.
3- You need to use floats or display:inline-block to place all child divs/elements in one row.
.main {
font-size: larger;
font-weight: bold;
width: 100%;
max-width: 1140px;
margin: 0 auto;
}
.column1 {
position: relative;
line-height: 30px;
width: 20%;
text-align: left;
float: left;
}
.column2 {
width: 60%;
float: left;
}
.column3 {
position: relative;
width: 20%;
text-align: right;
float: left;
}
<div class="row main">
<div class="column1">CODE</div>
<div class="column2 ">NAME</div>
<div class="column3">TOTAL</div>
</div>
If you are using padding or borders then you may need to use
box-sizing: border-box.
You can use bootstrap grid system.
Grid systems are used for creating page layouts through a series of
rows and columns that house your content.
So you can easily manage your layout depending on which device you are using :
.col-lg* classes refer to large sized devices.
.col-md* classes refer to medium sized devices .
.col-xs* classes refer to small sized devices.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
<div class="col-md-2 col-xs-2 bg-warning">CODE</div>
<div class="col-md-10 col-xs-10 bg-primary">NAME</div>
<div class="col-md-12 col-xs-12 bg-success">TOTAL</div>
</div>

Centering table with more rows

I have three numbers in a div element that is set to display table. I need to center numbers when they are divided in more rows for better experience on narrow devices.
Please run code snippet at full page and then reduce screen size (356px for example).
What I want:
case1: margin|div|div|div|margin
--------------------------------
case2: margin|div|div|margin
margin|div| |margin
--------------------------------
case3: margin|div|margin
margin|div|margin
margin|div|margin
Solved! ...added code + eddited snippet, feel free to try :)
Edit1: There´s aboutqi class which is set to display: table and margin: auto. On wide screen there is only one row and margin works perfect = it´s centered. But when there are 2 or 3 rows margin stop works and aboutqiitems are not centered.
.aboutqi {
margin: auto;
display: table;
}
.aboutqiitems {
float: left;
}
.aboutqiitem {
float: left;
margin-right: 0px;
margin-bottom: 30px;
width: 200px;
}
.aboutqiitem-inner {
width: 100%;
margin: auto;
}
.num {
margin: 0px;
margin-bottom: 20px;
padding-left: 13px;
font-size: 48px;
font-weight: bold;
color: #014493;
}
.num-center {
margin: auto;
display: table;
}
.num-center-last {
margin: auto;
display: table;
position: relative;
left: -7px;
}
/* My solution */
.aboutqiitems-solved {
float: left;
}
#media screen and (max-width: 616px) {
.aboutqiitems-solved {
max-width: 100%;
clear: both;
padding-left: calc((100% - 400px)/2);
}
}
#media screen and (max-width: 416px) {
.aboutqiitems-solved {
max-width: 100%;
clear: both;
padding-left: calc((100% - 200px)/2);
}
}
#media screen and (max-width: 216px) {
.aboutqiitems-solved {
max-width: 100%;
clear: both;
padding-left: 0px;
}
}
<div class="aboutqi">
<p>Hello, need to center numbers when there is more than 1 row.</p>
<div class="aboutqiitems">
<div class="aboutqiitem">
<div class="aboutqiitem-inner">
<div class="num-center"><p class="num">1</p></div>
</div>
</div>
<div class="aboutqiitem">
<div class="aboutqiitem-inner">
<div class="num-center"><p class="num">2</p></div>
</div>
</div>
<div class="aboutqiitem last">
<div class="aboutqiitem-inner">
<div class="num-center-last"><p class="num">3</p></div>
</div>
</div>
</div>
</div>
</br>
</br>
<!-- My Solution -->
<div class="aboutqi">
<p>How I solve it. Fell free to change page width :)</p>
<div class="aboutqiitems-solved">
<div class="aboutqiitem">
<div class="aboutqiitem-inner">
<div class="num-center"><p class="num">1</p></div>
</div>
</div>
<div class="aboutqiitem">
<div class="aboutqiitem-inner">
<div class="num-center"><p class="num">2</p></div>
</div>
</div>
<div class="aboutqiitem last">
<div class="aboutqiitem-inner">
<div class="num-center-last"><p class="num">3</p></div>
</div>
</div>
</div>
</div>
Example: http://www.dcit.sk/ ... almost at the bottom of the page
You should use media queries in your CSS sheet to help with this. It looks like your numbers are splitting onto 2 rows around 616px, so you want to do something along the following lines:
#media screen and (max-width: 616px) {
.aboutqiitems {
width: 100%;
}
.aboutqiitem {
display: block;
clear: both;
width: 100%;
}
}
Here is a jsfiddle that shows the extra code and how it works: https://jsfiddle.net/4be1k4jr/
You can read more about media queries and how to use them here:
http://www.w3schools.com/cssref/css3_pr_mediaquery.asp
If you're looking to keep the boxes at 200px instead of 100% (in case you have images or something) you can use the following media query. It's a little less clean, but works if you absolutely must keep 200px.
#media screen and (max-width: 616px) {
.aboutqiitems {
width: 100%;
}
.aboutqiitem {
display: block;
clear: both;
width: 200px;
position: relative;
left: 50%;
margin-left: -100px;
}
}
From what I'm taking from this question, you want to change the css when the screen width gets to a certain width. The easiest way to do this is with an #media tag.
Here's my css solution to this problem.
<style>
.aboutqi {
margin: auto;
display: table;
}
.aboutqiitems {
float: left;
}
.aboutqiitem {
float: left;
margin-right: 0px;
margin-bottom: 30px;
width: 200px;
}
.aboutqiitem-inner {
width: 100%;
margin: auto;
}
.num {
margin: 0px;
margin-bottom: 20px;
padding-left: 13px;
font-size: 48px;
font-weight: bold;
color: #014493;
}
.num-center {
margin: auto;
display: table;
}
.num-center-last {
margin: auto;
display: table;
position: relative;
left: -7px;
}
#media only screen and (max-width: 615px){
.aboutqiitem{
width: 100%;
}
.aboutqiitems{
width: 100%;
}
.num{
padding: 0;
}
.num-center-last {
left: 0;
}
}
</style>
Keep in mind that you also need to set a viewport width with a meta tag in your head tag.
<meta content="width=device-width" name="viewport">
This would also work:
#media only screen and (max-width: 615px){
.aboutqiitems{
float: none;
}
.aboutqiitem{
float: none;
margin: auto;
}
.num-center-last{
position: inherit;
}
}

How to horizontally centre floated text

if you go and reduce the width of the window to view the screen as if it were a mobile device you can see that the orange "badges" may not be entered (especially when only one badge fits per line) I want it to fit more badges in if possible whilst always keeping the badge, or group of badges on that line entered horizontally. The class is badge that isn't being centered Thank you in advance!! :)
jsfiddle: http://jsfiddle.net/avg24wrk/
This is the HTML
<div class="container">
<div class="sidebar">
<div class="sidebar-inner">
<p class="badge"><span class="vertical-align">Book a Free Consultation!</span></p>
<p class="badge"><span class="vertical-align">Second Point</span></p>
<p class="badge"><span class="vertical-align">Third Point</span></p>
</div>
</div>
and this is the CSS
* {
border: 0;
margin: 0;
padding: 0;
}
.sidebar {
float: left;
width: 25%;
color: #505050;
}
.sidebar-inner {
margin: 0 30px 0 35px;
}
.badge {
margin: 10px auto;
font-size: 24px;
text-align: center;
border-radius: 7px;
padding: 20px 20px;
background-color: #ed9727;
cursor: pointer;
}
#media screen and (max-width: 490px) {
.sidebar {
width: 100%;
}
.sidebar-inner {
padding-bottom: 20px;
border-bottom: 2px solid #505050;
margin: 0 30px;
overflow: hidden;
}
.badge {
float: left;
margin: 15px 10px;
max-width: 150px;
min-height: 50px;
display: table;
}
}
have you tried adding text-align: center; to class you want to center
since i you didn't mention which class you want to center so i will give you a simple rule try this
please mention class you want to center

Making block elements of ul nav automatically fill the whole width

I currently have this menu:
(source: gyazo.com)
I am using Twitter Bootstrap 3, the .container class. What I am trying to do is, making the elements width scale automatically according to the amount of the elements in the ul + the width of the container.
I have attempted to do this, but this doesn't work:
#media (min-width: 428px) and (max-width: 991px) {
.container > #vote > #vote-list {
width: 100%;
background-color: red;
padding: 0;
}
#vote-list li {
width: auto;
float: left;
display: table-cell;
margin-left: 1px;
padding-left: 10px;
padding-right: 10px;
}
}
As you see I am firstly accessing #vote using .container because #vote is child of .container. #vote is basically the dark area you see that the nav is contained in. #vote-list is child of #vote.
#vote {
background-color: #161616;
min-height: 560px;
width: 100%;
}
I have tried changing the child selectors to direct access, but it is giving me the same results.
The html:
<body class="container">
<section id="vote">
<ul id="vote-list">
<li>MyNav</li>
<li>MyNav</li>
<li>MyNav</li>
<li>MyNav</li>
<li>MyNav</li>
</ul>
</section>
</body>
What did I do wrong? Let me know if you need more information
try this:
#vote-list {
width: 100%;
background-color: red;
padding: 0;
display: table; /* add this */
}
#vote-list li {
/* remove float:left; */
display: table-cell;
margin-left: 1px;
padding-left: 10px;
padding-right: 10px;
}
<section id="vote">
<ul id="vote-list">
<li>MyNav</li>
<li>MyNav</li>
<li>MyNav</li>
<li>MyNav</li>
<li>MyNav</li>
</ul>
</section>

Responsive WordPress feature box causing two issues

I created a feature box on my WordPress development site. With some help I made the feature box responsive and it's almost finished. You can see this feature box displaying properly in the below screenshot
However, I have two issues:
1) When I reduce my browser window in size, the navbar turns expands onto two lines and covers my feature box (pictured). I want the feature box to move down as the navbar expands.
2) On my phone the red ribbon in my feature box expands beyond the width of the page and causes the navbar to display incorrectly (pictured). I don't want the red ribbon to expand beyond the width of my navbar.
I created the navbar using CSS and an image. Using CSS, I created a large red rectangle that expands beyond the margin of my feature box. I then used an image of a red triangle and positioned this beneath the red rectangle. You can see how I did this by looking at "#text-4" in my CSS.
My relevant CSS is
.featured-box {
border-radius: 20px;
background-color: #000;
color: #fff;
padding: 20px;
margin: 0 auto;
margin-top: 10px;
overflow: visible;
width: auto;
max-width: 1160px;
}
.featured-box h4 {
font-size: 20px;
color: #fff;
}
.myimage {
float:right;
}
.featured-box p {
padding: 0 0 20px;
}
.featured-box ul {
margin: 0 0 20px;
}
.featured-box ul li {
list-style-type: disc;
margin: 0 0 0 30px;
padding: 0;
align: right;
}
.featured-box .enews p {
padding: 10 10 10 10px;
color: #fff;
float: left;
width: 220 px;
margin: 10 10 10 10px;
}
.featured-box .enews #subscribe {
padding: 20 20 20 20px;;
}
.featured-box .enews #subbox {
background-color: #fff;
margin: 0;
width: 300px;
}
.featured-box .enews .myimage {
float: right;
margin-left: 10px;
margin-right: 50px;
width: auto;
}
section.enews-widget {
overflow: hidden;
}
.featured-box .enews input[type="submit"] {
background-color: #d60000;
padding: 10 10 10 10px;
width: 150px;
}
#media screen and (min-width: 1140px) {
div.featured-box {
margin-top: 10%;
}
}
#media only screen and (max-width: 767px) {
section.enews-widget {
clear: both;
}
.myimage {
float: none;
}
.myimage img {
display: block;
height: auto;
margin: 0 auto;
}
}
#text-4 > div:nth-child(1) > h4:nth-child(1) {
color: #fff;
font-size: 1.3em; font-weight: normal;
text-transform: uppercase;
background-color: #d60000;
position: relative;
margin: 0px -60px 20px -20px;
padding: 18px 0px 16px 20px;
}
#text-4 > div:nth-child(1) > h4:nth-child(1):after {
content: '';
display: block; height: 40px; width: 40px;
background: url(http://bryancollins.eu/wp/wp-content/uploads/2014/04/fold.png) no-repeat 0 0;
position: absolute; right: 0px; bottom: -40px;
}
.page p { line-height: 1.2em; }
.page a { color: #1badd2; text-decoration: none; }
.widget li {
margin: 0;
padding: 2px 0px 8px 35px;
display: inline; position: relative;
border-bottom: none;
}
.featured-box .widget li {
list-style: none;
background: url("http://bryancollins.eu/wp/wp-content/uploads/2014/04/arrow.png") no-repeat scroll 0 10px rgba(0, 0, 0, 0);
display: inline;
margin: 0 0 0 30px;
padding: 0 0 0 40px;
}
My HTML:
<div class="featured-box widget-area">
<section id="text-4" class="widget widget_text">
<div class="widget-wrap">
<h4 class="widget-title widgettitle">
Get this for free
</h4>
<div class="textwidget">
<div class="myimage">
<img src="http://bryancollins.eu/wp/wp-content/uploads/2014/04/Book-cover.png"></img>
</div>
</div>
</div>
</section>
<section id="enews-ext-3" class="widget enews-widget">
<div class="widget-wrap">
<div class="enews">
<h4 class="widget-title widgettitle">
33 Creative Strategies for your next writing proje…
</h4>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing…
</p>
<div class="arrows">
<ul>
<li>
List item 1
</li>
<li>
List item 2
</li>
<li>
List item 3
</li>
</ul>
</div>
<p>
Tuo vero id quidem, inquam, arbitratu. Illud mihi …
</p>
<form id="subscribe" name="33 Creative Strategies for your next writing project" onsubmit="if ( subbox1.value == 'First Name') { subbox1.value = ''; } if ( subbox2.value == 'Last Name') { subbox2.value = ''; }" target="_blank" method="post" action="<!-- Begin MailChimp Signup Form --> <div id="mc_embed_signu…s="button"></div> </form> </div> <!--End mc_embed_signup-->"></form>
</div>
</div>
</section>
Thanks for the help.
Fix for 1.) Needed to increase top margin between 1024px and 1140px can be more or less than 14% but looks ok at 14!
#media screen and (min-width: 1024px) and (max-width: 1140px) {
div.featured-box {
margin-top: 14%;
}
}
User fixed with margin-top 130px.
Fix for 2.)
Try adding 'overflow: hidden' to the same media query:
#media screen and (min-width: 1024px) {
div.featured-box {
margin-top: 130px;
overflow: hidden;
}
}
For your first issue, on "desktop size" your featured box has a margin-top of 10%, but then once you resize below 1040px, the featured box takes on the default CSS which has a margin-top of 10px and the navigation has fixed positioning meaning that the featured box margin top is from the top of the window, not from below the navigation.
To resolve this, you can change the default CSS to use a percentage for the top margin, however I would advise using relative positioning and using pixels to set the margin. Another option is to increase the breakpoint so that the "mobile" navigation replaces the default navigation as soon as it beings to wrap.
For your second issue you can either set "overflow:hidden;" on the containing div or you can set the maximum scale to 1 with a viewport meta tag(mobile), I'd advise doing both:
<meta name="viewport" content="initial-scale=1, maximum-scale=1">