I'm trying to center a Div that will have varying width's (based on content of a website).
I read about a relative positioning technique here:
http://www.tightcss.com/centering/center_variable_width.htm
But I thought there has to be an easier way to do it?
That's a pretty solid method that should work well in most browsers. It's not really that complex when you break it down. Here's a basic example:
<style type="text/css">
#hideoverflow { overflow: hidden; }
#outer { position: relative; left: 50%; float: left; }
#inner { position: relative; left: -50%; float: left; }
</style>
<div id="hideoverflow">
<div id="outer">
<div id="inner">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed id velit vel augue fringilla rhoncus at et odio. Suspendisse potenti. Aliquam justo libero, commodo ut iaculis in, placerat vel purus.
</div>
</div>
</div>
#Talon; you can do it like this http://jsfiddle.net/sandeep/7PXQF/
CSS:
.container{
background-color:red;
text-align:center;
}
.center{
background-color:yellow;
display:inline-block;
text-align:left;}
HTML:
<div class="container">
<div class="center">
<p>This is a div with an much wider width, to make the yellow div go off the page to the right. We'll type a bit more to be sure.</p>
<p>Most people will see a horizontal scroll bar on the bottom, unless their screen is very wide.</p>
</div>
</div>
Well, it can't get any simpler than this and has full support on all browsers; doesn't even need a container:
.centered {
display:table;
margin:0 auto;
}
<div class="centered">
content
</div>
Here is a working fiddle: https://jsfiddle.net/1tnprnoz/
Now with flex-box you can easily achieve this with justify-content: center;.
#container{
background: gray;
display: flex;
justify-content: center;
}
<div id="container">
<div id="content" style="width: 200px; padding: 5px; background: #ffa637;">
This is a centered div This is a centered div This is a centered div This is a centered div
</div>
</div>
This can also be achieved by applying margin: auto to the containers child selector #container>*.
#container{
background: #c7c7c7;
}
#container>*{
margin: auto;
}
<div id="container">
<div id="content" style="width: 200px; padding: 5px; background: #ffa637;">
This is a centered div This is a centered div This is a centered div This is a centered div
</div>
</div>
Note: content div is styled inline as these styles are generated styles and are out of the scope of this question.
Related
I working in a page builder
For a shop, I am creating. I can change the CSS which is great.
I’m struggling to get a responsive resize of the images in this 4 column row. Since the images are different heights I have to have to set a height and have responsive width. Is there any way to get it to scale correctly?
The width is auto and the height is a set height based on the size of the screen.
You can see that when I resize it separates from the box and then sometimes get squished.
object-fit property
I did your design by using display : flex; and object-fit : cover; properties. I think that this object-fit property directly on the image is the only lacking property to make your images still looking good despite the screen resolution.
Notice the use of object-position : center; which makes the resizing always axed on the center of the image.
index.html
<div class="foo">
<div class="bar">
<img src="https://picsum.photos/200/300" alt="">
<div>
<h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
<p>$42</p>
</div>
</div>
<div class="bar">
<img src="https://picsum.photos/500/200" alt="">
<div>
<h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
<p>$42</p>
</div>
</div>
<div class="bar">
<img src="https://picsum.photos/200/700" alt="">
<div>
<h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
<p>$42</p>
</div>
</div>
<div class="bar">
<img src="https://picsum.photos/500/400" alt="">
<div>
<h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
<p>$42</p>
</div>
</div>
</div>
style.css
body {
background-color: #f7f7f7;
font-family: Arial, Helvetica, sans-serif;
}
.foo {
width: 100%;
display: flex;
gap: 20px;
}
.bar {
background-color: #ffffff;
display: flex;
flex-direction: column;
width: 100%;
border-radius: 15px;
overflow: hidden;
box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
}
.bar > img {
width: 100%;
height: 200px;
object-fit: cover;
object-position: center;
}
h4 {
color:#9ccf74;
}
.bar > div {
padding: 10px;
height: 100px;
}
*I have added padding inside the divs so you're able to see the boxes
I need to display these 2 items next to each other on desktop screen sizes.
I the purple bordered box which contains the 2 elements is set to display flex.
The circle div sqaushes up.
I have set is to a height: 200px width: 200px - this is fine before I set the parent to display flex.
How can I make sure that the circle stays at the set width and height and the rest of the content in the red box resizes - rather than the other way around?
If you could also please explain like I'm 5 why this is happening that would be really appreciated.
.card__inner {
display: flex;
}
.news__feature-image {
border-radius: 100%;
width: 200px;
height: 200px;
background: red;
}
<article class="card">
<div class="card__inner">
<div class="news__feature-image"></div>
<div class="card__content">
<header class="news__header">
<span class="new__post-date">
18 Sep
</span>
<a class="card__link" href="/">
Read More
</a>
</header>
<h2 class="news__title">Lorem ipsum title this is a title etc Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed turpis est, eleifend.</h2>
</div>
</div>
</article>
I have tried using flex grow and flex shrink on the feature image and the content.
I have added settings for the flex items that contains the circle. By default the setting for flex-shrink is 1, which allows it to shrink if necessary. Setting it to zero ensures the circle is displayed as desired.
* {
box-sizing: border-box;
}
.container {
border: thin solid green;
padding: 1rem;
}
.inner {
border: thin solid purple;
display: flex;
padding: 1rem;
}
.circle {
width: 200px;
height: 200px;
background-color: red;
border-radius: 100%;
flex-shrink: 0;
}
<div class="container">
<div class="inner">
<div class="circle"></div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
</div>
</div>
This question already has answers here:
vertical-align with Bootstrap 3
(26 answers)
Closed 7 years ago.
I'm using Twitter Bootstrap and am trying to vertically center text next to an image that is larger than the current text.
I can't use line-height because the text goes over more than one line and I also want to allow for possible additions to the text; so I went to the display: table; display: table-cell; method but it still refuses to work.
Here is my HTML for the respective section:
<div class="fs">
<div class="container">
<div class="wrapper row">
<div class="icon col-md-2">
<a href="" target="blank">
<img src="fs-icon.jpg" width="170" height="76" alt="Flying Solo Icon">
</a>
</div>
<div class="text col-md-10">
<span>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam tincidunt, lacus sed pharetra luctus, odio purus faucibus nunc, mattis molestie sapien libero sit amet leo.
</span>
</div>
</div>
</div>
<!-- Container Ends -->
</div>
<!-- FS Ends -->
CSS:
#about .fs {
padding: 30px 0;
}
#about .fs .wrapper {
display: table;
}
#about .fs .icon {
display: table-cell;
}
#about .fs .text {
display: table-cell;
min-height: 76px;
vertical-align: middle;
}
... and here it is on CodePen: http://codepen.io/anon/pen/XbdRXE
I think the CSS may be clashing with the behavior of the default Bootstrap CSS. However, you could bypass this method altogether and try using this as a custom class:
.vertical-center {
display: inline-block;
vertical-align: middle;
float: none;
}
Just apply it to your span element.
I have a weird issue with some of my divs. Right now my divs are set up to take up the whole screen then have a background color then a background image over it. However, while everything displays properly, if I try to add any content specifically everything disappears except for my background color. I've never had this problem before and I believe it has something to do with how my images and my div are set up. But I can't find a solution so I was wondering if any of you guys could help! I've included the html and css down below!
Here is the jsfiddle that might help: http://jsfiddle.net/e7C87/1/ the red section is the section where I'm trying to place a nav bar and where the background image dissapears
Html (the area with the is the div that's giving me issues all the other divs displays correctly):
<div id="induoIntro" class="divide">
<div class="graphic" style="background-color:#ff837b">
<p id="introGraphic"></p>
<nav>
Designers
Developers
Directors
</nav>
</div>
<div class="textBody">
<p>A rag-tag group of desginers, directors and developers hoping to collaborate with you in an effort to satisfy your endeavours, beautify the web, and enginneer a functional interaction; we'll even guaraantee affordability.</p>
</div>
</div>
<div id="designers" class="divide">
<div class="graphic" style="background-color:#FFB37B">
<p id="designGraphic"></p>
</div>
<div class="textBody">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque ut posuere mauris. Nulla faucibus consectetur mi, nec luctus eros vulputate non. Cras id suscipit metus </p>
</div>
</div>
<div id="developers" class="divide">
<div class="graphic" style="background-color:#CEE28F">
<p id="devGraphic"></p>
</div>
<div class="textBody">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque ut posuere mauris. Nulla faucibus consectetur mi, nec luctus eros vulputate non. Cras id suscipit metus </p>
</div>
</div>
<div id="directors" class="divide">
<div class="graphic" style="background-color:#C195DA">
<p id="directGraphic">
</div>
<div class="textBody">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque ut posuere mauris. Nulla faucibus consectetur mi, nec luctus eros vulputate non. Cras id suscipit metus </p>
</div>
</div>
CSS:
.divide {
height:200%;
}
.graphic {
display:table;
height:50%;
width:100%;
}
.graphic p {
display: table-cell;
vertical-align: middle;
text-align: center;
}
#introGraphic {
background-image: url(../images/WAInduo-02.svg);
background-size: 50% 50%;
background-repeat: no-repeat;
background-position: center;
}
#designGraphic {
background-image: url(../images/WAdesign-03.svg);
background-size: 100% 80%;
background-repeat: no-repeat;
background-position: center;
}
#devGraphic {
background-image: url(../images/WAdevelop-04.svg);
background-size: 100% 80%;
background-repeat: no-repeat;
background-position: bottom center;
}
#directGraphic {
background-image:url(../images/WAdirect-05.svg);
background-size: 100% 80%;
background-repeat: no-repeat;
background-position: center;
}
.textBody {
display:table;
height:50%;
width:75%;
margin:auto;
}
.textBody p {
display: table-cell;
vertical-align: middle;
text-align: center;
font-family: 'Dosis', sans-serif;
font-size:45px;
margin:auto;
}
Okay, you have a lot of irrelevant code there. Here is a JSFiddle and the code that is relevant to your problem (it can be difficult to determine, but if possible, it really helps to provide only the requisite code that we need to solve your problem): JSFiddle
HTML:
<div id="header">
</div>
<div id="induoIntro" class="divide">
<div class="graphic">
<p>Graphic Test</p>
<nav>Test</nav>
</div>
</div>
CSS:
html, body {
height: 100%;
}
#header {
position: absolute;
height: 70px;
top: 0;
width: 100%;
background-color: #fff;
}
.divide {
height: 200%;
}
.graphic {
display: table;
height: 50%;
width: 100%;
background: #ff837b;
}
.graphic p {
display: table-cell;
vertical-align: middle;
text-align: center;
}
As you can see from the JSFiddle, the <p> contents of "Graphic Test" are appearing properly, but the <nav> content is not. Well, if you look at the CSS, you see that any <p> element that is a child of an element with the .graphic class has special instructions, namely display: table-cell, vertical-align: middle;, and text-align: center;.
The <nav> class, however, has no such special instructions. If you remove those instructions from your .graphic p selector, you'll see that "Graphic Test" disappears as well. Where is it going? You can find it using your browser's built-in code inspector, but I'll just tell you: it's moving up to the top of the document.
But wait, isn't that where your header is? Exactly. You have an absolutely positioned header, which means it is removed from the normal document flow and placed on top of the document. So, in effect your <nav> element is being hidden by your header. To illustrate this, we'll give some opacity to your header and see the element sitting behind it:
JSFiddle
Now, if we go back to your original provided JSFiddle and do the same thing to the header there, this is what you'll see: JSFiddle
So to solve this, you should take the CSS properties you have for .graphic p and copy them to a new selector, .graphic nav, or something similar. Hope this helps! :-)
Im creating a website where the header, footer, body are all 100% width of the page, but I need all the content to be centered of the page no matter the resolution. I've tried using a wrapper but then the header and stuff are only 100% width of the wrapper and not the page.
I'm going out on a limb and guess that the background color/imagery is 100% wide, but you want the actual content to be centered (with a fixed width?). Here is sample code that uses an internal wrapper div on each item to keep internal content centered. I would recommend doing something totally different and possibly using repeating backgrounds on the html and body elements, but I don't know what your page looks like.
So.., the following will work, but will alarm HTML purists because of the extra markup :)
You can view a (super ugly) example of this method on this sample page I put together.
CSS:
.wrapper {
width: 960px; /* fixed width */
margin: 0 auto; /* center */
}
HTML:
<div id="header">
<div class="wrapper">
<h1>My Title</h1>
</div>
</div>
<div id="content">
<div class="wrapper">
<h2>Sub Title</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat.</p>
</div>
</div>
<div id="footer">
<div class="wrapper">
<p class="credits">Copyright 2009 by Your Company.com, LLC</p>
</div>
</div>
you can't do this with a div element unless it has a specified width.
for just text, you can use
<div style="text-align: center;">text content</div>
this should work for you:
The CSS:
body {
background-color: #e1ddd9;
font-size: 12px;
font-family: Verdana, Arial, Helvetica, SunSans-Regular, Sans-Serif;
color:#564b47;
margin: 20px 140px 20px 140px;
text-align: center;
}
#content {
width: 100%;
padding: 0px;
text-align: left;
background-color: #fff;
overflow: auto;
}
The HTML:
<body>
<div id="content">
<p><b>center</b><br /><br />
This BOX ist centered and adjusts itself to the browser window.<br />
The height ajusts itself to the content.<br />
</div>
</body>
This example was taken from this site, which I found a while ago and always refer to it for nice simple, clean css templates:
http://www.mycelly.com/
Have a play with this
body {
text-align: center;
position: relative;
}
#content {
width: 100%;
height: auto;
position: relative;
margin-left: auto;
margin-right: auto;
}
Then in the HTML
<html>
<body>
<div id="header"></div>
<div id="content">
<!-- place all of your content inside of here -->
</div>
</body>
</html>