Is the border length less than the width of the div element?

I came across this code snippet

div {
   width: 200px;
   border-bottom: 1px solid magenta;
   height: 50px;   
}
<div></div>

The div has a width of 200px which causes the bottom border to also be 200px long. However, I am looking for a way to keep the div's width at 200px but have the border-bottom only 100px in length. How can I achieve this?

Answer №1

To achieve a decorative border effect without using additional markup, you can utilize pseudoelements in CSS. For example:

div {
  width   : 200px;
  height  : 50px;   
  position: relative;
  z-index : 1;
  background: #eee;
}

div:before {
  content : "";
  position: absolute;
  left    : 0;
  bottom  : 0;
  height  : 1px;
  width   : 50%;  /* or 100px */
  border-bottom:1px solid magenta;
}
<div>Item 1</div>
<div>Item 2</div>

It is worth noting that the use of :after is also supported from IE8 onwards.

Edit:

If you require a right-aligned border, simply replace left: 0 with right: 0.

For a center-aligned border, you can just set left: 50px;.

Answer №2

In more recent browsers, another method to achieve this effect is by using a negative spread box-shadow. Take a look at the updated example here: http://jsfiddle.net/AbCdE/123/

box-shadow: 0px 18px 5px -18px cyan;

Although I believe the safest and most reliable approach is the previously accepted solution. Just wanted to offer an alternative technique.

Answer №3

My new addition is placing a line underneath the h3 tag as follows

    <h3 class="home_title">Your title here</h3> 
    .home_title{
        display:block;
    }
    .home_title::after {
        display:block;
        clear:both;
        content : "";
        position: relative;
        left    : 0;
        bottom  : 0;
        max-width:250px;
        height  : 1px;
        width   : 50%;
        border-bottom:1px solid #e2000f;
        margin:0 auto;
        padding:4px 0px;
    }

Answer №4

One way to achieve a gradient effect is by using linear gradients:

