I am trying to position a styled heading to be centered and to sit slightly above it's parent div. I assumed the heading would have to be absolutely positioned to be able to be shifted outside of the parent by adding a negative margin to the top. I can center it when it's position is relative, but I can not add a negative margin to bring it outside of the parent.
HTML
<div class="container">
<div class="row">
<div class="col-md-6">
<div class="content">
<h2 class="content-box-title">A Heading</h2>
<div class="content-body">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsam, culpa illum! Sint maiores numquam provident, consequatur voluptatibus, repudiandae, dolorum sed, itaque saepe magni fugit mollitia! Repellat dignissimos, quas esse itaque.</p>
</div>
</div>
</div>
</div>
</div>
CSS
.content {
position: relative;
background-color: #d5d5d5;
}
.content-box-title {
position: absolute;
background-color: grey;
text-align: center;
padding: 10px;
margin: -40px auto;
}
.content-body {
padding: 20px;
}
http://jsfiddle.net/LsohxL3m/
By making the title an inline-block, you can center it using text-align: center on the parent div. You don't need absolute positioning or auto margin then, so you can just use a negative top margin. You can use text-align left on the content to keep that left aligned:
.content {
background-color: #d5d5d5;
text-align: center;
}
.content-box-title {
display: inline-block;
background-color: grey;
text-align: center;
padding: 10px;
margin-top: -40px;
}
.content-body {
padding: 20px;
text-align: left;
}
Here's a fiddle.
I adjusted that fiddle to include this css content:
.content h2 {
box-sizing: border-box;
margin: -60px auto 0;
width: 100%;
}
an it seemed to center the h2 and set it just a bit above the content div.
but after deeper investigation, it looks like you just need to as a width to your original tag, and the box-sizing rule to assist with the padding/margin on the element.
I appended this to your existing .content-box-title css and it worked fine:
width: 100%;
box-sizing: border-box;
Check now: http://jsfiddle.net/LsohxL3m/1/
I changed the styling of the title as follows:
.content-box-title {
background-color: grey;
text-align: center;
padding: 10px;
margin-top: -40px;
margin-left: auto;
margin-right: auto;
}
You can use translate:
.content-box-title {
position: absolute;
background-color: grey;
text-align: center;
padding: 10px;
margin-top: -40px;
left:50%;
transform: translate(-50%, 0);
}
http://jsfiddle.net/LsohxL3m/6/
change
.content-box-title {
position: absolute;
background-color: grey;
text-align: center;
padding: 10px;
margin: -40px auto;
}
.content-body {
padding: 20px;
}
to
.content-box-title {
transform: translateY(-60px);
background-color: grey;
margin-top: 60px;
text-align: center;
padding: 10px;
}
.content-body {
padding: 0 20px 20px 20px;
}
Here is a sample
Related
i have a page which consists of css cards everything is working fine but the only problem is text which i need to display on css cards is over flowing (but i want it to be displayed with in the card).
and one more thing to be fixed is hover effect, when i hover over an element remaining css cards are arranged in zig zag manner.
check the following code.
<!DOCTYPE html>
<html>
<head>
<title>Easy</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.self{
margin-left:160px;
}
* {
box-sizing: border-box;
}
body {
font-family: Arial, Helvetica, sans-serif;
}
/* Float four columns side by side */
.column {
float: left;
width: 25%;
padding: 0 10px;
margin-bottom: 20px;
flex-basis: 25%;
}
/* Remove extra left and right margins, due to padding */
.row {margin: 0 -5px;}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
Responsive columns
#media screen and (max-width: 600px) {
.column {
width: 100%;
display: block;
margin-bottom: 20px;
}
}
/* Style the counter cards */
.card {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 1);
padding: 16px;
text-align: center;
background-color: #f1f1f1;
transition: width 2s,height 4s;
transition-timing-function: ease-in-out;
}
.card:hover{
background-color: lightgreen;
transition-timing-function: ease-in-out;
width: 300px;
height: 300px;
}
</style>
</head>
<body style="background-color: #dae2e3;">
<div class="self">
<div class="column w3-button">
<div class="card">
<h1><?php echo"$value"; ?></h1>
<h3 style="overflow-x:page-break-inside:inherit;"><?php echo"$description";?></h3>
<h3><?php echo"$index";?></h3>
</div>
</div>
</div>
<?php }?>
This problem is produced because you gave fixed height to the card when hover.
Remove height: 300px; and your hover problem will be gone.
.self {
margin-left: 160px;
}
* {
box-sizing: border-box;
}
body {
font-family: Arial, Helvetica, sans-serif;
}
/* Float four columns side by side */
.column {
float: left;
width: 25%;
padding: 0 10px;
margin-bottom: 20px;
flex-basis: 25%;
}
/* Remove extra left and right margins, due to padding */
.row {
margin: 0 -5px;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
#media screen and (max-width: 600px) {
.column {
width: 100%;
display: block;
margin-bottom: 20px;
}
}
/* Style the counter cards */
.card {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 1);
padding: 16px;
text-align: center;
background-color: #f1f1f1;
transition: width 2s, height 4s;
transition-timing-function: ease-in-out;
}
.card:hover {
background-color: lightgreen;
transition-timing-function: ease-in-out;
width: 300px;
/*height: 300px;*/
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<body style="background-color: #dae2e3;">
<div class="self">
<div class="column w3-button">
<div class="card">
<h1>my card have an overflow problem</h1>
<h3 style="overflow-x: page-break-inside:inherit;">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Atque quisquam voluptate velit perferendis. Vel, cum dignissimos consequatur recusandae quisquam, quibusdam! Amet possimus sunt veritatis quos, nostrum minima deleniti, voluptatem repellat.</h3>
<h3>1</h3>
</div>
</div>
</div>
</body>
I designed a website mobile-first and used a media query to make two columns sit next to each other once the screen expands. Instead it made a grid on my mobile version and I have whitespace on the bottom of my tablet version
If I switch the min to max it just goes to my mobile-first design ignores the media query CSS Grid altogether (as expected) for the tablet and desktop version
#media (min-width: 600px) {
.authentic {
display: grid;
grid-template-columns: repeat(2, 50%);
grid-template-areas: 'bowl content';
height: 100%;
margin: 0px;
}
.right-col {
grid-area: content;
padding: 0 10%;
text-align: left;
align-self: center;
}
}
Here's the HTML if it helps
<section class="authentic">
<div class="right-col">
<h2>Authentic. Awesome.</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Itaque aliquam id ullam vel quia voluptatem nobis nisi error nihil saepe!</p>
</div>
<img src="images/dumpling.jpeg" alt="Dumplings in a bowl" />
</section>
Here is the full HTML and CSS code for full reference:
HTML
<!doctype html>
<!-- Fun project to help me learn responsive layout design -->
<html lang="en">
<head>
<meta charset="utf-8">
<title>Yummy Eats</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="hero-bg">
<section class="top">
<header>
yummy.eats
</header>
<h1><span>The Healthy </span> Eating Experience</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Mollitia eius impedit porro similique officiis
eligendi, asperiores maxime est praesentium libero.</p>
<div class="form-container">
<form action="">
<div class="form-left">
<label for="city">Enter your city:</label>
<input type="text" name="city" id="city">
<p>Example: "San Diego"</p>
</div>
<input type="button" value="Find Food Now">
</form>
</div>
</section>
</div>
<section class="authentic">
<div class="right-col">
<h2> Authentic. Awesome.</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Itaque aliquam id ullam vel quia voluptatem
nobis nisi error nihil saepe!</p>
</div>
<img src="images/dumpling.jpeg" alt="Dumplings in a bowl" />
</section>
</body>
</html>
CSS
#import url('https://fonts.googleapis.com/css?family=Montserrat:400,700&display=swap');
/*Margin affects outside. Padding affects inside */
/*Custom Properties */
:root {
--leading: 2em;
}
body {
font-family: 'Montserrat', sans-serif;
margin: 0;
}
.hero-bg {
background: #307D99 url('images/bg.jpg');
color: white;
text-align: center;
padding-bottom: 4em;
padding-top: 1em;
/* for margin and padding its good to use em */
}
header {
padding: 1em 5em 3em 10em;
}
.hero-bg a {
color: white;
text-decoration: none;
/* ^^this removes url underlining */
font-weight: bold;
font-size: 1.2rem;
/*for text we use rem */
}
section {
margin: 0 1em;
}
h1 {
font-size: 2.5rem;
margin: 2em 0 1.2em;
}
h1 span {
text-transform: uppercase;
display: block;
/* display:block puts things on their own line, nothing is on the right or left to it */
font-size: 1.4rem;
position: relative;
z-index: 1;
}
h1 span::before {
content: ' ';
position: absolute;
width: 3em;
background: #00BFFF;
height: .4em;
bottom: 0;
z-index: -1;
margin-left: -.3em;
}
.hero-bg p {
font-weight: bold;
margin: 0 1em 3em;
/* font-size: .759rem; */
}
.form-container {
background-color: white;
margin: 2em -1em 0;
padding: 2em;
}
label {
color: #2D7D98;
font-weight: bold;
display: block;
margin-bottom: 1em;
font-size: 1.2em;
}
input[type=text] {
border: 1px solid #707070;
width: 100%;
padding: 1em;
border-radius: .5em;
margin-top: 1.2em;
box-sizing: border-box;
}
.form-container p {
color: gray;
margin-bottom: 1.5em;
font-weight: normal;
font-size: .9em;
margin-top: .3em;
}
input[type=button] {
background-color: #F89104;
border: none;
width: 100%;
color: white;
font-weight: bold;
padding: 1em 0;
border-radius: .5em;
font-size: 1.3em;
cursor: pointer;
}
input[type=button]:hover {
background-color: #bb690a;
}
.authentic {
margin: 0;
}
.right-col {
text-align: center;
margin: 3em 1em;
}
h2 {
text-transform: uppercase;
position: relative;
}
h2::before {
content: ' ';
position: absolute;
width: 8em;
background: #00BFFF;
height: .4em;
bottom: 0;
z-index: -1;
margin-left: -.3em;
}
img {
width: 100%;
/* this is super important if you are using images to get your website to fit the entire page */
}
p {
line-height: var(--leading);
}
/* Media query put in place to change the width for the section (form) when the user enlarges the screen into desktop format */
#media (min-width: 730px) {
section {
margin: 0 4em;
}
.form-container {
margin: 2em 4em 0;
}
}
#media (min-width: 600px) {
.hero-bg {
text-align: left;
}
.hero-bg p {
margin: 0 0 3em;
}
.hero-bg section {
width: 65%;
}
.form-container {
margin: 2em 0 0;
padding: 2em;
border-radius: .5em;
box-shadow: 10px 10px 10px rgba(0, 0, 0, .3);
}
form {
display: flex;
}
.form-left {
width: 70%;
}
label {
margin: 0;
}
input[type="button"] {
height: fit-content;
font-size: 1.1em;
margin-left: 1em;
margin-top: 2.2em;
padding: .7em 0;
width: 30%;
}
/* CSS Grid Usage: put the text and bowl next to each other and then changed the order by putting the bowl on the left
since it came before content in grid-template-areas */
.hero-bg p {
margin-bottom: 0px;
}
.authentic {
display: grid;
grid-template-columns: repeat(2, 50%);
grid-template-areas: "bowl content";
height: 100%;
margin: 0px;
}
.right-col {
grid-area: content;
padding: 0 10%;
text-align: left;
align-self: center;
}
img {
grid-area: bowl;
}
h2 {
margin: 0;
}
}
/* A fadeIn effect for the top section */
.top {
animation: fadeIn 4s forwards;
}
#keyframes fadeIn {
from {
opacity: 0;
transform: translateY(-2em);
}
to {
opacity: 1;
transform: translateY(-2em);
}
#keyframes overlay {
0% {
opacity: 0;
}
30% {
1;
}
70% {
1;
}
100% {
0;
}
}
}
Concerning the height / whitespace at the bottom, you should add
html, body {
height: 100%;
}
Otherwise the .authentic element`s 100% height has no height to refer to.
Concerning the other details, you have to add more details (i.e. code) to your question (i.e. the full relevant CSS and HTML code, not only the media query and part of the HTML)
Trying to recreate this layout:
The problem is the white box on top of the black. I don't know the height of the black box. How can I create the white box on top of the others?
This is my code:
https://codepen.io/olefrankjensen/pen/oQyZpX?editors=1100
HTML
<div class="container">
<div class="formbox">[login form markup here...]</div>
<div class="box">
<h1>Don't have an account</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusamus dicta cumque harum.</p>
<button>SIGN UP</button>
</div>
<div class="box">
<h1>Have an account</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusamus dicta cumque harum.</p>
<button>LOGIN</button>
</div>
</div>
CSS
// reset
h1, h2, h3, h4, h5, h6, p, button, div {
margin: 0;
padding: 0;
line-height: 1;
}
// font
#import url('https://fonts.googleapis.com/css?family=Roboto');
// colors
$white: rgba(255,255,255,.8);
:root {
font-size: 16px;
}
body {
background-image: url(
https://picsum.photos/1200/800?);
display: flex;
justify-content: center;
align-items: center;
font-family: 'Roboto', sans-serif;
color: $white;
}
.container {
width: 900px;
margin: 50px auto;
background: rgba(0,0,0,.5);
position: relative;
display: flex;
}
.box {
padding: 5rem 3rem;
float: left;
h1, p {
margin-bottom: 1rem;
}
button {
padding: 1rem 2rem;
background: transparent;
border: 2px solid $white;
border-radius: 6px;
color: $white;
text-align: center;
font-size: 1rem;
cursor: pointer;
text-transform: uppercase;
}
}
.formbox {
width: 500px;
background: white;
margin-left: -50px;
margin-top: -25px;
box-shadow: 0 0 50px black;
display: block;
position: absolute;
}
I have to absolute position the .formbox to not make it "interfere" with the other flex items in the flex container .container. But as a consequence it has no height.
How can I give the Login overlay full height of it's parent?
When position is absolute, top, bottom, right and left properties work relative to the first parent with position: relative.
So, you don't really need the margin-top: -25px or margin-left: -50px, you can just add top: -25px; bottom: -25px; left: -25px to .formbox and you are done. If you need to move it to the right, just add right: XXpx and remove left (or set a positive value like left: 50%).
https://codepen.io/anon/pen/gQKWNq?editors=1100
I could go with adding to formbox class a height of 100% + 50px (margin -25px what you have took x 2). You can do this with calc function: https://developer.mozilla.org/en-US/docs/Web/CSS/calc
.formbox {
width: 500px;
background: white;
margin-left: -50px;
margin-top: -25px;
box-shadow: 0 0 50px black;
display: block;
position: absolute;
height: calc(100% + 50px);
}
In my header, I want my logo to the left, and text to the right of it:
If there's enough space, text should be in one line vertically-align to the middle.
If there isn't, text should reflow to 2 lines, preferably aligned to the middle as well.
I can satisfy (1) with the below code. But once I reduce the width of the whole page, the h1 is displayed below the img. If I remove the h1 {display:inline-block}, then the h1 is displayed correctly to the left of the img - but then the vertical-align dosn't take effect.
How can I achieve both?
<img src="http://test.tipulnagish.co.il/wp-content/uploads/2015/11/tick-1015459_640.png">
<div>
<h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</h1>
</div>
img {
height: 100px;
width: 120px;
margin: 15px 10px 15px 10px;
float: left;
}
div {
height: 130px;
line-height: 130px;
}
h1 {
display: inline-block;
line-height:normal;
vertical-align: middle;
}
Demo
You can try this https://jsfiddle.net/4mzj50sx/6/
HTML
<div class="div">
<img src="http://test.tipulnagish.co.il/wp-content/uploads/2015/11/tick-1015459_640.png">
<div>
<h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</h1>
</div>
</div>
CSS
.div {
display: table;
}
div {
display: table-cell;
vertical-align: middle;
}
img {
height: 100px;
width: 120px;
margin: 15px 10px 15px 10px;
display: table-cell;
vertical-align: middle;
}
You do not need tables to line something up....
Just use inline-block to line the divs up, and then use a before element to center the text in the right div with...
Fiddle: https://jsfiddle.net/46xqLngb/
<div class="wrapper">
<div class="left">
<img src="http://test.tipulnagish.co.il/wp-content/uploads/2015/11/tick-1015459_640.png">
</div>
<div class="right">
<h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</h1>
</div>
</div>
img {
height: 100px;
}
div.wrapper {
width: 100%;
height: 130px;
}
div.left {
width: 13%;
display: inline-block;
vertical-align: top;
height:100%;
}
div.right {
width: 83%;
display: inline-block;
vertical-align: top;
height: 100%;
}
h1 {
line-height: normal;
vertical-align: middle;
margin: 0;
height: 100%;
padding: 0;
}
h1:before {
content: '';
width: 0;
height: 100%;
vertical-align: middle;
display: inline-block;
}
The HTML Part
<div class="mainHeader">
<img src="http://test.tipulnagish.co.il/wp-content/uploads/2015/11/tick-1015459_640.png" class="img"/>
<p class="textStyle">WOW</p>
</div>
The CSS Part:
.mainHeader{
border: 1px solid red;
width: 100%
}
.img {
width: 100px;
height: 120px;
display: inline-block;
vertical-align: middle;
margin: 15px 10px 15px 10px;
}
.textStyle {
display: inline-block;
vertical-align: center;
}
I guess this helps You
Another vertical alignment question...
Given the following,
HTML:
<div class="thing">
<h2>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et tempus mauris. Vivamus imperdiet congue iaculis.</h2>
</div>
<div class="thing">
<h2>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et tempus mauris.</h2>
</div>
CSS:
.thing{
background: #eee;
margin: 0 auto 10px;
width: 625px
}
h2{
display: inline-block;
font-size: 15px;
line-height: 20px;
margin: 0 0 0 40px;
vertical-align: middle;
}
.thing:before{
background: red;
content: '';
display: inline-block;
height: 40px;
position: absolute;
vertical-align: middle;
width: 40px;
}
What do I need to do in order to have the text and the red box vertically centered? This should work for headings of any height.
Fiddle
You just need to remove the position:Absolute that breaks the inline-block behavior. Instead use some trick to fight the space between inline-block elements and be sure the two elements can be aside on the containers width. Try this:
.thing{
font-size:0; /*ADD*/
}
h2{
/*margin: 0 0 0 40px; REMOVE*/
width:585px; /*ADD*/
}
.thing:before{
/*position: absolute; REMOVE*/
}
Check this Demo Fiddle
I guess your question has two answers.
Here's solution one: (Make the red box equal the height of the .thing content.
.thing{
background: #eee;
margin: 0 auto 10px;
position: relative; // Make .thing:before relate it's position to this
width: 625px
}
h2{
font-size: 15px;
line-height: 20px;
margin: 0 0 0 40px;
}
.thing:before{
background: red;
content: '';
height: 100%; // Height will equal .thing height
position: absolute;
width: 40px;
}
Demo 1
Second solution: (Make text center vertically)
.thing{
background: #eee;
margin: 0 auto 10px;
min-height: 40px;
position: relative;
width: 625px
}
h2{
font-size: 15px;
line-height: 20px;
margin: 0 0 0 40px;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
.thing:before{
background: red;
content: '';
height: 40px;
left: 0;
position: absolute;
top: 0;
width: 40px;
}
Demo 2