div {
    width:200px;
    height:100px;
    display:block;
    background-image: linear-gradient(to right, #000 1px, rgba(255,255,255,0) 1px), linear-gradient(to left, #000 0.2rem, rgba(255,255,255,0) 1px);
    background-position: bottom;
    background-size: 100% 50px;
    background-repeat: no-repeat;
    border-bottom: 2px solid #333;
    border-top: 1px solid blue;
}
<div></div>

Answer №5

If you want a border to be a different size than the div itself, one solution is to create another div underneath it that is centered or positioned absolutely, with the desired 1 pixel border and a height of 1 pixel.

For examples and demonstrations, you can visit this link.

I have kept the original border visible so you can compare widths. There are two versions provided: one with a width of 100 and the other centered with a width of 100. Simply delete the one you do not want to use.

Answer №6

While I may be late to the party, for those interested in creating two borders (specifically on the bottom and right in my situation), you can utilize the method outlined in the accepted answer and incorporate an :after pseudo-element for the second line. Simply adjust the properties as follows: Check out this example on JSFiddle

div {
  width:500px;
  height:500px;   
  position: relative;
  z-index: 1;
}
div:before {
  content: "";
  position: absolute;
  left: 25%;
  bottom: 0;
  height: 1px;
  width: 50%;
  border-bottom: 1px solid magenta;
}
div:after {
  content: "";
  position: absolute;
  right: 0;
  bottom: 25%;
  height: 50%;
  width: 1px;
  border-right: 1px solid magenta;
}

Answer №7

In a recent project of mine, I implemented something similar to this technique. I wanted to share it here for others to benefit from. By adding another div as a child element and styling it with a border of small width, you can easily position it to the left, center, or right using basic CSS.

Here is the HTML code snippet:

    <div>
        content 
        <div class ="ac-brdr"></div>
    </div>

And the accompanying CSS code:

   .active {
    color: magneta;
  }
   .active .ac-brdr {
        width: 20px;
        margin: 0 auto;
        border-bottom: 1px solid magneta;
  }

Answer №8

If you're looking to create a horizontal line with a specific width, this code snippet can help you achieve that:

For more information on how to customize the width of a horizontal line, you can visit this link.

<hr width="50%">

The above code will generate a horizontal line with a width of 50%. You have the option to modify or create a different class to further style the line according to your preferences.

Answer №9

In order to create a bottom border between pictures within a div container, I discovered that the most effective single line of code to use was border-bottom-style: inset;

Answer №10

section{
    font-size: 20px;
    line-height: 22px;
    display:inline-block;
    width:180px;
    text-align:center;
}

section::after {
    background: #ff9933 none repeat scroll 0 0;
    content: "";
    display: block;
    height: 3px;
    margin-top: 12px;
    width: 90px;
    margin:auto;
}

Answer №11

To apply a bottom border only to half of an HTML element, you can wrap the desired content with a span element.

Here is an example of how to achieve this:

<div> <span>content here </span></div>

Below is the CSS code for styling the elements:

 div{
    width:200px;
    height:50px;   
    }
 span{
        width:100px;
        border-bottom:1px solid magenta;   
    } 

Answer №12

I successfully achieved the reverse of this by utilizing :after and ::after to extend my bottom border precisely 1.3rem:

When I applied both :before and :after simultaneously, my element became distorted because they are arranged horizontally with display: flex, flex-direction: row, and align-items: center.

This technique can be used to adjust width or height, or potentially any mathematical dimension modifications:

a.nav_link-active {
  color: $e1-red;
  margin-top: 3.7rem;
}
a.nav_link-active:visited {
  color: $e1-red;
}
a.nav_link-active:after {
  content: '';
  margin-top: 3.3rem;      // combined margin and height
  height: 0.4rem;          // should match active link margin
  background: $e1-red;
  margin-left: -$nav-spacer-margin;
  display: block;
}
a.nav_link-active::after {
  content: '';
  margin-top: 3.3rem;      // combined margin and height
  height: 0.4rem;          // should match active link margin
  background: $e1-red;
  margin-right: -$nav-spacer-margin;
   display: block;
}

Apologies, this is presented in SCSS; simply scale up the values by 10 and substitute variables with standard measurements.

Answer №13

Creating a smaller border length on the right side of a parent div using pseudo-elements

@import url(https://fonts.googleapis.com/css?family=Raleway);

body{
 font-family: 'Raleway', sans-serif;
}

 
div {
  width   : 200px;
  height  : 50px;   
  position: relative;
  z-index : 1;
  background-color: #f5f5f5;
  margin: 20px auto;
  padding: 20px;
  color:#726E97;
}

div:before {
  content : "";
  position: absolute;
  right    : 0;
  top     : 25%;
  height  : 50px;
  width   : 50%;
  border-right:5px solid #726E97;
}
<div>BOX 1</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to render Material Icons on Vue/Quasar web app hosted on Azure

I am facing an issue with my webapp built using Vue and Quasar. I added the necessary icon imports to my quasar-user-options.js file. import '@quasar/extras/material-icons/material-icons.css' import "@quasar/extras/material-icons-outlined/ma ...

What is the best way to enable a DOM element's height to be resized?

I have a widget displayed in the corner of my browser that I would like to make resizable by dragging the header upwards to increase its height. The widget's position remains fixed with its bottom, left, and right properties unchanged, but I need the ...

Searching for specific values within data attributes using jQuery wildcards

I am currently utilizing the data_attribute on this page and have the following elements with data attributes. No. 1.<div class="row hidden" data-party-registration-source-type-id="1"> 2.<div class="row hidden" data-party-registration-source- ...

Methods for activating touch-like scrolling through the use of mouse grabbing and dragging

Let me show you a simple illustration of the task I'm attempting to accomplish: <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> ...

Guide on calculating the total sum of numbers from multiple selected elements based on class within a div that has a designated id

https://i.sstatic.net/uyjPk.pngI'm currently working on a task that involves summing up the number of listeners from various servers. These values are stored within a div with the id="channel1", and each number of listeners is located in a span elemen ...

Issue encountered while attempting to deactivate certain foundation CSS and JavaScript files

I am attempting to deactivate certain CSS files in the foundation framework, as they are unnecessary for my project. After installing foundation via a gem, I followed Ryan Bates' recommendation to modify the foundation_and_overrides.scss file by repl ...

Building a Multiple Choice Text Box with HTML

I am looking to create a section on my website where I can display text, and I have 9 buttons on the page that I want to stay within the same page without redirecting. The goal is for the text to update based on which button is clicked, with a fading ani ...

Jquery function for determining height across multiple browsers

I am currently facing an issue with setting the height of table cells in my project. While everything works smoothly on most browsers, Firefox seems to add borders to the overall height which is causing inconsistency across different browsers. If anyone k ...

How can I make a zigzag pattern with CSS?

Every now and then, I come across this unique shape and corner in web design. Take a look at this sample image where I have highlighted it in red. Can anyone provide advice on how to recreate this using CSS? https://i.sstatic.net/qYWlg.jpg ...

Adjusting the background color using CSS according to the browser's dimensions

I'm having trouble identifying the issue with this code. The background color remains white and doesn't change as expected. <style type="text/css"> h1 { position: absolute; text-align: center; width: 100%; ...

Automatically Updating Div Content Without the Use of PHP

I've been searching everywhere online for a solution to my problem. I have an HTML page with a div that references another HTML page, and all I want is to refresh that div on a specific interval. Currently, the page loads correctly, but I need the di ...

Setting images on various screens with Bootstrap can be achieved by utilizing the responsive class utilities

Is it possible to display 2 images on a small screen and 4 images on a large screen using Bootstrap? How can this be achieved? <div class="row"> <div class="col-sm-6 col-md-3 col-lg-2"> <img src=""> </ ...

A guide to retrieving nested values from a JSON object using JavaScript

Having trouble accessing specific values from a heavily nested JSON content returned by a URL? You're not alone! Many tutorials only cover simple nesting examples. Take a look at the code snippet I've been working on: $(document).ready(function ...

Exploring jQuery's ability to retrieve CSS values

Is there a way to use jQuery to read a specific CSS value from a class? The situation is as follows: HTML: <div class="box"></div> CSS: .box { margin-top: 100px; } I want to determine if the .box class has a margin-top of 100px in order t ...

Incorporating a horizontal divider into a row within a table

I have been facing an issue with applying a border-bottom rule to each row in my table. Despite trying different approaches, the rule is not being displayed as expected. After inspecting the code and checking the relevant classes, I am still unable to pin ...

PHP SimpleXML: What is the best way to parse an HTML document?

As I attempt to use simplexml_load_string to load an HTML file as XML, numerous errors and warnings related to the HTML content arise leading to a failed process. Is there a method to effectively load an html file using SimpleXML? The HTML document in que ...

Obtaining the value of an HTML span tag during execution on a PHP Webdriver client and Selenium can be achieved

Currently working with the php webdriver client along with selnium Struggling to retrieve the value from the span tag showing Rs 350 <div class="fareBlock busDataBlock"> <div> <span class="fareStart">< ...

The Bootstrap4 classes for buttons, including btn and btn-primary, do not properly style links using the "a" tag

Incorporating Bootstrap version 4.1 without any custom styles on my page, I have the following code snippet for button display: <div class="btn-group" role="group" aria-label=""> <a class="btn btn-primary" type="button" href="{% url 'website ...

Error: Unable to retrieve current location using 'Geolocation' in React

import "./App.css"; import { useState, useEffect } from "react"; function App() { const [lat, setLat] = useState(null); const [long, setLong] = useState(null); const [data, setData] = useState(null); const [error, setError] = u ...

Using JavaScript to Render an Array in HTML through Looping (with JSON)

I am facing an issue where only the first value of my associative array is being returned when I loop through it in my HTML file. However, when I check it using console.log, all data is displayed correctly. There is no error displayed in the HTML, but not ...